Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Polymorphism with Switch statement based on Uml diagram

 
Ranch Foreman
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know why it says these errors

/RunEmployee.java:16: error: <identifier> expected
   System.out.println("Enter name: ");
                     ^
/RunEmployee.java:16: error: illegal start of type
   System.out.println("Enter name: ");
                      ^
/RunEmployee.java:17: error: <identifier> expected
    name = s.nextLine();
        ^
/RunEmployee.java:53: error: ';' expected
public void emp() {
               ^
/RunEmployee.java:63: error: illegal start of expression
public class RunEmployee {
^

i think my main class is a mess (i think all of it is a mess). i need someone's knowledge to help me with this. My brain is drained.
so here's the mess. sorry in advance

 
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have an extra character on line 15. I'll leave it for you to find.
Lines 15-17 need to be inside a method.
 
Zylith Zen
Ranch Foreman
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so it has to be in the main? as well as the others?
 
Carey Brown
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you have some general messes to clean up. I'd start by fixing your indentation.

In your Employee class you have an emp() method. The method name is terrible because it doesn't describe what it does. "enterData" or some such would be more apt. And following how that is used in your sub-classes then that would mean that Employee would also need an enterData() method that prompts for and sets the "name" field. This would  also mean that this method in you sub-classes would need to call super.enterData() in order to set "name".
 
Carey Brown
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We've been through this before...

There are two accepted brace styles:
K&R (Kernigan & Ritchie)
Alman
Indentation should consist of multiples of 4 spaces.

Personally I think that Alman makes it easier for beginners to pick out errors in enclosing blocks of code.
 
Zylith Zen
Ranch Foreman
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it more complicated if i put the prinlns in the main method?
 
Saloon Keeper
Posts: 13379
295
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More complicated than what?

You want your application to compile and run, right?
 
Zylith Zen
Ranch Foreman
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh sorry. wrong choice of word. i meant an option. nevermind that and yes i do
 
Marshal
Posts: 74354
334
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zylith Zen wrote:is it more complicated if i put the prinlns in the main method?

It is a good idea to move as much code out of the main() method  as possible; i agree with Winston that the correct length of a main() method is one statement.
 
Zylith Zen
Ranch Foreman
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is this right?

 
Campbell Ritchie
Marshal
Posts: 74354
334
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.
You haven't written a constructor, so the name, hours worked, and wage rate will all default to 0/null. There is no guarantee that the setXXX() methods will ever be called, so that error may persist for ever.
I don't think you should write a setWage() method at all, because the wage should be calculated by the object.
Don't try calculating the wage in the print method. The print method has a very difficult name to read.
Don't write a print method like that; override toString(). Make it return something like this:-

Zylith Zen: wage=123456789 hours@$0.000000000000000000000000000000000001=$0.00

Use getWage() instead of the wage variable. In fact, I think you should get rid of the wage variable altogether because of the risk that sometimes wage ≠ hour × rate.
 
Zylith Zen
Ranch Foreman
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like this?

 
Saloon Keeper
Posts: 1614
52
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see how this code even compiles at the moment:


The syntax you would be looking for would be this.name = name; etc.
You are allowed to use capital Letters for member variable names, parameters and local variables, but please refrain from doing so.
It is a pretty strong convention in Java to only capitalize names of YourClasses and type parameters, when you get to generics.

 
Zylith Zen
Ranch Foreman
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:I don't see how this code even compiles at the moment:



Why?
 
Jesse Silverman
Saloon Keeper
Posts: 1614
52
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is Name defined?  I didn't see it anywhere.

The Java compiler is case-sensitive.

Name, name, nAmE and NAME are four different variables to Java.

All are legal, but only the second is considered acceptable convention by most Java programmers.

EDIT -- Sorry, I meant only 'name'.  Trying to finish up before going down to lunch and rushing a bit.


 
Zylith Zen
Ranch Foreman
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
other than that. what else is wrong?
 
Carey Brown
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Run it through your compiler and fix the errors, then re-post it here. Be respectful, show some effort and not expect us to sort out your compilation problems.
 
Carey Brown
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 74354
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All employess should have some sort of payment; there should be an abstract getEarnings() method or similar in the Employee class. It can be implemented by all the different subclasses.
 
Zylith Zen
Ranch Foreman
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im trying to create object of the 2 but idk how to fix it

/RunEmployee.java:98: error: constructor FullTimeEmployee in class FullTimeEmployee cannot be applied to given types;
       FullTimeEmployee fullTimeEmployee = new FullTimeEmployee();
                                                               ^
 required: String,double
 found: no arguments
 reason: actual and formal argument lists differ in length
/RunEmployee.java:99: error: constructor PartTimeEmployee in class PartTimeEmployee cannot be applied to given types;
       PartTimeEmployee partTimeEmployee = new PartTimeEmployee();
                                                                 ^
 
Jesse Silverman
Saloon Keeper
Posts: 1614
52
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are allowed to create as many constructors for any of your classes as you believe you need in a good design.

For your classes, as far as we can tell, you have created the following constructors:



What follows your new must match a defined constructor.
You have one choice for each of these classes (well, only one possible call), that which matches the ones above.

If you define no constructors at all for your class, the compiler inserts one for free that does nothing interesting, that you can call with no parameters as you have.
It takes no arguments and is called the "Default, no-Args constructor".

If you define even one constructor for your class, you do NOT get that "Default, no-Args constructor" from the compiler for free.
If you think you need one and can decide some reasonable values to use when called with no arguments, you need to define one.

A better solution is to call your constructor that you defined, passing appropriate values.
That is why you defined it.

You should have been taught the syntax for this in your lessons:




Cheers!
 
Zylith Zen
Ranch Foreman
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But theres no real value since the user have to input the name, monthly salary, rate per hour, hours worked and wage
 
Campbell Ritchie
Marshal
Posts: 74354
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those values should be input into the constructor. I would think hours worked can be input repeatedly, so that can be a method parameter. The other values should probably be constructor parameters. Otherwise you are permittng your Employee objects to be created in an incoisnstent state.
 
Zylith Zen
Ranch Foreman
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think i fixed the constructor but this error came up

/RunEmployee.java:30: error: constructor Employee in class Employee cannot be applied to given types;
   private FullTimeEmployee() {
                                           ^
 required: String
 found: no arguments
 reason: actual and formal argument lists differ in length
/RunEmployee.java:74: error: constructor Employee in class Employee cannot be applied to given types;
   private PartTimeEmployee() {
                                            ^


this is what i did in the 2 classes

 
Campbell Ritchie
Marshal
Posts: 74354
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you change your constructor to accept those parameters? What you have shown here isn't a constructor.
 
Campbell Ritchie
Marshal
Posts: 74354
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, you did show us a constructor. You haven't changed it. You are now allowing instances to be created via that factory method which are in an inconsistent state: the salary is 0 and the name is null.

Bad: allows instance in an inconsistent state:-
Worse: allows instance in an inconsistent state, and the default constructor might not do what you want:-
Good: allows instance only in a consistent state, and the if obviates the risk of anything being null:-Note:
  • 1: After a throw, there is no need for an else.
  • 2: It is probably better to use methods of this class than the if.


  • [ed]tAdded block starting “Worse”. Then deleted its constructor. Sorry for all the changes.
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic