Hi I need help with practice programming assignment number 2
Hi, I need help with practice programming assignment number 2 on page 476 from chapter 6 of the 7th edition of the book , java:an introduction to problem solving and programming. . It tells me to a project from chapter 5 where I have to make a class called person, or in my case I have to call it PersonCh6. but it says to add the following four constructor methods: one for each instance variable, One with two parameters for the two instance variables, and a default constructor, It says to be sure that each constructor sets all of the instance variables. Our teacher has provided us with a driver program to test each methods, and we have to get the class to work with the test program provided to us, we cant change any of the test program code at all.
I did the original project from chapter 5 and got it right, but I am having a hard time with this new one.
here is the code I wrote for the person or \"PersonCh6\" class for this new assignment
import java.util.Scanner;
public class PersonCh6 // class name
{
private String name ;
private int age;
public void readInput()
{
System.out.println(\"What is the person\'s name?\"); // outputs to screen
name = scanner.nextLine();
System.out.println(\"What is the person\'s age?\");
age = scanner.nextInt();
}
public void writeOutput()
{
System.out.println(\"Name : \"+name); // outputs name and age user typed to screen
System.out.println(\"Age : \"+age);
}
public defaultPerson() //sets default values
{
name = null;
age = 0;
}
public newName(String newName)// constructs name attribute of person
{
name = newName;
age = 0;
}
public newAge( int newAge); // constructs age attribute of person
{
age = newAge;
name = null;
}
public Person(String newName, int newAge )//constructs name and age of person
{
name = newName;
age = newAge;
}
public boolean isSamePerson(Person newPerson)
{
if(name == newPerson.getName() && age = newPerson.getAge())
return true;
return false;
}
public boolean isSameName(PersonCh6 other) //checks if persons names are the same
{
return this.name.equals(other.getName());
}
public boolean isSameAge(PersonCh6 other) // checks if ages are the same
{
return (this.age == other.getAge());
}
public boolean isOlderThan(PersonCh6 other)
{ // determines which person is older than the other
return (this.age > other.getAge());
}
public boolean isYoungerThan(PersonCh6 other) // determines which person is younger
{
return (this.age < other.getAge());
}
@Override
public boolean equals(Object obj)
{
PersonCh6 other = (PersonCh6) obj;
if(this.name.equals(other.getName()) && (this.age == other.getAge())) //this checks if information the user typed lines up with the parameters of each testcase in the personCh6 test.
return true;
else
return false;
}
public void setAge(int newAge)
{
if (newAge >= 0)
age = newAge;
else
{
System.out.println(\"error : age should not be negative\");
System.exit(0);
}
}
public void setPerson (String newName, int newAge )
{
setName(newName);
setAge(newAge);
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
Here is the code provided to me for the test program I have to use to......test the PersonCh6 class I have to write.
If any one can help me troubleshoot the issues I am having with this that would be great.
Solution
I have refactored some code and fixed syntax errors. All test case has passed.
import java.util.Scanner;
public class PersonCh6 // class name
{
private String name;
private int age;
private Scanner scanner;
public PersonCh6() {
scanner = new Scanner(System.in);
name = null;
age = 0;
}
public PersonCh6(String newName, int newAge)// constructs name and age of person
{
scanner = new Scanner(System.in);
name = newName;
age = newAge;
}
public PersonCh6(String newName)// constructs name attribute of person
{
name = newName;
age = 0;
}
public PersonCh6(int newAge) // constructs age attribute of person
{
age = newAge;
name = null;
}
public void readInput() {
System.out.println(\"What is the person\'s name?\"); // outputs to screen
name = scanner.nextLine();
System.out.println(\"What is the person\'s age?\");
age = scanner.nextInt();
}
public void writeOutput() {
System.out.println(\"Name : \" + name); // outputs name and age user typed to screen
System.out.println(\"Age : \" + age);
}
public boolean isSamePerson(PersonCh6 newPerson)
{
if(name == newPerson.getName() && age == newPerson.getAge())
return true;
else
return false;
}
public boolean isSameName(PersonCh6 other) // checks if persons names are
// the same
{
return this.name.equals(other.getName());
}
public boolean isSameAge(PersonCh6 other) // checks if ages are the same
{
return (this.age == other.getAge());
}
public boolean isOlderThan(PersonCh6 other) { // determines which person is
// older than the other
return (this.age > other.getAge());
}
public boolean isYoungerThan(PersonCh6 other) // determines which person is
// younger
{
return (this.age < other.getAge());
}
@Override
public boolean equals(Object obj) {
PersonCh6 other = (PersonCh6) obj;
if (this.name.equals(other.getName()) && (this.age == other.getAge()))
// this checks if information the user typed lines up with the
// parameters
// of each test case in the personCh6 test.
return true;
else
return false;
}
public void setAge(int newAge) {
if (newAge >= 0)
age = newAge;
else {
System.out.println(\"error : age should not be negative\");
System.exit(0);
}
}
public void set(String newName, int newAge) {
this.name = newName;
setAge(newAge);
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
}





