Examples:
Write a java program using stacks that take 3 non-negative (base 10) long integer numbers, and a base B (B is in the range 2–9) and writes the number to the screen as a base B number. The program prompts the user for 3 numbers and bases and then outputs them.
Use as input:
// StackException.java
public class StackException extends Exception {
/**
* Default constructor
*/
public StackException(String msg) {
super(msg);
}
}
// EmptyStackException.java
public class EmptyStackException extends StackException {
/**
* Default constructor
*/
public EmptyStackException() {
super("Stack is empty.");
}
}
// FullStackException.java
public class FullStackException extends StackException {
/**
* Default constructor
*/
public FullStackException() {
super("Stack is full.");
}
}
// ListStackADT.java
public interface ListStackADT<T> {
public boolean isEmpty();
public void push(T value);
public T pop() throws EmptyStackException;
public T peek() throws EmptyStackException;
}
// ListStackDataStrucClass.java
/**
* This class represents a stack
*/
public class ListStackDataStrucClass<T> implements ListStackADT<T> {
// Inner class
class StackNode {
// Data fields
private T value;
private StackNode next;
/**
* Default constructor
*/
public StackNode() {
this.value = null;
this.next = null;
}
/**
* Overloaded constructor
*
* @param value
* - value of the node
* @param next
* - next node
*/
public StackNode(T value, StackNode next) {
this.value = value;
this.next = next;
}
/**
* Copy constructor
*
* @param node
* - Stack node
*/
public StackNode(StackNode node) {
this.value = node.getValue();
this.next = node.getNext();
}
/**
* @return - the value
*/
public T getValue() {
return value;
}
/**
* @return - the next node
*/
public StackNode getNext() {
return next;
}
/**
* Sets the value
*
* @param value
* - the value to set
*/
public void setValue(T value) {
this.value = value;
}
/**
* Sets the next node
*
* @param next
* - the next to set
*/
public void setNext(StackNode next) {
this.next = next;
}
}
// Data fields for ListStackDataStrucClass
private StackNode top;
/**
* Default constructor
*/
public ListStackDataStrucClass() {
this.top = null;
}
/**
* Overloaded constructor
*/
public ListStackDataStrucClass(StackNode node) {
this.top = node;
}
/**
* Copy constructor
*/
public ListStackDataStrucClass(ListStackDataStrucClass stack) {
// Set top as empty
setTop(null);
try {
// Check if stack is not empty
if (!stack.isEmpty()) {
// Copy elements form stack to this stack
while (!stack.isEmpty())
push((T) stack.pop());
}
} catch (EmptyStackException e) {
System.out.println(e.getMessage());
}
}
/**
* @return - the top node
*/
public StackNode getTop() {
return top;
}
/**
* Sets the top node
*
* @param top
* - the top to set
*/
public void setTop(StackNode top) {
this.top = top;
}
/**
* Checks whether the stack is empty
*
* @return - returns true if stack is empty, false otherwise
*/
public boolean isEmpty() {
return (getTop() == null);
}
/**
* Throws exception if stack is empty
*
* @throws EmptyStackException
* - if stack is empty
*/
public void ifEmpty() throws EmptyStackException {
if (isEmpty())
throw new EmptyStackException();
}
/**
* Adds a value to the stack
*
* @param value
* - value to be inserted
*/
public void push(T value) {
// Check if stack is empty
if (isEmpty())
setTop(new StackNode(value, null));
else {
// Create new node
StackNode newNode = new StackNode(value, getTop());
// Set newNode as the top
setTop(newNode);
}
}
/**
* Returns and remove the top node from the stack
*
* @return - Returns the top node in the stack
* @throws EmptyStackException
* - if stack is empty
*/
public T pop() throws EmptyStackException {
if (isEmpty())
throw new EmptyStackException();
else {
T value = getTop().getValue();
// Set new top
setTop(getTop().getNext());
return value;
}
}
/**
* Returns but does not remove the top node from the stack
*
* @return - Returns the top node in the stack
* @throws EmptyStackException
* - if stack is empty
*/
public T peek() throws EmptyStackException {
if (isEmpty())
throw new EmptyStackException();
else
return getTop().getValue();
}
/**
* Returns the stack contents
*/
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("[ ");
// Check if stack is not empty
if (!isEmpty()) {
StackNode node = getTop();
// Traverse through the stack
while (node != null) {
sb.append(node.getValue() + " ");
}
}
sb.append("]");
return sb.toString();
}
}
// BaseConverter.java
import java.util.Scanner;
public class BaseConverter {
// Inner class
class BaseNumber {
// Data fields
private long number;
private long base;
/**
* Default constructor
*/
public BaseNumber() {
this.number = 0;
this.base = 10;
}
/**
* Overloaded constructor
*
* @param number
* - number
* @param base
* - base to which the number is to be converted
*/
public BaseNumber(long number, long base) {
this.number = number;
this.base = base;
}
/**
* @return - the number
*/
public long getNumber() {
return number;
}
/**
* @return - the base
*/
public long getBase() {
return base;
}
/**
* @param number
* - the number to set
*/
public void setNumber(long number) {
this.number = number;
}
/**
* @param base
* - the base to set
*/
public void setBase(long base) {
this.base = base;
}
}
private static final int MIN_BASE = 2;
private static final int MAX_BASE = 9;
private static final int NUM = 3;
// Data fields for BaseConverter class
private BaseNumber[] baseNumber;
/**
* Default constructor
*/
public BaseConverter() {
baseNumber = new BaseNumber[NUM];
// Get base number
inputPrompt();
}
/**
* Gets a number and its base and sets the base number
*/
private void inputPrompt() {
// Scanner to get user input
Scanner in = new Scanner(System.in);
// Get NUM base numbers
for (int i = 0; i < NUM; i++) {
System.out.println("Number " + (i + 1));
// Get number
long number = -1;
while (number < 0) {
System.out.print("Enter a positive integer: ");
number = in.nextLong();
}
// Get base
long base = -1;
while ((MIN_BASE > base) || (base > MAX_BASE)) {
System.out.print("Enter output base[" + MIN_BASE + " - " + MAX_BASE + "]: ");
base = in.nextLong();
}
// Set base number
baseNumber[i] = new BaseNumber(number, base);
System.out.println();
}
}
/**
* Converts the base 10 number to target base and returns it.
* @param bNumber - number to be converted to a base
*/
private String convert(BaseNumber bNumber) {
// Get base 10 number
long number = bNumber.getNumber();
// Get target base
long targetBase = bNumber.getBase();
// Create stack to hold data
ListStackDataStrucClass<Long> stack = new ListStackDataStrucClass<Long>();
// Convert
while (number != 0) {
// Push number % targetBase into stack
stack.push(number % targetBase);
// Set number
number = number / targetBase;
}
// Get the number converted to base
StringBuffer sb = new StringBuffer();
// Get contents from stack
while (!stack.isEmpty())
try {
sb.append(stack.pop());
} catch (EmptyStackException ese) {
System.out.println(ese.getMessage());
}
return sb.toString();
}
/**
* Converts NUM BaseNumber(s) to target bases
*/
private String convertAll() {
String result = new String();
// Convert all NUM BaseNumber to target base
for (int i = 0; i < NUM; i++)
result = result + (baseNumber[i].getNumber() + " (Base 10) = " + convert(baseNumber[i]) + " (Base " + baseNumber[i].getBase() + ")\n");
return result;
}
public String toString() {
return convertAll();
}
public void processAndPrint() {
System.out.println(this);
}
}
// BaseConverterDemo.java
public class BaseConverterDemo {
public static void main(String[] args) {
// Create BaseConverter object
BaseConverter baseConv = new BaseConverter();
baseConv.processAndPrint();
}
}
SAMPLE OUTPUT:
if this answer is helpful to you please give positive rating.if any doubts please provide comment i will try to clarify your doubts.
Get Answers For Free
Most questions answered within 1 hours.