GUI java code..

User Generated

nyrmm11

Programming

Description

I have this program, ((The program computes the Heart Rate needed for slow burn (fat loss, e.g.65% of maximum for that age) and fast burn (e.g. much higher rate) for aerobics)) as this pic shows


the code is provided and all the job is ::
1-crate project and name it:: hr_al Then, create a package and name it:: com.ra_al.hr Then, create a class and name it: hr_al
2- edit the code ( if it necessary) and run it in IntelliJ ide.
3- zip the project file..



import javax.swing.JLabel;
import javax.swing.JTextField;

public class HeartRatePanel extends javax.swing.JPanel
{
private JLabel sP;
private JLabel ageInputLabel;
private JLabel outputLabel65;
private JLabel outputLabel80;
private JLabel resultLabel65;
private JLabel resultLabel80;
private JTextField age;
private int[] ageDecades = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
private int[] rate65 = { 136, 130, 123, 117, 110, 104, 97, 91, 84, 78 };
private int[] rate80 = { 168, 160, 152, 144, 136, 128, 120, 112, 104, 96 };

public HeartRatePanel() {
sP = new JLabel(" ZONE TRAINING HEART RATES ");


ageInputLabel = new JLabel(" Enter Age (integer): ");
outputLabel65 = new JLabel(" Heart Rate for FatBurn (65% of Max)= ");
outputLabel80 = new JLabel(" Heart Rate for Cardio (80% of Max)= ");
resultLabel65 = new JLabel(". . . . . ");
resultLabel80 = new JLabel(". . . . . ");

age = new JTextField(5);
age.addActionListener(new ageListener(null));

add(sP);
add(ageInputLabel);
add(age);


add(outputLabel65);
add(resultLabel65);

add(outputLabel80);
add(resultLabel80);

setBackground(new java.awt.Color(219, 211, 40));
}

private int getLow(int[] x, int age)
{
int save = 0;
for (int i = 0; i < x.length; i++) {
if ((age >= x[i]) && (age < x[(i + 1)]))
save = i;
}
return save;
}

private int getInterpolate(int age, int[] rateArray)
{
int k = getLow(ageDecades, age);
int x = age;
int x0 = ageDecades[k];
int y0 = rateArray[k];

int x1 = ageDecades[(k + 1)];
int y1 = rateArray[(k + 1)];

int heartRate = y0 + (y1 - y0) * (x - x0) / (x1 - x0);
return heartRate;
}

private class ageListener implements java.awt.event.ActionListener
{
private ageListener() {}

public void actionPerformed(java.awt.event.ActionEvent event)
{
String text = HeartRatePanel.this.age.getText();

int age = Integer.parseInt(text);
int heartRate65 = HeartRatePanel.this.getInterpolate(age, rate65);
int heartRate80 = HeartRatePanel.this.getInterpolate(age, rate80);

resultLabel65.setText(Integer.toString(heartRate65));
resultLabel80.setText(Integer.toString(heartRate80));
}
}
}

import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;


public class HeartRate
{
public HeartRate() {}

public static void main(String[] args)
{
JFrame frame = new JFrame("Heart Rate");
frame.setDefaultCloseOperation(3);

HeartRatePanel panel = new HeartRatePanel();

frame.getContentPane().add(panel);


frame.setSize(new Dimension(300, 150));
frame.setResizable(false);
frame.setVisible(true);
}
}

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Workin...


Anonymous
Really helped me to better understand my coursework. Super recommended.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags