×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Create a Program to Implement Overloading Methods in Java Assignment Solution

July 15, 2024
Dr. Marlowe Emberly
Dr. Marlowe
🇬🇧 United Kingdom
Java
Dr. Marlowe Emberly is a trailblazer in the field of computer science, having earned her PhD from the University of Cambridge, UK, renowned for its excellence in research and academia. With eight years of experience in the industry, Marlowe has completed over 800 Java Homework assignments with unparalleled dedication and expertise.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Use Python libraries effectively by importing only what you need. For example, if you're working with data, using libraries like pandas and numpy can save time and simplify complex tasks like data manipulation and analysis.
News
In 2024, the Biden-Harris Administration has expanded high-dosage tutoring and extended learning programs to boost academic achievement, helping programming students and others recover from pandemic-related setbacks. These initiatives are funded by federal resources aimed at improving math and literacy skills​

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); } }

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.