+1 (315) 557-6473 

Create a Program to Implement Overloading Methods in Java Assignment Solution.


Instructions

Objective
Write a java assignment program to implement overloading methods.

Requirements and Specifications

Add on to Lab 1. For Lab 2, lets add methods to the person class to learn more about overloading methods. We need to add methods to set a person’s address and set their height and weight several different ways using method overloading, as discussed below.
Lab Requirements:
Make a copy of your Lab1 to add functionality to
Add private instance variables to the Person class to store and address
  1. street, city, state, and zip
  2. All as Strings
Create the following three setAddress() methods.
  1. public void setAddress(String street, String city, String state, String zip)
  2. public void setAddress(String city, String state, String zip)
  3. public void setAddress(String state, String zip)
  4. Allow a client to supply a null or empty street and city values. If the values are null or empty strings, set the value to “Unknown”.
  5. If the client uses a null or empty state or zip, the program should tell the user that the values must be used and then exit.
  6. Keep in mind you can call existing methods to simplify your logic and make the code easier to maintain
Create a method, outputAddress(), that outputs the persons address as shown below…
Street: 3219 Lenard St
City: Yosomite
State: NY
Zip: 38203
Add private instance variables to store a person’s height and weight
  1. Both height and weight should be of type double
  2. Height will be in inches and weight will be in pounds
Create the following setHeight() and setWeight() methods.
  1. public void setHeight(double inches)
  2. public void setHeight(int inches)
  3. public void setHeight(int feet, int inches)
  4. public void setHeightCm(double centimeters)
  5. public void setWeight(double pounds)
  6. public void setWeight(int pounds)
  7. public void setWeightKilo(double kilograms)
  8. Keep in mind you can call existing methods to simplify your logic and make the code easier to maintain
Create a method, outputHeight(), that outputs the person’s height in feet and inches as shown below…
Height 5' 7''
Create a method, outputWeight(), that outputs the person’s weight to 2 decimal places in lbs as shown below…
Weight: 210.76 lbs
All code should be put in a package names code.class1.your_name
Create a driver method or class to test you program
Source Code
package code.class1.yourname;
public class Person {
private int age;
private String name;
private String street;
private String city;
private String state;
private String zip;
private double height;
private double weight;
@Override
public String toString() {
return "Name " + this.name + "Age " + this.age;
}
public Person() {
this("No name", 0);
}
public Person(String newName, int newAge) {
setName(name);
setAge(age);
setAddress(null, null, "?", "?");
}
public void setHeight(double inches) {
height = inches;
}
public void setHeight(int inches) {
setHeight((double) inches);
}
public void setHeight(int feet, int inches) {
setHeight(inches + feet * 12);
}
public void setHeightCm(double centimeters) {
setHeight(centimeters / 2.54);
}
public void setWeight(double pounds) {
weight = pounds;
}
public void setWeight(int pounds) {
setWeight((double) pounds);
}
public void setWeightKilo(double kilograms) {
setWeight(kilograms / 2.20462);
}
public void outputHeight() {
int feet = (int) (height / 12);
int inches = (int) height % 12;
System.out.println("Height " + feet + "' " + inches + "\"");
}
public void outputWeight() {
System.out.printf("Weight: %.2f lbs\n", weight);
}
public void setAddress(String street, String city, String state, String zip) {
this.street = street == null || street.isEmpty() ? "Unknown" : street;
this.city = city == null || city.isEmpty() ? "Unknown" : city;
if (state == null || state.isEmpty()) {
System.out.println("Error: A state is required.");
System.exit(0);
}
if (zip == null || zip.isEmpty()) {
System.out.println("Error: A zip is required.");
System.exit(0);
}
this.state = state;
this.zip = zip;
}
public void setAddress(String city, String state, String zip) {
setAddress(street, city, state, zip);
}
public void setAddress(String state, String zip) {
setAddress(street, city, state, zip);
}
public void outputAddress() {
System.out.println("Street: " + street);
System.out.println("City: " + city);
System.out.println("State: " + state);
System.out.println("Zip: " + zip);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setName(String nameFirst, String nameLast) {
this.name = nameFirst + nameLast;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static Person createAdult() {
return new Person("Adult", 21);
}
public void setPerson(String name, int age) {
setName(name);
setAge(age);
}
public boolean compareName(Person otherPerson) {
if (name.equals(otherPerson.name)) {
return true;
} else {
return false;
}
}
public boolean samePerson(Person otherPerson) {
if (name.equals(otherPerson.name) && age == otherPerson.age) {
return true;
} else {
return false;
}
}
public int compareAge(Person otherPerson) {
return otherPerson.age - this.age;
}
public static Person createTeenager() {
return new Person("A teenager", 15);
}
public static Person createAdolescent() {
return new Person("A Adolescent", 9);
}
public static Person createPreschooler() {
return new Person("A preschooler", 5);
}
public static Person createToddler() {
return new Person("A toddler", 2);
}
}