Results 1 to 9 of 9
  1. #1
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

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

    Java 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
    and this

    Java 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
    3 errors

    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.

  2. #2
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    found 1 error - incorrect spelling for quantity

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    You really should learn how to read the errors as they tell you exactly what is wrong.

    Quote Originally Posted by hayden06f4i View Post
    Invoice.java:20: cannot find symbol
    symbol : variable quanity
    location: class Invoice
    quanity = count; // valid count assigned to quantity
    In you Invoice.java class at line 20, you are referencing a variable named quanity which doesn't exist.
    (Hint: Check your spelling.)
    Quote Originally Posted by hayden06f4i View Post
    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")
    ;
    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))
    Quote Originally Posted by hayden06f4i View Post
    InvoiceTest.java:13: cannot find symbol
    symbol : method getpartNumber()
    location: class Invoice
    System.out.printf ( "Part Number: %s\n", invoice1.getpartNumber
    () );
    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)

  4. #4
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

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

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    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.
    Java Code:
         //Invoice invoice1 = new Invoice ( "1234", "Hammer", "2", "14.99")
    
         //It should be this
         Invoice invoice1 = new Invoice("1234", "Hammer", 2, 14.99);
    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.

  6. #6
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    Quote Originally Posted by StormyWaters View Post
    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.
    Java Code:
         //Invoice invoice1 = new Invoice ( "1234", "Hammer", "2", "14.99")
    
         //It should be this
         Invoice invoice1 = new Invoice("1234", "Hammer", 2, 14.99);
    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.

    THANK YOU!

    i havent learned yet that " " mean it is a string specifically, i will try that and see if it works. THANK YOU so much for all your help. I am very thankful!

  7. #7
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    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?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Java Code:
    	public void setpartNumber ( String partNumber)
    	{
    		partNumber = partNumber; //accept the passed part value-assgn to  instance v partNumber
    	}// end method setpartNumber
    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".

    A common mistake, believe me.

    (Also same with the part description).

  9. #9
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    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

  1. Replies: 1
    Last Post: 10-24-2009, 03:13 AM
  2. Errors in the code (?)
    By AndersBjörnör in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 04:06 PM
  3. Replies: 3
    Last Post: 03-03-2009, 10:48 PM
  4. HELP ON errors occuring in the code
    By jaiminparikh in forum Advanced Java
    Replies: 6
    Last Post: 02-12-2009, 03:02 PM
  5. Replies: 3
    Last Post: 12-10-2008, 02:38 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
  •