debug my program? it's in Java. At line 108, I get the error “The field Person.age is not visible”. It won’t launch. This is what the output should look like: created Person [name: Fred Flintstone, age: 50] Duplicate entry: Person [name: Fred Flintsone, age: 50] created Student [major: Math, GPA: 3.1, name: Fred Flintstone, age: 50] created Student [major: politics, GPA: 3.1, name: George, age: 21] Family array full. Skipping: Student [major: Physics, GPA: 2.5, name: Jim, age: 21] Family array full. Skipping: person [name: Robert, age: 10] Family array full. Skipping: Person [name: Clement, age: 32] *****family listing: Person [name: Fred Flintstone, age: 50] Student [major: Math, GPA: 3.1, name: Fred Flintsone, age: 50] Person [name: Wilma Flintstone, age: 48] Student [major: Math, GPA: 3.1, name: Fred Flintstone, age: 50] Person [name: Wilma Flintstone, age: 48] Student [major: politics, GPA: 3.1, name: Georgie, age: 21] Student [major: Politics, GPA: 3.1, name: George, age: 21] Student [major: Geology, GPA: 2.9, name: John, age 18] Student [major: music, GPA: 3.2, name: Jane, age: 21] Student [major: Gymnastics, GPA: 4.0, name: Tarzan, age: 22] below is my program: package family; //@SuppressWarnings("EqualsandHashcode") class Person { private String name; private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person [name: "+ name +", age: " + age + "]"; } @Override //@SuppressWarnings("SuspiciousIndentAfterControlStatement") public boolean equals(Object obj) { if(this == obj) return true; if (obj != null) { } else { return false; } if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if(age != other.age) return false; if(name == null) { if(other.name !=null) return false; } else if(!name.contentEquals(other.name)) return false; return true; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } @SuppressWarnings("EqualsAndHashcode") class Student extends Person { private String major; private double gpa; public Student(String name, int age, String major, double gpa) { super(name, age); this.major = major; this.gpa = gpa; } public Student() { } public String getMajor() { return major; } public void setMajor(String major) { this.major=major; } public double getGpa() { return gpa; } public void setGpa(double gpa) { this.gpa = gpa; } // public double getAge() { // return age; // } // public double setAge(double age) // { // this.age = age; // } @Override public String toString() { return "Student [major: " + major + ", GPA" + gpa+ ", name: " + getName()+ ", age" + age + "]"; } @Override @SuppressWarnings("SuspiciousIndentAfterControlStatement") public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if(getClass() != obj.getClass()) return false; Student other = (Student) obj; if(gpa != other.gpa) return false; if(major == null) { if(other.major != null) return false; } else if (!major.contentEquals(other.major)) return false; return true; } } public class Family { private final Person familyPersons[]; private int numPersons = 0; public Family(int num) { familyPersons = new Person[num]; } private void addPerson(Person p) { for (int i = 0; i < numPersons; i++) { if(familyPersons[i].equals(p)) { System.out.println("Duplicate energy: "+p); return; } } if (numPersons != familyPersons.length) { familyPersons[numPersons++] = p; } else { System.out.println("Family array full. Skipping... " + p); } } private void printOutFamily() { for(int i = 0; i < numPersons; i++) { System.out.println(familyPersons[i]); } } @SuppressWarnings("empty-statement") public static void main(String[] args) { Family f = new Family(8); Person fred = new Person("Fred Flintstone", 50); System.out.println("created " + fred); f.addPerson(fred);; f.addPerson(fred);; Student fredStudent = new Student("Fred Flintstone", 50, "Math", 3.1); System.out.println("created "+fredStudent); f.addPerson(fredStudent);; Person wilma = new Person("Wilma Flintstone", 48); f.addPerson(wilma);;; Student george = new Student("George", 21, "politics", 3.1); System.out.println("Created "+george); f.addPerson(george);; george.setName("Georgie"); f.addPerson(new Student("George", 21, "Politics", 3.1)); f.addPerson(new Student("John", 18, "Geology", 2.9)); f.addPerson(new Student ("Jane", 21, "music", 3.2)); f.addPerson(new Student("Tarzan", 22, "Gymastics", 4.0)); f.addPerson(new Student("Jim", 21, "Physics", 2.5)); f.addPerson(new Person("Clemente", 32)); System.out.println("*******family listing: ***********"); f.printOutFamily(); } }
Question
hello! can you help me debug my program? it's in Java. At line 108, I get the error “The field Person.age is not visible”. It won’t launch.
This is what the output should look like:
created Person [name: Fred Flintstone, age: 50]
Duplicate entry: Person [name: Fred Flintsone, age: 50]
created Student [major: Math, GPA: 3.1, name: Fred Flintstone, age: 50]
created Student [major: politics, GPA: 3.1, name: George, age: 21]
Family array full. Skipping: Student [major: Physics, GPA: 2.5, name: Jim, age: 21]
Family array full. Skipping: person [name: Robert, age: 10]
Family array full. Skipping: Person [name: Clement, age: 32]
*****family listing:
Person [name: Fred Flintstone, age: 50]
Student [major: Math, GPA: 3.1, name: Fred Flintsone, age: 50]
Person [name: Wilma Flintstone, age: 48]
Student [major: Math, GPA: 3.1, name: Fred Flintstone, age: 50]
Person [name: Wilma Flintstone, age: 48]
Student [major: politics, GPA: 3.1, name: Georgie, age: 21]
Student [major: Politics, GPA: 3.1, name: George, age: 21]
Student [major: Geology, GPA: 2.9, name: John, age 18]
Student [major: music, GPA: 3.2, name: Jane, age: 21]
Student [major: Gymnastics, GPA: 4.0, name: Tarzan, age: 22]
below is my program:
package family;
//@SuppressWarnings("EqualsandHashcode")
class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
@Override
public String toString()
{
return "Person [name: "+ name +", age: " + age + "]";
}
@Override
//@SuppressWarnings("SuspiciousIndentAfterControlStatement")
public boolean equals(Object obj)
{
if(this == obj)
return true;
if (obj != null) {
} else {
return false;
}
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if(age != other.age) return false;
if(name == null) {
if(other.name !=null)
return false;
}
else if(!name.contentEquals(other.name))
return false;
return true;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}
@SuppressWarnings("EqualsAndHashcode")
class Student extends Person {
private String major;
private double gpa;
public Student(String name, int age, String major, double gpa)
{
super(name, age);
this.major = major;
this.gpa = gpa;
}
public Student()
{
}
public String getMajor()
{
return major;
}
public void setMajor(String major)
{
this.major=major;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa)
{
this.gpa = gpa;
}
// public double getAge() {
// return age;
// }
// public double setAge(double age)
// {
// this.age = age;
// }
@Override
public String toString()
{
return "Student [major: " + major + ", GPA" + gpa+ ", name: " + getName()+ ", age" + age + "]";
}
@Override
@SuppressWarnings("SuspiciousIndentAfterControlStatement")
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if(getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if(gpa != other.gpa)
return false;
if(major == null)
{
if(other.major != null)
return false;
}
else if (!major.contentEquals(other.major))
return false;
return true;
}
}
public class Family
{
private final Person familyPersons[];
private int numPersons = 0;
public Family(int num)
{
familyPersons = new Person[num];
}
private void addPerson(Person p)
{
for (int i = 0; i < numPersons; i++)
{
if(familyPersons[i].equals(p))
{
System.out.println("Duplicate energy: "+p);
return;
}
}
if (numPersons != familyPersons.length)
{
familyPersons[numPersons++] = p;
}
else
{
System.out.println("Family array full. Skipping... " + p);
}
}
private void printOutFamily()
{
for(int i = 0; i < numPersons; i++)
{
System.out.println(familyPersons[i]);
}
}
@SuppressWarnings("empty-statement")
public static void main(String[] args) {
Family f = new Family(8);
Person fred = new Person("Fred Flintstone", 50);
System.out.println("created " + fred);
f.addPerson(fred);;
f.addPerson(fred);;
Student fredStudent = new Student("Fred Flintstone", 50, "Math", 3.1);
System.out.println("created "+fredStudent);
f.addPerson(fredStudent);;
Person wilma = new Person("Wilma Flintstone", 48);
f.addPerson(wilma);;;
Student george = new Student("George", 21, "politics", 3.1);
System.out.println("Created "+george);
f.addPerson(george);;
george.setName("Georgie");
f.addPerson(new Student("George", 21, "Politics", 3.1));
f.addPerson(new Student("John", 18, "Geology", 2.9));
f.addPerson(new Student ("Jane", 21, "music", 3.2));
f.addPerson(new Student("Tarzan", 22, "Gymastics", 4.0));
f.addPerson(new Student("Jim", 21, "Physics", 2.5));
f.addPerson(new Person("Clemente", 32));
System.out.println("*******family listing: ***********");
f.printOutFamily();
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images