Unsure of how to setup the setPerson function in my book class: Package edu.ece
ID: 2247163 • Letter: U
Question
Unsure of how to setup the setPerson function in my book class:
Package edu.ece 373. lab1: public class Person { //Fields for a Person private String name: private int maxBooks: //Constructors public Person () { name = "unknown": maxBooks = 2: } public person(string aName, int maxBooks) { name = aName: this, maxBooks = maxBooks: } public Person(string aName) { this (aName, theta): } //getters and setters 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 { //Fields for a Person private String title: private String author: private Person person: //Constructors public Book() { title = "unknown": author = "unknown": person = new Person (): } public String getTitle (){ return title: } public void setTitle (String atitle){ title = atitle: } public String getAuthor (){ return author: } public void setAuthor (Sting aauthor) { author = aauthor: } public Person getPerson(){ return person: public void setPerson (Person person){ this.person = person: } }Explanation / Answer
Some of the posible ways to implement the setPerson() method in your book class is as follows:
void setPerson(Person p){ // As person object is already created in the constructor
// we can take a person object and copy its values to the
// person member of the object.
person.setName(p.getName());
person.setMaxBooks(p.getMaxBOOKS());
}
OR we can also consider following function as person member has been initialized with default values in the constructor, we can put values in the setPerson function as follows:
void setPeron(String nm, int mb){
person.setName(nm);
person.setMaxBooks(mb);
}
The setPerson() as implemented in the code also seems fine.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.