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 15:00
Which of the following statements tests if students have a grade of 70 or above, as well as fewer than five absences? a: if(grade > = 70 and daysabsent < = 5): b: if(grade > = 70 or daysabsent < = 5): c: if(grade > 70 and daysabsent < = 5): d: if(grade > 70 or daysabsent < = 5): i took the test the answer is a
Answers: 1
question
Computers and Technology, 22.06.2019 21:00
Describir textbook icon_person mira los dibujos y describe lo que está pasando. usa los verbos de la lista.
Answers: 1
question
Computers and Technology, 23.06.2019 10:50
Your friend kayla is starting her own business and asks you whether she should set it up as a p2p network or as a client-server network. list three questions you might ask to kayla decide which network to use and how her answers to those questions would affect your recommendation.
Answers: 2
question
Computers and Technology, 23.06.2019 13:30
Drag the tiles to the correct boxes to complete the pairs. match the errors with their definitions. #name #value #ref when a formula produces output that is too lengthy to fit in the spreadsheet cell arrowright when you enter an invalid cell reference in a formula arrowright when you type text in cells that accept numeric data arrowright when you type in a cell reference that doesn’t exist arrowright reset next
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
History, 16.07.2019 20:30
Questions on the website: 13722359