Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Class Creating Help

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Class Creating Help

    I just started a Java class a few weeks ago and we're learning to create our own classes. I think I understand it a little, but I cannot figure out why I can't get the class to multiply the price of an item by the quantity. It either always equals 0.00, or i get a <identifier> expected error. Any help would be great. Thanks!

    public class Invoice
    {
    	//Part Number
    	private String partNumber;
     
    	public void setPartNumber( String name )
    	{
    		partNumber = name;
    	}
    	public String getPartNumber()
    	{
    		return partNumber;
    	} //End Number
    	//Part Description
    	private String partDesc;
     
    	public void setPartDesc( String name )
    	{
    		partDesc = name;
    	}
    	public String getPartDesc()
    	{
    		return partDesc;
    	} //End Description
    	//Quantity
    	public int partQuantity;
     
    	public void setPartQuantity( int name )
    	{
    		partQuantity = name;
    	}
    	public int getPartQuantity()
    	{
    		return partQuantity;
    	} //End Quantity
    	//Part Price
    	public double partPrice;
     
    	public void setPartPrice( double name )
    	{
    		partPrice = name;
    	}
    	public double getPartPrice()
    	{
    		return partPrice;
    	} //End Price
    	//Calculate Invoice Amount
     
    	double amount = partQuantity * partPrice;
     
    	//End Amount
     
    	//Output
    	public void displayMessage()
    	{
    		System.out.printf("Part Number: %s\n", getPartNumber() );
    		System.out.printf("Part Description: %s\n", getPartDesc() );
    		System.out.printf("Part Quantity: %d\n", getPartQuantity() );
    		System.out.printf("Part Price: %.2f\n", getPartPrice() );
    		System.out.printf("Total Invoice Amount: %.2f\n", amount );	
    	}
    }


    import java.util.Scanner;
     
    public class InvoiceTest
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner( System.in );
    		int theQuantity;
    		double thePrice;
     
    		Invoice myInvoice = new Invoice();
    		//Get and Set Part Number
    		System.out.println("Please enter the part number: ");
    		String theNum = input.nextLine();
    		myInvoice.setPartNumber( theNum );
    		//Get and Set Part Description
    		System.out.println("Please enter the part description: ");
    		String theDesc = input.nextLine();
    		myInvoice.setPartDesc( theDesc );
    		//Get and Set Part Quantity
    		System.out.println("Please enter the part quantity: ");
    		theQuantity = input.nextInt();
    		myInvoice.setPartQuantity( theQuantity );
    		//Get and Set Part Price
    		System.out.println("Please enter the part price: ");
    		thePrice = input.nextDouble();
    		myInvoice.setPartPrice( thePrice );
     
    		myInvoice.displayMessage();
    	}
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Class Creating Help

    double amount = partQuantity * partPrice;

    You have that outside of a method. It's probably confused what to do with it.

    How about this:
     
    public double getAmount()
    {
    return (getPartPrice() * getPartQuantity());
    }

    And later in getMessage()

    System.out.printf("Total Invoice Amount: %.2f\n", getAmount() );
    Last edited by javapenguin; February 1st, 2011 at 11:29 PM.

Similar Threads

  1. Creating a scaleUp main method in a new class
    By Brainz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 08:58 AM
  2. Help with creating a class
    By cdawg_2010 in forum Loops & Control Statements
    Replies: 4
    Last Post: November 1st, 2010, 07:04 AM
  3. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  4. Creating a class for points in three dimensions.
    By joachim89 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 8th, 2010, 07:13 PM
  5. Creating a class file
    By ipatch in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2009, 07:19 PM