Question

In: Computer Science

Create a class called Height Copy the code for Height given below into the class. Remember...

  • Create a class called Height

  • Copy the code for Height given below into the class.

  • Remember – test the methods as you go

  • Take a few minutes to understand what the class does. There are comments where you will have to be changing code. A list of changes are given

    • Change the setFeet and setInches mutators to make sure the height and width will not be less than 0, no matter what is passed in to it
    • Change constructor that has formal parameters so that it calls the set methods to change the 3 private instance variables
    • Change the method setHeight, so that it calls the set methods to change the feet and inches instance variables
    • Write the method totalInches. This method is described in the comments
    • Write the method totalFeet. This method is described in the comments
  • Test the Height class to make sure the methods you added/changed work correctly.

Code for class Height

public class Height
{
    private String name;
    private int feet;
    private double inches;


    public Height()
    {
        name = "";
        feet = 0;
        inches = 0.0;
    }
    public Height(String name, int feet, double inches)
    {
        // use the sets to create an object with the values passed in

    }
    public void setHeight(int newFeet, double newInches)
    {
        // use the sets to set the feet and inches with the values passed in

    }
    public void setName(String name)
    {
        this.name = name;
    }
    public void setFeet(int newFeet)
    {
        // write the code to set the feet to the formal
        // parameter - since the feet can't be less than 0
        // make the code check and if that might happen, 
        // it should set the feet to 0


    }
    public void setInches(double newInches)
    {
        // write the code to set the inches to the formal
        // parameter - since the inches can't be less than 0
        // make the code check and if that might happen, 
        // it should set the inches to 0

    }   

    public String getName()
    {
        return name;
    }
    public int getFeet()
    {
        return feet;
    }
    public double getInches()
    {
        return inches;
    }


    // write the method totalInches. This method returns the total 
    // number of inches tall the person is. For example, if the
    // person is 5 feet 3.5 inches, this method would return 63.5 since
    // 63.5 inches is the height of the person in inches





    // write the method totalFeet. This method returns the total feet 
    // tall the person is as a decimal. For example, if a person 
    // is 5 feet 6 inches tall, this method would return 5.5, the height
    // of the person in feet


}
  • Once you have the class working
  • Create a class called Main – copy the code below into the class
  • You can run the code as is, before making changes. [If you submit the Main and Height classes at this point, you should get 6 points, if the class is correct.]
  • Do Part A and Part B at the top of main
  • Follow the comments to write the methods in the Main class. (Remember the methods will be public and static) Submit the code after you get each method working for points.
  • NOTICE - there is a Scanner object already declared for you to use (keyboard)
  • Besides the main method you will be writing four methods – make sure you use these exact method names - also follow the directions in the comments, it suggests an order to writing the code
    • printMenu – displays the following menu on the screen – allows the user to enter a selection, and returns the selection. To save on typing – the print statements to print the menu are given below and can be copied directly, so you don’t have to type them. You do have to allow the user to type in their selection and return it.

System.out.println("1. Change Person 1 Data");

System.out.println("2. Change Person 2 Data");

System.out.println("3. Print Person 1 Data");

System.out.println("4. Print Person 2 Data");

System.out.println("5. Compare Heights of Person 1 and 2 with graph");

System.out.println("6. Exit");

System.out.print("Enter Selection: ");

  • changePersonData– This method has a Height object passed in. It allows the user to enter new values for the object passed in. It has two error checking loops to make sure the values are positive. When it executes it looks like this:

Enter the name: Lori

Enter number of feet: -8

Invalid feet - try again!!!

Enter number of feet: -1

Invalid feet - try again!!!

Enter number of feet: 3

Enter number of inches: -4

Invalid inches - try again!!!

Enter number of inches: -5

Invalid inches - try again!!!

Enter number of inches: 2.5

  • printPersonData – Directions in comments in main

  • printLineOfGraph – Directions in comments in main - make sure you uncomment the method calls in the method printTotalInchesGraph

  • Put a loop in main so the menu will keep displaying until the user types choice to quit

  • When you have it running correctly, upload the Height and Main class to zybook and submit it for grading.

Code for class Main

import java.util.Scanner;
public class Main
{
    public static Scanner keyboard = new Scanner(System.in);

    public static void main(String [] args)
    {

        Height person1 = new Height();
        Height person2 = new Height("John", 6, 2);
// Part A        
        // Write the statement to change the name of person1 
        // to be "Lori"

// Part B        
        // Write the statement to change the height of person1 
        // to be 5 foot 6 inches


        int choice = 0;
// Implement each of the methods called from the switch statement below.
// I have put a number in comments, they are only suggestions as
// to the order you might want to implement them

// So for each comment method call, uncomment it and then implement it
// below main. A description is given for each method below main. 

// After you have them implemented put the switch statement into a loop so  
// that the menu will continue to display on the screen until the 
// user chooses option 6

// 1. choice = printMenu();

          switch(choice)
          {
          case 1:
// 4. changePersonData(person1);
              break;
          case 2:
// 4. changePersonData(person2);
              break;
          case 3:
// 2. printPersonData(person1);
              break;
          case 4:
// 2. printPersonData(person2);
              break;
          case 5:
// 3. Do the method printLineOfGraph  which is called from in printTotalInchesGraph - directions are below
       printTotalInchesGraph(person1, person2); 
              break;
          case 6:
             break;
          default:
              System.out.println("Invalid - Try Again!!!");
              break;
         }   

      // This should only display on the screen after the 
      // user has quite the menu
      System.out.println("The End");
    }



    // printMenu 
    // This method prints the menu on the screen. It then
    // asks the user to type in their selection  (as an integer) and 
    // the method returns the selection. You do NOT have
    // to type all of the menu in - see directions in Zybooks
    // to copy and paste











    // changePersonData
    // This method has an object of the Height class passed in
    // It then asks the user to type in a new name and 
    // calls the method to change the name.
    // Then it asks the user to type in the height in feet
    // the method will error check the feet and continue asking 
    // the user to enter the feet until a valid feet is entered.
    // the feet must be zero or larger.
    // It then calls the method to update the feet
    // Lastly, the method asks the user to type in the inches.
    // Similarly to the feet, it will error check and continue
    // asking to type it in until the user types a valid (zero 
    // or greater) inches
    // Finally it calls the method to change the inches
    // See instructions on zbooks, too
    // Points for this method might not show up until you put the loop in main









    // printPersonData 
    // This method has an object of the height class passed in.
    // It uses the object and the methods for the object to print the 
    // objects information in the form:
    /*  
        Height Data
        Name: John
        Height: 6 feet 2.0 inches
        Height in Inches: 74.00 inches
        Height in Feet: 6.17 feet
    */






   // This method calls printLineGragh - make sure you uncomment the two method calls in this method
   // when you implement printLineOfGraph
     public static void printTotalInchesGraph(Height obj1, Height obj2)
    {
        System.out.println("Name: " + obj1.getName());
    //    printLineOfGraph(obj1.totalInches());
        System.out.println("Name: " + obj2.getName());
    //    printLineOfGraph(obj2.totalInches());

    }

    // printLineOfGraph
    // This method has a double passed into it
    // It will print as many asterisks as the value passed in
    // for example:    printLineOfGraph(4.8) will print ****
    // Notice: it does not worry about the decimal, it just 
    // prints the number of asterisks as the whole number
    // after printing the asterisks in a row it prints a new line
    // you can typecast the double formal parameter and 
    // store it in a new variable, and then use that in the loop





} // End of Main class

A sample output is given

1. Change Person 1 Data
2. Change Person 2 Data
3. Print Person 1 Data
4. Print Person 2 Data
5. Compare Heights of Person 1 and 2 with graph
6. Exit
Enter Selection: 4

Height Data
Name: John
Height: 6 feet 2.0 inches
Height in Inches: 74.00 inches
Height in Feet: 6.17 feet

1. Change Person 1 Data
2. Change Person 2 Data
3. Print Person 1 Data
4. Print Person 2 Data
5. Compare Heights of Person 1 and 2 with graph
6. Exit
Enter Selection: 2
Enter the name: Minnie
Enter number of feet: -1
Invalid feet - try again!!!
Enter number of feet: -8
Invalid feet - try again!!!
Enter number of feet: 3
Enter number of inches: 2.8
1. Change Person 1 Data
2. Change Person 2 Data
3. Print Person 1 Data
4. Print Person 2 Data
5. Compare Heights of Person 1 and 2 with graph
6. Exit
Enter Selection: 3

Height Data
Name: Lori
Height: 5 feet 6.0 inches
Height in Inches: 66.00 inches
Height in Feet: 5.50 feet

1. Change Person 1 Data
2. Change Person 2 Data
3. Print Person 1 Data
4. Print Person 2 Data
5. Compare Heights of Person 1 and 2 with graph
6. Exit
Enter Selection: 1
Enter the name: Mickey
Enter number of feet: 4
Enter number of inches: -1
Invalid inches - try again!!!
Enter number of inches: -8
Invalid inches - try again!!!
Enter number of inches: 2.5
1. Change Person 1 Data
2. Change Person 2 Data
3. Print Person 1 Data
4. Print Person 2 Data
5. Compare Heights of Person 1 and 2 with graph
6. Exit
Enter Selection: 3

Height Data
Name: Mickey
Height: 4 feet 2.5 inches
Height in Inches: 50.50 inches
Height in Feet: 4.21 feet

1. Change Person 1 Data
2. Change Person 2 Data
3. Print Person 1 Data
4. Print Person 2 Data
5. Compare Heights of Person 1 and 2 with graph
6. Exit
Enter Selection: 4

Height Data
Name: Minnie
Height: 3 feet 2.8 inches
Height in Inches: 38.80 inches
Height in Feet: 3.23 feet

1. Change Person 1 Data
2. Change Person 2 Data
3. Print Person 1 Data
4. Print Person 2 Data
5. Compare Heights of Person 1 and 2 with graph
6. Exit
Enter Selection: 5

Name: Mickey
**************************************************
Name: Minnie
**************************************

1. Change Person 1 Data
2. Change Person 2 Data
3. Print Person 1 Data
4. Print Person 2 Data
5. Compare Heights of Person 1 and 2 with graph
6. Exit
Enter Selection: 6
The End

Solutions

Expert Solution

Program:

import java.util.Scanner;
class Height
{
private String name;
private int feet;
private double inches;

public Height()
{
name = "";
feet = 0;
inches = 0.0;
}
public Height(String name, int feet, double inches)
{
// use the sets to create an object with the values passed in
setName(name);
setHeight(feet,inches);
}
public void setHeight(int newFeet, double newInches)
{
// use the sets to set the feet and inches with the values passed in
setFeet(newFeet);
setInches(newInches);
}
public void setName(String name)
{
this.name = name;
}
public void setFeet(int newFeet)
{
// write the code to set the feet to the formal
// parameter - since the feet can't be less than 0
// make the code check and if that might happen,
// it should set the feet to 0
if(newFeet>0)
{
this.feet=newFeet;
}
else
{
this.feet=0;
}


}
public void setInches(double newInches)
{
// write the code to set the inches to the formal
// parameter - since the inches can't be less than 0
// make the code check and if that might happen,
// it should set the inches to 0
if(newInches>0)
{
this.inches=newInches;
}
else{
this.inches=0.0;
}

}   

public String getName()
{
return name;
}
public int getFeet()
{
return feet;
}
public double getInches()
{
return inches;
}

// write the method totalInches. This method returns the total
// number of inches tall the person is. For example, if the
// person is 5 feet 3.5 inches, this method would return 63.5 since
// 63.5 inches is the height of the person in inches

public double totalInches()
{
return (feet*12)+inches;
}
// write the method totalFeet. This method returns the total feet
// tall the person is as a decimal. For example, if a person
// is 5 feet 6 inches tall, this method would return 5.5, the height
// of the person in feet
public double totalFeet()
{
return (feet+(inches/12));
}


}


public class Main
{
public static Scanner keyboard = new Scanner(System.in);

public static void main(String [] args)
{

Height person1 = new Height();
Height person2 = new Height("John", 6, 2);
// Part A
// Write the statement to change the name of person1
// to be "Lori"
       person1.setName("Lori");

// Part B
// Write the statement to change the height of person1
// to be 5 foot 6 inches
       person1.setHeight(5,6);


int choice = 0;
// Implement each of the methods called from the switch statement below.
// I have put a number in comments, they are only suggestions as
// to the order you might want to implement them

// So for each comment method call, uncomment it and then implement it
// below main. A description is given for each method below main.

// After you have them implemented put the switch statement into a loop so
// that the menu will continue to display on the screen until the
// user chooses option 6
boolean t=true;
       while(t) //loop runs until user choice is 6
       {
       choice = printMenu(); //getting users choice of operation

               switch(choice)
               {
               case 1:
                   changePersonData(person1);
                   break;
               case 2:
                   changePersonData(person2);
                   break;
               case 3:
                   printPersonData(person1);
                   break;
               case 4:
                   printPersonData(person2);
                   break;
               case 5:
                   // 3. Do the method printLineOfGraph which is called from in printTotalInchesGraph - directions are below
                   printTotalInchesGraph(person1, person2);
                   break;
               case 6:
                   t=false;
                   break;
               default:
                   System.out.println("Invalid - Try Again!!!");
                   break;
                   }
         
       }
       // This should only display on the screen after the
       // user has quite the menu
       System.out.println("The End");
   }
// printMenu
// This method prints the menu on the screen. It then
// asks the user to type in their selection (as an integer) and
// the method returns the selection. You do NOT have
// to type all of the menu in - see directions in Zybooks
// to copy and paste
public static int printMenu()
   {
       System.out.print("1.Change person 1 data\n2.Change person 2 data\n 3.print person 1 data\n4.print person 2 data\n5.Compare Heights of Person 1 and person 2 with graph\n6.Exit\nEnter Selection:");
       int c=keyboard.nextInt(); //taking the user option
       return c;
   }


// changePersonData
// This method has an object of the Height class passed in
// It then asks the user to type in a new name and
// calls the method to change the name.
// Then it asks the user to type in the height in feet
// the method will error check the feet and continue asking
// the user to enter the feet until a valid feet is entered.
// the feet must be zero or larger.
// It then calls the method to update the feet
// Lastly, the method asks the user to type in the inches.
// Similarly to the feet, it will error check and continue
// asking to type it in until the user types a valid (zero
// or greater) inches
// Finally it calls the method to change the inches
// See instructions on zbooks, too
// Points for this method might not show up until you put the loop in main

   public static void changePersonData(Height p)
   {
       System.out.println("Enter the name:");
       p.setName(keyboard.next()); //getting the persons name
       System.out.println("Enter number of feet:");
       int f=keyboard.nextInt(); //getting person feet for height
       while(f<0) //repeating the loop until user gives correct value which is 0 or positive integer because height of a person cannot be negative
       {
           System.out.println("Invalid feet - try again!!!");
           f=keyboard.nextInt();
       }
       p.setFeet(f);
       System.out.println("Enter number of inches:");
       double in=keyboard.nextDouble(); //getting inches for height
       while(in<0) //repeating the loop until user gives correct value which is 0 or positive integer because height of a person cannot be negative
       {
           System.out.println("Invalid inches - try again!!!");
           in=keyboard.nextDouble();
       }
       p.setInches(in);
      
   }

// printPersonData
// This method has an object of the height class passed in.
// It uses the object and the methods for the object to print the
// objects information in the form:
/*
Height Data
Name: John
Height: 6 feet 2.0 inches
Height in Inches: 74.00 inches
Height in Feet: 6.17 feet
*/
   public static void printPersonData(Height p)
   {
       System.out.println("Height Data"); // printing the person data
       System.out.println("Name:"+" "+p.getName()); //getting pbject p's name
       System.out.println("Height:"+" "+p.getFeet()+" "+"feet"+" "+p.getInches()+" "+"inches"); // getting height in feets and inches
       System.out.println("Height in Inches:"+" "+String.format("%.2f", p.totalInches())+" "+"inches"); //getting height in inches
        System.out.println("Height in Feet:"+" "+p.totalFeet()+" "+"feet"); // getting height in feets
   }

// This method calls printLineGragh - make sure you uncomment the two method calls in this method
// when you implement printLineOfGraph
public static void printTotalInchesGraph(Height obj1, Height obj2)
{
System.out.println("Name: " + obj1.getName()); // displaying obj1 name
printLineOfGraph(obj1.totalInches()); //getting height of obj1 in inches to draw the graph
       System.out.println();//for new line
System.out.println("Name: " + obj2.getName()); // displaying obj2 name
printLineOfGraph(obj2.totalInches()); //getting height of obj2 in inches to draw the graph
       System.out.println();//for new line

}

// printLineOfGraph
// This method has a double passed into it
// It will print as many asterisks as the value passed in
// for example: printLineOfGraph(4.8) will print ****
// Notice: it does not worry about the decimal, it just
// prints the number of asterisks as the whole number
// after printing the asterisks in a row it prints a new line
// you can typecast the double formal parameter and
// store it in a new variable, and then use that in the loop
   public static void printLineOfGraph(double x)
   {
       int r=(int)x; //converting double value to integer
       for(int i=0;i<r;i++)
           System.out.print('*');
   }
} // End of Main class

OUTPUT :

PROGRAM SCREENSHOTS:


Related Solutions

Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not reduce fractions, do not use "const," do not provide any constructors, do not use three separate files. You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. It is required that you provide these member...
Write a Java class called Employee (Parts of the code is given below), which has three...
Write a Java class called Employee (Parts of the code is given below), which has three private fields firstName (String), lastName (String) and yearsEmployed (double). Implement accessor/mutator methods for the private fields and toString() method, that returns a String representation, which contains employee name and years she was employed. public class Employee { private String firstName; ... } Write a client program called EmployeeClient that creates an instance of Employee class, sets values for the fields (name: John Doe, yearsEmployed:...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public final class Account implements Comparable {     private String firstName;     private String lastName;     private int accountNumber;     private double balance;     private boolean isNewAccount;     public Account(             String firstName,             String lastName,             int accountNumber,             double balance,             boolean isNewAccount     ) {         this.firstName = firstName;         this.lastName = lastName;         this.accountNumber = accountNumber;         this.balance = balance;         this.isNewAccount = isNewAccount;     }     /**      * TO DO: override equals      */     @Override     public boolean equals(Object other) {...
Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
Given the following code below: public class Calculation { //method that returns cube of the given...
Given the following code below: public class Calculation { //method that returns cube of the given number public int findMax(int arr[]){    int max=0;    for(int i=1;i<arr.length;i++){        if(max<arr[i]){           max=arr[i]; }     }     return max;   } //method that returns cube of the given number   public static int cube(int n){        return n*n*n;     }   } (5 points) Define the class CalculationTest a subclass of TestCase (10 points) Define a setUp() method (20 points) Define a test method testfindMax that exercises Calculation.findMax() in Calculation class (5 points)Define...
java code: Problem 1: Create a class called Elevator that can be moved between floors in...
java code: Problem 1: Create a class called Elevator that can be moved between floors in an N-storey building. Elevator uses a constructor to initialize the number of floors (N) in the building when the object is instantiated. Elevator also has a default constructor that creates a five storey building. The Elevator class has a termination condition that requires the elevator to be moved to the main (i.e., first) floor when the object is cleaned up. Write a finalize() method...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT