Starting from:
$30

$24

Programming Assignment 2 Solution

In this PA, you need to apply a factory method pattern to create objects for P20, based on the lecturenotes of 9/20. Here are the interfaces you need to implement:

Interface IFactory{

IStringToBeHandled create_STBHandler();

ICheckBalancedString create_Checker();

IStack create_Stack(int choice); // choice 1: using an arraylist; 2: using doubly-linked list

}

Interface IDoublyLinkedList{

boolean remove (int index); // remove a node at index

boolean add(INode node, int index); //add a new node at the position index

INode get_HeadNode(); // return the first node in the list

int get_size(); // return the number of nodes in the list

}

Interface INode {

void set_Data(char c);

char get_Data();

vod set_Pred(INode n);

void set_Succ(INode n) ;

INode get_Pred();

INode get_Succ();

}





In order to apply the factory method pattern, you need to hide the classes implementing the following interface. But you can use class names implementing the above interfaces.





You need to implement another class using a doubly-linked list to implement IStack.

interface IStringToBeHandled {

void get_Input_String(); //get input string to make sure it has only “(“ and “)” character. If not, report an error. Otherwise, call the object of type ICheckBalancedString to check whether the input string has a balanced “(“s and “)”s. So, make sure the setter method is called before this method is called

void set_CheckBalancedString(ICheckBalancedString check); // set a check object. This method should be called before get_Input_String() is called.





String print_Input_String(); /print the input String in the console

}





interface ICheckBalancedString{

void setStringTobeChecked(String s); // set the string to be checked.

boolean checkBalancedParenthesis(); // check whether the string has a balanced number of “(“s and “)”s. This method should be called after setStringTobeChecked is called. Return true if the string has a balanced number of “(“s and “)”s.

void set_Stack(IStack stack); // set a stack object. This method should be called before CheckBalancedParethesis() is called





String get_Checked_String(); //return the checked string

}

interface IStack {

void push(char c); // push a char c onto a stack

char pop(); // pop a stack from a stack

int size(); // return the number of elements on the stack

}





Once you are done, you should have the following the Application class where the objects are created via the factory class you implemented.

class Application {

public static void main(){

// use the factory class to create objects with their class names, connect the objects together via using the setter method appropriately, check the input string, and print out the result in the console;

……………
}





Requirements for PA2,

You must use the Eclipse IDE to create a Java project.
You must follow the instruction to submit your Java project (http://www.cs.wmich.edu/~wwshen/cs1120_Fall2012/documents/HowtoSubmitPAinEclipse.pdf).








More products