diff --git a/src/main/java/chapter06/bmi/Bmi.java b/src/main/java/chapter06/bmi/Bmi.java index cb191ef..7746c15 100644 --- a/src/main/java/chapter06/bmi/Bmi.java +++ b/src/main/java/chapter06/bmi/Bmi.java @@ -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); + } } \ No newline at end of file diff --git a/src/main/java/chapter06/dogyears/DogYearsConverter.java b/src/main/java/chapter06/dogyears/DogYearsConverter.java new file mode 100644 index 0000000..402ce91 --- /dev/null +++ b/src/main/java/chapter06/dogyears/DogYearsConverter.java @@ -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)); + } +} \ No newline at end of file diff --git a/src/main/java/chapter06/dogyears/TheProgram.java b/src/main/java/chapter06/dogyears/TheProgram.java deleted file mode 100644 index 87fa23e..0000000 --- a/src/main/java/chapter06/dogyears/TheProgram.java +++ /dev/null @@ -1,24 +0,0 @@ -package chapter06.dogyears;// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub. -import java.lang.Math; - -public class TheProgram { - - public static void main(String[] args) - { - - System.out.println("The dog's age is: " + convertToHumanAge(3)); - } - - public static int convertToHumanAge (int dogYears) - { - double dogAge = 0; - final double POST = 5.3333333333; - - if (dogYears > 0) - { - dogAge += 13; - dogAge += (POST * (dogYears - 1)); - } - return (int) Math.round(dogAge); - } -} \ No newline at end of file diff --git a/src/main/java/chapter06/piechart/Poll.java b/src/main/java/chapter06/piechart/Poll.java index aca0c83..7fd9239 100644 --- a/src/main/java/chapter06/piechart/Poll.java +++ b/src/main/java/chapter06/piechart/Poll.java @@ -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); + } } \ No newline at end of file diff --git a/src/main/java/chapter06/piechart/PollControlPanel.java b/src/main/java/chapter06/piechart/PollControlPanel.java index 7ccd3b2..d4207db 100644 --- a/src/main/java/chapter06/piechart/PollControlPanel.java +++ b/src/main/java/chapter06/piechart/PollControlPanel.java @@ -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(); + } } \ No newline at end of file diff --git a/src/main/java/chapter06/piechart/PollDisplayPanel.java b/src/main/java/chapter06/piechart/PollDisplayPanel.java index c813dd7..ddd4022 100644 --- a/src/main/java/chapter06/piechart/PollDisplayPanel.java +++ b/src/main/java/chapter06/piechart/PollDisplayPanel.java @@ -1,150 +1,158 @@ -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 javax.swing.*; +import java.awt.*; + /** - * A chapter06.piechart.PollDisplayPanel holds the vote counts and - * displays the numbers and the pie chart for - * the current vote counts. + * A class that holds the vote counts and displays the numbers and the pie chart for them. */ +public class PollDisplayPanel extends JPanel { + private String name1; + private String name2; + private String name3; + private int count1; + private int count2; + private int count3; + + public PollDisplayPanel(String name1, String name2, String name3) { + setBackground(Color.WHITE); + this.name1 = name1; + this.name2 = name2; + this.name3 = name3; + count1 = 0; // These are optional as Java automatically initializes class properties to reasonable defaults + count2 = 0; // (e.g., int to 0, float to 0.0f, any Object to null). However, it is considered bad style to + count3 = 0; // rely on this. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html + } + + public void vote1() { + count1++; + } + + public void vote2() { + count2++; + } + + public void vote3() { + count3++; + } -import java.awt.Color; -import java.awt.Graphics; -import javax.swing.JPanel; -import java.math.*; - -public class PollDisplayPanel extends JPanel -{ - private String name1, name2, name3; - // Declare the int fields count1, count2, count3: - private int count1, count2, count3; - - // Constructor - public PollDisplayPanel(String nm1, String nm2, String nm3) - { - setBackground(Color.WHITE); - name1 = nm1; - name2 = nm2; - name3 = nm3; - count1 = 0; // optional - count2 = 0; // optional - count3 = 0; // optional - } - - // Increments count1 - public void vote1() - { - count1++; - } - - // Increments count2 - public void vote2() - { - count2++; - } - - // Increments count3 - public void vote3() - { - count3++; - } - - // Returns a string representation of this object - public String toString() - { - return ("Tami: " + count1 + " Brian: " + count2 + " Liz: " + count3); - } - - // Redefines JPanel's paintComponent to draw this pie chart - public void paintComponent(Graphics g) - { - super.paintComponent(g); - - int w = getWidth(); - int h = getHeight(); - int x = w/2; - int y = h/2; - int r = Math.min(w, h) / 4; - drawPieChart(g, x, y, r); - drawLegend(g, x, y, r); - } - - // Draws the pie chart. - // To avoid gaps in the picture, the following algorithm is used: - // 1. set fromDegree to 0; - // 2. draw the red sector and increment fromDegree by its size - // 3. draw the green sector and increment fromDegree by its size - // 4. set the size of the blue sector to the remaining - // area, 360 - fromDegree, but not less than 0: - // degrees = Math.max(360 - fromDegree, 0); - // (Occasionally, due to rounding errors, fromDegree may become 361, - // for example, count1 = 5, count2 = 11, count3 = 0.) - private void drawPieChart(Graphics g, int x, int y, int r) - { - int total = count1 + count2 + count3; - int fromDegree = 0; - - if (total > 0) - { - int degrees; - g.setColor(Color.RED); - degrees = countToDegrees(count1, total); - drawSector(g, x, y, r, fromDegree, degrees); - fromDegree += degrees; - - g.setColor(Color.GREEN); - degrees = countToDegrees(count2, total); - drawSector(g, x, y, r, fromDegree, degrees); - fromDegree += degrees; - - g.setColor(Color.BLUE); - degrees = Math.max(360 - fromDegree, 0); - drawSector(g, x, y, r, fromDegree, degrees); + @Override + public String toString() { + return (name1 + ": " + count1 + ", " + name2 + ": " + count2 + ", " + name3 + ": " + count3); } - else - { - g.setColor(Color.LIGHT_GRAY); - drawSector(g, x, y, r, fromDegree, 360); + + /** + * Overrides the {@link JPanel#paintComponent(Graphics) paintComponent()} method in {@link JPanel} to + * draw a pie chart. + */ + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + + int width = getWidth(); + int height = getHeight(); + int x = width / 2; + int y = height / 2; + int radius = Math.min(width, height) / 4; + drawPieChart(g, x, y, radius); + drawLegend(g, x, y, radius); + } + + /** + * Draws a pie chart. + *
+ * To avoid gaps in the picture, the following algorithm is used: + *