Results 1 to 7 of 7
  1. #1
    ebrown086 is offline Member
    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Default Syntax error, insert "{" to complete ClassBody

    Hey guys, I have been posting my questions here as a new programmer for a few months now, and I have something else for you now. My prompt is to create a program under the following guidelines:
    Worker Program
    Create a Worker class. Each Worker has a name and a salary rate. The Worker class should have two constructors: a default constructor, and a constructor with parameters. Write a method computePay that takes in an integer parameter for the number of hours the Worker worked. The Worker class has two subclasses - SalariedWorker and HourlyWorker. An HourlyWorker gets paid their hourly rate for the first 40 hours, then gets paid time and a half for any additional hours. A SalariedWorker gets paid their hourly rate for 40 hours regardless of how many hours they worked. Write a test class that constructs an HourlyWorker and a SalariedWorker, asks the user how many hours each person worked, then outputs each worker's salary.

    I understand that I will need 4 classes, one called Worker- the superclass to all the others, one called computePay- where all the math is supposed to be done, HourlyWorker- a worker who is getting payed by the hour, and SalariedWorker- worker who gets payed flatrate. I have all four classes set up as shown below:
    Superclass Worker:
    Java Code:
    public class Worker {
    	public static void main(String[] args){
    		
    	}
    	public Worker(){
    	}
    	public Worker(int h, int i, int m, int t, String s, String n, String q, String b, String z){
    		System.out.println("Your employee worked" + h + "hours, and should be payed $"+t);
    		System.out.println("Your employee worked" + h + "hours, and should be payed $"+t);
    	}
    
    }
    Subclass computePay:
    Java Code:
    import javax.swing.JOptionPane;
    
    
    public class computePay extends Worker{
    	public computePay(){
    		
    	}
    	public computePay(int h, String i, String f){
    		
    	}
    
    }
    Subclass HourlyWorker:
    Java Code:
    import javax.swing.JOptionPane;
    
    
    public class HourlyWorker extends Worker{//Syntax error, insert "{" to complete ClassBody
    	
    	
    		j = JOptionPane.showInputDialog("Please input the amount of hours the employee worked this week");
    		h = Integer.parseInt(j);
    		t = 0;
    		for(int h <= 40; t+=8){//Syntax error on token "<=", ; expected
    				for(h>40; t+=12); //Syntax error on token "(", ; expected after this token
    		}
    		System.out.println("Your employee worked" + h + "hours, and should be payed $"+t);
    }//Syntax error, insert "}" to complete ClassBody
    And Sublclass SalariedWorker:
    Java Code:
    public class SalariedWorker extends Worker{
    
    
    }
    Now, I havent gotten to SalariedWorker because I keep getting stuck at the HourlyWorker with multiple error messages. If anybody could tell me what I am doing wrong as well as provide a brief way to do it right, and a short explination as to why that is right, I would dearly appreciate it...this whole deal is frustrating me...damn I need another coffee...

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Syntax error, insert "{" to complete ClassBody

    It would be nice if you told us what the errors are so we don't need to guess; for one thing: ordinary statements belong in a method body, not on their own at the class level.
    edit: I saw you put the error messages in the comments; all you have to do is read them and check a tutorial on how for loops are constructed because your attempts don't make sense.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Syntax error, insert "{" to complete ClassBody

    As Jos said, though. You have code that is not inside an executable block, which is what the compiler is pointing out. It's guess at a cure is that you need another set of {}. Presumably that code should be in a method or constructor?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    ebrown086 is offline Member
    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Default Re: Syntax error, insert "{" to complete ClassBody

    It is a constructor. Any idea on how to fix it? I am a little lost... :/

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Syntax error, insert "{" to complete ClassBody

    Well, you already did it with the Worker class, so you seem to know what a constructor looks like.
    So try that same thing with the HourlyWorker class.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    ebrown086 is offline Member
    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Default Re: Syntax error, insert "{" to complete ClassBody

    Quote Originally Posted by Tolls View Post
    Well, you already did it with the Worker class, so you seem to know what a constructor looks like.
    So try that same thing with the HourlyWorker class.
    I built it similarly, but my problem is with the loops. I just need to know what is up with my loops and then I can fix this....

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Syntax error, insert "{" to complete ClassBody

    Quote Originally Posted by ebrown086 View Post
    I built it similarly, but my problem is with the loops. I just need to know what is up with my loops and then I can fix this....
    Your two loops in lines #10 and line #11 are simply utterly incorrect; a for-loop has an initialization part, a condition part and an inccrement/update part; all parts are optional, but doing stuff like 'int n <= 40' doesn't make sense anywhere except in your imagination; please read a text book on the subject before you start coding.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Syntax error on token "(", @ expected and more errors
    By ebrown086 in forum New To Java
    Replies: 4
    Last Post: 08-17-2013, 05:31 PM
  2. Syntax error on token "}", super expected
    By amentjacob1 in forum New To Java
    Replies: 9
    Last Post: 07-06-2012, 04:15 AM
  3. Syntax error in eclipse for "enum" defination.
    By gunwantw in forum Eclipse
    Replies: 1
    Last Post: 07-08-2010, 09:21 AM
  4. Syntax error on token "(", ; expected
    By baltimore in forum AWT / Swing
    Replies: 3
    Last Post: 10-28-2009, 12:19 AM
  5. Syntax error on token "(", ; expected
    By romilc in forum New To Java
    Replies: 7
    Last Post: 10-24-2009, 01:23 AM

Tags for this Thread

Posting Permissions

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