Results 1 to 18 of 18
  1. #1
    BeginnerJava is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default HELP!!! Completely confused on how to fix this program!!!

    Here is what I'm working on for the last 6 hours. I'm frustrated and confused. I created a class named Purchase (see below) that contains an invoice number, amount of sale, and amount of sales tax. I included set methods for invoice # and sale amount as well as a calculation for sales tax with the set method for sale amount. Included also was a display method that displays a purchase's detail.

    Java Code:
    public class Purchase 
    {
        private int invoice;
        private double sale;
        private double salesTax;
    
    public int getInvoice()
    {
    	return invoice;
    }
        	
    public double getSale ()
    {
    	return sale;
    }   	
    
    public double getTax ()
    {
    	return salesTax;
    }
    public void setInvoice(int invoiceNumber)
    {	
    	invoice = invoiceNumber;
    		
    }
    public void setSale (double saleAmount)
    {
    	sale = saleAmount;
    	salesTax = (saleAmount * .05);
    }
    public void DisplaySale ()
    {	
    	System.out.println("Invoice # >" + invoice + "Sale Amount = " + sale
    		+ "Sales Tax = " + salesTax);
    }
    }
    Now I have to create an application that declares a Purchase object and prompts the user for purchase details. The user cannot proceed until a number is between 1000 and 8000 is entered.
    An cannot proceed until the user enters a nonnegative value for sale amount. After a valid Purchase object has been created, display the object's invoice number, sale amount, and sales tax.

    Here is what I have so far with MANY errors....I know that I either need some type of loop and/or if statement. PLEASE HELP....I'M DESPERATE!! I also don't know if I declared the Purchase object correctly.

    Java Code:
    public class Purchase 
    {
    
        private int invoice;
        private double sale;
        private double salesTax;
    
    public int getInvoice()
    {
    	return invoice;
    }
        	
    public double getSale ()
    {
    	return sale;
    }   	
    
    public double getTax ()
    {
    	return salesTax;
    }
    public void setInvoice(int invoiceNumber)
    {	
    	invoice = invoiceNumber;
    		
    }
    public void setSale (double saleAmount)
    {
    	sale = saleAmount;
    	salesTax = (saleAmount * .05);
    }
    public void DisplaySale ()
    {	
    	System.out.println("Invoice # >" + invoice + "Sale Amount = " + sale
    		+ "Sales Tax = " + salesTax);
    }
    }
    Last edited by Fubarable; 06-11-2011 at 03:30 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Unless I am missing something you have posted the Purchase code twice, but not the code for the class(es) containing your application.

    Perhaps you could post that code, and the compiler messages that you find troublesome? (when posting code use the "code" tags. You put [code] at the start of the code and [/code] at the end).

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by BeginnerJava View Post
    Here is what I have so far with MANY errors....I know that I either need some type of loop and/or if statement. PLEASE HELP....I'M DESPERATE!! I also don't know if I declared the Purchase object correctly....
    You appear to have posted your Purchase class twice, and I'll bet that's not what you wanted to do. Also, when posting code here, please use code tags so that your code retains its formatting and is readable. I've added code tags for you this time but ask that you use them in the future.

    So for now, please post your latest code where you try to use the Purchase class, and also post the actual error messages that this code generates. Also, please don't beg for help here. We'll help you if we can and if you give us enough information so that your question is in fact answerable.
    Last edited by Fubarable; 06-11-2011 at 03:40 AM.

  4. #4
    BeginnerJava is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    .

    So for now, please post your latest code where you try to use the Purchase class, and also post the actual error messages that this code generates. Also, please don't beg for help here. We'll help you if we can and if you give us enough information so that your question is in fact answerable.
    Sorry, I'm new to both JAVA and this forum. I don't intend to beg for help here. I'm just frustrated with the coding and errors and was looking for another set of eyes to look at my code. I made a mistake by posting the Purchase class twice.

  5. #5
    BeginnerJava is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    Here is the code for the application that declares a Purchase object. Hopefully, I used the code tags correctly.

    Java Code:
    import java.util.Scanner;
    public class CreatePurchase 
    {
    
        public static void main(String[] args)
    {
        int invoiceNumber;
        double SaleAmount;
        int invoiceNumber = 0;
        double SaleAmount = 0;
        Purchase newPurchase = new Purchase();
    }
        
    Scanner inputDevice = new Scanner(System.in);
    System.out.print ("Enter a number between 1000 and 8000:>");
    invoiceNumber = inputDevice.nextInt();
    inputDevice.nextLine();
    if (invoiceNumber < 1000 || invoiceNumber > 8000)
    {
    	System.out.print ("The number entered is invalid");
    	System.out.println("Please enter an invoice number between 1000 and 8000:>");
    	invoiceNumber = inputDevice.nextInt();
    }
    else
    
    	System.out.println ()
    	 newPurchase.setInvoice(invoice);
    	 newPurchase.setSale(saleAmount);
    }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Yep, your code tag look great.

    Some suggestions:
    • First of all (and in sorry in advance), you'll want to scrap your code and start over as you'll be better off.
    • Next, make sure that when you re-do this, you only add a little bit of code at a time, you compile frequently -- usually after each new line of code, and that you don't add any new code until all current compiler errors are fixed. Otherwise you'll end up adding code to bad code, and then end up with too many errors to fix.
    • Be sure that code statements are contained within constructor or method blocks. Your current code above has code statements that are out naked in the class, and that will never work. For instance in your program, all your code will likely need to be in the main method, but if you match your braces, you'll see that your main method ends right after the line, Purchase newPurchase = new Purchase();, and all code after the next closing brace is hanging out in the class outside of any method or constructor.
    Last edited by Fubarable; 06-11-2011 at 03:59 AM.

  7. #7
    BeginnerJava is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    Thanks, I will try that. Any suggestions at this point will help.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    What are the compiler messages you are having trouble with?

    Also it would be a very good idea to be consistent with the indenting. The code in a block (between { and the matching }) should be indented. Like this:

    Java Code:
    import java.util.Scanner;
    public class CreatePurchase 
    {
    
        public static void main(String[] args)
        {
            int invoiceNumber;
            double SaleAmount;
            int invoiceNumber = 0;
            double SaleAmount = 0;
            Purchase newPurchase = new Purchase();
        }
        
        Scanner inputDevice = new Scanner(System.in);
        System.out.print ("Enter a number between 1000 and 8000:>");
        invoiceNumber = inputDevice.nextInt();
        inputDevice.nextLine();
        if (invoiceNumber < 1000 || invoiceNumber > 8000)
        {
            System.out.print ("The number entered is invalid");
            System.out.println("Please enter an invoice number between 1000 and 8000:>");
            invoiceNumber = inputDevice.nextInt();
        }
        else
        System.out.println ()
        newPurchase.setInvoice(invoice);
        newPurchase.setSale(saleAmount);
    }
    }
    As this makes clear the braces don't match - there is an extra } at the end. The code starting "Scanner inputDevice = ..." is outside the main() method which is not what I think you mean. The "else" looks awfully bare: perhaps you forgot a brace on that line.

  9. #9
    BeginnerJava is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    I have gotten so many compiler errors, I can't even list them all. They all start on Line 24 and continue through the end. At this point, I'm so frustrated and confused with this code that I probably should just scrap it and start over. I'm a complete novice at Java, and am having so much difficulty with this. I've been working on this for the last 7 hours with no luck.

    I really appreciate your help. I'll try anything!

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Other suggestions:
    • Study your texts and your notes. They'll have examples that will be similar to what you're doing and you need to fully understand what they're doing and why.
    • Never guess when adding code. If you don't understand what you're doing, re-read your notes and text, since as you're finding out, the compiler is not forgiving and won't tolerate guesses.



    Good luck!

  11. #11
    JDScoot is offline Member
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    24
    Rep Power
    0

    Default

    Yes I would scrap the CreatePurchase class code. I would start with a stripped down version of the code and then slowly start filling in as you compile and test as Fubarable suggested.
    Java Code:
    public class CreatePurchase 
    {
        public static void main(String[] args)
        {
            int invoiceNumber;
            double SaleAmount;
            invoiceNumber = 0;
            SaleAmount = 0.0;
            Purchase newPurchase = new Purchase();
    
            // start your user input code here
    
            // validate user input
    
            // set the variable values of the Purchase class by calling the setter methods
    
            // Call the DisplaySale() method
    
            // All other code should go inside this curly brace or inside another method.
        }   
    
    }

  12. #12
    BeginnerJava is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    @JDScoot,

    Thanks for the advice. Looking at what you posted has made a world of difference!!! After working on this for so long, I think I lost focus and just kept adding code and getting more errors. I'm starting over right now.......

  13. #13
    BeginnerJava is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    I started over with new code and am happy to say that I only have 3 errors! Here is the new code and errors:

    Java Code:
    public class CreatePurchase 
    {
        public static void main(String[] args)
        {
            int invoiceNumber;
            double saleAmount;
            invoiceNumber = 0;
            SaleAmount = 0.0;
            Purchase newPurchase = new Purchase();
    		Scanner input = new Scanner(System.in);
    		
            // start your user input code here
    		
    		System.out.println
    			("Enter invoice number: ");
    		System.out.print
    			("Enter a number between 1000 and 8000.");
    		invoiceNumber = input.nextInt();
    		
    		System.out.println
    			("Enter sale amount: ");
    		System.out.print
    			("Enter a number greater than 0: ");
    		saleAmount = input.nextDouble ();
            
            // validate user input
            
    		while (invoiceNumber < 1000 || invoiceNumber > 8000)
    			System.out.println
    				("You entered an invalid number.");
    			System.out.println
    				("Please enter a number between 1000 and 8000.");
    			invoiceNumber = input.nextInt ();
    			
    		while (saleAmount < 0)
    			System.out.println
    				("You entered an invalid number.");		
    			System.out.println
    				("Please enter a number greater than 0.");
    			saleAmount = input.nextDouble ();
    				
            // set the variable values of the Purchase class by calling the setter methods
            
            	newPurchase.setInvoice(invoiceNumber);
            	newPurchase.setSale(saleAmount);
    
            // Call the DisplaySale() method
    			
    		public static void DisplaySale();
    			
    		{	
    			System.out.println("Invoice # >" + invoice + "Sale Amount = " + sale
    								+ "Sales Tax = " + salesTax);
    		}
    
            // All other code should go inside this curly brace or inside another method.
        }   
    
    }
    Here are the error messages:

    Java Code:
    JAVA Class\CreatePurchase.java:58: illegal start of expression
            public static void DisplaySale();
            ^
    JAVA Class\CreatePurchase.java:58: illegal start of expression
            public static void DisplaySale();
                   ^
    JAVA Class\CreatePurchase.java:58: ';' expected
            public static void DisplaySale();
                         ^
    3 errors
    Any suggestions??

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're declaring a method, DisplaySale, inside of another method, main, and this is not allowed in Java. Solution: declare the method outside of the main method. In addition, your method has a semi-colon at the end of its declaration:

    Java Code:
    public static void DisplaySale()[color="red"][b];[/b][/color]
    You'll want to check your notes and example code to see if this is proper form.

  15. #15
    JDScoot is offline Member
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    24
    Rep Power
    0

    Default

    You're so close now. As Fubarable suggested, you cannot declare a method inside of another method. Besides, you already have the DisplaySale() method declared in the Purchase class. All you need to do is call that method instead. You call that method the same way you call the setInvoice() or setSale() methods. Use the newPurchase reference and use the dot operator to call the DisplaySale() method and that should be it.

  16. #16
    JDScoot is offline Member
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    24
    Rep Power
    0

    Default

    Were you able to get it figured out?

  17. #17
    BeginnerJava is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    @JDScoot,

    Yes, I was able to compile and run it thanks to your help!!! I can't thank you enough! I was completely frustrated. Starting over and breaking down the code within the comment fields really made a difference! THANK YOU!!! :)

  18. #18
    JDScoot is offline Member
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    24
    Rep Power
    0

    Default

    My pleasure. I'm glad you were able to get it working and I'm glad I was able to help.

Similar Threads

  1. Confused on how to start this Math program
    By gnng in forum New To Java
    Replies: 3
    Last Post: 04-16-2011, 12:14 AM
  2. completely new
    By battosai16 in forum New To Java
    Replies: 1
    Last Post: 08-05-2010, 07:43 PM
  3. wait until directory is completely deleted
    By nickvandewiele in forum New To Java
    Replies: 1
    Last Post: 03-26-2010, 01:08 PM
  4. Completely new to Java problem
    By Seamo14 in forum New To Java
    Replies: 4
    Last Post: 10-09-2008, 12:50 PM
  5. I am completely stuck
    By jpnym15 in forum New To Java
    Replies: 2
    Last Post: 11-14-2007, 06:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •