Results 1 to 13 of 13
Thread: Getting back weird results
- 03-02-2011, 05:25 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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?:
Note:My code isn't finished. Why am I getting the Book@ part?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
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
- 03-02-2011, 05:27 AM #2
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>. */
- 03-02-2011, 05:36 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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
- 03-02-2011, 05:46 AM #4
The toString method takes no parameters and returns a String representation of the object. It doesn't do the printing itself.
- 03-02-2011, 05:48 AM #5
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.
- 03-02-2011, 05:50 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Im not sure what that means, can you give a example? ThanksBy 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.
- 03-02-2011, 05:53 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Also can you help me understand why it's giving out those values?
Book@3e25a5
Book@19821f
Book@19821f
- 03-02-2011, 06:01 AM #8
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()); } }
- 03-02-2011, 06:19 AM #9
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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; }
- 03-02-2011, 06:35 AM #10
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Alright, I think that solves the problem, but now how would I go in doing stage 3?
- 03-02-2011, 07:25 AM #11
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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
- 03-02-2011, 10:50 PM #12
Constructor not method.
Check with you teacher as I believe this line of code should be: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();
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:Book b3 = new Book(b2);
Using == compares Object references and not their contents. HINT: what method is this in?Java Code:if (number.getTitle() == this.title && number.getPrice() == this.price && number.getPages() == this.pages){
- 03-03-2011, 02:00 AM #13
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Similar Threads
-
Weird problem with 36 and 39?
By jh7468 in forum New To JavaReplies: 3Last Post: 02-06-2011, 08:01 PM -
weird problem
By GPB in forum New To JavaReplies: 2Last Post: 02-28-2010, 12:04 PM -
Seriously weird output
By gandalf5166 in forum Java AppletsReplies: 4Last Post: 02-27-2010, 04:16 AM -
posting results back to parent page
By carag in forum New To JavaReplies: 0Last Post: 07-29-2009, 12:29 PM -
Weird Error?
By sciguy77 in forum New To JavaReplies: 4Last Post: 01-20-2009, 02:32 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks