Results 1 to 9 of 9
- 11-03-2010, 07:57 PM #1
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
Beginner - can't get through, mind looking at code and errors?
So based on the huge help you guys were this morning, im going to post up again, here is what i have thus far (2 files)
and thisJava Code:// Ex3.13 Invoice.java (JHtP page104) //invoice a hardware store might use to represent an invoice //for an item sold at the store public class Invoice { private String partNumber; // part number for invoice private String partDescription; //part description for invoice private int quantity; //number of parts being purchased private double pricePerItem; // price per item // 4-arg constructor public Invoice (String partNumber, String partDescription, int count, double price) { setpartNumber ( partNumber ); setpartDescription ( partDescription ); if (count > 0) //determine whether count is positive quanity = count; // valid count assigned to quantity if (price > 0.0) // determine whether price is positive pricePerItem = price; // valid price assign to pricePerItem setquantity ( quantity ); setpricePerItem ( price ); } // end 4-arg constructor // set Part number: public void setpartNumber ( String partNumber) { partNumber = partNumber; //accept the passed part value-assgn to instance v partNumber }// end method setpartNumber // get Part number: public String partNumber () { return partNumber; } //end method getpartNumber // set partDescription: public void setpartDescription ( String partDescription ) { partDescription = partDescription; // accept the passed part value-assgn to instance v partDesc }// end method setpartDescription //get partDescription: public String partDescription () { return partDescription; }// end method getpartDescription // set quantity: public void setquantity ( int count) { quantity = count; }// end method set quantity // get quantity: public int count () { return quantity; } //end method get quantity // set pricePerItem public void setpricePerItem ( double price ) { pricePerItem = price; }// end method set pricePerItem //get pricePerItem public double price () { return pricePerItem; } // end method get pricePerItem // method to getInvoiceAmount (multiplies quantity * price per item) public double invoiceAmount () { return quantity * pricePerItem; }// end method getInvoiceAmount } // end public class Invoice
3 errorsJava Code:// Ex3.13 InvoiceTest.java (JHtP page104) // public class InvoiceTest { public static void main ( String args [] ) { //create instance of Invoice passing 4 argument values Invoice invoice1 = new Invoice ( "1234", "Hammer", "2", "14.99"); //display invoice1 System.out.println ("Original invoice information"); System.out.printf ( "Part Number: %s\n", invoice1.getpartNumber () ); } }//end public class InvoiceTest
Invoice.java:20: cannot find symbol
symbol : variable quanity
location: class Invoice
quanity = count; // valid count assigned to quantity
^
InvoiceTest.java:9: cannot find symbol
symbol : constructor Invoice(java.lang.String,java.lang.String,java.lan g.String
,java.lang.String)
location: class Invoice
Invoice invoice1 = new Invoice ( "1234", "Hammer", "2", "14.99")
;
^
InvoiceTest.java:13: cannot find symbol
symbol : method getpartNumber()
location: class Invoice
System.out.printf ( "Part Number: %s\n", invoice1.getpartNumber
() );
^
3 errors
I have to still do more on the InvoiceTest file with regards to String.out.print to show the info, but i wanted to compile and see the errors i have up until now to see what needs to be fixed. there were like 10 errors, i seem to have it down to 3 (hopefully)
Any help/hints are appreciated. Thanks.
- 11-03-2010, 08:43 PM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
found 1 error - incorrect spelling for quantity
- 11-03-2010, 08:44 PM #3
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
You really should learn how to read the errors as they tell you exactly what is wrong.
In you Invoice.java class at line 20, you are referencing a variable named quanity which doesn't exist.
(Hint: Check your spelling.)
In your InvoiceTest.java class at line 9, you are trying to call a constructor for the Invoice class that doesn't exist
(Hint: Invoice(String, String, String, String) is not the same as Invoice(String, String, int, double))
In your InvoiceTest.java class at line 13, you ar calling the method getpartNumber() on an instance of the Invoice class. That method doesn't exist.
(Hint: Look inside your Invoice class for the method)
- 11-03-2010, 09:00 PM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
Stormy,
I appreciate what your saying about reading the errors, but honestly, im 10 days into learning Java, and have literally be starting at this program since this morning. I don't understand all the details of what its asking for when i see the errors yet.
thank you for your help, just as you posted i caught the fact that i didnt have the word "get" in any of my set/get methods.
im down to the 1 error, InvoiceTest line 9......don't know yet....
- 11-03-2010, 09:10 PM #5
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Look at the parameters you are passing to the Constructor. They are all String objects(since they are all surrounded by the "" characters). Since the only constructor in the Invoice class needs the parameters to be (String, String, int, double) just change the "2" and "14.99" into the int and double.
Notice how the 2 and 14.99 are passed directly into the constructor. Java recognizes the 2 as a type int and the 14.99 as a type double.Java Code://Invoice invoice1 = new Invoice ( "1234", "Hammer", "2", "14.99") //It should be this Invoice invoice1 = new Invoice("1234", "Hammer", 2, 14.99);
- 11-03-2010, 09:12 PM #6
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
- 11-05-2010, 12:57 AM #7
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
ok, so it is running and displaying:
C:\SimplyJava\Invoice>java InvoiceTest
Original invoice information
Part Number: null
Part Description: null
Quantity: 2
Price: 14.99
i know i read something about if a reference type isn't specifed it's default is null? is that right? what do i need to change here to fix that?
- 11-05-2010, 09:29 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
The method parameter "partNumber" is hiding the attribute "partNumber", so that line is assigning the method parameter to itself. In order to specify the attribute in this situation you need to use "this.partNumber".Java Code:public void setpartNumber ( String partNumber) { partNumber = partNumber; //accept the passed part value-assgn to instance v partNumber }// end method setpartNumber
A common mistake, believe me.
(Also same with the part description).
- 11-05-2010, 01:52 PM #9
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
guys, first off, sorry for making a second post, but i posted the first one, and AFTER remembered someone saying "if you have any future questions, just make a new post", in addition, i wanted to show all the updated full code, so it would show the new string.out's like it should.
it won't happen again, sorry.
and secondly, guys thank you so much for your help, i really appreciate it alot, it has been a huge help. to not only learn "what" my problem (s) is/was, but also the reasons why i needed to make the changes. Thank you.
Similar Threads
-
Beginner Spring with Hibernate integrated. Is this code correct
By pestilencia in forum Web FrameworksReplies: 1Last Post: 10-24-2009, 02:13 AM -
Errors in the code (?)
By AndersBjörnör in forum New To JavaReplies: 1Last Post: 10-09-2009, 03:06 PM -
hi,can anyone help with the following code,i could not complie because its got errors
By omutaoka in forum New To JavaReplies: 3Last Post: 03-03-2009, 09:48 PM -
HELP ON errors occuring in the code
By jaiminparikh in forum Advanced JavaReplies: 6Last Post: 02-12-2009, 02:02 PM -
beginner grade 11-need help with a math code involving returning values from a method
By bobmasta5 in forum New To JavaReplies: 3Last Post: 12-10-2008, 01:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks