inherit
27278
0
Aug 3, 2024 9:13:18 GMT -8
Josh
Apple iManiac / eBay Addict
12,347
July 2004
jwd41190
|
Post by Josh on Jan 14, 2010 18:04:46 GMT -8
Hello Everyone, I am having problems once again programming. This time it is java I just finished c++. Here is what I have to do: Create a class called Employee that includes three pieces of information as instance variables-a first name (type string), a last name (type string), and a monthly salary (type double). Your class should have a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, set it to 0.0. Write a test application named EmployeeTest that demonstrates class Employee's capabilities. Create two Employee objects and display each object's yearly salary. Then give each Employee a 10% raise and display each Employee's yearly salary again. And Here is What I have so far: public class Employee {
public static void main( String args[] ) {
string firstName; string lastName; double monthlySalary;
//constructors
setfirstName( String fName ) { firstName = fName; }
String getfirstName () { return firstName; }
setlastName( String lName ) { lastName = lName; }
String getlastName () { return lastName; }
setmonthlySalary( double Sal ) { monthlySalary = Sal; }
public String getmonthlySalary () { return monthlySalary; } } }
I am not sure if I am even going in the right direction we just start java and I am confused. Any help would be great. Thanks
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Jan 15, 2010 13:51:52 GMT -8
I rarely use Java (although I have to get really good at it in the next few years for my major) so I can't give you all the answers just by looking at this. What I do know is that you probably need a separate class for the main method. Also, all those methods you have defined are not constructors, they are just the getters and setters for the object. A constructor is the method which is invoked when a new instance of the class is created (in this case it would be in the main method).
Here's what you need for your constructors.
public class Employee { public Employee() { // Set all values to default values. }
// This is an overloaded constructor. public Employee(string first, string last, double salary) { // Set attributes to the values being passed by the parameters. }
// Your getters and setters. }
The main method should look something like...
public class Main { public static void main( String args[] ) { // Create employee objects. Employee e1 = new Employee(); Employee e2 = new Employee("David", "Koresh", 5000);
// Do what the programming assignment asks.
} }
|
|
inherit
100824
0
May 13, 2012 5:37:49 GMT -8
Michael
14,585
March 2007
wrighty
|
Post by Michael on Jan 15, 2010 15:01:09 GMT -8
jordan, a new class isn't required. public class Employee { public static void main( String args[] ){ String firstName; String lastName; double monthlySalary;
//constructors } public static String setfirstName( String fName ){ firstName = fName; }
public static String getfirstName (){ return firstName; }
public static String setlastName( String lName ){ lastName = lName; }
public static String getlastName (){ return lastName; }
public static double setmonthlySalary( double Sal ){ monthlySalary = Sal; }
public String getmonthlySalary (){ return monthlySalary; } }
String variable types, require a capital letter for the word String. As it's actually done through a stucture or something like that. Not sure the above code will work, but you can't have a function in a function!
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Jan 15, 2010 19:25:09 GMT -8
Ah, thanks for the info Wrighty. I've been spending so many hours learning and writing in C++ that I've temporarily forgotten everything else. ;P
I forgot that strings are built in data types as well cause in C++ you have to either include a premade string class or make your own.
|
|
inherit
100824
0
May 13, 2012 5:37:49 GMT -8
Michael
14,585
March 2007
wrighty
|
Post by Michael on Jan 15, 2010 20:09:17 GMT -8
For what he's doing he'll probably have to include the java.utils.scanner package.
|
|
inherit
27278
0
Aug 3, 2024 9:13:18 GMT -8
Josh
Apple iManiac / eBay Addict
12,347
July 2004
jwd41190
|
Post by Josh on Jan 19, 2010 10:50:15 GMT -8
Thanks guys, but I'm stuck again and this is due tonight This is what I got so far now: public class Employee {
//instance variables at top
private string firstName; private string lastName; private double monthlySalary;
public Employee() { monthlySalary = 0.0; } public void String setfirstName( String fName ) { firstName = fName; }
public void String getfirstName () { return firstName; }
public void String setlastName( String lName ) { lastName = lName; }
public void String getlastName () { return lastName; }
public void double setmonthlySalary( double Sal ) { monthlySalary = Sal; }
public double getmonthlySalary () { return monthlySalary; } }
public class EmployeeTest {
public static void main(String args[]) { Employee e1 = new Employee(25000); Employee e2 = new Employee(50000, "Blue"); System.out.println("Employee 1's Yearly Salary is" + e1.getmonthlySalary()); System.out.println("Employee 2's Yearly Salary is" + e2.getmonthlySalary()); e1.setmonthlySalary(30000); System.out.println("Employee 1's Yearly Salary with 10% raise is " + e1.setmonthlySalary());
} }
I have 3 errors.... public class EmployeeTest { public static void main(String args[]) { Employee e1 = new Employee(25000); Employee e2 = new Employee(50000, "Blue"); System.out.println("Employee 1's Yearly Salary is" + e1.getmonthlySalary()); System.out.println("Employee 2's Yearly Salary is" + e2.getmonthlySalary()); e1.setmonthlySalary(30000); System.out.println("Employee 1's Yearly Salary with 10% raise is " + e1.setmonthlySalary()); } }
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Jan 19, 2010 12:58:10 GMT -8
What are the errors that you are getting? Also, you can't call this line because you don't have an overloaded constructor that deals with an integer and a string.
Employee e2 = new Employee(50000, "Blue");
|
|
inherit
27278
0
Aug 3, 2024 9:13:18 GMT -8
Josh
Apple iManiac / eBay Addict
12,347
July 2004
jwd41190
|
Post by Josh on Jan 19, 2010 17:58:42 GMT -8
Ok, don't worry about it thanks though I went ahead ans submitted what I had. I'm gonna try and get some help on this later. Thanks again.
|
|
inherit
100824
0
May 13, 2012 5:37:49 GMT -8
Michael
14,585
March 2007
wrighty
|
Post by Michael on Jan 20, 2010 6:32:17 GMT -8
public Employee() { monthlySalary = 0.0; } That needs changing, you need 3 of those in total. One to handle no parameters (above), one for just earnings, and one for earnings & color. (or whatever) I can code it up for you if you want?
|
|