Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Number 2 You can ignore writing the code for the interface ListStackADT<T> Lab 4

ID: 3849275 • Letter: N

Question

Number 2

You can ignore writing the code for the interface ListStackADT<T>

Lab 4 Directions (Stacks) Program #1 1. Show the ArraystackADT interface 2. create the ArraystackDatastrucclass T, with the following methods default constructor, overloaded constructor copy constructor initializestack, mpty Stack, isFullStack, push, peek, void pop 3. Create the PrimeFactorizationDemoclass: instantiate an Array StackDatastrucclass Integer object with 50 elements. Use a try catch block in the main using pushes/pops 4. Exception classes: StackException, StackunderflowException, Stack OverflowException 5. Show the 4 outputs for the following: 3.960 1.234 222 222 13,780 Program #2 1. Show the ListstackADT T interface 2. Create a ListstackDatastrucclass T> with the following methods: default constructor, overloaded constructor, copy constructor, getTop, setTop isEmpty, if Empty (if empty throw the exception), push, peek, pop, tostring. 3. Create a private inner class of ListStack T called StackNode T with the following methods: default constructor, overloaded constructor, copy constructor, getValue, getNext, setValue, setNext 4. Create a Baseconverter class (non-generic) with the following methods: default constructor, inputprompt, convert [converts a BaseNumber to a converted String], convertAll [instantiate a string object], tostring, processAndPrint

Explanation / Answer

Solution:

import java.math.BigInteger;
import java.util.Scanner;

// Staxck Exception
class StackException extends RuntimeExeptionException{
}
// FullStackException
class FullStackException extends StackException {
}
// EmptyStackException
class EmptyStackException extends StackException {
}
// Interface
interface ListStackADT<T> {}
// Class defenition of ListStackDataStrucClass
class ListStackDataStrucClass<T extends Number> implements ListStackADT<T> {
// // Class defenition of StackNode
   private class StackNode<T extends Number> {
// private members
       private StackNode<T> next;
private T value;
// Constructor
public StackNode(){
// Set null values
           next = null;
value = null;
}
       // Function to get values
public T getValue(){
           // return the values
return value;
}
       // Function to set Values
public void setValue(T val){
       // Set the va;ues
value = val;
}
       // Function to get next
public StackNode<T> getNext(){
       // return the next
return next;
}
public void setNext(StackNode<T> nex){
           // set the next
next = nex;
}
}
   // private members
private StackNode<T> Top;
private int maximumNodes;
private int cou;
   // Constructor
public ListStackDataStrucClass(){
Top = null;
cou = 0;
}
// Function that return size
public int getSize(){
return cou;
}

public ListStackDataStrucClass(T firVal, int maximum){
this();
this.maximumNodes = maximum;
Top.setValue(firVal);
}

public StackNode<T> getTop() {
return Top;
}

public void setTop(StackNode<T> item) {
Top = item;
}

public void push(T item) {
StackNode<T> node = new StackNode<>();
node.setValue(item);
node.setNext(null);
  
if(Top == null){
Top = node;
}
else{
node.setNext(Top);
Top = node;
}
  
cou++;
}
  
public T pop() throws StackException {
if(isEmpty()){
throw new StackException();
}
StackNode<T> node = Top;
Top = node.getNext();
cou--;
return node.getValue();
}

public T peek() {
return Top.getValue();
}

public boolean isEmpty(){return Top == null;}

public boolean ifEmpty() throws EmptyStackException {
if (Top == null) {
throw new EmptyStackException();
}
return false;
}

public String toString(){return "";}
}

class BaseConveterDemo {
class BaseNumber{
long num;
long bas;

public long getNumber(){
return num;
}

public void setNumber(long num){
this.num = num;
}

public long getBase(){
return bas;
}

public void setBase(long bas){
this.bas = bas;
}
}

BaseNumber baseNumber;
ListStackDataStrucClass<Long> lstStack;

public BaseConveterDemo(){
baseNumber = new BaseNumber();
lstStack = new ListStackDataStrucClass<>();
}

public void inputPrompt(){
System.out.print("Enter the forrm of A base B ");
Scanner scn = new Scanner(System.in);
baseNumber.setNumber(scn.nextLong());
scn.next();
baseNumber.setBase(scn.nextLong());
}

public String convert(){
return baseNumber.getNumber() + "(bas" + baseNumber.getBase() + ")";
}

public String convertAll(){
return new String(baseNumber.getNumber() + "(bas" + baseNumber.getBase() + ")");
}

public void processAndPrint(){
try{
while(baseNumber.num != 0){
Long rem = baseNumber.num % baseNumber.bas;
if (rem != 0) {
lstStack.push(rem);
}else{
lstStack.push(new Long(0));
}
baseNumber.setNumber(baseNumber.num / baseNumber.bas);
}
String dout = "";
while(!lstStack.isEmpty()){
dout += "(" + lstStack.pop() + "*" + baseNumber.bas
+ "^" + lstStack.getSize() + ")"
+ "+";
}
  
System.out.println(new String(dout.getBytes(), 0, out.length() - 1));
}
catch(StackException exp){
exp.printStackTrace();
}
}
}

class Main {
public static void main(String[] args) {
BaseConveterDemo bc = new BaseConveterDemo();
bc.inputPrompt();
bc.processAndPrint();
}
}