File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Implement a class Employee Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Implement a class Employee" Watch "Implement a class Employee" New topic
Author

Implement a class Employee

Rob Samberg
Greenhorn

Joined: Jan 28, 2006
Posts: 8
An employee has a name (a string) and salary (a double). I'm supposed to write a default constructor, a constructor with two parameters (name and salary), and methods to return the name and salary. Then I have to write a small program that tests my class.
Please help!!!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

What do you have so far?


[Jess in Action][AskingGoodQuestions]
Rob Samberg
Greenhorn

Joined: Jan 28, 2006
Posts: 8
I did mention I'm brand new to this I hope. Well I am. I really don't know what the heck I'm doing. I'd appreciate anything at all that you think might help!
I don't mind reading and figuring stuff out, but I don't even know where to look.
Thanks,
Rob
[code]
public class Employee
{

public Employee()
{
name = "unknown";
}

public Employee()
{
salary = 0;
}


public class EmployeeTester
{

public EmployeeTester ()
{
name = "Robert Samberg";
salary = 50000;

System.out.println("name" "salary")

public void newSalary(double amount)
{
double newSalary = salary + (salary/20.0);
salary = newSalary;
}
[code]

Can someone tell me what if anything is right with this code?
[ January 28, 2006: Message edited by: Rob Samberg ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Well, I'll try to help a little with what you do have. I'll start with the Emplyee class:

The Java compiler won't let you do this. If you have two constructors they must have different signitures meaning, the must take a different number or type of parameters. These two constructors should be combined to give you your default constructor:

As for this bit:

You should avoid putting a main() method into this class at all, leave that for your EmployeeTester class. Instead you should write getter and setter methods to access your Employee object's name and salary i.e. getName(), setName(), getSalary(), setSalary(). These methods can be called by the main() method in your EmployeeTester class to mutate and access information regarding any Employee object. Hope this can get you started.

Garrett


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Rob Samberg
Greenhorn

Joined: Jan 28, 2006
Posts: 8
Ok - that's great - I'm going to work on that for a while. Thanks. If you can think of anything else that might help, even if it's just general Java stuff for a beginner, I'd appreciate it.
Thanks again.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Rob Samberg:
... If you can think of anything else that might help, even if it's just general Java stuff for a beginner, I'd appreciate it...

Use Code Tags to preserve your formatting.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
Are you guys using an IDE? Creating getters and setters are catchy the first two times after that it is just tiresome. IDE's will help a lot with that.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Gerardo Tasistro:
Are you guys using an IDE? Creating getters and setters are catchy the first two times after that it is just tiresome. IDE's will help a lot with that.


IMO for a beginner, it is necessary to write getters and setters by hand. No IDE, just a text editor. In much the same way that many simple arithmetic problems can be solved on a calculator, it is a lot more educational to learn to do it yourself first. You know "Give a man a fish..."
[ January 28, 2006: Message edited by: Garrett Rowe ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Implement a class Employee