Client Login Register
Calculate Price & Order →
← Back to all articles

Write a class to calculate BMI in java, programming…

Published on November 30, 2026 • Category: Academic Writing
Write a class to calculate BMI in java, programming…

Write a class to calculate BMI in java, programming homework help

Participant

Maximum Score: 17 pts (see red text for point breakdown)

In this assignment you will create a class named Participant. Then you will test your class with a tester application we provide. You should read the descriptions of the Participant class methods below, but then open and read the tester application’s main method in detail before you write them. The tester application will help you understand how your methods will be used.

Create a file named Participant.java in your PSA8 directory. Your Participant class will have the following features.

1. Instance Variables (3 pts)

  •  name: The name of the participant; this should be a String.
  •  weight: The weight (lb) of the participant; this should be an int.
  •  height: The height (ft) of the participant; this should be a double.

2. Constructors (3 pts)

  •  Your Participant class should have three constructors.
  •  The first constructor should take in no parameters and set the instance variables to the following default values.

o name: “Joe Schmoe” o weight: 200 o height: 6.0

  •  The second constructor should take in only one parameter, a String to which to set the name. The other two instance variables should be set to default values listed above.
  •  The third constructor should take an appropriate number of parameters to set every instance variable. If weight or height is negative, this constructor should not set the variable to this negative value; instead it should print the following and set the value to the default value listed above: “Incorrect height, height must be positive. Height will be set to default value (6.0).” OR “Incorrect weight, weight must be positive. Weight will be set to default value (200).”

3. Methods (3 pts)  For every instance variable, you will create a getter and a setter.

o The getter will return the current value of that instance variable. o The setter will take a parameter and set the instance variable to that value.

Note that if height or weight is negative, you should not update the value. Instead, you should print the following message: “Incorrect height, height must be positive. Height will not be updated.” OR

“Incorrect weight, weight must be positive. Weight will not be updated.”

  •  Create a method named toString that takes no parameters and returns a String. The toString output should match the output the tester provides.
  •  Finally, create a getBMI method that calculates and returns the Body Mass Index (BMI) or the participant. The formula for calculating BMI is: = h ×703 h h 2 Testing the Participant Class: For this assignment, you MUST use our tester application. Incorrect execution of the tester application will result in a potentially low grade.
  •  Download ParticipantTester.java from Blackboard and save it in your PSA8 directory. We highly recommend you review the testing application prior to starting the assignment: it will help you understand what is required of you.
  •  Please note: This tester does NOT test for all possible failure conditions and represents a basic testing method. More extensive automated testing is often applied in industry.

page2image22608

  •  The output should EXACTLY match the output shown in PSA8-correct-output.txt (6 pts).
  •  Note: If your implementation is incorrect, the tester will help you determine why. You can use the tester to determine default values, and mistakes you may have made. Comments:

 Comments in the style detailed in the book ARE required. Your code must be properly commented to receive full credit (2 pts).

/**_x000D_

* ParticipantTester.java_x000D_

*_x000D_

* A class that tests the correctness of a Participant class._x000D_

*_x000D_

* @author Sat Garcia_x000D_

*/_x000D_

_x000D_

public class ParticipantTester_x000D_

{_x000D_

private static int numPassed = 0;_x000D_

private static int numFailed = 0;_x000D_

private static int testNumber = 0;_x000D_

private static final String defaultName = "Joe Schmoe";_x000D_

private static final int defaultWeight = 200;_x000D_

private static final double defaultHeight = 6.0;_x000D_

private static final double defaultBMI = 3905.5555555555557;_x000D_

_x000D_

/**_x000D_

* Method to test default constructor, using getters to retrieve fields._x000D_

*/_x000D_

public static void testDefaultConstructor() {_x000D_

System.out.println("Creating a default participantn");_x000D_

_x000D_

Participant participant = new Participant();_x000D_

_x000D_

System.out.println("Test #" + testNumber++ + ": Testing correct default name using the getName methodn");_x000D_

if(participant.getName().equals(defaultName)) {_x000D_

System.out.println("tTest PASSED!n");_x000D_

numPassed++;_x000D_

}_x000D_

else_x000D_

{_x000D_

System.out.println("tTest FAILED!n");_x000D_

System.out.println("tReason: " + participant.getName() + " is incorrect, the default name should be " + defaultName + "n");_x000D_

numFailed++;_x000D_

}_x000D_

_x000D_

System.out.println("Test #" + testNumber++ + ": Testing correct default weight using the getWeight methodn");_x000D_

if(participant.getWeight() == defaultWeight) {_x000D_

System.out.println("tTest PASSED!n");_x000D_

numPassed++;_x000D_

}_x000D_

else_x000D_

{_x000D_

System.out.println("tTest FAILED!n");_x000D_

System.out.println("tReason: " + participant.getWeight() + " is incorrect, the default weight should be defaultWeightn");_x000D_

numFailed++;_x000D_

}_x000D_

_x000D_

System.out.println("Test #" + testNumber++ + ": Testing correct default height using the getHeight methodn");_x000D_

if(participant.getHeight() == defaultHeight) {_x000D_

System.out.println("tTest PASSED!n");_x000D_

numPassed++;_x000D_

}_x000D_

else_x000D_

{_x000D_

System.out.println("tTest FAILED!n");_x000D_

System.out.println("tReason: " + participant.getHeight() + " is incorrect, the default height should be " + defaultHeight + "n");_x000D_

numFailed++;_x000D_

}_x000D_

_x000D_

System.out.println("Test #" + testNumber++ + ": Testing the getBMI method on the first participantn");_x000D_

if(participant.getBMI() == defaultBMI) {_x000D_

System.out.println("tTest PASSED!n");_x000D_

numPassed++;_x000D_

}_x000D_

else_x000D_

{_x000D_

System.out.println("tTest FAILED!n");_x000D_

System.out.println("tReason: " + participant.getBMI() + " is incorrect, the default BMI should be " + defaultBMI + "n");_x000D_

numFailed++;_x000D_

}_x000D_

}_x000D_

_x000D_

/**_x000D_

* Method to test the participant's "setter" methods._x000D_

*/_x000D_

public static void testSetters() {_x000D_

System.out.println("Test #" + testNumber++ + ": Testing each set method using a default participantn");_x000D_

Participant participant = new Participant();_x000D_

_x000D_

participant.setName("Sally Jane");_x000D_

System.out.println("Test #" + testNumber++ + ": Testing setName("Sally Jane"") method using the getName methodn"");_x000D_




Share

We only email you; unsubscribe in one click. No address is ever sold.

⚡ Order Now 💬 WhatsApp 📞

Just now