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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

(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
public class Address {
    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:
private Address address;
  • Also alter your multiple argument constructor to pass in an address variable:  
    public Person(... Address newAddress)
  • 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.
public Address getAddress() {
    //fill in method body here
}

public void setAddress(Address a)
    //fill in method body here
}
  • Finally, alter your toString method for Person as follows:
    @Override public String toString() {
        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
Sample Output (Note that user input may vary):
 
Welcome!

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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY