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
- street, city, state, and zip
- All as Strings
Create the following three setAddress() methods.
- public void setAddress(String street, String city, String state, String zip)
- public void setAddress(String city, String state, String zip)
- public void setAddress(String state, String zip)
- 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”.
- 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.
- 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
- Both height and weight should be of type double
- Height will be in inches and weight will be in pounds
Create the following setHeight() and setWeight() methods.
- public void setHeight(double inches)
- public void setHeight(int inches)
- public void setHeight(int feet, int inches)
- public void setHeightCm(double centimeters)
- public void setWeight(double pounds)
- public void setWeight(int pounds)
- public void setWeightKilo(double kilograms)
- 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);
}
}
Related Samples
Explore our sample solutions at ProgrammingHomeworkHelp.com! From Java assignments to complex algorithm challenges, our examples showcase the quality and depth of our work. See firsthand how we can help you achieve top grades and deepen your programming knowledge.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java