(Java) Take a look

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Topic Video
Question

(Java)

Take a look at the following Person.java code:

import java.util.Scanner;

import java.io.*;

 

class Person {

publicStringname;

publicintage;

publicStringgender;

 

// add additional member

privateAddressaddress;

 

// default constructor

publicPerson(){

name="unkonwn";

age=0;

gender="NA";

address=newAddress();

}

 

// argument constructor

publicPerson(Stringname,intage,Stringgender,Addressaddress){

this.name=name;

this.age=age;

this.gender=gender;

this.address=address;

}

 

publicvoidgreeting(){

System.out.println("Hi, my name is " + name + "!");

}

 

publicvoidgreeting(StringotherName){

System.out.println("Hi, " + otherName + "!");

}

 

publicvoidsetName(Stringname){

this.name=name;

}

 

publicvoidsetAge(intage){

this.age=age;

}

 

publicvoidsetGender(Stringgender){

this.gender=gender;

}

 

publicStringgetName(){

returnthis.name;

}

 

publicintgetAge(){

returnthis.age;

}

 

publicStringgetGender(){

returnthis.gender;

}

 

publicAddressgetAddress(){

// fill in method body here

returnaddress;

}

 

publicvoidsetAddress(Addressa){

// fill in method body here

address=a;

}

 

@Override

publicStringtoString(){

return"Name: "+name+"\nAge: "+age+"\nGender: "+gender+"\nAddress: "+address;// fill in here!;

}

}

 

  • Let's update it to contain a copy constructor, as follows:
    • It takes in a Person object as a parameter
    • It assigns this to have the same age, name and gender as the parameter
    • It assigns address a deep copy of the address field. In other words, you will need to call the Address constructor to achieve the deep copy.
    • See example above of a deep copy in the Copy Constructor portion of the lesson notes.
  • 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, Activity 9.1
* @author
* CIS 36B
*/

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: ");
        gender = input.next();
        
        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 = new Address(number, street);
       
        person1 = new Person(name, age, gender, address);
    
        Person person2 = //fill in line to call copy constructor here
       
        System.out.print("\nEnter the name of another person with your age and gender: ");
        name = input.nextLine();

       
        System.out.print("Enter " + name + "'s street number: ");
        number = input.nextInt();
       
        System.out.print("Enter " + name + "'s street name: ");
        input.nextLine();
        street = input.nextLine();

        person2.setName(name);
        person2.getAddress().setNumber(number);
        person2.getAddress().setStreet(street);

           System.out.println("\nYour Summary:\n" + person1);       
        System.out.println("\nAnother student with your "
            + "same age and gender: \n" + person2);

        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: Wen Li
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

Enter the name of another person with your age and gender: C.J. Nguyen
Enter C.J. Nguyen's street number: 4567
Enter C.J. Nguyen's street name: S. 7th St

Your Summary:
Name: Wen Li
Age: 23
Gender: Male
Address: 2141 W. Elm Ave

Another student with your same age and gender:
Name: C.J. Nguyen
Age: 23
Gender: Male
Address: 4567 S. 7th St

Expert Solution
Step 1

I have answered this question in step 2.

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education