Good Evening, Below is the java program that I need help on. Help is much appreciated :)
In Listing 19.1, GenericStack is implemented using composition. Define a new stack class that extends ArrayList. Include two methods, min and max which will return the minimum and maximum values in the stack. Write a test program that generates and displays at least 15 random Integer objects and uses the stack’s push method to place them on the stack. Once all the values are on the stack, display the minimum and maximum objects. Then use the stack to display the objects in reverse order.
Repeat the process two more times, once with random Character objects, and once with random Date objects.
Solution:
All necessary comments ar eprovided with the JAVA code
/**********************************
class, GenericStack.java
**********************************/
package myPackage;
import java.util.*;
// Defined a generic class which extends ArrayList
//T extends Comparable<T> is required to compare objects of
different kind to find minimum and maximum from
stack
public class GenericStack<T extends
Comparable<T>> extends ArrayList<T>{
private ArrayList<T> stack = new
ArrayList<T> (); // to be used as stack to store values
private int top; // keeps track of top most element in
stack
public GenericStack()
{
top=-1; // First element will be
stored at top =0
}
public T max() // this function returns max element
stored in stack
{
if (stack.isEmpty()) // checking
whether stack is not empty
{
throw new
IllegalStateException("Can't get a max element from an empty
list.");
}
else
{
T max =
stack.get(0);// assuming element at index 0 in ArrayList Stack is
maximum
for(T
element:stack)
{
/*
obj1.compareTo(obj2) function returns
-1 if obj1 < obj2
0 if obj1= obj2
1 if obj1>obj2
*/
if(element.compareTo(max)>0) // here we are
comparing elements of array list with our max
{
max=element; // whenever
element > max, value of max will be updated in loop
}
}
return
max;
}
}
public T min()
{
if (stack.isEmpty()) // checking
whether stack is not empty
{
throw new
IllegalStateException("Can't get a max element from an empty
list.");
}
else
{
T min =
stack.get(0); // assuming element at index 0 in ArrayList Stack is
minimum
for(T
element:stack)
{
/*
obj1.compareTo(obj2) function returns
-1 if obj1 < obj2
0 if obj1= obj2
1 if obj1>obj2
*/
if(element.compareTo(min)<0) // here we are
comparing elements of array list with our min
{
min=element; // whenever
element < min , value of min will be updated in loop
}
}
return
min;
}
}
public void push(T element)
{
top++; // initially top is set to
-1 , thus before adding element to stack top is incremented by
1;
stack.add(top, element);
}
public void pop()
{
if (top==-1) // can not pop from
empty stack
{
throw new
IllegalStateException("Can't get a max element from an empty
list.");
}
else
{
System.out.println("Popping elements from stack ...");
for(int i=top;
i>=0;i--)
{
System.out.print(stack.get(i)+"-->");
stack.remove(i); // deleting element from
stack
top--;// decrementing top after deletion of
element
}
System.out.println(" ");
}
}
}
/**********************************
class, Test.java
**********************************/
package myPackage;
import java.util.*;
public class Test {
public static void main(String[] args) {
// Working with random
integers
System.out.println("******************************");
System.out.println("Test run for
random Integers");
System.out.println("******************************");
GenericStack<Integer>
randomIntegers = new GenericStack<Integer>();
int num;
for (int i = 0; i < 15; i++)
{
num = (int)
(Math.random() * ((20 - 1) + 1)) + 1; // generating random integers
between 1 and 20
System.out.println("Random Number pushed onto stack : " +
num);
randomIntegers.push(num);
}
System.out.println(" ");
// finding maximum integer
int max =
randomIntegers.max();
System.out.println("Maximum number
in stack : " + max);
// finding minimum integer
int min =
randomIntegers.min();
System.out.println("Maximum number
in stack : " + min);
// POP Integers
System.out.println(" ");
randomIntegers.pop();
// Working with random
characters
System.out.println("******************************");
System.out.println("Test run for
random characters");
System.out.println("******************************");
GenericStack<Character>
randomChar = new GenericStack<Character>();
Random r = new Random();
char c;
for (int i = 0; i < 15; i++)
{
c = (char)
(r.nextInt(26) + 'a'); // generating random chars
System.out.println("Random Character pushed onto stack : " +
c);
randomChar.push(c);
}
System.out.println(" ");
// finding maximum char
char maxChar =
randomChar.max();
System.out.println("Maximum number
in stack : " + maxChar);
// finding minimum integer
char minChar =
randomChar.min();
System.out.println("Maximum number
in stack : " + minChar);
// POP Characters
System.out.println(" ");
randomChar.pop();
// Working with random
characters
System.out.println("******************************");
System.out.println("Test run for
random dates");
System.out.println("******************************");
GenericStack<Date>
randomDate = new GenericStack<Date>();
Random rnd = new Random();
Date date;
for (int i = 0; i < 15; i++)
{
date = new
Date(Math.abs(System.currentTimeMillis() - rnd.nextLong()));
System.out.println("Random Date pushed onto stack : " +
date);
randomDate.push(date);
}
System.out.println(" ");
// finding maximum char
Date maxDate =
randomDate.max();
System.out.println("Maximum number
in stack : " + maxDate.toString());
// finding minimum integer
Date minDate =
randomDate.min();
System.out.println("Maximum number
in stack : " + minDate.toString());
// POP Dates
System.out.println(" ");
randomDate.pop();
}
}
/**********************************
Output
**********************************/



Good Evening, Below is the java program that I need help on. Help is much appreciated...