Wednesday, March 25, 2009

Solution




Lab1– Queue Data Structure
You have now used the stack class as part of your lab work. The stack concept is a very common algorithm which uses the inventory concept of last in first out (LIFO). The last item pushed onto the stack is the first item popped off the stack.

One implementation of the Queue problem is shown in the Queue Example folder. Create a command prompt window and type java TestQueue. Notice how read-only JTextFields are grayed out so that the user cannot write into those objects. Also notice that pressing the ENTER key when entering data creates an event which does the same as pressing the Button object. This makes entering data very fast into the GUI.

Another useful algorithm would be a class that would provide an inventory of first in first out (FIFO) commonly called a queue.












Here is an example of FIFO which follows the diagrams above:
1. Put a 5
2. Put a 45
3. Put a 12
4. Take a 5
5. Take a 45
6. Take a 12
7. FIFO inventory empty

Note the first item 5 is the first item to be removed.

The assignment is to create a queue class which incorporates the FIFO storage method. You might consider the stack class as a starting model. If you did that you would be able to use the concept of a put() which would be similar to the push() from the stack class but the creation of a take() would be very different than the pop() from the stack class. The take(), to duplicate the above pictorial example, would take the bottom item and then need to shift all the current inventory down one cell and reduce the depth of the inventory by one.

The queue will be only a maximum of five deep for ease of testing and will only store integers.

Of course, be sure to provide the ability to not over flow the inventory system or to protect from taking data from an empty inventory system.

Create a test Java GUI application to test the above class. Give the user the ability to Put into the queue, Take from a queue or Display the current contents of the queue. There should be confirming data indicating the number of items currently in the stack after each put() or take(). There should be a message generated if the stack is overflowed or empty.

The project should have a file containing the class QueueIntSimple, a file containing the extended Application class to test the class (e.g. TestQueueIntSimple). Don’t forget to supply proof of your program as part of what you turn in.

The GUI applet or application might look like the follow diagram:






















Note 1 – the use of a “Display Queue” button, that each time pressed will display the current contents of the queue as text in the window frame as shown.

Note 2 – the Queue class should be a non-GUI application that provides only the behavior specified above. The Test Queue class should be the GUI application designed to test the Queue class. It can also contain the main() method required by all Java applications.

LAB 2 This would be an expansion of the FIFO class design of Lab 5. Here you would consider two types of data to be stored in inventory; one type of data would have a higher priority. The higher priority data would be retrived on a FIFO basis before standard data is retrived.

There are several design approches to this problem. A couple are discussed here. The first is to create two queues, one to hold the standard priority data and a second queue to hold the high priority data. The second method would use only one queue and place data at the end of the queue if standard data or to insert the high priority data at the proper location in the queue.

The first method is based on removing data from the beginning of the high priory queue if there is any data there before removing data from the standard priority queue. The put processes would place data at the end of each appropreate queue. From the user’s perspective, there is only a single queue with two priorities when implementing this approach.

With the second method there might be a put_high() method would place data ahead of data placed with put(). Of course put_high() data would not be placed ahead of other put_high() data already in inventory. The take() method would still take data from the beginning of the array, no matter what type of data. You might consider the need to keep track of both the total count in inventory as well as the count_high of total number of high priority data items in inventory

The queue(s) will be only ten deep for ease of testing and will only store integers. Design note – a total of 10 items can be stored in the data queue at any one point in time independent of the implementation choice chosen. The 10 items could be all high-priority data, all low-priority data or any combination of the two.

The project should have a file(s) containing the class(es), a file containing the test program to demonstrate the developed class(es). Don’t forget to supply proof of your program as part of what you turn in. Remember that Alt button with Prnt Scrn button captures the active window to the Windows clip board memory for pasting in a program like Word.



Lab 3 The instructor recommends invoking the example in the “Inheritance Example” folder – java TestEmployee.

The following will be the inheritance for the Employee class:










The Person class will have the data String for name and a means to get and set the name. The Employee class will extend from Person and add the capabilities to have an employee number as an int and an employee salary as a double.

Create an array of 100 Employees. Then create a user interface that will display a means for entering in the data and then saving the data to one of the Employee array objects. The “Save Employee” button will save the data to the array. The “Forward” and “Back” button will allow the user to move and display the current data in each array object. The “Clear Employee” button will allow the users to null out the String for the name, and set the salary and employee number to zero.

The project should have a file(s) containing the class(es), a file containing the test program to demonstrate the developed class(es). Don’t forget to supply proof of your program as part of what you turn in. Remember that Alt button with Prnt Scrn button captures the active window to the Windows clip board memory for pasting in a program like Word.


EXTRA CREDIT
Lab Simple inheritance program. Refer to the Test.java code below and run the Java console application found in FirstLookAnimals.


















The Unified Modeling Language (UML) notation to represent inheritance is an arrow from the derived class to the base class as shown in th above diagram.

A very simple start to understanding polymorphism will be found in the subdirectory to this lab called Shapes. Go take a look.

The following code is the starting code for the test program. Please add the ability to supply how many animals to create rather that my hard coded 10. Also give the user the opportunity to loop to select another number of animals to create. The Test.java program should also be converted from a console to GUI application.

//Test.java

//FirstLook at inheritance.
//Thing.java, Animal.java, Dog.java, Cat.java, Rabbit.java
//and Test.java make up this first simple example.

public class Test extends Object
{
public static void main( String args[] )
{
System.out.println( "Welcome to simple inheritance Test!" );

Animal barnyard[];
barnyard = new Animal[10];//cannot use Thing as it cannot speak()

for( int z = 0; z < 10; z++)//proof of dynamic binding at run time!!!!
{ //10 could be a variable selected by user
//fewer selections as Animal cannot be used
int selection = ( int ) ( Math.random() * 4 );//0 through 3

switch( selection )
{
case 0:
barnyard[z] = new Dog(); //child to parent
break;
case 1:
barnyard[z] = new Cat(); //child to parent
break;
case 2:
barnyard[z] = new Rabbit();//child to parent
break;
case 3:
barnyard[z] = new Animal();//parent to parent
break;
}//end switch

}//end for

for( int z = 0; z < 10; z++)
{
barnyard[z].name();
barnyard[z].speak();

}//end for
System.out.println();
}//end main
}//end Test class
/* proof
Welcome to simple inheritance Test!

Cat Meow
Dog Woof
Animal Blank
Cat Meow
Animal Blank
Animal Blank
Rabbit Wiggle Nose
Animal Blank
Cat Meow
Rabbit Wiggle Nose
*/

You are to write the classes that support the above Test.java program and produce a proof similar to that shown above. The classes in the project will be Thing, Animal, Cat, Rabbit and Dog. You must write the Animal, Cat, Rabbit, Dog and revise Test.

The Thing class extends Object. The Animal class extends Thing. Finally, Cat, Rabbit and Dog extend Animal.

I will start you out with the code for Thing.
//Thing.java

public class Thing extends Object
{
public void name()
{
System.out.print("\nThing");
}

}//end Thing

You will need to add the ability to speak() to each of the other classes, which can be simply a System.out.print("\nWoof"); if it was the Dog class, for example.

Here is a suggested window:

















Every time you press the Display button you generate the selected number of animal types. Place the output in a scrolling text area. Add number in front of the animal types to keep track of what is happening to the output.

No comments:

Post a Comment