Semester Project NEED HELP

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 2113 Views - Last Post: 14 December 2009 - 10:05 AM Rate Topic: -----

#1 sajohnson05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 09-September 09

Semester Project NEED HELP

Post icon  Posted 10 December 2009 - 09:34 PM

Below are the three lines of code i am having trouble with getting going. If anyone could please help me it would be great help. I have put in what the teacher wants and what i have so far. Please Help!
an int field at the class level named ‘index’ that starts = 0 and then is incremented by one each time a new order is constructed
for (int index=0;index<2000; index++ );		
}

a private int field for each order instance named ‘id’ which is set equal to the value of ‘index’ at the time of its instantiation
int id;

a constructor that takes as its first argument the number of minutes since the simulation began and as its second the required minutes of processing from when it starts on a machine until it finishes. It must also update index and id values.
public Order(int min, int processing) {}

*Edited: completly unjustified "Intermediate" tag removed

This post has been edited by pbl: 10 December 2009 - 09:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Semester Project NEED HELP

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Semester Project NEED HELP

Posted 10 December 2009 - 09:40 PM

Since index is supposed to be a class variable, this means use the keyword static. Remember, only instance and local variables can be declared within a for loop like you are doing. What the problem is asking for is this:
public class Order{
	 static int index = 0; //index is accessible to all Order objects
	 private int id;

	 public Order(int min, int processing){ //updates or assigns values to id and index
		  id = index; 
		  index++; //so each time you increment it, each Order object will see the change
		}
}


Was This Post Helpful? 0
  • +
  • -

#3 sajohnson05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 09-September 09

Re: Semester Project NEED HELP

Posted 11 December 2009 - 06:07 AM

is my getters correct below? And for getters do I need setters also?

	public int getId() {
		return id;
	}
	
	public int getProcessMinutes(int processing) {
		return processing;
	}

	public int getArrivalTime() {
		return index;
	}


Was This Post Helpful? 0
  • +
  • -

#4 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Semester Project NEED HELP

Posted 11 December 2009 - 06:20 AM

usually, when you say get method, you mean a method used to get one of this object's field, which is private. Therefore the method is declared public.
the get methods seems fine except this one:
public int getProcessMinutes(int processing) {
		return processing;
	}


this method recieves a parameter, int processing, and then retrun it.. not what you mean i guess..
should be as you declared the other two get methods:
public int getProcessMinutes() {
		return processing;
	}


assuming one of the global variables of the class is int processing.

anyway, i suggest to use the keyword "this when referring to this object's variables. it is much more readable.
as:
  public int getId() {
		return this.id;
	}
	
	public int getProcessMinutes(int processing) {
		return this.processing;
	}

	public int getArrivalTime() {
		return index;
	}



of course, assuming int id, and int processing are global variables in the class.
index does not get this because it is a static variable.

as for the set methods, what if you wanted to update the value of processing later in your code? it is declared private so you cannot refer to it.
set methods do exactly that. they refer to this object's global variables and update them.
as:
public void setProcessMinutes(int newProcessing){
this.processing = newProcessing;
}


Was This Post Helpful? 0
  • +
  • -

#5 sajohnson05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 09-September 09

Re: Semester Project NEED HELP

Posted 11 December 2009 - 08:58 AM

First off want to say thanks to both guys for the help. Have one more quick error.
			String string = "";
			for (Order o: waiting){
			string = string + o.getId() + "arrived t=" o.getArrivalTime()+ ", ";
}
			return string;

My error is on the o.getArrivalTime() this method .getArrivalTime() is undefined for type of String.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Semester Project NEED HELP

Posted 11 December 2009 - 09:04 AM

You need to include a plus sign between each endquote and the next component you are concatenating with the current String. Like so:
"arrived t=" + o.getArrivalTime vs. "arrived t=" o.getArrivalTime
Was This Post Helpful? 0
  • +
  • -

#7 sajohnson05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 09-September 09

Re: Semester Project NEED HELP

Posted 11 December 2009 - 10:19 AM

Any help to make constructor set the arrival and processing time
	static int index = 0; // index is accessible to all Order objects
	private int id;
	private int processing;
	private int min;
	

	public Order(int min, int processing) { // updates or assigns values to id
											// and index
		setId(index);
		index++; // so each time you increment it, each Order object will see
					// the change
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getId() {
		return this.id;
	}
	public void setProcessMinutes(int newProcessing){
		this.processing = newProcessing;
	}
	public int getProcessMinutes() {
		return this.processing;
	}
	public int getArrivalTime() {
		return this.index;
	}
	public String toString() {
		return "Order [Order " + ", finish: =" + "]";
	}


}

Was This Post Helpful? 0
  • +
  • -

#8 sajohnson05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 09-September 09

Re: Semester Project NEED HELP

Posted 11 December 2009 - 10:25 AM

Here is what the constructor needs to do.

a constructor that takes as its first argument the number of minutes
since the simulation began and as its second the required minutes of processing
from when it starts on a machine until it finishes. It must also update index and id values.
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Semester Project NEED HELP

Posted 11 December 2009 - 10:29 AM

...
public Order(int min, int processing) { // updates or assigns values to id
											// and index
		setId(index);
		index++; // so each time you increment it, each Order object will see
					// the change

		// Sets the variables once...
		this.min = min; // Sets the instance variable to the local. Note the this keyword
		this.processing = processing;
	}



The this keyword makes the program reference the instance variable not the argument variable. So the line this.min = min says that the instance variable called min needs to equal the parameter.
Was This Post Helpful? 0
  • +
  • -

#10 sajohnson05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 09-September 09

Re: Semester Project NEED HELP

Posted 11 December 2009 - 07:21 PM

I know the following is alot of code but i can not figure out where to put my main method in this code for my GUI to work. I thought i had everything right then my GUI would not appear and it seems that it is due to not have a main method. Any help would be great.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class SimulationModelTestingGui {
	
	private static JFrame appFrame;
	//
	private JPanel mainPanel;
	
	private JPanel commandPanel;
	
	private JPanel setupPanel;
	private JPanel setupHorizonPanel;
	private JPanel setupOrdersPanel;
	private JPanel setupProcessPanel;
	
	private JPanel currentPanel;
	private JPanel currentTimePanel;
	private JPanel currentQueuePanel;
	private JPanel currentMachinePanel;
	private JPanel currentFELPanel;
	
	private JPanel statsPanel;
	private JPanel statsNoWaitPanel;
	private JPanel statsAvgWaitPanel;
	private JPanel statsMaxWaitCntPanel;
	
	private JPanel messagePanel;
	
	//
	
	private JButton setupButton;
	private JButton nextButton;
	private JButton runButton;
	private JButton reportButton;
	
	//
	private JLabel setupHorizonLabel;
	private JTextField setupHorizonText;
	private JLabel setupOrdersLabel;
	private JTextField setupOrdersText;
	private JLabel setupProcessLabel;
	private JTextField setupProcessText;
	//
	private JLabel statsNoWaitLabel;
	private JTextField statsNoWaitText;
	private JLabel statsAvgWaitLabel;
	private JTextField statsAvgWaitText;
	private JLabel statsMaxWaitCntLabel;
	private JTextField statsMaxWaitCntText;
	
	private JLabel messageLabel;
	
	
	public void SimulationModelGui() {
		
	appFrame = new JFrame ("Simulation Model");	
	appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	initComponents();
	 
	appFrame.setSize(600,300);
	appFrame.setLocation(200,200);
	appFrame.setVisible(true);
			
	}
	

	
	private void initComponents(){
		
		mainPanel = new JPanel ();
		mainPanel.setLayout(new BorderLayout());
		setupPanel = new JPanel();
		setupHorizonPanel = new JPanel();
		setupOrdersPanel = new JPanel();
		setupProcessPanel = new JPanel();
		
		currentPanel = new JPanel();
		currentQueuePanel = new JPanel();
		currentTimePanel = new JPanel();
		currentMachinePanel = new JPanel();
		currentFELPanel = new JPanel();
		
	
		messagePanel = new JPanel();
		//
		nextButton = new JButton("Set Up Run");
		setupButton = new JButton ("Process Next Event");
		runButton = new JButton ("Run To Horizon");
		reportButton = new JButton ("Report Performance");
		//
		setupHorizonLabel = new JLabel("Minutes");
		setupHorizonText = new JTextField("0");
		setupOrdersLabel = new JLabel ("Mean");
		setupOrdersText = new JTextField("100");
		setupProcessLabel = new JLabel("Mean");
		setupProcessText = new JTextField ("80");
		//
		//
		statsNoWaitLabel = new JLabel ("% Orders Completed");
		statsNoWaitText = new JTextField ("0");
		statsAvgWaitLabel = new JLabel ("Minutes Ave.Excluding 0 Waits");
		statsAvgWaitText = new JTextField ("0");
		statsMaxWaitCntLabel = new JLabel ("Orders Maximum Ever Waiting");
		statsMaxWaitCntText = new JTextField ("0");
		//
		messageLabel = new JLabel ("Insert message here");
		//
		//
		//
		commandPanel.setLayout (new FlowLayout (FlowLayout.LEFT));
		commandPanel.setBorder(BorderFactory.createTitledBorder("Commands"));
		//
		setupPanel.setLayout(new BoxLayout (setupPanel,BoxLayout.Y_AXIS));
		setupPanel.setBorder (BorderFactory.createTitledBorder("Set Up Values"));
		setupHorizonPanel.setLayout(new GridLayout(1,2));
		setupHorizonPanel.setBorder (BorderFactory.createTitledBorder("Horizon"));
		setupOrdersPanel.setLayout (new GridLayout(1,2));
		setupOrdersPanel.setBorder (BorderFactory.createTitledBorder("Time Between Order"));
		setupProcessPanel.setLayout(new GridLayout(1,2));
		setupProcessPanel.setBorder (BorderFactory.createTitledBorder("Time To Process"));
		//
		currentPanel.setLayout(new GridLayout(3,1));
		currentPanel.setBorder (BorderFactory.createTitledBorder("CurrentState"));
		//
		statsPanel.setLayout(new GridLayout(0,1));
		statsPanel.setBorder (BorderFactory.createTitledBorder("Performan"));
		statsNoWaitPanel.setLayout (new GridLayout(1,2));
		statsNoWaitPanel.setBorder(BorderFactory.createTitledBorder("Served Without Wait"));
		statsAvgWaitPanel.setLayout (new GridLayout(1,2));
		statsAvgWaitPanel.setBorder (BorderFactory.createTitledBorder("Waiting Time"));
		statsMaxWaitCntPanel.setLayout(new GridLayout (1,2));
		statsMaxWaitCntPanel.setBorder (BorderFactory.createTitledBorder("Waiting Line"));
		//
			ButtonActionListener baEar = new ButtonActionListener();
			nextButton.addActionListener(baEar);
			setupButton.addActionListener(baEar);		
			commandPanel.add(setupButton);
			commandPanel.add(nextButton);
			commandPanel.add(runButton);
			commandPanel.add(reportButton);
			
		mainPanel.add(setupPanel, BorderLayout.WEST);
			setupPanel.add(setupHorizonPanel);
				setupHorizonPanel.add(setupHorizonText);
				setupHorizonPanel.add(setupHorizonLabel);
			setupPanel.add(setupProcessPanel);
				setupProcessPanel.add(setupOrdersText);
				setupProcessPanel.add(setupOrdersLabel);
			setupPanel.add(setupOrdersPanel);
				setupOrdersPanel.add(setupProcessText);
				setupOrdersPanel.add(setupProcessLabel);
				
		mainPanel.add(currentPanel, BorderLayout.CENTER);
		mainPanel.add(statsPanel, BorderLayout.EAST);
			statsPanel.add(statsNoWaitPanel);
				statsNoWaitPanel.add(statsNoWaitText);
				statsNoWaitPanel.add(statsNoWaitLabel);
			statsPanel.add(statsAvgWaitPanel);
				statsAvgWaitPanel.add(statsAvgWaitText);
				statsAvgWaitPanel.add(statsAvgWaitLabel);
			statsPanel.add(statsMaxWaitCntPanel);
				statsMaxWaitCntPanel.add(statsMaxWaitCntText);
				statsMaxWaitCntPanel.add(statsMaxWaitCntLabel);
		mainPanel.add(messagePanel, BorderLayout.SOUTH);
			messagePanel.add(messageLabel);
			
	}

	class ButtonActionListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			if (event.getSource() == setupButton){}
			else if (event.getSource() == nextButton){}
			else if (event.getSource() == runButton){}
			else if (event.getSource() == reportButton){}
				
			
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Semester Project NEED HELP

Posted 11 December 2009 - 07:26 PM

Why don't you just create a main() method within your GUI class, and just create a new instance of the GUI class within your main method?
Was This Post Helpful? 0
  • +
  • -

#12 sajohnson05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 09-September 09

Re: Semester Project NEED HELP

Posted 11 December 2009 - 07:40 PM

Is this what you are talking about. If i do that i have to close my Gui class off and then my initCompents() has this error Cannot make a static reference to the non-static method initComponents() from the type SimulationModelTestingGui
	public void SimulationModelGui() {
		public static void main(String[] args) {
	appFrame = new JFrame ("Simulation Model");	
	appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	initComponents();
	 
	appFrame.setSize(600,300);
	appFrame.setLocation(200,200);
	appFrame.setVisible(true);
			
	}

Was This Post Helpful? 0
  • +
  • -

#13 sajohnson05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 09-September 09

Re: Semester Project NEED HELP

Posted 12 December 2009 - 11:36 AM

a toString method that returns the order’s ID and information about its state in the format: “Order o_23: arrived t=125, finished t=222”

I am not sure what i need to set the int id to to make it return my toString.
	public String toString() {
		if(id == 1)
		return " [o_ " + ",arrived: = " + ", finish: =" + "]";
		else return "finished";
	}
}


Any Help appreciated

a toString method that returns the order’s ID and information about its state in the format: “Order o_23: arrived t=125, finished t=222”

I am not sure what i need to set the int id to to make it return my toString.
	public String toString() {
		if(id == 1)
		return " [o_ " + ",arrived: = " + ", finish: =" + "]";
		else return "finished";
	}
}


Any Help appreciated
Was This Post Helpful? 0
  • +
  • -

#14 sajohnson05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 09-September 09

Re: Semester Project NEED HELP

Posted 12 December 2009 - 11:44 AM

Also if anyone can help me on my GUI it would be much appreciated as well. Trying to finish this up today and these are my last two issues.

Thanks

View Postsajohnson05, on 11 Dec, 2009 - 06:40 PM, said:

Is this what you are talking about. If i do that i have to close my Gui class off and then my initCompents() has this error Cannot make a static reference to the non-static method initComponents() from the type SimulationModelTestingGui
	public void SimulationModelGui() {
		public static void main(String[] args) {
	appFrame = new JFrame ("Simulation Model");	
	appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	initComponents();
	 
	appFrame.setSize(600,300);
	appFrame.setLocation(200,200);
	appFrame.setVisible(true);
			
	}

Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Semester Project NEED HELP

Posted 12 December 2009 - 07:24 PM

Here is how you concatenate Strings to include int values. I would use this concept to include your id, min and processing variables in your toString() method.
String x = 1 + "";
//OR

int y = 1;
String z = y + "";



Now in terms of your conditional testing the id variable within your toString() method, I would remove it. You aren't using threading or loops to bring your orders closer to finish, so you don't need to test if it is finished in your toString() method.

Now for this section, there is a lot wrong with it. First, you cannot ever declare a method within a method. Next, your JFrame is declared as static, and therefore, can only be referenced by a static variable or method (which SimulationModelGui() isn't). So go ahead and remove the static modifier from your JFrame as you don't want each object of this class to share the same JFrame.

Next, the thing to do in your case would be to define a constructor method for your SimulationModelTestingGui class, and place the sequence of the code within the constructor. It looks like you are trying to do this in SimulationModelGui(). If you feel that method sets up your GUI to run fine, then just invoke it in the constructor you define. Then, define main() in your class as you would any other method (meaning not nested within another method) and create a SimulationModelTestingGui object in main().
 public void SimulationModelGui() {
		public static void main(String[] args) {
	appFrame = new JFrame ("Simulation Model");	
	appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	initComponents();
	 
	appFrame.setSize(600,300);
	appFrame.setLocation(200,200);
	appFrame.setVisible(true);
			
	}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2