In: Computer Science
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
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 }
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: ");
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
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: