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 code that i have to add four constructor methods to
import java.util.*;
public class PersonImproved// Counter is the name of the class and i saved it in a file named
PersonImproved.
{
//Data members
private String name;
private int age;
public void readInput()
{
Scanner keyboard = new Scanner(System.in);
System.out.println(\"What is the person\'s name?\");// This gets the program ready for
keyboard input by telling us that there is a new scanner called keyboard, that is accepting user
input.
name = keyboard.nextLine();
System.out.println(\"What is the person\'s age?\");
age = keyboard.nextInt();
while (age < 0)
{
System.out.println(\"Age cannot be negative.\");
System.out.println(\"Reenter age:\");
age = keyboard.nextInt();
}
}
public void writeOutput()
{
System.out.println(\"Name = \" + name);
System.out.println(\"Age = \" + age);
}
//method to set non negative age
public void set(String newName, int newAge)
{
name = newName;
if (newAge >= 0)
age = newAge;
else
{
System.out.println(\"ERROR: Used a negative age.\");
System.exit(0);
}
}
//method to setting name
public void setName(String newName)
{
name = newName;
}
//method to set age
public void setAge(int newAge)
{
if (newAge >= 0)
age = newAge;
else
{
System.out.println(\"ERROR: Used a negative age.\");
System.exit(0);
}
} //to get name
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
/*Boolean tests for if two persons are equal (same name and age), if two persons
have the same name, if two persons have the same age, if one person is older
than another, and if one person is younger than another.*/
public boolean equals(PersonImproved another)
{
return(this.name.equalsIgnoreCase(another.name)
&& this.getAge() == another.getAge());
}
public boolean isSameName(PersonImproved another)
{
return(this.name.equalsIgnoreCase(another.name));
}
public boolean isSameAge(PersonImproved another)
{
return(this.getAge() == another.getAge());
}
public boolean isOlderThan(PersonImproved another)
{
return(this.getAge() > another.getAge());
}
public boolean isYoungerThan(PersonImproved
another)
{
return(this.getAge() < another.getAge());
}
}
and this is the test file
public 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\");
S.
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
Repeat Programming Project 2 in Chapter 5. This time, add the follow.pdf
1. 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 code that i have to add four constructor methods to
import java.util.*;
public class PersonImproved// Counter is the name of the class and i saved it in a file named
PersonImproved.
{
//Data members
private String name;
private int age;
public void readInput()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the person's name?");// This gets the program ready for
keyboard input by telling us that there is a new scanner called keyboard, that is accepting user
input.
name = keyboard.nextLine();
System.out.println("What is the person's age?");
age = keyboard.nextInt();
while (age < 0)
{
System.out.println("Age cannot be negative.");
System.out.println("Reenter age:");
age = keyboard.nextInt();
}
}
public void writeOutput()
{
System.out.println("Name = " + name);
System.out.println("Age = " + age);
}
//method to set non negative age
2. public void set(String newName, int newAge)
{
name = newName;
if (newAge >= 0)
age = newAge;
else
{
System.out.println("ERROR: Used a negative age.");
System.exit(0);
}
}
//method to setting name
public void setName(String newName)
{
name = newName;
}
//method to set age
public void setAge(int newAge)
{
if (newAge >= 0)
age = newAge;
else
{
System.out.println("ERROR: Used a negative age.");
System.exit(0);
}
} //to get name
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
3. /*Boolean tests for if two persons are equal (same name and age), if two persons
have the same name, if two persons have the same age, if one person is older
than another, and if one person is younger than another.*/
public boolean equals(PersonImproved another)
{
return(this.name.equalsIgnoreCase(another.name)
&& this.getAge() == another.getAge());
}
public boolean isSameName(PersonImproved another)
{
return(this.name.equalsIgnoreCase(another.name));
}
public boolean isSameAge(PersonImproved another)
{
return(this.getAge() == another.getAge());
}
public boolean isOlderThan(PersonImproved another)
{
return(this.getAge() > another.getAge());
}
public boolean isYoungerThan(PersonImproved
another)
{
return(this.getAge() < another.getAge());
}
}
and this is the test file
public 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.");
4. 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();
5. 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();
6. 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));
7. 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);
8. 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("====================================");
9. 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: ");
10. 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");
11. 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));
}
}
the output should look similar to this
Solution
import java.util.Scanner;
public class PersonImproved {
// Data members
private String name;
private int age;
/**
* Default constructor
12. */
public PersonImproved() {
this.name = "No name";
this.age = 0;
}
/**
* Parameterized constructor with name
* @param name
*/
public PersonImproved(String name) {
this.name = name;
this.age = 0;
}
/**
* Parameterized constructor with age
* @param age
*/
public PersonImproved(int age) {
this.name = "No name";
this.age = age;
}
/**
* Parameterized constructor with name and age
* @param name
* @param age
*/
public PersonImproved(String name, int age) {
this.name = name;
this.age = age;
}
/**
* Gets the name and age from the user
*/
13. public void readInput() {
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the person's name?");// This gets the
// program ready for
// keyboard input by
// telling us that
// there is a new
// scanner called
// keyboard, that is
// accepting user
// input.
name = keyboard.nextLine();
System.out.println("What is the person's age?");
age = keyboard.nextInt();
while (age < 0) {
System.out.println("Age cannot be negative.");
System.out.println("Reenter age:");
age = keyboard.nextInt();
}
//Close scanner
keyboard.close();
}
/**
* Displays the name and age
*/
public void writeOutput() {
System.out.println("Name = " + name);
System.out.println("Age = " + age);
}
/**
* method to set non negative age
* @param newName
* @param newAge
*/
public void set(String newName, int newAge) {
14. name = newName;
if (newAge >= 0)
age = newAge;
else {
System.out.println("ERROR: Used a negative age.");
System.exit(0);
}
}
/**
* method to setting name
* @param newName
*/
public void setName(String newName) {
name = newName;
}
/**
* method to set age
* @param newAge
*/
public void setAge(int newAge) {
if (newAge >= 0)
age = newAge;
else {
System.out.println("ERROR: Used a negative age.");
System.exit(0);
}
}
/**
* Gets the name
* @return
*/
public String getName() {
return name;
}
/**
* Gets the age
15. * @return
*/
public int getAge() {
return age;
}
/*
* Boolean tests for if two persons are equal (same name and age), if two
* persons have the same name, if two persons have the same age, if one
* person is older than another, and if one person is younger than another.
*/
public boolean equals(PersonImproved another) {
return (this.name.equalsIgnoreCase(another.name) && this.getAge() == another.getAge());
}
public boolean isSameName(PersonImproved another) {
return (this.name.equalsIgnoreCase(another.name));
}
public boolean isSameAge(PersonImproved another) {
return (this.getAge() == another.getAge());
}
public boolean isOlderThan(PersonImproved another) {
return (this.getAge() > another.getAge());
}
public boolean isYoungerThan(PersonImproved another) {
return (this.getAge() < another.getAge());
}
}
public 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();
PersonImproved secretAgent1 = new PersonImproved();
System.out.println("Results of default constructor:");
System.out.println("Should be name = "No name" and");
System.out.println("age = 0.");
16. 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.");
PersonImproved secretAgent2 = new PersonImproved("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();
PersonImproved secretAgent3 = new PersonImproved(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();
PersonImproved secretAgent4 = new PersonImproved("Natasha", 39);
secretAgent4.writeOutput();
System.out.println();
17. 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: ");
18. 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.");
19. 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();
20. 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));
21. 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();
22. 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:
Test case 1: default constructor and
writeOutput() method.
Results of default constructor:
Should be name = "No name" and
23. age = 0.
Name = No name
Age = 0
====================================
Test case 2: readInput() method.
What is the person's name?
John
What is the person's age?
10
Verify name and age.
Should be whatever you just entered.
Name = John
Age = 10
====================================
Test case 3: constructor with just name.
Verify name = Boris and age = 0.
Name = Boris
Age = 0
====================================
Test case 4: constructor with just age.
Verify name is "No name" and age = 40.
Name = No name
Age = 40
====================================
Test case 5: constructor with both name & age.
Verify name = Natasha & age 39.
Name = Natasha
Age = 39
====================================
Test case 6:
getName: should be Natasha.
Verify results: Natasha
====================================
Test case 7:
getAge: should be 39
Verify results: 39
24. ====================================
Test case 8:
setName to Rocky
Verify results with getName(): Rocky
====================================
Test case 9:
setAge to 99
Verify results: 99
====================================
Test case 10: set method (both name & age)
and equals (both name and age)
Making two persons with same name and age:
First person:
Name = Bullwinkle
Age = 10
Second person:
Name = Bullwinkle
Age = 10
Verify results: should be true.
true
====================================
Test case 11:
equals (both name and age)
with different names, same ages.
First person:
Name = Bullwinkle
Age = 10
Second person:
Name = Boris
Age = 10
Verify results: should be false.
false
====================================
Test case 12:
equals (both name and age)
with different ages, same names.
25. First person:
Name = Bullwinkle
Age = 10
Second person:
Name = Bullwinkle
Age = 98
Verify results: should be false.
false
====================================
Test case 13:
equals (both name and age)
with different names and ages.
First person:
Name = Bullwinkle
Age = 10
Second person:
Name = Boris
Age = 98
Verify results: should be false.
false
====================================
Test case 14:
isSameName
with same names and ages.
First person:
Name = Bullwinkle
Age = 10
Second person:
Name = Bullwinkle
Age = 10
Verify results: should be true.
true
====================================
Test case 15:
isSameName
with same names, different ages.
26. First person:
Name = Bullwinkle
Age = 10
Second person:
Name = Bullwinkle
Age = 98
Verify results: should be true.
true
====================================
Test case 16:
isSameName
with different names, same ages.
First person:
Name = Bullwinkle
Age = 10
Second person:
Name = Boris
Age = 10
Verify results: should be false.
false
====================================
Test case 17:
isSameName
with different names, different ages.
First person:
Name = Bullwinkle
Age = 10
Second person:
Name = Boris
Age = 5
Verify results: should be false.
false
====================================
Test case 18:
isOlderThan
with 1st person older than the other.
27. First person:
Name = Bullwinkle
Age = 10
Second person:
Name = Boris
Age = 5
Verify results: should be true.
true
====================================
Test case 19:
isOlderThan
with same ages.
First person:
Name = Bullwinkle
Age = 10
Second person:
Name = Boris
Age = 10
Verify results: should be false.
false
====================================
Test case 20:
isOlderThan
with 1st person younger than the other.
First person:
Name = Bullwinkle
Age = 10
Second person:
Name = Boris
Age = 100
Verify results: should be false.
false
====================================
Test case 21:
isYoungerThan
with 1st person younger than the other.
28. First person:
Name = Bullwinkle
Age = 10
Second person:
Name = Boris
Age = 100
Verify results: should be true.
true
====================================
Test case 22:
isYoungerThan
with same ages.
First person:
Name = Bullwinkle
Age = 10
Second person:
Name = Boris
Age = 10
Verify results: should be false.
false
====================================
Test case 20:
isYoungerThan
with 1st person older than the other.
First person:
Name = Bullwinkle
Age = 10
Second person:
Name = Boris
Age = 9
Verify results: should be false.
false