Unsure of how to include the person class inside of the SetPerson function insid
ID: 2247156 • Letter: U
Question
Unsure of how to include the person class inside of the SetPerson function inside of the Book class. Here is a pictures of both classes:
o Person java SE A Persontest java 1 package edu.ece373.1ab1; D Book jave A 1 package educe 373. lab21. ; 3 import edu, ec373.ab.Person; 4 public class Person 6 public class Book 6 7 8 I/Fields for a person private string name ; private int maxBooks ; The 11 8 9 be 11 N/Fields for a Person private string title; private string author; private person person; I/constructors public person() { name = "unknown " maxBooks 2; 14 public person(string aName, int maxBooks) { name a Name ; this, maxBooks MaxBooks ; 16 17 18 19 20 21 - 13 U/Constructors 14 public Book(() { 15 title unknown 16 authors "unknown "; 17 person a new Person ); 18 19 20 public string gettitle ) return title; public person(string aName) this (alame, 6); 24 public void settitle(string atitle){ title - atitle; /getters and setters public string BetName () return name; 24 25 26 27 28 29 Be 31 26 27 28 29 public void setName (string aname) { name a Name ; public string getAuthor) return author ; 33 34 public void setAuthor(string aauthor) author = aauthor ; 36 37 38 public person getPerson() { return person; be 33 34-public int getMaxBooks (0) { return maxBooks; 36 37 public void setMaxBooks(Ant maxBooks){ 38 this .maxBooks = maxBooks; 39 4e public void display) 42 system.out.printin"Hi! You have successfully completed the PreLab!"); 44 45 46 47 48 ] public void setPerson(Person person){ this . person a person. Type here to search :23 AM 8/29/2011Explanation / Answer
1.First of all, you cannot access private variables of classPerson in other class Book, remove private.
2. We can easily access Person class in Book class in any function for testing I have written and run this code which is same as yours but with main part.
to compile we should use command javac -d . Person.java and javac -d . Book.java
and to run java edu.ece373.lab1.book
package edu.ece373.lab1;
public class Person{
String name;
int maxBooks;
public Person(){
name = "unknown";
maxBooks = 2;
}
public Person(String aName, int maxBooks){
name = aName;
this.maxBooks = maxBooks;
}
public Person(String aName){
this(aName,0);
}
public String getName(){
return name;
}
public void setName(String aName){
name = aName;
}
public int getMaxBooks(){
return maxBooks;
}
public void setMaxBooks(int maxBooks){
this.maxBooks = maxBooks;
}
public void display(){
System.out.println("Hi! You have successfully completed the PreLab!");
}
}
package edu.ece373.lab1;
import edu.ece373.lab1.Person;
public class Book{
static private String title;
static private String author;
static private Person person1, person2, person3;
public Book(){
title = "unknown";
author = "unknown"; //person using default constructor
person2 = new Person("Kaushal",50); // person using parameterided constructor
person3 = new Person("John");
}
public String getTitle(){
return title;
}
public void setTitle(String atitle){
title = atitle;
}
public String getAuthor(){
return author;
}
public void setAuthor(String aauthor){
author = aauthor;
}
public Person getPerson(){
return person1;
}
public void setPerson(Person person){
this.person1 = person1;
this.person2 = person2;
this.person3 = person3;
}
public static void displayinfo(Person p){
System.out.println("Person title: "+Book.title+" author: "+Book.author+" name: "+p.name+" maxBooks: "+p.maxBooks);
}
public static void main(String[] args){
Book b1 = new Book();
b1.setPerson(person1);
b1.displayinfo(person1);
b1.setTitle("Java ");
b1.setPerson(person2);
b1.displayinfo(person2);
b1.setPerson(person3);
b1.setTitle("c++ ");
b1.setAuthor("Herbert");
b1.displayinfo(person3);
}
}
run the above code using commands i already stated above, Every queries of yous will be more clear on how to access each and every function of any of two packages.
Thankyou
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.