Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 64 additions & 54 deletions src/main/java/chapter06/bmi/Bmi.java
Original file line number Diff line number Diff line change
@@ -1,65 +1,75 @@
package chapter06.bmi;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub.
// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub.
package chapter06.bmi;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class Bmi extends JFrame
implements ActionListener
{
JTextField inputLbs, inputInches, displayBmi;
/**
* A {@link JFrame} window that calculates BMI from pounds and inches.
*/
public class Bmi extends JFrame implements ActionListener {
private JTextField massInputField;
private JTextField heightInputField;
private JTextField bmiDisplayField;

public Bmi()
{
super("BMI Calculator");
JLabel labelLbs = new JLabel("Weight (lbs):", SwingConstants.RIGHT);
inputLbs = new JTextField(5);
JLabel labelInches = new JLabel("Height (inches):", SwingConstants.RIGHT);
inputInches = new JTextField(5);
JLabel labelBmi = new JLabel("BMI = ", SwingConstants.RIGHT);
displayBmi = new JTextField(5);
displayBmi.setEditable(false);
JButton go = new JButton("Compute");
go.addActionListener(this);
public Bmi() {
super("BMI Calculator");
JLabel labelLbs = new JLabel("Weight (lbs.):", SwingConstants.RIGHT);
massInputField = new JTextField(5);
JLabel labelInches = new JLabel("Height (in.):", SwingConstants.RIGHT);
heightInputField = new JTextField(5);
JLabel labelBmi = new JLabel("BMI = ", SwingConstants.RIGHT);
bmiDisplayField = new JTextField(5);
bmiDisplayField.setEditable(false);
JButton go = new JButton("Compute");
go.addActionListener(this);

Container c = getContentPane();
c.setBackground(Color.white);
JPanel p = new JPanel();
p.setLayout(new GridLayout(3, 2, 5, 5));
p.add(labelLbs);
p.add(inputLbs);
p.add(labelInches);
p.add(inputInches);
p.add(labelBmi);
p.add(displayBmi);
c.add(p, BorderLayout.CENTER);
c.add(go, BorderLayout.SOUTH);
}
Container c = getContentPane();
c.setBackground(Color.white);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 5, 5));
panel.add(labelLbs);
panel.add(massInputField);
panel.add(labelInches);
panel.add(heightInputField);
panel.add(labelBmi);
panel.add(bmiDisplayField);
c.add(panel, BorderLayout.CENTER);
c.add(go, BorderLayout.SOUTH);
}

public void actionPerformed(ActionEvent e)
{
int lbs = Integer.parseInt(inputLbs.getText());
int inches = Integer.parseInt(inputInches.getText());
double bmi = calculateBmi(lbs, inches);
DecimalFormat df = new DecimalFormat("00.0");
displayBmi.setText(df.format(bmi));
}
/**
* Called when the "Compute" button is clicked.
* @throws NumberFormatException when non-integers are entered into the height and mass input fields. Input
* verification should be used to mitigate this.
*/
@Override
public void actionPerformed(ActionEvent e) {
int pounds = Integer.parseInt(massInputField.getText());
int inches = Integer.parseInt(heightInputField.getText());
double bmi = calculateBmi(pounds, inches);
DecimalFormat df = new DecimalFormat("00.0");
bmiDisplayField.setText(df.format(bmi));
}

// Returns BMI equal to weight in kilograms divided
// over squared height in meters.
private double calculateBmi(int lbs, int inches)
{
double kg, meters;
kg = lbs * 0.454;
meters = inches * 0.0254;
return kg/(meters * meters);
}
/**
* Calculates body mass index.
* @param pounds mass in pounds.
* @param inches height in inches.
* @return BMI in kilograms per square meter.
*/
private double calculateBmi(int pounds, int inches) {
double kilograms = pounds * 0.454;
double meters = inches * 0.0254;
return kilograms / Math.pow(meters, 2);
}

public static void main(String[] args)
{
Bmi w = new Bmi();
w.setBounds(300, 300, 300, 160);
w.setDefaultCloseOperation(EXIT_ON_CLOSE);
w.setVisible(true);
}
public static void main(String[] args) {
Bmi w = new Bmi();
w.setBounds(300, 300, 300, 160);
w.setDefaultCloseOperation(EXIT_ON_CLOSE);
w.setVisible(true);
}
}
30 changes: 30 additions & 0 deletions src/main/java/chapter06/dogyears/DogYearsConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub.
package chapter06.dogyears;

import java.lang.Math;

/**
* A class that provides a method to convert dog years to human years.
*/
public class DogYearsConverter {
private static final double DOG_TO_HUMAN_YEAR_RATE = 5.3333333333;

/**
* Converts the given age in dog years to the equivalent age in human years.
* @param dogYears an age in dog years.
* @return the age in human years. If zero or a negative integer was passed, this returns zero.
*/
public static int convertToHumanAge(int dogYears) {
double dogAge = 0;

if (dogYears > 0) {
dogAge += 13;
dogAge += DOG_TO_HUMAN_YEAR_RATE * (dogYears - 1);
}
return (int) Math.round(dogAge);
}

public static void main(String[] args) {
System.out.println("The dog's age is: " + convertToHumanAge(3));
}
}
24 changes: 0 additions & 24 deletions src/main/java/chapter06/dogyears/TheProgram.java

This file was deleted.

44 changes: 22 additions & 22 deletions src/main/java/chapter06/piechart/Poll.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package chapter06.piechart;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub.
// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub.
package chapter06.piechart;

import java.awt.*;
import javax.swing.*;

public class Poll extends JFrame
{
public Poll()
{
super("Vote for Tami, Brian, or Liz");
/**
* A {@link JFrame} window that
*/
public class Poll extends JFrame {
public Poll() {
super("Vote for Tami, Brian, or Liz");

Container c = getContentPane();
c.setBackground(Color.WHITE);
PollDisplayPanel chart = new PollDisplayPanel("Tami", "Brian", "Liz");
PollControlPanel controls = new PollControlPanel(chart);
c.add(chart, BorderLayout.CENTER);
c.add(controls, BorderLayout.SOUTH);
}
Container c = getContentPane();
c.setBackground(Color.WHITE);
PollDisplayPanel chart = new PollDisplayPanel("Tami", "Brian", "Liz");
PollControlPanel controls = new PollControlPanel(chart);
c.add(chart, BorderLayout.CENTER);
c.add(controls, BorderLayout.SOUTH);
}

public static void main(String[] args)
{

Poll w = new Poll();
w.setBounds(300, 300, 400, 400);
w.setDefaultCloseOperation(EXIT_ON_CLOSE);
w.setVisible(true);
}
public static void main(String[] args) {
Poll w = new Poll();
w.setBounds(300, 300, 400, 400);
w.setDefaultCloseOperation(EXIT_ON_CLOSE);
w.setVisible(true);
}
}
96 changes: 46 additions & 50 deletions src/main/java/chapter06/piechart/PollControlPanel.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
package chapter06.piechart;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub.
// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub.
package chapter06.piechart;

/*
* Implements the control panel for the chapter06.piechart.Poll program
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PollControlPanel extends JPanel
implements ActionListener
{
private JButton button1, button2, button3;
private PollDisplayPanel chartPanel;

public PollControlPanel(PollDisplayPanel chart)
{
chartPanel = chart;

button1 = new JButton("Tami");
button1.setPreferredSize(new Dimension(80, 30));
button1.setToolTipText("Vote for Tami");
button1.addActionListener(this);

button2 = new JButton("Brian");
button2.setPreferredSize(new Dimension(80, 30));
button2.setToolTipText("Vote for Brian");
button2.addActionListener(this);

button3 = new JButton("Liz");
button3.setPreferredSize(new Dimension(80, 30));
button3.setToolTipText("Vote for Liz");
button3.addActionListener(this);

add(button1);
add(button2);
add(button3);
}

/**
* Processes button events
*/
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();

if (button == button1)
chartPanel.vote1();
else if (button == button2)
chartPanel.vote2();
else if (button == button3)
chartPanel.vote3();
chartPanel.repaint();
}

/**
* Implements the control panel for the {@link chapter06.piechart.Poll} program.
*/
public class PollControlPanel extends JPanel implements ActionListener {
private JButton button1;
private JButton button2;
private JButton button3;
private PollDisplayPanel chartPanel;

public PollControlPanel(PollDisplayPanel chart) {
chartPanel = chart;

button1 = new JButton("Tami");
button1.setPreferredSize(new Dimension(80, 30));
button1.setToolTipText("Vote for Tami");
button1.addActionListener(this);

button2 = new JButton("Brian");
button2.setPreferredSize(new Dimension(80, 30));
button2.setToolTipText("Vote for Brian");
button2.addActionListener(this);

button3 = new JButton("Liz");
button3.setPreferredSize(new Dimension(80, 30));
button3.setToolTipText("Vote for Liz");
button3.addActionListener(this);

add(button1);
add(button2);
add(button3);
}

public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();

if (button == button1) {
chartPanel.vote1();
} else if (button == button2) {
chartPanel.vote2();
} else if (button == button3) {
chartPanel.vote3();
}
chartPanel.repaint();
}
}
Loading