subject

Your task for this assignment is to re-write any programing assignment from the semester (again, pick a simple one) using a class from the Java Collections Framework, such as Queue, Stack, or LinkedList. This means you will use a JCF class in place of the class you wrote for the data structure. You might still need the class for the data object itself. For example, imagine that you wrote software for a stack of Car objects. You would still need the Car class, but instead of your Stack class you would instantiate your stack of Cars in the project class's main method using the JCF Stack class. You would also have to make sure that the method calls in your main method use the correct method names from the JCF Stack class, For example the method in the JFC Stack class to pop an element is:
pop()
which will remove and return the instance of the object from the top of the stack , so it would be used something like this:
if ( myStack. size() > 0 )
nextCar = myStack. pop();
else
// print an empty stack message
When you look at the documentation for a JCF class, be aware of inherited methods and methods specified in the Collection Interface, as well, since all JCF classes implement the Collection Interface. For example, size() is a method for all of the JCF classes.
Here is my code of "STACKS"
import java. util.*;
//stack class to define string array based push and pop methods
class Stack
{
int size;
int top;
String[] cities;
//function to check if stack is empty
boolean isEmpty()
{
return (top < 0);
}
Stack(int n)
{
top = -1;
size = n;
cities = new String[size];
}
//function to push element in Stack
boolean push(String x)
{
if (top >= size)
{
System. out. println("Stack Overflow");
return false;
}
else
{
cities[++top] = x;
return true;
}
}
//function to pop element from stack
String pop()
{
if (top < 0)
{
System. out. println("Stack Underflow");
return "0";
}
else
{
String x = cities[top--];
return x;
}
}
}
import java. util. Arrays;
//Driver code
class Driver
{
//function to reverse the string array implementing the stack class
public static void reverse(String cities[])
{
// Create a stack of capacity
// equal to length of cities array
int n = cities. length;
Stack obj = new Stack(n);
// Push all Strings into stack
for (int i = 0; i < n; i++)
obj. push(cities[i]);
//Pop all cities and put them back to str
for (int i = 0; i < n; i++)
{
String city = obj. pop();
cities[i]= city;
}
}
//driver function
public static void main(String args[])
{
//create the string array cities
String cities[] = { "Philadelphia, PA", "Harrisburg, PA", "Pittsburg, PA", "Cleveland, OH", "Toledo, OH", "Gary, IN", "Chicago, IL" };
//Printing the stack before reversing
System. out. println("Path from Philepedia to Chicago");
for(int i=0;i System. out. println(cities[i]);
//call reverse method
reverse(cities);
System. out. println();
//Printing the stack after reversing
System. out. println("Path from Chigago to Philipedia");
for(int i=0;i System. out. println(cities[i]);
}
}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 11:30
Me dangers of social media and the internetexplain what each means: 1) social media and phones have become an addiction.2) outside people have access to you all the time.3) cyberstalking4) cyberbullying5) catphishing6) viruses7) identity theft8) credit card fraud9) hacking10) money schemes
Answers: 1
question
Computers and Technology, 23.06.2019 17:00
The camera still is bad even with the new iphone xr and especially in low light it is even worst because you can see the pixels more if its in low light. if all you apple customers want apple to fix this then lets fill there feedback with complaints about the
Answers: 1
question
Computers and Technology, 24.06.2019 16:00
Your is an example of personal information that you should keep private.
Answers: 1
question
Computers and Technology, 24.06.2019 22:00
Is the process of organizing data to reduce redundancy. a. normalization b. primary keying c. specifying relationships d. duplication
Answers: 1
You know the right answer?
Your task for this assignment is to re-write any programing assignment from the semester (again, pic...
Questions
question
Chemistry, 13.07.2019 21:30
question
Mathematics, 13.07.2019 21:30
question
Mathematics, 13.07.2019 21:30
question
Mathematics, 13.07.2019 21:30
question
Mathematics, 13.07.2019 21:30
Questions on the website: 13722367