I NEED HELP IN MAKING THIS JAVA CODE TO INCLUDE 2 GUI AS SHOWN IN THE PICTURE BE
ID: 3599270 • Letter: I
Question
I NEED HELP IN MAKING THIS JAVA CODE TO INCLUDE 2 GUI AS SHOWN IN THE PICTURE BELOW THE CODE.
import java.util.*;
public class BMICalculator {
public static void main(String[] args) {
int sit , put , weight;
float height,Bmi, distance, tim, push;
Scanner input = new Scanner(System.in);
System.out.println("Input your weight in pounds: ");
weight = input.nextInt();
System.out.println(" Input your height in inches: ");
height = input.nextFloat();
System.out.println(" Input your sit up in this week: ");
sit = input.nextInt();
System.out.println(" Input your push up in this week: ");
push = input.nextFloat();
System.out.println(" Input your distance (ft) ran for exercise in this week: ");
distance = input.nextFloat();
System.out.println(" Input your distance time(s) ran in this week: ");
tim = input.nextFloat();
Bmi =weight*703/(height*height);
System.out.println(" Your body mass Index is: " + Bmi);
if (Bmi>25)
{
System.out.println(" You are somewhat overweight.");
System.out.println(" Recommendaton of the Week");
push = push*1.1f;
sit = (int)(sit*1.1);
System.out.println(" Push up." + push + "number");
System.out.println(" Sit up." + sit + "number");
}
else if (Bmi<18.5)
{
System.out.println(" You are somewhat underweight.");
System.out.println(" Recommendaton of the Week");
push = push*0.8f;
sit=(int) (sit*0.8);
}
else if (Bmi>18.5 && Bmi <25)
{
System.out.println(" Congratulations! You are within a healthy weight range.");
push=push*1.09f;
sit= (int) (sit*1.09);
System.out.println(" Recommendaton of the Week");
System.out.println(" Push up :" + push + " number");
System.out.println(" Sit up :" + sit + " number");
}
}
}
urce Design wt Weight (Pounds) Your BMI is Te Height (Inches) Te BMI Evaluation Sit ups Recommendation of the week Push ups Sit ups Distance run Push ups Distance time Calculate BMI ni BuExplanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BMICalculator extends JFrame implements ActionListener {
private JLabel lbLabel0 = new JLabel("Weight (Pounds)");
private JLabel lbLabel1 = new JLabel("Height (Inches)");
private JLabel lbLabel2 = new JLabel("Sit ups");
private JLabel lbLabel3 = new JLabel("Push Ups");
private JLabel lbLabel4 = new JLabel("Distance run");
private JLabel lbLabel5 = new JLabel("Distance time");
private JTextField text0 = new JTextField(20);
private JTextField text1 = new JTextField(20);
private JTextField text2 = new JTextField(20);
private JTextField text3 = new JTextField(20);
private JTextField text4 = new JTextField(20);
private JTextField text5 = new JTextField(20);
private JButton calculateBtn = new JButton("Calcuate BMI");
public BMICalculator() {
super("BMICalculator");
// create a new panel with GridBagLayout manager
JPanel newPanel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(10, 10, 10, 10);
// add components to the panel
constraints.gridx = 0;
constraints.gridy = 0;
newPanel.add(lbLabel0, constraints);
constraints.gridx = 1;
newPanel.add(text0, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
newPanel.add(lbLabel1, constraints);
constraints.gridx = 1;
newPanel.add(text1, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
newPanel.add(lbLabel2, constraints);
constraints.gridx = 1;
newPanel.add(text2, constraints);
constraints.gridx = 0;
constraints.gridy = 3;
newPanel.add(lbLabel3, constraints);
constraints.gridx = 1;
newPanel.add(text3, constraints);
constraints.gridx = 0;
constraints.gridy = 4;
newPanel.add(lbLabel4, constraints);
constraints.gridx = 1;
newPanel.add(text4, constraints);
constraints.gridx = 0;
constraints.gridy = 5;
newPanel.add(lbLabel5, constraints);
constraints.gridx = 1;
newPanel.add(text5, constraints);
constraints.gridx = 0;
constraints.gridy = 6;
constraints.gridwidth = 2;
constraints.anchor = GridBagConstraints.CENTER;
newPanel.add(calculateBtn, constraints);
calculateBtn.addActionListener(this);
// set border for the panel
newPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "BMI Calculator"));
// add the panel to this frame
add(newPanel);
pack();
setLocationRelativeTo(null);
}
public static void main(String[] args) {
// set look and feel to the system look and feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new BMICalculator().setVisible(true);
}
});
}
public void actionPerformed( ActionEvent e )
{
if ( e.getSource() == calculateBtn )
{
float height = Float.parseFloat(text1.getText()),
push = Float.parseFloat(text3.getText()),
distanceRun = Float.parseFloat(text4.getText()),
distanceTime = Float.parseFloat(text5.getText()), bmi;
int weight = Integer.parseInt(text0.getText()),
sit = Integer.parseInt(text2.getText());
String str= "";
bmi = weight*703/(height*height);
System.out.println(bmi);
if (bmi>25)
{
str = "You are somewhat overweight.";
push = push*1.1f;
sit = (int)(sit*1.1);
}
else if (bmi<18.5)
{
str = " You are somewhat underweight.";
push = push*0.8f;
sit=(int) (sit*0.8);
}
else if (bmi>18.5 &&bmi <25)
{
str = " Congratulations! You are within a healthy weight range.";
push=push*1.09f;
sit= (int) (sit*1.09);
}
new BMIEvaluation(bmi, sit, push, str).setVisible(true);
}
}
}
class BMIEvaluation extends JFrame {
private JLabel lbLabel0 = new JLabel("Your BMI is:");
private JLabel lbLabel1 = new JLabel("BMI Evaluation");
private JLabel lbLabelRec = new JLabel("Recommendation of the week");
private JLabel lbLabel2 = new JLabel("Sit Ups");
private JLabel lbLabel3 = new JLabel("Push Ups");
private JTextField text0 = new JTextField(10);
private JTextField text1 = new JTextField(30);
private JTextField text2 = new JTextField(10);
private JTextField text3 = new JTextField(10);
public BMIEvaluation(float bmi, int sit, float push, String str) {
super("BMIEvaluation");
text0.setText(String.valueOf(bmi));
text1.setText(str);
text2.setText(String.valueOf(sit));
text3.setText(String.valueOf(push));
// create a new panel with GridBagLayout manager
JPanel newPanel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(10, 10, 10, 10);
// add components to the panel
constraints.gridx = 0;
constraints.gridy = 0;
newPanel.add(lbLabel0, constraints);
constraints.gridx = 1;
newPanel.add(text0, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
newPanel.add(lbLabel1, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
newPanel.add(lbLabelRec, constraints);
constraints.gridx = 0;
constraints.gridy = 3;
constraints.gridwidth = 2;
newPanel.add(text1, constraints);
constraints.gridx = 0;
constraints.gridy = 4;
newPanel.add(lbLabel2, constraints);
constraints.gridx = 1;
newPanel.add(text2, constraints);
constraints.gridx = 0;
constraints.gridy = 5;
newPanel.add(lbLabel3, constraints);
constraints.gridx = 1;
newPanel.add(text3, constraints);
constraints.anchor = GridBagConstraints.CENTER;
// set border for the panel
newPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Evaluation"));
// add the panel to this frame
add(newPanel);
pack();
setLocationRelativeTo(null);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.