#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Are these classes doing what they suppose to?


    hi there!
    i have this homework and i honestly have no idea what my professor really want ...
    could you take a look of this and tell me if it does what she want?

    homework:
    Define a class named "Employee" with 3 instance variables:
    name_ name of the employee
    ssn_ social security number
    salary_ dollar amount of salaryAssignment
    For each instance variable, define two private methods, a getter and a setter. For example, for the instance
    variable called "name_", you will need two methods
    private String name()
    /* with no parameter, this gets the current value of the name_ variable and returns it to the caller. */
    private void name(String newName)
    /* with one parameter, this sets the new value of the name_ variable to the value in "newName" */

    For the class Employee you also need to define:
    a private initialize() method with a parameter for each instance variable.
    a toString() method that takes no parameters and returns a string representing the data in the object.
    a default constructor which supplies default values of your choice for each of the instance variables.
    an alternative constructor which takes a parameter for each of the instance variables.
    Therefore, in order to receive full credit, your class Employee will have 3 instance variables and 10 methods.
    Before you write class Employee, you need to write a class called "TestEmployee" whose only purpose is to
    test the pubic methods in your "Employee" class. Make sure that your test program calls each public method
    and prints the results to the system console.

    my code
    Code:
    class testEmployee{
    	public static void main(String[] args) {	
    		Employee employeeObj = new Employee();
    		System.out.println(employeeObj.toString());
    		Employee employeeObj2 = new Employee("sandeep", 20103706, 2829.66);
    		System.out.println(employeeObj2.toString());
    	}
    }
    
    class Employee{
    
    	//variable declarations
    	private String name_ ;
    	private int ssn_ ;
    	private double sallary_ ;
    
    	//getters and setters
    	private String getName () {
    		return name_;
    	}
    	private void setName (String name_) {
    		this.name_ = name_;
    	}
    	
    	private int getSsn() {
    		return ssn_;
    	}
    	private void setSsn (int ssn_) {
    		this.ssn_ = ssn_;
    	}
    	
    	private double getSallary () {
    		return sallary_;
    	}
    	private void setSallary (double sallary_) {
    		this.sallary_ = sallary_;
    	}
    
    	//constructors
    	Employee (){
    		//constructor
    		name_ = "alsih" ;
    		ssn_ = 333224444;
    		sallary_ = 5000.50;
    	}
    
    	Employee (String name_, int ssn_, double sallary_){
    		initialize(name_, ssn_,  sallary_);
    	 }	
    
    	private void  initialize(String name_, int ssn_, double sallary_){
    	 	setName(name_);
    	 	setSallary (sallary_);
    	 	setSsn (ssn_);
    	 }
    
    	public String toString(){
    	 	String dataString;
    	 	dataString = getName() + "  " + getSsn() + "  " + getSallary();
    	 	return dataString;
    	 }
    
    	
    }
    thanks for taking a look
  2. #2
  3. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,540
    Rep Power
    1621

    Understanding what the homework requests you to do


    You are almost there, but you have yet to fulfill the following requirement:

    Make sure that your test program calls each public method
    That means, in your TestEmployee class, you should somehow instantiate a new instance of Employee, then print out the outcome by calling to the getter method after calling each setter method for each class member declared, in addition to what you have already done in your main() method (so far your have done a good job of printing out values after invoking different constructors).

    Have you ever run your program and see what is the output? I would suggest you declare your classes in a new file each (one file one class), and have your classes declared with the keyword public:

    java Code:
    public class Employee{
    //your class content here..
    }
    When the programming world turns decent, the real world will turn upside down.
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    It's strange the getter and setter methods are private. What code is supposed to use them?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by NormR
    It's strange the getter and setter methods are private. What code is supposed to use them?
    thats exactly why am not sure what this program is meant to do...
  8. #5
  9. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,540
    Rep Power
    1621

    Could be an error


    It could be a print error or a silly careless mistake your professor made in hurry.

    Change them all to public and you'll be good to go.

    Alternatively, declare a public method in the Employee class which will print out values returned by getters before and after the setters are called. Then, have the main method invoke this one and the only public method in the class.
    When the programming world turns decent, the real world will turn upside down.

IMN logo majestic logo threadwatch logo seochat tools logo