• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java switch statement with multiple classes

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So here's the thing
there are 3 classes which is this one


and the main class will be the RunEmployee which i think will contain the switch statement


 
Angle Kuniyoshi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh wait i forgot to do something
 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please indent your code correctly; the contents of each method should be one place to the right of the {...}s, and you should have an empty line between each pair of methods.
Your implementation doesn't model full‑time and part‑time employees, but salaried and (hourly) waged employees, and surely each of them should be subclasses of Employee. I don't think you have implemented the wage correctly. You never set a wage; you simply report how many hours were worked and let the object calculate its wage.
You know that you can't use double arithmetic for calculating money in real life? Use integer arithmetic and denominate the money in ¢ or pence, or use BigDecimal. For more information, run the loop I showed in this old post (one compiler error needs correction), and look at the two links I posted there.

We can consider the switch later.
 
Angle Kuniyoshi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but I only followed the uml diagram tho
 
Bartender
Posts: 1737
63
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
Surely the UML diagram shows an inheritance relationship between the three classes, no?
 
Angle Kuniyoshi
Greenhorn
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They did actually that's why i said i forgot to do something.
ill post it later
 
Angle Kuniyoshi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


are these correct. sorry im still exploring inheritance and interfaces since it was just taught today
 
Jesse Silverman
Bartender
Posts: 1737
63
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
Sometimes we need to make a choice between something being an interface, and something being an abstract or concrete base class.

In Java, there are some restrictions provided by the language that help limit these decisions.

No interface can contain any writable, modifiable, changeable data.
If something which might be an interface or might be a class needs to define and contain its own writable data, it must be a class, not an interface.

In Java, a class can implement many interfaces, as long as they don't have any requirements that conflict with each other.

However, a class may directly extend only one class.

Making Employee an interface wouldn't work well because each and every Employee has his or her own name.

Neither making FullTimeEmployee a sub-class of PartTimeEmployee nor vice versa meets the requirements specified in the UML and description either.
It could be legal in Java, but would not be a correct implementation of the assigned work.
 
Jesse Silverman
Bartender
Posts: 1737
63
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
As an intermediate step between typing in Java code and just looking at the UML diagram, try describing the UML diagram aloud (well, maybe in print) in English.

What relationships is it requiring?

When you have that correct, the Java code for that isn't hard, definitely not after the first few times you do it, at least.
 
Angle Kuniyoshi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the uml diagram
Employee
-name:String
+ setName(): void
+ getName(): String

then both of these are pointing at the employee

FullTimeEmployee
- monthlySalary: double
+ setMonthlySalary(): void
+ getMonthlySalary(): double

PartTimeEmployee
- ratePerHour: double
- hoursWorked: int
- wage: double
+ setWage(): void
+ getWage(): double
 
Jesse Silverman
Bartender
Posts: 1737
63
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
If I recall correctly, standard UML does make clear the distinction between a class implementing an interface and a class sub-classing a super-class (also sometimes termed a derived class extending a base class, and also sometimes termed a child class inheriting a parent class--this last phrasing is not well-regarded by regulars on this forum and we avoid saying it that way).

Regardless, you should know for the course you are taking the difference between these in both the UML diagrams and in concepts.

In the matter at hand, it is logically clear from the information presented that they intend for Employee to be a common base-class (or super-class) to the two derived classes (or sub-classes).

There is no direct relationship between FullTimeEmployee and PartTimeEmployee apart from the fact that they are each sub-classing, or deriving from, or extending (you probably want to use the terminology preferred by your instructor) the common base class (or super-class, sorry that the world can't get together and use one set of terms) of Employee.

If that is clear as mud, first get this all clear to yourself.

The Java code is quite straight-forward once it is (but will feel new the first few times because it *is* new for you).

Trying to type the code out before this is clear is a very common mistake that can lead to confusion and frustration.
 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will find that while everyone replying here is both very competent and caring, many dislike UML so much that they would prefer to ignore it.

The discussion of whether UML is a useful tool that is quite often badly abused or a complete waste of time has taken place and will continue to take place elsewhere on this forum, for now, your course is using it so you must learn it.

I refreshed my memory from my favorite book, and it turns out the way you would know something is an interface, rather than an abstract or concrete class, is that you would see <<interface>> before or above its name in its box in the UML.  That is not shown there, so Employee is clearly, even from the UML, a base class rather than an interface.

Of course, the fact that it contains writable instance data (each Employee has a name) would logically tell us that it should be a base class and not an interface as well.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoever wrote that UML is confused about full‑time vs part‑time and salary vs wages.

Angle Kuniyoshi wrote:. . . the main class should inherit the 3 classes right?

No.

. . . are these correct. . . .

Almost certainly not. PartTime should not extend FullTime; if you are to use inheritance, both should extend Employee. Employee should probably be abstract, and should have an abstract getEarnings() method.
Giving the methods in the subclasses different names will make an Employee reference very difficult to use.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . many dislike UML . . .

Thirty years ago, UML was the bees' knees, but experience in the meantime has shown that it doesn't add anything useful to the software development process. Unfortunately, if you have been given that UML, you will only get good marks if you follow it. I suggest you show this discussion to your teachers.
 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Campbell on his feelings about the design that you have been given.

I consider whether UML used as Martin Fowler recommends it (rather than how it was commonly abused by so many) can be useful to be an open question.

I will note that all of Campbell's criticisms of the design can be discussed productively and agreed on just by looking at the UML, except that BigDecimal is not a standard UML data type I think.
All the rest of this discussion could totally be had at the UML level, and if different people were later going off and implementing it in Python, in Java, in C++, in C# and in Go, would be considered a reasonable way to present the design hierarchy by many, even today.

You may decide that your assignment was to implement the design as given, rather than to critique it and suggest improvements.

Which way you would best go would certainly depend on your instructor, and possibly where you are taking the course.

It is even possible that your next assignment might be "That was rather a mess, let's now correct the problems from the first design!"

I've done that many times.

The most conservative approach would be to just implement what was given, the best is subjective, the most time-consuming one (but one you would learn the most from) would be to do both and compare them.
 
Angle Kuniyoshi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it okay of i use polymorphism
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . BigDecimal is not a standard UML data type . . .

It is a built‑in type in basic Java®. And I hope I said BigInteger throughout, not BigDecimal. If I did, that was a mistake. Another mistake Sorry

. . . your assignment was to implement the design as given . . . just implement what was given . . .

That will be the only way to get good marks. As I said before, please discuss this thread with your teachers.
 
Angle Kuniyoshi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you check if this is ok. the format or placement of it?



then below will be the main method and switch statement
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angle Kuniyoshi wrote:Can you check if this is ok. the format or placement of it?

No, it isn't.Use spaces for indenting, not tabs. The methods should like like the above, if you use four spaces per level of indentation.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, always write a constructor for every class. It is sometimes necessary to have multiple constructors, but you can get problems if you allow a default constructor to be created. In this case, it allows you to create an Employee object without ever setting a name. The emp() method is not an adequate replacement for a constructor.
 
Jesse Silverman
Bartender
Posts: 1737
63
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

Campbell wrote:Use spaces for indenting, not tabs. The methods should like like the above, if you use four spaces per level of indentation.



Just because some posters have become confused in the past, it is essential that the source code gets *saved to disk* with spaces for indentation, rather than tabs.

Almost all decent editors let you choose "convert tabs to spaces" and set a value for "number of spaces per tab", so that whilst typing you are just casually hitting tab, but what the compiler and other readers see is ultimately all spaces.  You don't need to be hitting the space bar an enormous number of exactly-counted times.

Sounds silly, but some people take the "Use spaces for indenting, not tabs" advice to heart, and then come back with the complaint "UGH!!  My thumb is so tired!!"
 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Thumbs up!" is rarely so literal as in this case!
 
Angle Kuniyoshi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Angle Kuniyoshi wrote:Can you check if this is ok. the format or placement of it?

No, it isn't.



Can you point out whats wrong with it?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have already told you, and posted a corrected version of the first part of one class.
 
Angle Kuniyoshi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i fixed the indentions
are these classes ok?

 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angle Kuniyoshi wrote:i fixed the indentions

Only partially; your fields are only 1 space from the lefft, your methods 2 spaces, and your method bodies only 3 spacess. You need more space. Get a deccent text editor and set up its options to convert tab to 4 spaces automatically, and to supprot automatic indentation.


are these classes ok? . . .

Arfaid not. You have the extends right, but you still don't have any constructors. You still have a design which allows wage ≠ hours × hourlyRate.
 
reply
    Bookmark Topic Watch Topic
  • New Topic