Copy and paste the new test file below into PersonTest.java (you can erase the old contents of the file), and update the incomplete line.
(Java)
Look at the following Person.java code:
import java.util.Scanner;
import java.io.*;
class Person {
public String name;
public int age;
public String gender;
public void greeting() {
System.out.println("Hi, my name is " + name + "!");
}
public void greeting(String otherName) {
System.out.println("Hi, " + otherName + "!");
}
@Override
public String toString() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Gender: " + gender);
return "";
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public String getGender() {
return this.gender;
}
}
- Let's make some changes to this class.
- First, we are going to add a new class called Address to interact with the Person class.
- Add a new file to the folder where Person and PersonTest are stored. Name this file Address.java
private int number;
private String street;
/**
* Default constructor for the Address class
* Initializes number to 0 and street to
* "Street unknown"
*/
public Address() {
//write default constructor here
}
/**
* Constructor for the Address class
* @param number the street number
* @param street the name of the street
*/
public Address(int theNumber, String theStreet) {
//write two-argument constructor here
}
/**
* Returns the street number portion
* of the address
* @return the street number
*/
public int getNumber() {
return number;
}
/**
* Returns the name portion of the
* address
* @return the name of the street
*/
public String getStreet() {
return street;
}
//add setters here
/**
* Displays an address in the format number + " " + street;
* @return a String containing the address
*/
//Write a toString() method for Address here!
}
- Next, open your Person.java class.
- Add an additional member variable:
- Also alter your multiple argument constructor to pass in an address variable:
- Update this constructor:
- Assign a value to the new member variable address
- Additionally, update your default constructor to assign address a value of new Address(). In other words, you will call the Address default constructor to initialize the address member variable to have default values.
- Next, add a getter and setter method for the address variable to your Person class.
//fill in method body here
}
public void setAddress(Address a)
//fill in method body here
}
- Finally, alter your toString method for Person as follows:
return "Name: " + name +
"\nAge: " + age +
"\nGender: " + gender +
"\nAddress: " + //fill in here!;
}
- Copy and paste the new test file below into PersonTest.java (you can erase the old contents of the file), and update the incomplete line.
* PersonTest.java
* @author
* CIS 36B, Activity 8.2
*/
import java.util.Scanner;
public class PersonTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name, gender, street;
int age, number;
Person person1;
System.out.print("Welcome!\n\nEnter your name: ");
name = input.nextLine();
System.out.print("Enter your age: ");
age = input.nextInt();
System.out.print("Enter your gender: ");
input.nextLine();
gender = input.nextLine();
System.out.print("Enter the street number of your address: ");
number = input.nextInt();
System.out.print("Enter the name of your street: ");
input.nextLine();
street = input.nextLine();
Address address = //call Address constructor here
person1 = //call Person constructor here
System.out.println("\nYour Summary:\n" + person1);
input.close();
}
}
- When you get the correct output (as shown below), upload Address.java, Person.java and PersonTest.java to Canvas
Enter your name: Abdul Darwish
Enter your age: 23
Enter your gender: Male
Enter the street number of your address: 2141
Enter the name of your street: W. Elm Ave
Your Summary:
Name: Abdul Darwish
Age: 23
Gender: Male
Address: 2141 W. Elm Ave
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 1 images