Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Inputting commands from file?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DJP




PostPosted: Sat Mar 26, 2005 9:17 pm   Post subject: Inputting commands from file?

Alright, my teacher gave us another assignment that seems to be out of this David Flannagan book, but there is a part that I can't figure out how to do:

Quote:
Part 2

In this part, you will modify the program to read the events from a file.

Modify Restart() to start the system by reading in the system state from a file specified on the command line:

java GreenHouseControls -f examples.txt

The -f argument must be present. It serves no pupose in this assignment but is needed in a future assignemtn. The even information must be in the format specified like this:


addEvent (new Bell (tm + 9000));
addEvent (new ThermostatNight (tm));
addEvent (new LightOn (tm + 1000));
addEvent (new WaterOn (tm + 3000));
rings = 5;
addEvent (new FansOn (tm + 4000));
addEvent (new LightOff (tm + 2000));
addEvent (new FansOff (tm + 6000));
addEvent (new WaterOff (tm + 8000));
addEvent (new ThermostatDay (tm + 10000));

To simplify testing, you can assume a maximum of 20 events and at least 500 msec between events. If a line of data is incorrectly formatted or for an unknown event please print the line with an appropriate error message, then continue with the next line.


I know how to input string and numbers from a file (BufferedReader), but it looks to me like this assignment is asking me to input commands from a file. Is this possible or is there another way to meet this requirement?

These are the three java files we had to make work, and then modify to input from file:



Controller.java
 Description:

Download
 Filename:  Controller.java
 Filesize:  1.33 KB
 Downloaded:  176 Time(s)


Event.java
 Description:

Download
 Filename:  Event.java
 Filesize:  491 Bytes
 Downloaded:  174 Time(s)


GreenhouseControls.java
 Description:
The file that you run

Download
 Filename:  GreenhouseControls.java
 Filesize:  4.62 KB
 Downloaded:  183 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
EmptyVoid




PostPosted: Mon Mar 28, 2005 2:34 pm   Post subject: (No subject)

I believe I am doing the same assignment as you and I also can't figure out how to make the program read events from a file.

This is what I have been told to do:

Quote:
Your starting point of this assignment are the GreenhouseControls.java, Events.java. and Controller.java in the programs/c08 and programs/c08/controller directories in the ICS4M handout drive. You are going to modify these programs to show you the various ways you can build on the basic design model using the capabilities of Java. The assignment is divided into three parts to help you organize your programming efforts. The intermediate stages serve as checkpoints for you, you submit only the final program.

Part 2

In this part, you will modify the program to read the events from a file.

Modify Restart() to start the system by reading in the system state from a file specified on the command line:

java GreenHouseControls -f examples.txt

The -f argument must be present. It serves no pupose in this assignment but is needed in a future assignemtn. The even information must be in the format specified like this:


addEvent (new Bell (tm + 9000));
addEvent (new ThermostatNight (tm));
addEvent (new LightOn (tm + 1000));
addEvent (new WaterOn (tm + 3000));
rings = 5;
addEvent (new FansOn (tm + 4000));
addEvent (new LightOff (tm + 2000));
addEvent (new FansOff (tm + 6000));
addEvent (new WaterOff (tm + 8000));
addEvent (new ThermostatDay (tm + 10000));

To simplify testing, you can assume a maximum of 20 events and at least 500 msec between events. If a line of data is incorrectly formatted or for an unknown event please print the line with an appropriate error message, then continue with the next line.

After reading, Events must be stored in the EventSet according to the time they are to occur. Assume that no time number is more than 8 digits long. Restart() must be able to deal appropriately with any order of the Events. To accomplish this have Restart() save the relevant Event information in an LinkedList of strings and sort the Events by time before adding each events to EventSet. Use the <list>.add() to set up your linked list. This is shown in c09:List1.java and Collections.sort(<list>) shown in c09:ListSortSearch.Modify the Bell() to output a single "Bing!". When you read in a Bell event generate the appropriate number of Bell events as indicated by rings. These must be set to correct times. This is an alternative to generating the new Bell events within Bell(). It will allow them be sorted into their correct time sequence. At this point the output of the program should be identical the original program.

After the initial start, when restarting Restart() must provide to the user the option to recreate the EventSet from the linked list or read in a new file(supplied by the user). This must be achieved by prompting the user at the console. Please also allow the user the option to quit the program at this stage.

Part 3

In this part yo use a different data structure to simplify the Controller.java code.

In Controller.java, change the code to use a stack instead of an EventSet. Please see c09:StackL.java for an example of how to implement the stack functionality using a linked list. You will need to reverse the sort order to use the stack. Please see c09:Reverse.java for an example that uses a sort comparatore to reverse the natural sort order.


I have added the files listed for this program. I have no idea what I am suppose to do and any help would be greatly appreciated.



tme2.zip
 Description:
All the files listed for this program

Download
 Filename:  tme2.zip
 Filesize:  13.89 KB
 Downloaded:  151 Time(s)

DJP




PostPosted: Sat Apr 02, 2005 9:02 pm   Post subject: (No subject)

Ok, I found one way to do this problem (a friend who did this program the previous semester showed it to me): all you have to do it input all the commands into an array, then do a bunch of if statements to correspond with each command. There doesn't appear to be a way in Java to actually import commands directly.

One problem I ran into with this is that I can't create packages and import them (in this program's case, package is tme2, I can't import tme2.Controller as the assignment asks) when reading events from file. I get an error that says the txt file with the commands could not be found. Eliminating the package and import stuff allows it to read from file, and the program works normally.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: