Results 1 to 5 of 5
  1. #1
    evonevo is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default 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 :(

    Java 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

  2. #2
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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.

  3. #3
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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.

  4. #4
    evonevo is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default 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

  5. #5
    0026sd is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default Re: Simple 3 classes and incorect output

    What I think is that you're basically creating 2 people with this statement:

    Java 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:

    Java Code:
    System.out.println(person.getName());
    System.out.println(person.getMaxBooks());
    person.setMaxBooks(5);
    System.out.println(person.getMaxBooks());

Similar Threads

  1. simple file output question thanks in advance
    By farahm in forum New To Java
    Replies: 5
    Last Post: 12-05-2010, 04:34 PM
  2. problem with using classes and output
    By sjaakie in forum New To Java
    Replies: 3
    Last Post: 10-10-2010, 02:56 PM
  3. Replies: 3
    Last Post: 09-22-2010, 08:28 PM
  4. the explanation of output of simple java program
    By amol84 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 06:06 PM
  5. [SOLVED] Simple Q: Values between Classes
    By a45b22chp in forum New To Java
    Replies: 6
    Last Post: 04-25-2008, 06:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •