|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

05-05-2008, 04:47 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 12
|
|
|
[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:
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)
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, 04:52 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
Hi ubermeister,
Welcome to our community. 
Seems to me your application is ok, so what is your question there?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

05-05-2008, 04:54 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 12
|
|
Originally Posted by Eranga
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.
|
|

05-05-2008, 04:57 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
|
You have to get data at the TestEmployeeClass I think. Then after using instance and appropriate constructors do the processing. Got it?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

05-05-2008, 05:02 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 12
|
|
|
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, 05:09 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
You asking that how to implement that method? There have few errors in your code. Try this.
// 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();
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

05-05-2008, 05:12 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 12
|
|
Originally Posted by Eranga
You asking that how to implement that method? There have few errors in your code. Try this.
// 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.
|
|

05-05-2008, 05:16 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
Ah, you asking how to call that function on an employee object.
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();
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

05-05-2008, 05:22 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 12
|
|
Originally Posted by Eranga
Ah, you asking how to call that function on an employee object.
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
|
|

05-05-2008, 05:27 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
Last edited by Eranga : 05-05-2008 at 05:29 AM.
|
|

05-05-2008, 05:52 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 12
|
|
Originally Posted by Eranga
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.
thank you very much my last question is how you do step 8?
|
|

05-05-2008, 05:57 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
|
Did you create an array of Employee objects? If so, loop them on each and do the same process.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

05-05-2008, 06:25 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 12
|
|
Originally Posted by Eranga
Did you create an array of Employee objects? If so, loop them on each and do the same process.
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.
|
|

05-05-2008, 06:31 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
In simple way you can do this pal.
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

05-05-2008, 06:45 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 12
|
|
|
i'm taking those out
|
|

05-05-2008, 06:47 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
|
Is that what you looking for?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

05-05-2008, 06:51 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 12
|
|
|
ya that last bit of code was it now i'm just debugging thanks for all your help!!!
|
|

05-05-2008, 06:52 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
|
Oki, do it and let me know what happened. And there are so many way to do this.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

05-05-2008, 07:05 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 12
|
|
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
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
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, 07:14 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
|
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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
| |