Repeat Programming Project 2 in Chapter 5 This time add the
Repeat Programming Project 2 in Chapter 5. This time, add the following four constructor methods: one for each instance variable, one with two parameters for the two instance variables, and a default constructor. Be sure that each constructor sets all the instance variables. write a driver program to test each of the methods, including each of the four constructors and at least one true or false case for each of the test method
this is the project 2 in chapter 5
your output should be similar to this
DO NOT get fooled when it runs. The run will stop midway and ask for input, comply, then the program will continue to completion.
jGRASP exec: java PersonCh6Test Test case 1: default constructor and writeOutput() method Results of default constructor. Should be name \"No name\" and age = 0 Name No name Age-O Test case 2: readlnput() method What is the person\'s name? Bob What is the person\'s age? 42 Verify name and age Should be whatever you just entered Name = Bob Age 42Solution
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
class PersonCh6
{
private String name;
private int age;
public PersonCh6() //default constructor
{
name =\"No name\";
age=0;
}
public PersonCh6(String n) //constructor with name
{
name=n;
}
public PersonCh6(int a) //constructor with age
{
name =\"No name\";
age=a;
}
public PersonCh6(String n,int a) //constructor with and and age
{
name=n;
age=a;
}
public void writeOutput() //display name and age
{
System.out.println(\"name =\"+name+\" and age=\"+age);
}
public void readInput() //input name and age using Scanner object
{
Scanner input = new Scanner(System.in);
System.out.println(\"what is the Person\'s name?\");
name= input.next();
System.out.println(\"what is the Person\'s age?\");
age = input.nextInt();
}
public void setName(String n) //set and get methods for name and age
{
name = n;
}
public void setAge(int a)
{
age = a;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public void set(String n,int a)
{
name =n;
age = a;
}
public boolean isSameName(PersonCh6 p)
{
if(name == p.name)
return true;
else
return false;
}
public boolean isOlderThan(PersonCh6 p)
{
if(age > p.age)
return true;
else
return false;
}
public boolean isYoungerThan(PersonCh6 p)
{
if(age < p.age)
return true;
else
return false;
}
public boolean equals(PersonCh6 p)
{
if(name == p.name && age == p.age)
return true;
else
return false;
}
}
class PersonCh6Test
{
public static void main(String[] args)
{
System.out.println();
System.out.println(\"Test case 1: default constructor and\");
System.out.println(\"writeOutput() method.\");
System.out.println();
PersonCh6 secretAgent1 = new PersonCh6();
System.out.println(\"Results of default constructor:\");
System.out.println(\"Should be name = \\\"No name\\\" and\");
System.out.println(\"age = 0.\");
System.out.println();
secretAgent1.writeOutput();
System.out.println();
System.out.println(\"====================================\");
System.out.println(\"Test case 2: readInput() method.\");
secretAgent1.readInput();
System.out.println();
System.out.println(\"Verify name and age.\");
System.out.println(\"Should be whatever you just entered.\");
System.out.println();
secretAgent1.writeOutput();
System.out.println();
System.out.println(\"====================================\");
System.out.println(\"Test case 3: constructor with just name.\");
PersonCh6 secretAgent2 = new PersonCh6(\"Boris\");
System.out.println();
System.out.println(\"Verify name = Boris and age = 0.\");
System.out.println();
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"====================================\");
System.out.println(\"Test case 4: constructor with just age.\");
System.out.println();
System.out.println(\"Verify name is \\\"No name\\\" and age = 40.\");
System.out.println();
PersonCh6 secretAgent3 = new PersonCh6(40);
secretAgent3.writeOutput();
System.out.println();
System.out.println(\"====================================\");
System.out.println(\"Test case 5: constructor with both name & age.\");
System.out.println();
System.out.println(\"Verify name = Natasha & age 39.\");
System.out.println();
PersonCh6 secretAgent4 = new PersonCh6(\"Natasha\", 39);
secretAgent4.writeOutput();
System.out.println();
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 6:\");
System.out.println(\"getName: should be Natasha.\");
System.out.println();
System.out.println(\"Verify results: \"+ secretAgent4.getName());
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 7:\");
System.out.println(\"getAge: should be 39\");
System.out.println();
System.out.println(\"Verify results: \"
+ secretAgent4.getAge());
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 8:\");
System.out.println(\"setName to Rocky\");
secretAgent4.setName(\"Rocky\");
System.out.println();
System.out.println(\"Verify results with getName(): \"
+ secretAgent4.getName());
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 9:\");
System.out.println(\"setAge to 99\");
secretAgent4.setAge(99);
System.out.println();
System.out.println(\"Verify results: \"
+ secretAgent4.getAge());
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 10: set method (both name & age)\");
System.out.println(\"and equals (both name and age)\");
System.out.println(\"Making two persons\"
+ \" with same name and age:\");
secretAgent1.set(\"Bullwinkle\", 10);
secretAgent2.set(\"Bullwinkle\", 10);
System.out.println(\"First person: \");
secretAgent1.writeOutput();
System.out.println(\"Second person: \");
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"Verify results: should be true.\");
System.out.println(secretAgent1.equals(secretAgent2));
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 11:\");
System.out.println(\"equals (both name and age)\");
System.out.println(\"with different names, same ages.\");
secretAgent2.setName(\"Boris\");
System.out.println(\"First person: \");
secretAgent1.writeOutput();
System.out.println(\"Second person: \");
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"Verify results: should be false.\");
System.out.println(secretAgent1.equals(secretAgent2));
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 12:\");
System.out.println(\"equals (both name and age)\");
System.out.println(\"with different ages, same names.\");
secretAgent2.setName(\"Bullwinkle\");
secretAgent2.setAge(98);
System.out.println(\"First person: \");
secretAgent1.writeOutput();
System.out.println(\"Second person: \");
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"Verify results: should be false.\");
System.out.println(secretAgent1.equals(secretAgent2));
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 13:\");
System.out.println(\"equals (both name and age)\");
System.out.println(\"with different names and ages.\");
secretAgent2.setName(\"Boris\");
System.out.println(\"First person: \");
secretAgent1.writeOutput();
System.out.println(\"Second person: \");
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"Verify results: should be false.\");
System.out.println(secretAgent1.equals(secretAgent2));
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 14:\");
System.out.println(\"isSameName\");
System.out.println(\"with same names and ages.\");
secretAgent2.set(\"Bullwinkle\", 10);
System.out.println(\"First person: \");
secretAgent1.writeOutput();
System.out.println(\"Second person: \");
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"Verify results: should be true.\");
System.out.println(secretAgent1.isSameName(secretAgent2));
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 15:\");
System.out.println(\"isSameName\");
System.out.println(\"with same names, different ages.\");
secretAgent2.setAge(98);
System.out.println(\"First person: \");
secretAgent1.writeOutput();
System.out.println(\"Second person: \");
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"Verify results: should be true.\");
System.out.println(secretAgent1.isSameName(secretAgent2));
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 16:\");
System.out.println(\"isSameName\");
System.out.println(\"with different names, same ages.\");
secretAgent2.set(\"Boris\", 10);
System.out.println(\"First person: \");
secretAgent1.writeOutput();
System.out.println(\"Second person: \");
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"Verify results: should be false.\");
System.out.println(secretAgent1.isSameName(secretAgent2));
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 17:\");
System.out.println(\"isSameName\");
System.out.println(\"with different names, different ages.\");
secretAgent2.set(\"Boris\", 5);
System.out.println(\"First person: \");
secretAgent1.writeOutput();
System.out.println(\"Second person: \");
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"Verify results: should be false.\");
System.out.println(secretAgent1.isSameName(secretAgent2));
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 18:\");
System.out.println(\"isOlderThan\");
System.out.println(\"with 1st person older than the other.\");
System.out.println(\"First person: \");
secretAgent1.writeOutput();
System.out.println(\"Second person: \");
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"Verify results: should be true.\");
System.out.println(secretAgent1.isOlderThan(secretAgent2));
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 19:\");
System.out.println(\"isOlderThan\");
System.out.println(\"with same ages.\");
secretAgent2.setAge(10);
System.out.println(\"First person: \");
secretAgent1.writeOutput();
System.out.println(\"Second person: \");
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"Verify results: should be false.\");
System.out.println(secretAgent1.isOlderThan(secretAgent2));
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 20:\");
System.out.println(\"isOlderThan\");
System.out.println(\"with 1st person younger than the other.\");
secretAgent2.setAge(100);
System.out.println(\"First person: \");
secretAgent1.writeOutput();
System.out.println(\"Second person: \");
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"Verify results: should be false.\");
System.out.println(secretAgent1.isOlderThan(secretAgent2));
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 21:\");
System.out.println(\"isYoungerThan\");
System.out.println(\"with 1st person younger than the other.\");
System.out.println(\"First person: \");
secretAgent1.writeOutput();
System.out.println(\"Second person: \");
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"Verify results: should be true.\");
System.out.println(secretAgent1.isYoungerThan(secretAgent2));
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 22:\");
System.out.println(\"isYoungerThan\");
System.out.println(\"with same ages.\");
secretAgent2.setAge(10);
System.out.println(\"First person: \");
secretAgent1.writeOutput();
System.out.println(\"Second person: \");
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"Verify results: should be false.\");
System.out.println(secretAgent1.isYoungerThan(secretAgent2));
System.out.println(\"====================================\");
System.out.println();
System.out.println(\"Test case 20:\");
System.out.println(\"isYoungerThan\");
System.out.println(\"with 1st person older than the other.\");
secretAgent2.setAge(9);
System.out.println(\"First person: \");
secretAgent1.writeOutput();
System.out.println(\"Second person: \");
secretAgent2.writeOutput();
System.out.println();
System.out.println(\"Verify results: should be false.\");
System.out.println(secretAgent1.isYoungerThan(secretAgent2));
}
}
output:
Success time: 0.06 memory: 711680 signal:0






