Results 1 to 13 of 13
  1. #1
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default Getting back weird results

    Hi, I have a new cs lab. Here's the lab assignment;
    http://www.cs.uic.edu/~i107/spring11/lab7/lab7.html

    My question is, why when I run the code, do I get these results?:

    Name: Name Here!!
    Lab7, CS 107, Spring 2011


    <Absolute Java, 105, 1216>
    <Absolute Java, 124, 1272>
    Book@19821f
    Book@addbf1
    Book@addbf1
    Absolute Java(4th)
    125
    1273
    <Absolute Java, 105, 1216>
    Book@42e816
    false
    false
    false
    false
    false
    Note:My code isn't finished. Why am I getting the Book@ part?
    Also I need some help with the boolean part of this code, aka. part 3.

    Thanks!!

    Java Code:
    /*
    BookDriver.java
    Lab 7 for CS107, Spring 2011
      
    Be sure to comment out sections of code that don't work before
    you turn in your solution, since you get no points if your
    program doesn't compile.  
    
    
    DETAILS:  This problem has to do with storing books.
         For each book we want to keep track of the name (e.g. "Absolute Java"),
         the cost in dollars (e.g. 124), and number of pages (e.g. 1272)
         Default book name is "Absolute Java", default cost is 105, and default number of pages is 1216.
    
    Running this program will output this:
      
    Name: Dale Reed
    Lab7, CS 107, Spring 2011
    
    <Absolute Java,  105,  1216>
    <Absolute Java,  124,  1272>
    
    ......
    
    
    */
    
    
    public class BookDriver 
    {
    	// Do not change anything inside main() besides your name
        public static void main(String args[])
        {
        	
        	
            // Replace my name with yours in the statement below,
        	// but DO NOT CHANGE ANYTHING ELSE within main()
        	// except your name.
            System.out.println( "Name: Name Here!! \n" +
                                "Lab7, CS 107, Spring 2011 \n\n" );
            
            /*
             * Stage 1: Implement default constructor, fully qualified constructor, and toString method for the class Book.
             * Default book name is "Absolute Java", default cost is 105, and default number of pages is 1216. 
             * the toString() method should return a String value looks like <Absolute Java,  105,  1216>.
             */
            Book b1 = new Book();
            Book b2 = new Book("Absolute Java", 124, 1272); 
            System.out.println(b1); //the output should look like <Absolute Java,  105,  1216>
            System.out.println(b2);
            
            /*
             * Stage 2: Create get and set method for the class Book
             */
            b2.setTitle("Absolute Java(4th)");
            b2.setPrice(125);
            b2.setPages(1273);
            System.out.println(b2);
            System.out.println(b2.getTitle());//The output should be Absolute Java(4th)
            System.out.println(b2.getPrice());//The output should be 125
            System.out.println(b2.getPages());//The output should be 1273
            
            
            
            /*
             * Stage 3: Create copy constructor and equals method
             * Copy constructor should create a new instance of Book that has the same title, price, and number of pages as the original one.
             * The equals method returns true if and only if the two books have exactly the same title, price and number of pages.
             */
            Book b3 = new Book();
            System.out.println(b3);
            System.out.println(b2.equals(b3)); //The output should be "true"
            System.out.println(b3.equals(b2)); //The output should be "true"
            
            System.out.println(b1.equals(b2)); //The output should be "false"
            
            b3.setPrice(100);
            System.out.println(b2.equals(b3)); //The output should be "false"
            System.out.println(b3.equals(b2)); //The output should be "false"
            
            
            
        }//end main()
        
    
     
    
        
    }//end class BookDriver. Don't change anything above except printing your own name
    //                          
    
    
    // *********************************************************
    // You must write your solution inside class Book below
    class Book
    {
    	private String title;
    	private int price;
    	private int pages;
    	//TODO you need to create some private variables to store title and other two value.
    
    	 
    	public Book(){
    		setTitle("Absolute Java");
    		setPrice(105);
    		setPages(1216);
    		System.out.println("<"+getTitle() + ", " + getPrice() + ", " + getPages()+ ">");
    
    		
    		//TODO finish this method
    	}
    	
    	public Book(String title, int price, int pages){
    		this.setTitle(title);
    		this.setPrice(price);
    		this.setPages(pages);
    		System.out.println("<"+getTitle() + ", " + getPrice() + ", " + getPages()+ ">");
    
    		//TODO finish this method
    	}
    	public toString(){
    		//TODO finish this method 
    	}
    
    	/*TODO 
    	 * Create other two constructors, get and set methods, and  equals method.
    
    	 */
    	public boolean equals(Book one){
    		if (getTitle() == one.title && one.price == getPrice() && one.pages == getPages()){
    			return true;}
    		else{
    		return false;}
    		
    	}
    
    	public void setTitle(String title) {
    		this.title = title;
    	}
    
    	public String getTitle() {
    		return title;
    	}
    
    	public void setPrice(int price) {
    		this.price = price;
    	}
    
    	public int getPrice() {
    		return price;
    	}
    
    	public void setPages(int pages) {
    		this.pages = pages;
    	}
    
    	public int getPages() {
    		return pages;
    	}
    }//end class Book

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The answer lies in your instructions.
    Java Code:
    /*
             * Stage 1: Implement default constructor, fully qualified constructor, and toString method for the class Book.
             * Default book name is "Absolute Java", default cost is 105, and default number of pages is 1216. 
             * the [B]toString() method [/B]should return a String value looks like <Absolute Java,  105,  1216>.
             */

  3. #3
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    I still get the same results.

    Here is the updated code:

    Java Code:
    /*
    BookDriver.java
    Lab 7 for CS107, Spring 2011
      
    Be sure to comment out sections of code that don't work before
    you turn in your solution, since you get no points if your
    program doesn't compile.  
    
    
    DETAILS:  This problem has to do with storing books.
         For each book we want to keep track of the name (e.g. "Absolute Java"),
         the cost in dollars (e.g. 124), and number of pages (e.g. 1272)
         Default book name is "Absolute Java", default cost is 105, and default number of pages is 1216.
    
    Running this program will output this:
      
    Name: Dale Reed
    Lab7, CS 107, Spring 2011
    
    <Absolute Java,  105,  1216>
    <Absolute Java,  124,  1272>
    
    ......
    
    
    */
    
    
    public class BookDriver 
    {
    	// Do not change anything inside main() besides your name
        public static void main(String args[])
        {
        	
        	
            // Replace my name with yours in the statement below,
        	// but DO NOT CHANGE ANYTHING ELSE within main()
        	// except your name.
            System.out.println( "Name: Name Here!! \n" +
                                "Lab7, CS 107, Spring 2011 \n\n" );
            
            /*
             * Stage 1: Implement default constructor, fully qualified constructor, and toString method for the class Book.
             * Default book name is "Absolute Java", default cost is 105, and default number of pages is 1216. 
             * the toString() method should return a String value looks like <Absolute Java,  105,  1216>.
             */
            Book b1 = new Book();
            Book b2 = new Book("Absolute Java", 124, 1272); 
            System.out.println(b1); //the output should look like <Absolute Java,  105,  1216>
            System.out.println(b2);
            
            /*
             * Stage 2: Create get and set method for the class Book
             */
            b2.setTitle("Absolute Java(4th)");
            b2.setPrice(125);
            b2.setPages(1273);
            System.out.println(b2);
            System.out.println(b2.getTitle());//The output should be Absolute Java(4th)
            System.out.println(b2.getPrice());//The output should be 125
            System.out.println(b2.getPages());//The output should be 1273
            
            
            
            /*
             * Stage 3: Create copy constructor and equals method
             * Copy constructor should create a new instance of Book that has the same title, price, and number of pages as the original one.
             * The equals method returns true if and only if the two books have exactly the same title, price and number of pages.
             */
            Book b3 = new Book();
            System.out.println(b3);
            System.out.println(b2.equals(b3)); //The output should be "true"
            System.out.println(b3.equals(b2)); //The output should be "true"
            
            System.out.println(b1.equals(b2)); //The output should be "false"
            
            b3.setPrice(100);
            System.out.println(b2.equals(b3)); //The output should be "false"
            System.out.println(b3.equals(b2)); //The output should be "false"
            
            
            
        }//end main()
        
    
     
    
        
    }//end class BookDriver. Don't change anything above except printing your own name
    //                          
    
    
    // *********************************************************
    // You must write your solution inside class Book below
    class Book
    {
    	private String title;
    	private int price;
    	private int pages;
    	//TODO you need to create some private variables to store title and other two value.
    
    	 
    	public Book(){
    		title = "Absolute Java";
    		price = 105;
    		pages = 1216;
    		toString(title , price, pages);
    
    		
    		//TODO finish this method
    	}
    	
    	public Book(String title, int price, int pages){
    		this.setTitle(title);
    		this.setPrice(price);
    		this.setPages(pages);
    		toString(title , price, pages);
    
    		//TODO finish this method
    	}
    	public void toString(String title, int price, int pages){
    		System.out.println("<"+title + ", " + price + ", " + pages+ ">");
    
    		//TODO finish this method 
    	}
    
    	/*TODO 
    	 * Create other two constructors, get and set methods, and  equals method.
    
    	 */
    	public boolean equals(Book one){
    		if (getTitle() == one.title && one.price == getPrice() && one.pages == getPages()){
    			return true;}
    		else{
    		return false;}
    		
    	}
    
    	public void setTitle(String title) {
    		this.title = title;
    	}
    
    	public String getTitle() {
    		return title;
    	}
    
    	public void setPrice(int price) {
    		this.price = price;
    	}
    
    	public int getPrice() {
    		return price;
    	}
    
    	public void setPages(int pages) {
    		this.pages = pages;
    	}
    
    	public int getPages() {
    		return pages;
    	}
    }//end class Book

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The toString method takes no parameters and returns a String representation of the object. It doesn't do the printing itself.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    By the way, when you are in the class you have direct access to instance variables. There is no need to call accessor methods or pass them as parameters.

  6. #6
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    By the way, when you are in the class you have direct access to instance variables. There is no need to call accessor methods or pass them as parameters.
    Im not sure what that means, can you give a example? Thanks

  7. #7
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Also can you help me understand why it's giving out those values?

    Book@3e25a5
    Book@19821f
    Book@19821f

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    When you print an object the toString method is called. Idf you do not write a toString method in your class then it inherits the method from object. this is what the API says:

    this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())

    As to your other question:
    Java Code:
    class Foo {
        String name = "hello";
    
        public String getName() {
            return name;
        }
    
        public void goodMethod() {
            System.out.println(name);
        }
    
        public void badMethod() {
            System.out.println(getName());
        }
    
        public void anotherBadMethod(String s) {
            System.out.println(s);
        }
    
        public void printName() {
            anotherBadMethod(name);
            anotherBadMethod(getName());
        }
    }

  9. #9
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    So; is my toString supposed to look like this?

    Java Code:
    	public String toString(){
    		String result =("<"+getTitle() + ", " + getPrice() + ", " + getPages()+ ">");	
    //TODO finish this method 
    		return 	result;
    	}

  10. #10
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Alright, I think that solves the problem, but now how would I go in doing stage 3?

  11. #11
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    My work for stage 3 so far; how should I code the method Book()? I need it so that it can use the initial values, when it is first called upon, and then use the values previous set values after-wards.

    Java Code:
    /*
    BookDriver.java
    Lab 7 for CS107, Spring 2011
      
    Be sure to comment out sections of code that don't work before
    you turn in your solution, since you get no points if your
    program doesn't compile.  
    
    
    DETAILS:  This problem has to do with storing books.
         For each book we want to keep track of the name (e.g. "Absolute Java"),
         the cost in dollars (e.g. 124), and number of pages (e.g. 1272)
         Default book name is "Absolute Java", default cost is 105, and default number of pages is 1216.
    
    Running this program will output this:
      
    Name: Dale Reed
    Lab7, CS 107, Spring 2011
    
    <Absolute Java,  105,  1216>
    <Absolute Java,  124,  1272>
    
    ......
    
    
    */
    
    
    public class BookDriver 
    {
    	// Do not change anything inside main() besides your name
        public static void main(String args[])
        {
        	
        	
            // Replace my name with yours in the statement below,
        	// but DO NOT CHANGE ANYTHING ELSE within main()
        	// except your name.
            System.out.println( "Name: Name Here!! \n" +
                                "Lab7, CS 107, Spring 2011 \n\n" );
            
            /*
             * Stage 1: Implement default constructor, fully qualified constructor, and toString method for the class Book.
             * Default book name is "Absolute Java", default cost is 105, and default number of pages is 1216. 
             * the toString() method should return a String value looks like <Absolute Java,  105,  1216>.
             */
            Book b1 = new Book();
            Book b2 = new Book("Absolute Java", 124, 1272); 
            System.out.println(b1); //the output should look like <Absolute Java,  105,  1216>
            System.out.println(b2);
            
            /*
             * Stage 2: Create get and set method for the class Book
             */
            b2.setTitle("Absolute Java(4th)");
            b2.setPrice(125);
            b2.setPages(1273);
            System.out.println(b2);
            System.out.println(b2.getTitle());//The output should be Absolute Java(4th)
            System.out.println(b2.getPrice());//The output should be 125
            System.out.println(b2.getPages());//The output should be 1273
            
            
            
            /*
             * Stage 3: Create copy constructor and equals method
             * Copy constructor should create a new instance of Book that has the same title, price, and number of pages as the original one.
             * The equals method returns true if and only if the two books have exactly the same title, price and number of pages.
             */
            Book b3 = new Book();
            System.out.println(b3);
            System.out.println(b2.equals(b3)); //The output should be "true"
            System.out.println(b3.equals(b2)); //The output should be "true"
            
            System.out.println(b1.equals(b2)); //The output should be "false"
            
            b3.setPrice(100);
            System.out.println(b2.equals(b3)); //The output should be "false"
            System.out.println(b3.equals(b2)); //The output should be "false"
            
            
            
        }//end main()
        
    
     
    
        
    }//end class BookDriver. Don't change anything above except printing your own name
    //                          
    
    
    // *********************************************************
    // You must write your solution inside class Book below
    class Book
    {
    	private String title;
    	private int price;
    	private int pages;
    	//TODO you need to create some private variables to store title and other two value.
    
    	 
    	public Book(){
    		title = "Absolute Java";
    		price = 105;
    		pages = 1216;
    	//TODO finish this method
    	}
    	
    	public Book(String title, int price, int pages){
    		setTitle(title);
    		setPrice(price);
    		setPages(pages);
    		toString();
    
    		//TODO finish this method
    	}
    	public String toString(){
    		String result =("<"+title + ", " + price + ", " + pages+ ">");	
    //TODO finish this method 
    		return result;
    	}
    
    	/*TODO 
    	 * Create other two constructors, get and set methods, and  equals method.
    
    	 */
    	public boolean equals(Book number){
    		if (number.getTitle() == this.title && number.getPrice() == this.price && number.getPages() == this.pages){
    			return true;}
    		else{
    		return false;}
    		
    	}
    
    	public void setTitle(String title) {
    		this.title = title;
    	}
    	public String getTitle() {
    		return title;
    	}
    	public void setPrice(int price) {
    		this.price = price;
    	}
    	public int getPrice() {
    		return price;
    	}
    	public void setPages(int pages) {
    		this.pages = pages;
    	}
    	public int getPages() {
    		return pages;
    	}
    }//end class Book

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by jenxin View Post
    how should I code the method Book()?
    Constructor not method.

    Java Code:
             * Stage 3: Create copy constructor and equals method
             * Copy constructor should create a new instance of Book that has the same title, price, and number of pages as the original one.
             * The equals method returns true if and only if the two books have exactly the same title, price and number of pages.
             */
            Book b3 = new Book();
    Check with you teacher as I believe this line of code should be:
    Java Code:
    Book b3 = new Book(b2);
    If so what you need to do is add a third constructor that takes a Book as a parameter. Then you set the new Book objects instance variables to be the same as the instance variables of the other Book object.
    Java Code:
    if (number.getTitle() == this.title && number.getPrice() == this.price && number.getPages() == this.pages){
    Using == compares Object references and not their contents. HINT: what method is this in?

  13. #13
    jenxin is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Thanks Junky, yeah, I accidentally modified that line, I was wondering why it wasn't working, my friend pointed it out for me today, thanks for your help!!

Similar Threads

  1. Weird problem with 36 and 39?
    By jh7468 in forum New To Java
    Replies: 3
    Last Post: 02-06-2011, 08:01 PM
  2. weird problem
    By GPB in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 12:04 PM
  3. Seriously weird output
    By gandalf5166 in forum Java Applets
    Replies: 4
    Last Post: 02-27-2010, 04:16 AM
  4. posting results back to parent page
    By carag in forum New To Java
    Replies: 0
    Last Post: 07-29-2009, 12:29 PM
  5. Weird Error?
    By sciguy77 in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 02:32 AM

Posting Permissions

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