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, 22.06.2019 05:00
Pls do you believe that the use of 3d animation has grown in feature films over the last few years? if so, do you think the trend will continue? what are the forces driving this trend?
Answers: 2
question
Computers and Technology, 22.06.2019 11:00
How does a policy manual an organization? a. it boost productivity. b. it create awareness in employees about the organization’s values. c. it employees achieve targets. d. it safeguards the organization from liabilities.
Answers: 1
question
Computers and Technology, 23.06.2019 15:00
1. which of the following statements are true about routers and routing on the internet. choose two answers. a. protocols ensure that a single path between two computers is established before sending packets over it. b. routers are hierarchical and the "root" router is responsible for communicating to sub-routers the best paths for them to route internet traffic. c. a packet traveling between two computers on the internet may be rerouted many times along the way or even lost or "dropped". d. routers act independently and route packets as they see fit.
Answers: 2
question
Computers and Technology, 23.06.2019 23:30
The keyboard usually has six rows of keys. which of the following is not one of the key group categories? letter keys number keys control keys graphic keys
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, 05.02.2020 08:55
question
Mathematics, 05.02.2020 08:55
Questions on the website: 13722361