Program 6 - Command Line

Goal Description Hints Passoff

Due Date - 9 Dec

Goal

 
To learn to parse commands read from user input. To learn more string processing methods. To work with arrays more.
CS 142
Description
 

You should create a program that opens a new Win3D. It then starts reading lines from System.in and performs commands on those lines. Each command starts with the name of the command followed by argument values that tell how each command should be performed.

Step 1 - create a place to store Obj3D objects by name.

You should create a class for holding Obj3D objects and finding them by name. First create a class NamedObj that contains the fields name and object. These are both public.

Then create a class ObjTable. This clas should contain a private field that is an array of NamedObj. There should also be a nObjects field telling you how many objects have been placed in the array. The clas ObjTable should have the following methods:

  • void add(String name, Obj3D obj) - This should create a new NamedObj object and put the name and object in it. The new object should be added to the private array and the number of objects in the array should be incremented. The new object can be added to the array at the index specified by nObjects.
  • Obj3D find(String name) - this should loop through all of the objects that you have added to your array (zero through nObjects-1) looking for an object whose name matches the name parameter. Use equalsIgnoreCase() for your matching.

Test your ObjTable before going on so that you are sure that it is working correctly.

Step 2 - process command lines.

Create a loop in your main program that will continuously read command lines from System.in and do what they say to do. For each command you should output a question mark "?" to tell the user that you are ready for a new command.

Each command has a name and then some parameters. The command names and the parameters are all separated by spaces.

The commands that you must implement are as follows. The names of the commands are in bold and the parameters are in italics.

  • axes size - This will create a new Axes(size) object and add it to the window. For example the command "axes 3.5" will create an Axes(3.5) object and add it to the window.
  • lookFrom x y z - This will set the look from point to be (x,y,z).
  • lookAt x y z - This will set the look at point to be (x,y,z).
  • sphere name - This will create a new SphereObj and will add it to the window. It will also add the SphereObj and its name to your ObjTable so that it can be found again.
  • box name - This will create a new BoxObj and will add it to the window. It will also add the BoxObj and its name to your ObjTable so that it can be found again.
  • cone name - This will create a new ConeObj and will add it to the window. It will also add the ConeObj and its name to your ObjTable so that it can be found again.
  • cylinder name - This will create a new CylinderObj and will add it to the window. It will also add the CylinderObj and its name to your ObjTable so that it can be found again.
  • move name x y z - This will use the find() method on our ObjTable to locate the object by this name. If there is no such object, then this will write out an error. Having found the object it will use setLocation(x,y,z) method to move the object to a new location.
  • scale name x y z - This will use the find() method on our ObjTable to locate the object by this name. If there is no such object, then this will write out an error. Having found the object it will use setScale(x,y,z) method to resize the object.
  • rotx name angle - This will use the find() method on our ObjTable to locate the object by this name. If there is no such object, then this will write out an error. Having found the object it will use setRotX(angle) method to rotate the object .
  • roty name angle - This will use the find() method on our ObjTable to locate the object by this name. If there is no such object, then this will write out an error. Having found the object it will use setRotY(angle) method to rotate the object .
  • rotz name angle - This will use the find() method on our ObjTable to locate the object by this name. If there is no such object, then this will write out an error. Having found the object it will use setRotZ(angle) method to rotate the object .
  • color name red green blue - This will use the find() method on our ObjTable to locate the object by this name. If there is no such object, then this will write out an error. Having found the object it will use setColor(red,green,blue) to change the color of the object.
  • quit - this will stop the loop from reading new commands and will end the program.

Each of your commands should be implemented as a method that accepts a Scanner as a parameter and will read the rest of the command from the Scanner and do the command.

You should read the first token from a command using the Scanner's next() method. You can then decide which of your command methods to call and pass the scanner to that method.

Example

? axes 5
? lookFrom 10 10 10
? sphere face
? color face 1.0 0.6 0.6
? scale face 0.5 0.6 0.4
? move face 3 3 3
? box seat
? scale seat 0.5 0.5 0.5
? move seat 3 0 3
? color seat 0 0 1
? quit

This would interactively do the same thing as the following code.

Win3D win = new Win3D("interactive");
ObjTable table=new ObjTable();
Obj3D tmp;

win.add(new Axes(5));

win.setLookFrom(10,10,10);

tmp=new SphereObj();
table.add("face",tmp);

tmp=table.find("face");
tmp.setColor(1.0,0.6,0.6);

tmp=table.find("face");
tmp.setScale(0.5,0.6,0.4);

tmp=table.find("face");
tmp.setLocation(3,3,3);

tmp=new BoxObj();
table.add("seat",tmp);

tmp=table.find("seat");
tmp.setScale(0.5,0.5,0.5);

tmp=table.find("seat"); 
tmp.setLocation(3,0,3); 

tmp=table.find("seat"); 
tmp.setColor(0,0,1);
Hints
 
  • Don't forget to create your array in the constructor of ObjTable and to initialize nObjects to zero. If you create your array with at least 1000 items in it, you should never run out.
  • Your main program should set up the window and then enter a loop like
    		boolean quit=false;
    		do
    		{ 
    			// read a command and call its method using a lot of if statements
    			// when you read a "quit" command, don't call a method, just set quit=true;
    		} until (quit)
    		
  • Test each command to be certain it works before doing the next one.
Passoff
 

__ 3) NamedObj and ObjTable are implemented correctly as their own classes

__ 2) look at and look from work correctly

__ 1) axes works

__ 1) sphere works

__ 1) box works

__ 1) cylinder works

__ 1) cone works

__ 1) move works

__ 1) scale works

__ 3) each rotate command works correctly

__ 1) quit works

__ 1) error messages appear correctly when a wrong name is used.