Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    ubermeister is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default [SOLVED] few steps i need help with

    ok this is what my assignment calls for. i need to create a main() function that would implement as having the following steps:

    1. creating an object employee1 w/out perameter
    2.creating an object employee2 with the following parameters (mary,1111,42000)
    3.display employee1 data
    4.display employee2 data
    5. calling getData function to enter data for employe1
    6.redisplaying employee1 data
    7. creating an arrary of employee objects with 3 members
    8. writing the loop statment to accept data and print data for each of the array member.

    this is what i have so far:
    Java Code:
    public class TestEmployeeClass
    {
    		public static void main(String[] args)
    		{
    
    		EmployeeClass s1= new EmployeeClass();
    			s1.display();
    			s1.displayTotal();
    
    		EmployeeClass s1= new EmployeeClass(MARY,1111,7500);
    			s1.display();
    			s1.displayTotal();


    THIS code below is the 2nd part to it. (the code below works fine, use as guide for the one above)

    Java Code:
    import java.util.Scanner;
    public class EmployeeClass
    {
    	static int total=0;
    
    	//member data
    
    	String name;
    	int id;
    	double salary;
    
    
    
    	//constructor
    
    	EmployeeClass()
    	{
    		name="";
    		id=0;
    		salary=0.0;
    	}
    
    	EmployeeClass(String n,int i, double s)
    	{
    
    		name=n;
    		id=i;
    		salary=s;
    	}
    
    	public void getdata()
    	{
    		Scanner kb= new Scanner(System.in);
    		String s;
    
    		System.out.print("Enter Name : ");
    		name=kb.nextLine();
    
    		System.out.print("Enter ID : ");
    		id=kb.nextInt();
    
    		//class buffer
    
    		s=kb.nextLine();
    
    		System.out.print("Enter Salary : ");
    		name=kb.nextLine();
    	}
    	public void display()
    	{
    		System.out.printf("NAME = %s\n",name);
    		System.out.printf("ID = %s\n",id);
    		System.out.printf("SALARY = %s\n",salary);
    
    	}
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Hi ubermeister,

    Welcome to our community. :)

    Seems to me your application is ok, so what is your question there?

  3. #3
    ubermeister is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Hi ubermeister,

    Welcome to our community. :)

    Seems to me your application is ok, so what is your question there?
    thanks i need help with steps 1-8 for the first code I put above. I think i have followed 1-4 so its just steps 5-8 i need assistance doing.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You have to get data at the TestEmployeeClass I think. Then after using instance and appropriate constructors do the processing. Got it?

  5. #5
    ubermeister is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    ya i know that has to be done but HOW do you go about doing that.

    take step 5.

    i wouldn't know what to do after this

    public void getdata()
    {

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You asking that how to implement that method? There have few errors in your code. Try this.

    Java Code:
       // Collect data from the user        
    	public void getdata() {
    		Scanner kb = new Scanner(System.in);
    
    		System.out.print("Enter Name : ");
    		name = kb.nextLine();
    
    		System.out.print("Enter ID : ");
    		id = kb.nextInt();
    
    		System.out.print("Enter Salary : ");
    		salary = kb.nextInt();
    	}

  7. #7
    ubermeister is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    You asking that how to implement that method? There have few errors in your code. Try this.

    Java Code:
       // Collect data from the user        
    	public void getdata() {
    		Scanner kb = new Scanner(System.in);
    
    		System.out.print("Enter Name : ");
    		name = kb.nextLine();
    
    		System.out.print("Enter ID : ");
    		id = kb.nextInt();
    
    		System.out.print("Enter Salary : ");
    		salary = kb.nextInt();
    	}
    nonono thats not the code i'm referring to.
    i'm talking about the FIRST code i have up i need to ADD a getData function with employee1 info in the main() function.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ah, you asking how to call that function on an employee object.

    Java Code:
        public static void main(String[] args) {
    		EmployeeClass s1= new EmployeeClass();
    			s1.display();
    
    		EmployeeClass s2= new EmployeeClass("MARY",1111,7500);
    			s2.display();
    			
                    EmployeeClass s3 = new EmployeeClass();
                            s3.getdata();
                            s3.display();
        }

  9. #9
    ubermeister is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Ah, you asking how to call that function on an employee object.

    Java Code:
        public static void main(String[] args) {
    		EmployeeClass s1= new EmployeeClass();
    			s1.display();
    
    		EmployeeClass s2= new EmployeeClass("MARY",1111,7500);
    			s2.display();
    			
                    EmployeeClass s3 = new EmployeeClass();
                            s3.getdata();
                            s3.display();
        }
    yes!!! ty ty now how do you redisplay employee1 data

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    As like this.

    Java Code:
    s1.display();
    Since you have set the default values, display that. If you want to get the data from the user and display, call the getData() first and then display them.
    Last edited by Eranga; 05-05-2008 at 04:29 AM.

  11. #11
    ubermeister is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    As like this.

    Java Code:
    s1.display();
    Since you have set the default values, display that. If you want to get the data from the user and display, call the getData() first and then display them.
    thank you very much my last question is how you do step 8?

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you create an array of Employee objects? If so, loop them on each and do the same process.

  13. #13
    ubermeister is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Did you create an array of Employee objects? If so, loop them on each and do the same process.
    Java Code:
    EmployeeClass[] students= new EmployeeClass[3];
    	for(int k =0;k < employee.length; k++)
    	{
    System.out.printf("\nWorking on employee : %d\n", (k+1));
    // create an instance for each member
    
    employees[k] = new StudentClass();
    employees[k].getdata();
    employees[k].display();
    employees[k].displayTotal();
    thats my array i just don't know what to WRITE for the loops.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In simple way you can do this pal.

    Java Code:
                    EmployeeClass[] collection = new EmployeeClass[] {new EmployeeClass(), 
                    new EmployeeClass(), new EmployeeClass()};
                    
                    for(int i = 0; i < collection.length; i ++) {
                        collection[i].getdata();
                    }
                    for(int j = 0; j < collection.length; j++) {
                        collection[j].display();
                    }
    What is displayTotal() do. I can't see anything about it.

  15. #15
    ubermeister is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    i'm taking those out

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  17. #17
    ubermeister is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    ya that last bit of code was it now i'm just debugging thanks for all your help!!!

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  19. #19
    ubermeister is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    ya theirs a million way to do this yet 1 mistake screws everythign up >_< (i'm more of a networking guy!!)

    these are my errors

    Java Code:
    C:\Documents and Settings\Beserk\Desktop\TestEmployeeClass.java:24: cannot find symbol
    symbol  : variable employee
    location: class TestEmployeeClass
    				for(int k =0;k < employee.length; k++)
    				                 ^
    C:\Documents and Settings\Beserk\Desktop\TestEmployeeClass.java:28: cannot find symbol
    symbol  : variable employees
    location: class TestEmployeeClass
    					employees[k] = new EmployeeClass();
    					^
    C:\Documents and Settings\Beserk\Desktop\TestEmployeeClass.java:29: cannot find symbol
    symbol  : variable employees
    location: class TestEmployeeClass
    					employees[k].getdata();
    					^
    C:\Documents and Settings\Beserk\Desktop\TestEmployeeClass.java:30: cannot find symbol
    symbol  : variable employees
    location: class TestEmployeeClass
    					employees[k].display();

    final code

    Java Code:
    public class TestEmployeeClass
    {
    		public static void main(String[] args)
    		{
    
    		EmployeeClass s1= new EmployeeClass();
    			s1.display();
    
    
    	EmployeeClass s2= new EmployeeClass("MARY",1111,75000);
    			s2.display();
    
    
    	EmployeeClass s3 = new EmployeeClass();
    		    s3.getdata();
    		    s3.display();
    
    		s1.display();
    
    
    
    	//array
    	EmployeeClass[] students= new EmployeeClass[3];
    	for(int k =0;k < employee.length; k++)
    		{
              System.out.printf("\nWorking on employee : %d\n", (k+1));
    
    // create an instance for each member
    	employees[k] = new EmployeeClass();
                 employees[k].getdata();
    	employees[k].display();
    
    
       EmployeeClass[] collection = new EmployeeClass[] {new EmployeeClass(),
    	 new EmployeeClass(), new EmployeeClass()};
    
            for(int i = 0; i < collection.length; i ++) {
            collection[i].getdata();
     }
          for(int j = 0; j < collection.length; j++) {
          collection[j].display();
         }
    }
    }
    }

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Page 1 of 2 12 LastLast

Similar Threads

  1. differents steps??
    By Yel in forum New To Java
    Replies: 5
    Last Post: 01-04-2008, 04:43 AM
  2. bcr steps 0.2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-01-2007, 09:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •