Question

In: Computer Science

the topic is STack and Generic stack in java It's really urgent!! Generic Stack due 10/16/2020...

the topic is STack and Generic stack in java

It's really urgent!!

Generic Stack due 10/16/2020 Friday 2 pm Mountain Time

Generic Stack

Make It Generic!

The first part of this lab is making your stack generic. Take the code from your working StringStack class - everything except the main method - and paste it into the GenericStack class. Change the name of the constructors to match the new name of the class, then modify the whole class so it uses generics, and can store any type that a programmer asks for.

Numbers and Readers

To take advantage of polymorphism, Java has a lot of inheritance hierarchies built-in. You may not have known that simple numeric classes, like Integer and Float, are actually part of one! Java contains a generic number class creatively called Number, and pretty much any class in Java whose purpose is to store numbers inherits from this class.

There are many types of Readers that can extract characters from different things, such as Strings, Files, and arrays.

Scrolls of Numbers

A few years back, I bought a box of random numbers from an intrepid salesman who insisted they were special. I'd like to find out if that's really the the case. Unfortunately, I've stored the numbers in a bunch of different places - some of them are in Strings, others are in files, it's a bit of a mess.

So I created a generic calculator that, using Readers, can read in a list of numbers from any source, put them into a stack, and then add them all up. Since I'm a good programmer, I put each of these steps in their own functions... but it looks like I accidentally erased some of them. Your next task in the lab is to repair my generic stack calculator by fixing the functions.

  1. makeStack is supposed to take in a Reader, of any kind, and return a stack of Numbers from the data in that Reader. Luckily, the only thing broken here is the function signature - fix it by specifying the appropriate return type, function name, and parameters, using generics if appropriate.
  2. evaluate is supposed to take a stack of Numbers and add all of the numbers together, returning the result as a double. At some point, it looks like I replaced this entire function with a single return 0.0. Whoops. You'll have to re-implement this function as well as re-write its signature, again using generics if appropriate.
    A tip for this function: the Number class can't be directly added to a primitive (like a double, int, etc.) However, a Number object can be converted into a primitive with the use of the doubleValue() function. You may need to use this!
  3. I use the parse function to help with makeStack, but I must have scuffed off the return type at some point. Find the right return type by looking at the function and how it is used in makeStack.

Wrapping up

you should get output that looks somewhat like this (exact numbers may vary due to floating-point rounding):

76.4
76.39999961853027
4584425.0
15.324

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.*;

public class GenericStack {

/* YOUR CODE HERE
* Just like in the ArrayList lab, copy your StringStack code, excluding the
* main method, here.
* Then, modify it so it's generic!
*/

public static void main(String[] args) {
// If any of these lines cause a compilation error, your stack hasn't
// been properly made generic.
GenericStack intStack = new GenericStack<>();
GenericStack stringStack = new GenericStack<>();
GenericStack> listStack = new GenericStack<>();
}

}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.*;
import java.io.*;
import java.math.*;

public class Calculator {

public static void main(String[] args) throws FileNotFoundException, IOException {

String numbers = "56 3 7 2.0 8.4";
char[] moreNumbers = "1.0 2 3 4 5 0.324".toCharArray();

GenericStack stack1 = makeStack(new StringReader(numbers));
System.out.println(evaluate(stack1));

GenericStack stack2 = new GenericStack<>();
for (String token : numbers.split(" ")) {
stack2.push(Float.parseFloat(token));
}
System.out.println(evaluate(stack2));

GenericStack stack3 = makeStack(new FileReader("numbers.txt"));
System.out.println(evaluate(stack3));

GenericStack stack4 = makeStack(new CharArrayReader(moreNumbers));
System.out.println(evaluate(stack4));
}
  
/* This function is meant to take in a Reader called "reader" and return a
* stack of Numbers.
*
* Remember: don't change anything that's already there. Just add your new
* code where the comment is to fix the function's signature.
*/
public static /* ??? */ throws IOException {
GenericStack stack = new GenericStack<>(64);
char[] data = new char[64];
reader.read(data);
String tokens = new String(data);
for (String token : tokens.split(" ")) {
stack.push(parse(token));
}
return stack;
}

/* This function is meant to take in a stack of ANY KIND of Number
* called "stack", and return the double you get if you add all of the
* numbers together.
* The function must be able to accept a stack of ANY KIND of Number!
*
* Hint: use wildcard generics!
*/
public static /* ??? */ {
/* implement me! */
return 0.0;
}

/* This function is missing a return type.
* Examine it, and see if you can tell what type it should return.
* (Spoiler: it's probably what you think it is.)
*/
public static /* ??? */ parse(String s) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) { }

try {
return Double.parseDouble(s);
} catch (NumberFormatException e) { }

return new BigDecimal(s);
}

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.*;

public class StringStack {
   private String [] stack;
   public int size, top;


public StringStack(int size) {
   this.top=-1;
   this.size=size;
   stack= new String[size];
  
}
public StringStack() {
   this.size=10;
   this.top=-1;
   stack= new String[this.size];
  
}
public boolean empty() {
return this.top==-1;
}
public String peek() {
   if (this.top==-1) {
       throw new NoSuchElementException("Empty stack!");
   }
   else {
       return this.stack[this.top];
   }
  
}
public String pop() {
if (this.top==-1) {
   throw new NoSuchElementException("Empty stack!");
}
else {
   return this.stack[this.top--];
}
}
public String push(String s) {
  
   if (this.top>=this.size-1) {
   throw new IllegalStateException("Stack is full!");
}
this.stack[++this.top]= s;
return this.stack.toString();
}
public int search(String s) {
   int dist = 0;
   int top = this.top;
   while(top>=0) {
       if(this.stack[top]==s) {
           return dist;
       }
       dist++;
       top--;
   }
   return -1;
} /* I removed the public static void main method from StringStack Class because I was getting a prompt saying the question is too long on Chegg there should not be an issue we just have to convert the methods above to generic as mentioned by copy and pasting it in GenericStack class and then modifying it. This all the formation*/

Solutions

Expert Solution

SOLUTION :

CONSIDERING THE CONDITIONS AND PARAMETERS WITH REQUIREMENTS FROM THE QUESTION.

HERE CODE :

// GenericStack.java

import java.util.*;

public class GenericStack<T> {
  
/*
* Just like in the ArrayList lab, copy your StringStack code, excluding the
* main method, here.
* Then, modify it so it's generic!
*/
  
// convert the array to generic type T
private T [] stack;
public int size, top;

// parameterized constructor
public GenericStack(int size) {
this.top=-1;
this.size=size;
stack= (T[])new Object[size];
  
}
  
// default constructor
public GenericStack() {
this.size=10;
this.top=-1;
stack= (T[])new Object[this.size];
  
}
  
// returns true if stack is empty else return false
public boolean empty() {
return this.top == -1;
}
  
// return the top element of stack
public T peek() {
if (this.top==-1) {
throw new NoSuchElementException("Empty stack!");
}
else {
return this.stack[this.top];
}
  
}
  
// remove and return the top element of stack
public T pop() {
if (this.top==-1) {
throw new NoSuchElementException("Empty stack!");
}
else {
return this.stack[this.top--];
}
}
  
// insert s at top of stack
public String push(T s) {
  
if (this.top>=this.size-1) {
throw new IllegalStateException("Stack is full!");
}
  
this.stack[++this.top]= s;
return this.stack.toString();
}
  
public int search(String s) {
int dist = 0;
int top = this.top;
  
while(top>=0) {
if(this.stack[top]==s) {
return dist;
}
dist++;
top--;
}
return -1;
}
  
public static void main(String[] args) {

// If any of these lines cause a compilation error, your stack hasn't
// been properly made generic.
GenericStack intStack = new GenericStack<>();
GenericStack stringStack = new GenericStack<>();
GenericStack listStack = new GenericStack<>();
  
  
}
  
}

//end of GenericStack.java

// Calculator.java

import java.util.*;
import java.io.*;
import java.math.*;

public class Calculator {
  
public static void main(String[] args) throws FileNotFoundException, IOException {
  
String numbers = "56 3 7 2.0 8.4";
char[] moreNumbers = "1.0 2 3 4 5 0.324".toCharArray();

GenericStack stack1 = makeStack(new StringReader(numbers));
System.out.println(evaluate(stack1));

GenericStack stack2 = new GenericStack<>();
for (String token : numbers.split(" ")) {
stack2.push(Float.parseFloat(token));
}
System.out.println(evaluate(stack2));

// Uncomment the below line to execute it (since I don't have the file have commented it)
//GenericStack stack3 = makeStack(new FileReader("numbers.txt"));
//System.out.println(evaluate(stack3));

GenericStack stack4 = makeStack(new CharArrayReader(moreNumbers));
System.out.println(evaluate(stack4));
  
}
  
/* This function is meant to take in a Reader called "reader" and return a
* stack of Numbers.
*
* Remember: don't change anything that's already there. Just add your new
* code where the comment is to fix the function's signature.
*/
public static GenericStack makeStack(Reader reader) throws IOException {
GenericStack stack = new GenericStack<>(64);
char[] data = new char[64];
reader.read(data);
String tokens = new String(data);
  
for (String token : tokens.split(" ")) {
stack.push(parse(token));
}
return stack;
}
  
/* This function is meant to take in a stack of ANY KIND of Number
* called "stack", and return the double you get if you add all of the
* numbers together.
* The function must be able to accept a stack of ANY KIND of Number!
*
* Hint: use wildcard generics!
*/
public static double evaluate(GenericStack<? extends Number> stack) {
double total = 0; // set total to 0
// loop over the stack, adding the numbers to total
while(!stack.empty())
{
total += ((Number)stack.peek()).doubleValue(); // get the top element of stack
stack.pop(); // remove the top element
}
  
return total;
}
  
/* This function is missing a return type.
* Examine it, and see if you can tell what type it should return.
* (Spoiler: it's probably what you think it is.)
*/
public static Number parse(String s) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) { }

try {
return Double.parseDouble(s);
} catch (NumberFormatException e) { }

return new BigDecimal(s);
}
}

//end of Calculator.java

OUTPUT:

NOTE : PLEASE UPVOTE ITS VERY MUCH NECESSARY FOR MR A LOT. PLZZZZ..........


Related Solutions

Generic Stack due 10/16/2020 Friday 12 pm Mountain Time Generic Stack Make It Generic! The first...
Generic Stack due 10/16/2020 Friday 12 pm Mountain Time Generic Stack Make It Generic! The first part of this lab is making your stack generic. Take the code from your working StringStack class - everything except the main method - and paste it into the GenericStack class. Change the name of the constructors to match the new name of the class, then modify the whole class so it uses generics, and can store any type that a programmer asks for....
Please give Java Code ASAP Matrix Multiplication Due Friday 30th October 2020 by 23:55. (2 marks)...
Please give Java Code ASAP Matrix Multiplication Due Friday 30th October 2020 by 23:55. For this exercise, you are to find the optimal order for multiplying a sequence of matrices. Note: you do not actually have to perform any matrix multiplications. As usual, your program will prompt for the name of an input file and the read and process the data contained in this file. The file contains the following data. N, the number of matrices to be multiplied together...
A company issues 800,000 of 10% bonds dated 6/1/16 which are due 6/1/26. interest is paid...
A company issues 800,000 of 10% bonds dated 6/1/16 which are due 6/1/26. interest is paid annually at 6/1. the market rate for the bond is 9.75% on 5/1/19 the company retired all of these bonds for 802,000 plus accrued interest. prepare journal entries from 2016 to 2019
My visual stuido class IntegerSet Assignment Due: 1:00p, Wednesday, 10/28/2020 Create a class to represent a...
My visual stuido class IntegerSet Assignment Due: 1:00p, Wednesday, 10/28/2020 Create a class to represent a Set of Integers called IntegerSet, to hold a integers in the range 0 <= x <= 100. Recall that a set cannot contain duplicates. Thus the set will be represented as an array of bool elements. If array element a[i] is true, then the set contains integer i. If array element a[j] is false, then the set does not contain integer j. Provide the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT