473,387 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

I need help with this problem

Create a class called Employee. An Employee should have four pieces of information as instance variables – first name (type String), last name (type String), monthly salary (type double), and dependents(type int). Your class should have a constructor that initializes the four instance variables. Provide a get and set method for each instance variable. In addition, provide a method named getAnnualSalary that determines the annual salary of the employee (i.e. monthly salary multiplied by 12), and returns the annual salary (a double value). Write a test application named EmployeeTest that demonstrates class Employee's capabilities.

This what I have so far but I and I can't get the test to work because of some problem with the constructor.

Expand|Select|Wrap|Line Numbers
  1. package employee;
  2.  
  3. public class Employee {
  4.  
  5.     private String firstName;
  6.     private String lastName;
  7.     private double _monthlySalary; // monthly salary
  8.     public int dependents;
  9.  
  10.     //constructor
  11.    public Employee(String first, String last, double salary, int dep)
  12.    {
  13.       firstName= first;
  14.       lastName= last;
  15.       _monthlySalary= salary;
  16.       dependents= dep;
  17.      }
  18.  
  19.    // end Employee constructor
  20.    public String getFirstName()
  21.    {
  22.        return firstName;
  23.    }
  24.  
  25.    public void setFirstName(String first)
  26.     {
  27.         firstName= first;
  28. }
  29.    public String getLastName()
  30.    {
  31.        return lastName;
  32.    }
  33.     public void setLastName(String last)
  34.     {
  35.         lastName= last;
  36. }
  37.     public int getDependents()
  38.     {
  39.         return dependents;
  40. }
  41.     public void setDependents (int dep)
  42.     {
  43.         dependents= dep;
  44.     }
  45.  
  46. public void setMonthlySalary ( double salary)
  47. {
  48.     _monthlySalary= salary;
  49.  }
  50. public double getMonthlySalary ()
  51. {
  52.     return _monthlySalary;
  53. }
  54. public double getAnnualSalary()
  55. {
  56.     return getMonthlySalary() *12;
  57. }
  58. }


here is the test


Expand|Select|Wrap|Line Numbers
  1. package employee;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class EmployeeTest
  7. {
  8.     public static void main( String args[] )
  9.     {
  10.         Employee e1 = new Employee ();
  11.  
  12.         Scanner input = new Scanner( System.in );
  13.         String first;
  14.         String last;
  15.         double salary;
  16.  
  17.         System.out.print( "Enter the first name of the first employee: " );
  18.         first = input.next();
  19.         employee1.setFirstName( first );
  20.  
  21.         System.out.print( "Enter the last name of the first employee: " );
  22.         last = input.next();
  23.         employee1.setLastName( last );
  24.  
  25.         System.out.print( "Enter the first employee's monthly salary: " );
  26.         salary = input.nextDouble();
  27.         employee1.setMonthlySalary( salary );
  28.  
  29.         System.out.print( "Enter the first name of the second employee: " );
  30.         first = input.next();
  31.         employee2.setFirstName( first );
  32.  
  33.         System.out.print( "Enter the last name of the second employee: " );
  34.         last = input.next();
  35.         employee2.setLastName( last );
  36.  
  37.         System.out.print( "Enter the second employee's monthly salary: " );
  38.         salary = input.nextDouble();
  39.         employee2.setMonthlySalary( salary );
  40.  
  41.  
  42.         System.out.printf( "Now displaying employees' full names and annual salary.\n");
  43.         System.out.printf( employee1.getFirstName()," ", employee1.getLastName(), "     ", employee1.getAnnualSalary(), "\n" );
  44.         System.out.printf( employee2.getFirstName()," ", employee2.getLastName(), "     ", employee2.getAnnualSalary(),  "\n" );
  45.  
  46.  
  47.     }
  48. }
Sep 20 '09 #1
2 2213
r035198x
13,262 8TB
Please use code tags if you have to post code.
What error message did you get?
Sep 20 '09 #2
Frinavale
9,735 Expert Mod 8TB
What's the problem?
Your class looks fine........
Sep 21 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: kazack | last post by:
I posted a similiar question in this newsgroup already and got an answer which I already knew but didn't get the answer I was looking for so I am reposting the code and question differently in the...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
2
by: bryan | last post by:
I have a situation that's pretty delicate that I need some help on. I've been stumped for awhile I just need some advice on the best possible solution. The problem: I have a site I'm making...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
13
by: PinkBishop | last post by:
I am using VS 2005 with a formview control trying to insert a record to my access db. The data is submitted to the main table no problem, but I need to carry the catID to the bridge table...
25
by: Jon Slaughter | last post by:
I have some code that loads up some php/html files and does a few things to them and ultimately returns an html file with some php code in it. I then pass that file onto the user by using echo. Of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.