Import java.lang.math; Math.randon ( ) 1. Create a Java Class called Dieter . Th
ID: 3746149 • Letter: I
Question
Import java.lang.math;
Math.randon ( )
1. Create a Java Class called Dieter.
The class has the following variables:
: string that is the Dieter’s name
: int that is the Dieter’s weight in pounds
: int that is the Dieter’s height in inches
BMI : double that is the Body Mass Index for the Dieter
The class has the following public instance methods (all take no parameters):
: constructor that prompts the user for a name, weight and height and creates an instance of Dieter using these inputs
: randomly changes the Dieter’s weight
1/10 of the time it increases the weight by a random integer from 1 to 5
9/10 of the time it decreases the weight by a random integer from 1 to 10
It also prints the amount of weight lost or gained
: randomly changes the Dieter’s weight
1/10 of the time it decreases the weight by a random integer from 1 to 5
9/10 of the time it increases the weight by a random integer from 1 to 10
It also prints the amount of weight lost or gained
: prints a report containing the Dieter’s name, weight and height. Also printed is a statement for the Dieter based on the following BMI values (should include the BMI value with 2 digit after the decimal):
o Underweight: BMI is less than 18.5.
o Normal weight: BMI is 18.5 to 24.9.
o Overweight: BMI is 25 to 29.9.
o Obese: BMI is 30 or more
The class has the following private method defined and used (no parameters):
: returns a double value which is the Dieter’s Body Mass Index: BMI
The formula for BMI is:
703 x weight (lbs) / [height (in)]2
The class has the following private static variable:
totalWeightChange : Amount of weight lost/gained by all dieters.
The class has the following static method that takes no parameters:
: Simply returns totalWeightChange
2. Write a Java class application called Dieterapp. Dieterapp should create a Dieter.
A menu based loop will then be run with the following choices:
person: use the constructor to create a new Dieter
diet: runs the diet method
party: runs the party method
report: runs the report method
quit: ends the loop
An error message should be printed if an illegal command is entered.
The total weight change for all dieters should be displayed at the end of the run.
Explanation / Answer
JAVA PROGRAM
import java.lang.math;
Math.randon ( )
public class Dieter {
public static void main(String[] Allen) {//string in dieter name
Scanner input = new Scanner(System.in);
int dieter weight;
System.out.print("Input weight in pounds: "); //input weight in pounds
double weight = input.nextDouble();
int dieter height;
System.out.print("Input height in inches: "); //input height in inches
double inches = input.nextDouble();
double BMI = weight * 0.5734892 / (inches * 0.0152 * inches * 0.0152); //body mass index of the dieter
System.out.print("Body Mass Index is " + BMI+" ");
}
public class instance {
//declaring public instance methods
public static void main (String [] args) {
Scanner input = new Scanner (System.in);
//constructor prompts the user for name,weight,height
int name;
int weight;
int height;
int bMI;
system.out.print("Enter the dieter name:");
System.out.print ("Enter the corresponding Weight in Pounds: ");
weight = input.upcommingInt();
System.out.print ("Enter the corresponding Height in Inches: ");
height = input.upcommingInt();
bMI = (weight * 703) / (height * height);
System.out.printf ("Your Body Mass Index (BMI) is %d ", bMI);
private static int idealBodyWeight(int pounds) {
int pounds = MINIMUM_WEIGHT_POUND;
if ((WEIGHT > INCREASES_ONE TENTH_RANDOM)) {
//1/10 of weight increase by a random integer
{
pounds += (weight - MAXIMUM_WEIGHT_RANDOM.INTEGER) * WEIGHT INCREASES_PER_POUND;
Random.integer
}
pounds += inches *WEIGHT INCREASES_PER_POUND;
return pounds;
}
}
//9/10 of the time it decreases the weight by a random integer from 1 to 10
double totalWeight = 0.0d;
for (Item i : items)
{
totalWeight += i.getWeight();
}
int randomInteger = 1 to 10;
double random = integer.random() * totalWeight;
for (int i = 0; i < items.length; ++i)
{
random -= items[i].getWeight();
if (random <= 0.01)
{
randomIndex = i;
break;
}
}
Item myRandomItem = items[randomInteger];
Scanner scanner = new Scanner(System.in);
println("Enter weight increases in pounds": ");
double weight = scanner.nextdouble();
//1/10 of the time it decreases the weight by a random integer from 1 to 5
pounds += (weight - MINIMUM_WEIGHT_RANDOM.INTEGER) * WEIGHT DECREASES_PER_POUND;
Random.integer
}
pounds += inches *WEIGHT DECREASES_PER_POUND;
return pounds;
}
//9/10 of the time it increases the weight by a random integer from 1 to 10
double totalWeight = 0.01;
for (Item i : items)
{
totalWeight += i.getWeight();
}
// Now choose a random integer
int randomInteger = 1 to 10;
double random = integer.random() * totalWeightdecrease;
for (int i = 0; i < items.length; ++i)
{
random -= items[i].getWeightdecrease();
if (random <= 0.01)
{
randomIndex = i;
break;
}
}
Item myRandomItem = items[randomInteger];
println("can calculate BMI by showing decreasing weight in pounds":)
System.out.println ("BMI VALUES");
System.out.println ("Underweight: less than 18.5");
System.out.println ("Normal: between 18.5 and 24.9");
System.out.println ("Overweight: between 25 and 29.9");
System.out.println ("Obese: 30 or greater");
}
}
//totalWeightChange : Amount of weight lost/gained by all dieters.
static int count=0;
private static variable()
{
count++;
}
private static variable()
{
Variablevalue obj1=new Variablevalue();
Variablevalue obj2=new Variablevalue();
obj1.variablevalue();
obj2.variablevalue();
System.out.println("weight gain by dieters");
System.out.println("weight loss by dieters");
return(0); //returns the total weight change wwith no parameters
}
//java class application called Dieterapp
class dieterap
public static String(String[] args)
throws Exception {
new Thread(new Run()).start();
run.stringbuffer(diet);//runs the diet method
StringBuffer b = new StringBuffer();
run.stringbuffer(party); //runs the party method
for (int i = 0; i < args.length; i++) {
if (args[i].report() > i) { //checking reports
b.append(args[i].report(i));
} else {
run.stringbuffer(report); //report method
b.append('?');
}}
}
return b.toString();
quit()
}
Hope you understand and this helps you.All the best.