Results 1 to 20 of 23
- 05-05-2008, 03:47 AM #1
Member
- Join Date
- May 2008
- Posts
- 12
- Rep Power
- 0
[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); } }
- 05-05-2008, 03:52 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Hi ubermeister,
Welcome to our community. :)
Seems to me your application is ok, so what is your question there?
- 05-05-2008, 03:54 AM #3
Member
- Join Date
- May 2008
- Posts
- 12
- Rep Power
- 0
- 05-05-2008, 03:57 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You have to get data at the TestEmployeeClass I think. Then after using instance and appropriate constructors do the processing. Got it?
- 05-05-2008, 04:02 AM #5
Member
- Join Date
- May 2008
- Posts
- 12
- Rep Power
- 0
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()
{
- 05-05-2008, 04:09 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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(); }
- 05-05-2008, 04:12 AM #7
Member
- Join Date
- May 2008
- Posts
- 12
- Rep Power
- 0
- 05-05-2008, 04:16 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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(); }
- 05-05-2008, 04:22 AM #9
Member
- Join Date
- May 2008
- Posts
- 12
- Rep Power
- 0
- 05-05-2008, 04:27 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
As like this.
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.Java Code:s1.display();
Last edited by Eranga; 05-05-2008 at 04:29 AM.
- 05-05-2008, 04:52 AM #11
Member
- Join Date
- May 2008
- Posts
- 12
- Rep Power
- 0
- 05-05-2008, 04:57 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you create an array of Employee objects? If so, loop them on each and do the same process.
- 05-05-2008, 05:25 AM #13
Member
- Join Date
- May 2008
- Posts
- 12
- Rep Power
- 0
thats my array i just don't know what to WRITE for the loops.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();
- 05-05-2008, 05:31 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In simple way you can do this pal.
What is displayTotal() do. I can't see anything about it.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(); }
- 05-05-2008, 05:45 AM #15
Member
- Join Date
- May 2008
- Posts
- 12
- Rep Power
- 0
i'm taking those out
- 05-05-2008, 05:47 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-05-2008, 05:51 AM #17
Member
- Join Date
- May 2008
- Posts
- 12
- Rep Power
- 0
ya that last bit of code was it now i'm just debugging thanks for all your help!!!
- 05-05-2008, 05:52 AM #18
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Oki, do it and let me know what happened. And there are so many way to do this.
- 05-05-2008, 06:05 AM #19
Member
- Join Date
- May 2008
- Posts
- 12
- Rep Power
- 0
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(); } } } }
- 05-05-2008, 06:14 AM #20
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, there is a big error. I can't find any array named employee there. It should be students
Did you use any IDE for coding?
Similar Threads
-
differents steps??
By Yel in forum New To JavaReplies: 5Last Post: 01-04-2008, 04:43 AM -
bcr steps 0.2
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-01-2007, 09:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks