lfnetwork.com mark read register faq members calendar

Thread: Java Code
Thread Tools Display Modes
Post a new thread. Add a reply to this thread. Indicate all threads in this forum as read. Subscribe to this forum. RSS feed: this forum RSS feed: all forums
Old 02-22-2011, 11:05 AM   #1
RpTheHotrod
LFN Staff Emeritus
 
RpTheHotrod's Avatar
 
Join Date: Apr 2002
Posts: 2,798
LFN Staff Member 10 year veteran! Forum Veteran 
Java Code

Unfortunately I do not have access to a compiler right now, and I'm not sure yet how arrays work with java as I have not had the time to research it and we have not covered java arrays yet.

I'm trying to come up with a way that will allow multiple objects to be created via a loop with user input. Would something like this work, or will arrays not work with objects like this? I know the code is inefficient, but we're being asked to specifically do it this way.




import javax.swing.JOptionPane;

//Class
public class Employee_Tester
{

//Main Statement
public static void main(String[] args)
{

objArray = new int[11];
int x = 1;


//Creating Obj1 (no parameters)
Employee obj[x] = new Employee();
obj[x].setName("Susan Meyers");
obj[x].setDepartment("Accounting");
obj[x].setPosition("Vice President");
obj[x].setIdNumber(47899);
x++;

//Creating Obj2 (with paramaters)
Employee obj[x] = new Employee("Mark Jones", "IT", "Programmer",39119);
x++;

//Creating obj3 (by use of copy contructor)
Employee obj[x] = new Employee(new Employee("Joy Rogers", "Manufacturing", "Engineer",81774));
x++;

//Windows output window
JOptionPane.showMessageDialog(null, "Congratulations. You are successful to use object!");
char exit;
do
{
//Creating object for upcoming user input
Employee obj[x] = new Employee();

obj[x].setName(JOptionPane.showInputDialog("Please enter name."));
obj[x].setDepartment(JOptionPane.showInputDialog("Please enter the department."));
obj[x].setPosition(JOptionPane.showInputDialog("Please enter the position."));
String idNumStr = JOptionPane.showInputDialog("Please enter the id number.");

int idNumResult = Integer.parseInt(idNumStr);
obj[x].setIdNumber(idNumResult);
x++;

String _exit = JOptionPane.showInputDialog("Would you like to exit? Y/N");

exit = _exit.charAt(0);
}

while(!(exit == 'Y' || exit == 'y'));


System.exit(0);
}

}


Current in-game name is #include
RpTheHotrod is offline   you may: quote & reply,
Old 02-22-2011, 12:11 PM   #2
Hallucination
Baron von Sexy
 
Hallucination's Avatar
 
Join Date: Apr 2005
Posts: 3,875
Current Game: HKO closed beta
You'll want to start x at 0, since that is where Java arrays start. If you did the do while loop properly you shouldn't need the System.exit(0) line, it should just end on its own.

Edit: You'll also want the array to be an array of Employees, not an array of integers. I'm a bit rusty with Java, but it should look something like:
Employee objArray = new Employee[11];

That would also mean that you don't have to declare the elements of objArray, you just have to assign them. I.E.
Employee obj[x] = new Employee();
would become:
objArray[x] = new Employee();

And lastly, you need to change all instances of obj[x] to objArray[x], otherwise you're referencing an array that doesn't exist.

I made all those changes, and the only error that is coming up is that I don't have an Employee class.


Let's kill ourselves.

Last edited by Hallucination; 02-22-2011 at 12:24 PM.
Hallucination is offline   you may: quote & reply,
Old 02-22-2011, 12:28 PM   #3
RpTheHotrod
LFN Staff Emeritus
 
RpTheHotrod's Avatar
 
Join Date: Apr 2002
Posts: 2,798
LFN Staff Member 10 year veteran! Forum Veteran 
Quote:
Originally Posted by Hallucination View Post
You'll want to start x at 0, since that is where Java arrays start. If you did the do while loop properly you shouldn't need the System.exit(0) line, it should just end on its own.

Edit: You'll also want the array to be an array of Employees, not an array of integers. I'm a bit rusty with Java, but it should look something like:
Employee objArray = new Employee[11];

That would also mean that you don't have to declare the elements of objArray, you just have to assign them. I.E.
Employee obj[x] = new Employee();
would become:
objArray[x] = new Employee();

And lastly, you need to change all instances of obj[x] to objArray[x], otherwise you're referencing an array that doesn't exist.

I made all those changes, and the only error that is coming up is that I don't have an Employee class.
I agree but the instructor wants us to start with obj1. As for system exit we were instructed that when using the pane a system exit is mandatory. Odd.

Anyway I received thus response from someone.



You can declare an array of objects using:
Employee[] obj = new Employee[10];
substituting 10 for however many Employee objects you want to create

After declaring the array, you can assign each one a value by using
obj[x] = new Employee();

You can't have a variable in the variable name at declaration. obj[x] doesn't turn into obj1 where you say "creating obj1," unfortunately.

That objArray needs an int[] in front of it too.
int[] objArray = new int[11];

Other than that it seems fine.


Current in-game name is #include
RpTheHotrod is offline   you may: quote & reply,
Old 02-22-2011, 12:31 PM   #4
RpTheHotrod
LFN Staff Emeritus
 
RpTheHotrod's Avatar
 
Join Date: Apr 2002
Posts: 2,798
LFN Staff Member 10 year veteran! Forum Veteran 
Quote:
Originally Posted by Hallucination View Post
You'll want to start x at 0, since that is where Java arrays start. If you did the do while loop properly you shouldn't need the System.exit(0) line, it should just end on its own.

Edit: You'll also want the array to be an array of Employees, not an array of integers. I'm a bit rusty with Java, but it should look something like:
Employee objArray = new Employee[11];

That would also mean that you don't have to declare the elements of objArray, you just have to assign them. I.E.
Employee obj[x] = new Employee();
would become:
objArray[x] = new Employee();

And lastly, you need to change all instances of obj[x] to objArray[x], otherwise you're referencing an array that doesn't exist.

I made all those changes, and the only error that is coming up is that I don't have an Employee class.
Just saw your edit

So how's this:


Here's the Employee Class
Code:
//Class Header
public class Employee 
		{
			//data members
			private String name, department, position;
			private int idNumber;
			
			//default constructor
			public Employee()
			{
				name = "";
				department = "";
				position = "";
				idNumber = 0;
			}
			
			//parameter constructor
			public Employee(String _name, String _department, String _position, int _idNumber)
			{
				name = _name;
				department = _department;
				position = _position;
				idNumber = _idNumber;
			}
			
			//copy constructor
			public Employee(Employee constructor)
			{
				name = constructor.name;
				department = constructor.department;
				position = constructor.position;
				idNumber = constructor.idNumber;
					
			}
						
			//Mutator Methods
			public void setName(String _name)
			{
				name = _name;
			}
			
			public void setDepartment(String _department)
			{
				department = _department;
			}
			
			public void setPosition(String _position)
			{
				position = _position;
			}
			
			public void setIdNumber(int _idNumber)
			{
				idNumber = _idNumber;
			}


			//Accessor methods
			public String getName()
			{
				return name;
			}
			
			public String getDepartment()
			{
				return department;
			}
			
			public String getPosition()
			{
				return position;
			}
			
			public int getIdNumber()
			{
				return idNumber;
			}

			public String toString()
			{
				String str = "Name: " + name + "\nDepartment: " + department +
							"\nPosition: " + position + "\nidNumber: " + idNumber;
				return str;
			}
		}
Actual Program
Code:
import javax.swing.JOptionPane;

//Class
public class Employee_Tester 
{

	//Main Statement
	public static void main(String[] args) 
	{
		
		int x = 1;
		
		Employee[] obj = new Employee[11];

		obj[x] = new Employee();
		obj[x].setName("Susan Meyers");
		obj[x].setDepartment("Accounting");
		obj[x].setPosition("Vice President");
		obj[x].setIdNumber(47899);
		x++;
		
		//Creating Obj2 (with paramaters)
		obj[x] = new Employee("Mark Jones", "IT", "Programmer",39119);
		x++;

		//Creating obj3 (by use of copy contructor)
		obj[x] = new Employee(new Employee("Joy Rogers", "Manufacturing", "Engineer",81774));
		x++;

		//Windows output window
		JOptionPane.showMessageDialog(null, "Congratulations. You are successful to use object!");
		char exit;
		
		do
		{
		//Creating object for upcoming user input
		obj[x] = new Employee();
		
		obj[x].setName(JOptionPane.showInputDialog("Please enter name."));
		obj[x].setDepartment(JOptionPane.showInputDialog("Please enter the department."));
		obj[x].setPosition(JOptionPane.showInputDialog("Please enter the position."));
		String idNumStr = JOptionPane.showInputDialog("Please enter the id number.");
		
		int idNumResult = Integer.parseInt(idNumStr);
		obj[x].setIdNumber(idNumResult);
		x++;
		
		String _exit = JOptionPane.showInputDialog("Would you like to exit? Y/N");
		
		exit = _exit.charAt(0);
		}
		
		while(!(exit == 'Y' || exit == 'y'));
		
		
		System.exit(0);
	}

}


Current in-game name is #include

Last edited by RpTheHotrod; 02-22-2011 at 03:06 PM.
RpTheHotrod is offline   you may: quote & reply,
Old 02-22-2011, 11:30 PM   #5
Hallucination
Baron von Sexy
 
Hallucination's Avatar
 
Join Date: Apr 2005
Posts: 3,875
Current Game: HKO closed beta
Alright, just copypasta'd the employee class in, and my code is running fine. I did the same with yours, it's working.

And ignore what I said about the System.exit bit, there's a reason I hate UI programming.


Let's kill ourselves.
Hallucination is offline   you may: quote & reply,
Old 02-23-2011, 01:50 PM   #6
RpTheHotrod
LFN Staff Emeritus
 
RpTheHotrod's Avatar
 
Join Date: Apr 2002
Posts: 2,798
LFN Staff Member 10 year veteran! Forum Veteran 
I decided to try an input tester. The program seems to run fine and as intended, but for some reason I get an error that says "exception in thread main". I don't see any issues, though. The program runs as intended, there's no compiler errors. Everything seems normal. Any ideas?



Code:
import javax.swing.JOptionPane;

//Class
public class Employee_Tester 
{

	//Main Statement
	public static void main(String[] args) 
	{
		
		int x = 1;
		
		Employee[] obj = new Employee[11];

		obj[x] = new Employee();
		obj[x].setName("Susan Meyers");
		obj[x].setDepartment("Accounting");
		obj[x].setPosition("Vice President");
		obj[x].setIdNumber(47899);
		x++;
		
		//Creating Obj2 (with paramaters)
		obj[x] = new Employee("Mark Jones", "IT", "Programmer",39119);
		x++;

		//Creating obj3 (by use of copy contructor)
		obj[x] = new Employee(new Employee("Joy Rogers", "Manufacturing", "Engineer",81774));
		x++;

		//Windows output window
		JOptionPane.showMessageDialog(null, "Congratulations. You are successful to use object!");
		char exit;
		
		do
		{
		//Creating object for upcoming user input
		obj[x] = new Employee();
		
		obj[x].setName(JOptionPane.showInputDialog("Please enter name."));
		obj[x].setDepartment(JOptionPane.showInputDialog("Please enter the department."));
		obj[x].setPosition(JOptionPane.showInputDialog("Please enter the position."));
		String idNumStr = JOptionPane.showInputDialog("Please enter the id number.");
		
		//Convert input String to Int
		int idNumResult = Integer.parseInt(idNumStr);
		obj[x].setIdNumber(idNumResult);
		x++;
		
		//Option to continue the loop or exit
		String _exit = JOptionPane.showInputDialog("Would you like to continue? Y/N");
		exit = _exit.charAt(0);
		}
		
		while(exit == 'Y' || exit == 'y');
		
		//Testing input
		for(int counter = 1; counter <= x; counter++)
			
			JOptionPane.showMessageDialog(null, obj[counter].toString());
					
		System.exit(0);
	}

}


Current in-game name is #include
RpTheHotrod is offline   you may: quote & reply,
Old 02-23-2011, 08:24 PM   #7
RpTheHotrod
LFN Staff Emeritus
 
RpTheHotrod's Avatar
 
Join Date: Apr 2002
Posts: 2,798
LFN Staff Member 10 year veteran! Forum Veteran 
I fixed it. Silly me with a typo.


//Testing input
for(int counter = 1; counter < x; counter++)

JOptionPane.showMessageDialog(null, obj[counter].toString());

System.exit(0);


Current in-game name is #include
RpTheHotrod is offline   you may: quote & reply,
Post a new thread. Add a reply to this thread. Indicate all threads in this forum as read. Subscribe to this forum. RSS feed: this forum RSS feed: all forums
Go Back   LucasForums > Network > JediKnight Series > Community > Yoda’s Swamp > Java Code

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:33 AM.

LFNetwork, LLC ©2002-2011 - All rights reserved.
Powered by vBulletin®
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.