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.


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

  2. #2
    Registered Users javapenguin's Avatar
    Join Date
    May 2010
    North Central Illinois
    My Mood
    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.
    Please mark solved threads as "Solved" by going to threads and clicking "Mark Solved"
    Using code tags

    How To ask questions the smart way
    Java Tutorials
    Java API
    Save Our Snips
    Hit THANKS to thank a person. Hit REP+ to increase reputation (or decrease it). Cannot give to same person for a while.
    Shooting Park Road Baptist Church Site

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, 08:04 AM
  3. Replies: 0
    Last Post: April 11th, 2010, 09: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