Help with this programming/java project. Thank you. Program 1. Implement a stack
ID: 3886018 • Letter: H
Question
Help with this programming/java project. Thank you.
Program 1.
Implement a stack using an array. Make your stack size 5. Use lowercase stack for your class name, do not use Stack class defined in java.
stack() creates an empty stacks, stacks is new and empty.push(item) adds a new item to the stacks, stacks is modified.pop() removes and returns an item, stacks is modified.isEmpty() returns a boolean and tests for an empty stacks, stacks is not modified.size() returns the int size of the stacks, stacks is not modifiedprint() prints the stacks from front to rear, stacks is not modified.top() prints the front element, stacks is not modified.
Driver should print the results of the following
push(redShirt)
push(greenShirt)
push(yellowPants)
push(purpleSock)
push(pinkSocks)
size()
push(blueShirt)
size()
pop()
pop()
size()
pop()
top()
pop()
pop()
size()
isEmpty()
Program 2.
Your program uses call stack for methods.
For instance, if your program has a main method (all java programs have one!) and it calls a computeArea method, then the call stack will have main first pushed, then computeArea. When your program returns from computeArea, that call information is popped off the call stack. The remaining method call (in this case, main) tells the program where to return. You can think of calling top() or peek() to get the top element of the stack without popping it off the stack.
Running the program see the call stack as the having the following stack methods
push(main)
push(computeArea)
pop(computeArea)
pop(main)
Another example, if your program contains a main method that calls computeBalance which in turn calls computeInterest, the stack would have main, computeBalance, computeInterest. Each would be popped off in turn when the method completes and returns.
Write a small program with 4 methods named method1, method2, method3 and method4. These methods only need to have print statements saying "In method1" and "Exiting method1". (Similar to the first program above) In main, start with a print statement "In main method" then call the various methods in various orders. End your main method with "Exiting main" print statement. Submit your callStackLastName.java and callStackDriverLastName.java with a comment explaining your output.
Program 3.
Implement the Towers of Hanoi in java.
Print the number of moves given the number of disks you are playing.
Explanation / Answer
Program 1:
package Online;
import java.util.NoSuchElementException;
import java.util.Scanner;
//Class stack definition
class stack
{
//Instance variable to store the strings
protected String item[];
//Instance variable to top position, stack size and length
protected int topPos, stackSize, len;
//Constructor
public stack(int n)
{
//Initializes the instance variables
stackSize = n;
len = 0;
topPos = -1;
//Creates a string array of given size
item = new String[stackSize];
}//End of constructor
//Method to check stack is empty or not
public boolean isEmpty()
{
//Returns true if top position is -1
return topPos == -1;
}//End of method
//Method to check stack is full or not
public boolean isFull()
{
//Returns true if top position is equal to one less than stack size
return topPos == stackSize -1 ;
}//End of method
//Method to return the size of stack
public int size()
{
//Returns the size of stack
return len ;
}//End of method
//Method to add an item to the stack
public void push(String data)
{
//Checks for overflow condition
if(topPos + 1 >= stackSize)
//Throws an IndexOutOfBoundsException exception
throw new IndexOutOfBoundsException("Overflow Exception");
//Checks the size is available or not
if(topPos + 1 < stackSize )
//Adds an item to the stack top
item[++topPos] = data;
//Increase the length by one
len++ ;
}//End of method
//Method to delete an item from the stack top position
public String pop()
{
//Checks for underflow condition
if( isEmpty() )
//Throws a NoSuchElementException
throw new NoSuchElementException("Underflow Exception");
//Decrease the length by one
len-- ;
//Returns the deleted item
return item[topPos--];
}//End of method
//Method to return the top item from the stack
public String top()
{
//Returns the top item from the stack
return item[topPos];
}//End of method
}//End of class
//Driver class StackDriver
public class StackDriver
{
//Main method definition
public static void main(String[] args)
{
//Creates a scanner class object to accept size
Scanner scan = new Scanner(System.in);
//Accepts the size of the stack
System.out.println("Enter Size of Stack ");
int n = scan.nextInt();
//stack class object created with given size
stack stk = new stack(n);
//Checks the methods
stk.push("redShirt");
stk.push("greenShirt");
stk.push("yellowPants");
stk.push("purpleSock");
stk.push("pinkSocks");
System.out.println("Size of Stack: " + stk.size());
stk.push("blueShirt");
System.out.println("Size of Stack: " + stk.size());
System.out.println("Popped Element: " + stk.pop());
System.out.println("Popped Element: " + stk.pop());
System.out.println("Size of Stack: " + stk.size());
System.out.println("Popped Element: " + stk.pop());
System.out.println("Top Element: " + stk.top());
System.out.println("Popped Element: " + stk.pop());
System.out.println("Popped Element: " + stk.pop());
System.out.println("Size of Stack: " + stk.size());
stk.isEmpty();
}//End of main
}//End of class
Sample Run:
Enter Size of Stack
10
Size of Stack: 5
Size of Stack: 6
Popped Element: blueShirt
Popped Element: pinkSocks
Size of Stack: 4
Popped Element: purpleSock
Top Element: yellowPants
Popped Element: yellowPants
Popped Element: greenShirt
Size of Stack: 1
Program 3:
package Online;
import java.util.Scanner;
//Class TowersOfHanoi definition
public class TowersOfHanoi
{
//Method to give solution to the TowersOfHanoi
public void solve(int disc, String start, String aux, String end)
{
//Checks if the number of discs is one
if (disc == 1)
{
//Display the result
System.out.println(start + " -> " + end);
}//End of if
//If the discs is more than one
else
{
//Recursively calls the method with disc - 1 discs, auxiliary becomes the end and end becomes the auxiliary
solve(disc - 1, start, end, aux);
//Displays the result
System.out.println(start + " -> " + end);
//Recursively calls the method with disc - 1 discs
solve(disc - 1, aux, start, end);
}//End of else
}//End of method
//Main method definition
public static void main(String[] args)
{
//Creates an object of TowersOfHanoi class
TowersOfHanoi towersOfHanoi = new TowersOfHanoi();
//Creates scanner class object
Scanner scanner = new Scanner(System.in);
//Accepts number of discs
System.out.print("Enter number of discs: ");
int discs = scanner.nextInt();
//Calls the method
towersOfHanoi.solve(discs, "A", "B", "C");
}//End of main method
}//End of class
Sample Run:
Enter number of discs: 4
A -> B
A -> C
B -> C
A -> B
C -> A
C -> B
A -> B
A -> C
B -> C
B -> A
C -> A
B -> C
A -> B
A -> C
B -> C
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.