0 To start the assignment off we will alter the ClassTracing

0] To start the assignment off, we will alter the ClassTracing project that was covered in class. Please download the project from resources in laulima and import it into eclipse. Before continuing, run the code, and make sure you have no errors. Open up a text document, and cut and paste the program output into the text document. It should look like this;

Name: John Doe, Birth Year: 2000, Height: 5.0ft, Weight: 120.0lb BMI: 36.0
Name: Bart Simpson, Birth Year: 2010, Height: 4.0ft, Weight: 100.0lb BMI: 37.5

Name: Homer Simpson, Birth Year: 50, Height: 5.0ft, Weight: 300.0lb BMI: 90.0
SECOND STUDENT BMI IS: 37.5 **********************************

Name: Clone 0, Birth Year: 1900, Height: 10.0ft, Weight: 200.0lb BMI: 30.0
Name: Clone 1, Birth Year: 1901, Height: 11.0ft, Weight: 201.0lb BMI: 27.40909

Name: Clone 2, Birth Year: 1902, Height: 12.0ft, Weight: 202.0lb BMI: 25.249998
Name: Clone 3, Birth Year: 1903, Height: 13.0ft, Weight: 203.0lb BMI: 23.423077

I like malasadas, I like them a lot. Whenever malasadas are plentiful, I gain some weight. We will now add functionality in the Student class to gain and lose weight.

a] Add two functions to the Student.java file. The gain function will allow you to input a number like 10.0 and then add 10% to the Students weight.

void gain(float percent){
weight=weight + weight*percent/100.0f;

}

Now add the lose function in a similar way.
b] Homer Simpson always loved donuts, but recently was given a chance to eat some malasadas.

Needless to say, he gained 10% of his body weight. After the line

firstStudent.ShowInfo();

Add a line that makes him gain 10% of his weight, and then add

firstStudent.ShowInfo();

On the other hand Bart Simpson ate one malasada, but proceeded to skateboard all over town. He lost 5% of his body weight. After the line

System.out.println(\"SECOND STUDENT BMI IS: \"+secondStudent.BMI());

Add a line which makes Bart lose 5% of his body weight. After adding this line of code, add

System.out.println(\"SECOND STUDENT BMI IS: \"+secondStudent.BMI());

Run the code. What has changed in the output? Look at the output you pasted in the text editor. What looks different?

Main Program.java

public class MainProgram {

   public static void main(String[] args) {

       Student firstStudent = new Student();

       Student secondStudent = new Student(\"Bart Simpson\",2010, 4, 100);

      

       firstStudent.ShowInfo();

       secondStudent.ShowInfo();

      

       firstStudent.name = \"Homer Simpson\";

       firstStudent.birthYear = 50;

       firstStudent.weight = 300;

       firstStudent.ShowInfo();

       System.out.println(\"SECOND STUDENT BMI IS: \"+secondStudent.BMI());

      

       System.out.println(\"**********************************\");

       Student [] robotStudent = new Student [4];

       for (int i = 0; i < 4; i++){

           robotStudent[i] = new Student();

           robotStudent[i].name = \"Clone \"+i;

           robotStudent[i].birthYear = 1900 + i;

           robotStudent[i].weight = 200 + i;

           robotStudent[i].height = 10 + i;

           robotStudent[i].ShowInfo();

       }

   }

}

Student.java

public class Student {

   String name;

   int birthYear;

   float height;

   float weight;

  

   Student () {

       name = \"John Doe\";

       birthYear = 2000;

       height = 5;

       weight = 120;

   }

   Student (String nm, int year, float h, float w){

       name = nm;

       birthYear = year;

       height = h;

       weight = w;

   }

  

   void ShowInfo(){

       System.out.println(\"Name: \" + name + \", Birth Year: \" + birthYear + \", Height: \" + height + \"ft, Weight: \"+weight+\"lb\");

       System.out.println(\"BMI: \"+BMI());

      

   }

   void gain (float percent){

       weight=weight + weight*percent/100.0f;

   }

  

   float BMI(){

       // BMI is your weight (in kilograms) divided by your height squared (in centimeters)

       float bmi = (weight * 0.45f) / (height * 12 * 0.025f);

       return bmi;

   }

}

Solution

public class Student {

   String name;
   int birthYear;
   float height;
   float weight;

   Student() {
       name = \"John Doe\";
       birthYear = 2000;
       height = 5;
       weight = 120;
   }

   Student(String nm, int year, float h, float w) {
       name = nm;
       birthYear = year;
       height = h;
       weight = w;
   }

   void ShowInfo() {
       System.out.println(\"Name: \" + name + \", Birth Year: \" + birthYear
               + \", Height: \" + height + \"ft, Weight: \" + weight + \"lb\");
       System.out.println(\"BMI: \" + BMI());

   }

   void gain(float percent) {
       weight = weight + weight * percent / 100.0f;
   }

   void loss(float percent) {
       weight = weight - weight * percent / 100.0f;
   }

   float BMI() {
       // BMI is your weight (in kilograms) divided by your height squared (in
       // centimeters)
       float bmi = (weight * 0.45f) / (height * 12 * 0.025f);
       return bmi;
   }

}


public class MainProgram {

   public static void main(String[] args) {
       Student firstStudent = new Student();
       Student secondStudent = new Student(\"Bart Simpson\", 2010, 4, 100);

       firstStudent.ShowInfo();
       firstStudent.gain(10);
       firstStudent.ShowInfo();

       secondStudent.ShowInfo();
       System.out.println(\"SECOND STUDENT BMI IS: \" + secondStudent.BMI());
       secondStudent.loss(5);
       System.out.println(\"SECOND STUDENT BMI IS: \" + secondStudent.BMI());

       firstStudent.name = \"Homer Simpson\";
       firstStudent.birthYear = 50;
       firstStudent.weight = 300;
       firstStudent.ShowInfo();

       System.out.println(\"SECOND STUDENT BMI IS: \" + secondStudent.BMI());

   }

}

OUTPUT:

Name: John Doe, Birth Year: 2000, Height: 5.0ft, Weight: 120.0lb
BMI: 36.0
Name: John Doe, Birth Year: 2000, Height: 5.0ft, Weight: 132.0lb
BMI: 39.6
Name: Bart Simpson, Birth Year: 2010, Height: 4.0ft, Weight: 100.0lb
BMI: 37.5
SECOND STUDENT BMI IS: 37.5
SECOND STUDENT BMI IS: 35.625
Name: Homer Simpson, Birth Year: 50, Height: 5.0ft, Weight: 300.0lb
BMI: 90.0
SECOND STUDENT BMI IS: 35.625

0] To start the assignment off, we will alter the ClassTracing project that was covered in class. Please download the project from resources in laulima and impo
0] To start the assignment off, we will alter the ClassTracing project that was covered in class. Please download the project from resources in laulima and impo
0] To start the assignment off, we will alter the ClassTracing project that was covered in class. Please download the project from resources in laulima and impo
0] To start the assignment off, we will alter the ClassTracing project that was covered in class. Please download the project from resources in laulima and impo
0] To start the assignment off, we will alter the ClassTracing project that was covered in class. Please download the project from resources in laulima and impo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site