Simple 3 classes and incorect output
HI,
I'm beginner in Java. I've got 3 simple classes from the beginner examples.
Classes are listed below. Books object uses Person object to store data on who has the book ( :) like I said it's beginner level) however when I'm printing out the maxBooks value then instead of default 10 for a person my console prints out 20. Even if I set it to 5 then console System.out.println( book.getPerson().getMaxBooks() + '\n'); prints 15 ??? I just cant figure it out :(
Code:
public class Person {
String name;
int maxBooks;
public Person (){
this.name = "Name not set";
this.maxBooks = 10;
}
public Person (String name, int maxBooks){
this.name = name;
this.maxBooks = maxBooks;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMaxBooks() {
return maxBooks;
}
public void setMaxBooks(int maxBooks) {
this.maxBooks = maxBooks;
}
}
public class Book {
String title;
Person person;
public Book(){ this.title = "Title not set";}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
public class MyLibrary {
public static void main(String[] args) {
Book book = new Book();
Person person = new Person("Pete", 10);
book.setTitle("Java 2");
book.setPerson(person);
System.out.println( book.getPerson().getName() + '\n');
System.out.println( book.getPerson().getMaxBooks() + '\n');
System.out.println( book.getPerson().maxBooks + '\n');
System.out.println('\n');
book.getPerson().setMaxBooks(5);
System.out.println( book.getPerson().getMaxBooks() + '\n');
}
}
My console output after I run MyLibrary is :
Pete
20 // shoudn't this be 10 ??
20 //(Just testing what's the difference).
15 // 5 ? looks like somehow all is increased by 10 ???
While it should be 10,10 and 5 for maxbooks ???????
Above 3 classes are in the same project in the same package in Eclipse (which I'm using to write code and compile examples) and of course each in different class file.
BTW
What is the difference of using object Person in Books between
public class Book extends Person ? For me as a beginner at a glance it looks the same as in both cases methods and fields of Person are usable from Book
Thanks
Evo
Re: Simple 3 classes and incorect output
Haha, this confused me for a second. You are surrounding /n with apostraphes, indicating that they are chars. /n has a numeric value of 10. When you add an int to a char, you get an int. Instead, use quotation marks for a new line. BUT, you don't need /n at all here because you are using println, which already goes to the next line.
Re: Simple 3 classes and incorect output
And book.getMaxBooks() is a getter. It allows one class to access another classes variables. Ideally, your class variables will be private or protected, in which case accessing them directly (book.maxBooks) would NOT work.
Basically, a book is not a type of person. You are correct that it would work here, but that's because your program is very simple. What if there were 5 people who all checked out the same book? You would need 5 person objects, but only 1 book object.
Re: Simple 3 classes and incorect output
LOL, thanks mate !
What about second: What is the difference of using object Person in Book class and
public class Book extends Person ?
Thanks again !
Evo
Re: Simple 3 classes and incorect output
What I think is that you're basically creating 2 people with this statement:
Code:
book.getPerson().getMaxBooks();
Because the Book class creates a Person object AND the MyLibrary class also creates a Person Object, I think you're adding the 2 values together... See where you've defined the number of books in the MyLibrary class to 10, you've also set the maxbooks to 10 int he constructor so everything seems to be adding those 2 numbers together. Hence when you reset the max Books to 5.. 10 + 5 = 15.
Here's what you should have written:
Code:
System.out.println(person.getName());
System.out.println(person.getMaxBooks());
person.setMaxBooks(5);
System.out.println(person.getMaxBooks());