Project: import java.util.Scanner; public class Project4 { /** * @param args the
ID: 3577778 • Letter: P
Question
Project:
import java.util.Scanner;
public class Project4
{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int userSelection;
Car grabbed;
Tree<Car> inventory = new Tree<Car>();
Scanner input = new Scanner(System.in);
Place frontFoyer = new Place("Front Foyer");
Place backFoyer = new Place("Back Foyer");
Place livingRoom = new Place("Living Room");
Place diningRoom = new Place("Dining Room");
Place kitchen = new Place("Kitchen");
Place pantry = new Place("Pantry");
Place study = new Place("Study");
frontFoyer.addNeighbor(diningRoom);
diningRoom.addNeighbor(frontFoyer);
frontFoyer.addNeighbor(livingRoom);
livingRoom.addNeighbor(frontFoyer);
frontFoyer.addNeighbor(backFoyer);
backFoyer.addNeighbor(frontFoyer);
kitchen.addNeighbor(pantry);
pantry.addNeighbor(kitchen);
pantry.addNeighbor(diningRoom);
diningRoom.addNeighbor(pantry);
kitchen.addNeighbor(backFoyer);
backFoyer.addNeighbor(kitchen);
backFoyer.addNeighbor(study);
study.addNeighbor(backFoyer);
diningRoom.addItem(new Car(1965,"Shelby Mustang GT 350"));
diningRoom.addItem(new Car(2017,"Zoom Zoom"));
livingRoom.addItem(new Car(1910,"Model T"));
pantry.addItem(new Car(2016,"Honda Accord"));
pantry.addItem(new Car(2016,"Toyota Camry"));
pantry.addItem(new Car(2017,"Dodge Asp"));
kitchen.addItem(new Car(1974,"Pontiac LeMans"));
kitchen.addItem(new Car(2016,"Kia Lemon"));
study.addItem(new Car(1958,"Ford Edsel"));
System.out.println("Graph Vertices are:");
displayLocationAndNeighbors(frontFoyer, "FF:");
displayLocationAndNeighbors(backFoyer, "BF:");
displayLocationAndNeighbors(livingRoom, "LR:");
displayLocationAndNeighbors(diningRoom, "DR:");
displayLocationAndNeighbors(pantry, "Pan:");
displayLocationAndNeighbors(kitchen, "Kit:");
displayLocationAndNeighbors(study, "Stu:");
Place currentLocation = frontFoyer;
do
{
displayLocationAndNeighbors(currentLocation, " You are currently at vertex "+ currentLocation );
System.out.print(" Inventory: ");
inventory.printTree();
System.out.print(" Where would you like to go? (-2 to pick up item / -1 to exit): ");
userSelection = input.nextInt();
if (userSelection == -1)
System.out.println("Bye bye");
else if(userSelection == -2)
{
grabbed = currentLocation.grabItem();
if(grabbed != null)
{
System.out.println("You Grabbed: " + grabbed);
inventory.add(grabbed);
}
else
System.out.print(" There is nothing in here ");
}
else if(userSelection < 0 || userSelection >= currentLocation.neighbors().length)
{
System.out.println(" " + userSelection + " is not a valid value ");
}
else
{
currentLocation = currentLocation.neighbors()[ userSelection ];
}
} while (userSelection != -1);
}
public static void displayLocationAndNeighbors(Place locationToDisplay, String header)
{
System.out.print(header);
if (locationToDisplay.neighbors().length == 0)
System.out.println(locationToDisplay + " has no Neighbors");
else
{
// display this place's neighbors
System.out.println("...neighbors of " + locationToDisplay + " are: ");
for (int i = 0; i < + locationToDisplay.neighbors().length; i++)
{
System.out.println(" " + i + " - " + locationToDisplay.neighbors()[i]);
}
}
System.out.println("Item Here: " + locationToDisplay.peekItem());
}
}
Driver:
public class Place implements Comparable<Place>
{
private String description;
private LinkedBag<Place> neighborsLinkedList;
private LinkedBag<Car> itemsHere = new LinkedBag<Car>();
private boolean[ ][ ] edges;
public Place (String newDescription)
{
description = newDescription;
neighborsLinkedList = new LinkedBag<Place>();
}
public String getDescription()
{
return description;
}
public void addItem(Car obj)
{
itemsHere.add(obj);
}
public void addEdge(int source, int target)
{
edges[source][target] = true;
}
public boolean isEdge(int source, int target)
{
return edges[source][target];
}
public Car grabItem()
{
return itemsHere.grab();
}
public String peekItem()
{
return itemsHere.toString();
}
public void addNeighbor(Place otherPlace)
{
neighborsLinkedList.add(otherPlace);
}
public Place[] neighbors()
{
int i = 0;
Place[ ] answer = new Place [neighborsLinkedList.getSize()];
if (neighborsLinkedList.getSize()!= 0)
{
for (Node<Place> cursor = neighborsLinkedList.getHeadOfList();
cursor!= null; cursor = cursor.getLink() )
{
answer[i] = cursor.getData();
i++;
}
}
return answer;
}
public String toString()
{
return "<" + description + ">";
}
public int compareTo(Place anotherPlace)
throws ClassCastException
{
return description.compareToIgnoreCase(anotherPlace.getDescription());
}
}
Node:
/**
* @param <E>
*/
public class Node<E>
{
private E data; // decalere the E data
private Node<E> link; // decalere and link points to a Node of the same class
/**
* Constructor initialize the the E data and the Node<E> link
* @param initialData
* @param initialLink
*/
public Node(E initialData, Node<E> initialLink)
{
data = initialData;
link = initialLink;
}
/**
* SetData method set the initial E data
* @param initialData
*/
public void setData(E initialData)
{
data = initialData;
}
/**
* SetLink method set the initial Node<E> link
* @param initialLink
*/
public void setLink(Node<E> initialLink)
{
link = initialLink;
}
/**
* getData method Get the data of the E data
* @return data
*/
public E getData()
{
return data;
}
/**
* getLink method
* @return data
*/
public Node<E> getLink()
{
return link;
}
}
LinkedBag:
public class LinkedBag<E extends Comparable<E>>
{
private Node<E> head; // decalere the StringNode head
private int numElements; // decalere the int numElements
/**constructor
* No-arg Constructor
*/
public LinkedBag()
{
head = null;
numElements = 0;
}
/**
* getSize method gets the size of the int
* @return numElements
*/
public int getSize()
{
return numElements;
}
/**
* The Add method receives a E object to add and return nothing.
* @param element
*/
public void add(E element)
{
boolean placed = false;
Node<E> cursor = head, previous = null;
if(head == null)
head = new Node<E>(element, head);
else
{
while(cursor!= null && !placed)
{
if( element.compareTo(cursor.getData()) <= 0)
{
if (previous == null)
head = new Node<E> (element, head);
else
{
previous.setLink(new Node<E>(element, previous.getLink()));
}
placed = true;
}
else
{
previous = cursor;
cursor = cursor.getLink();
}
}
if(!placed && previous !=null)
{
previous.setLink(new Node<E>(element, null));
}
}
numElements++;
}
/**
* The countOccurence method receives a E object and returns number
* of elements that match
* @param target
* @return numOccurrences
*/
public int countOccurrences(E target)
{
Node<E> cursor;
int numOccurrences = 0;
for(cursor = head; cursor!= null; cursor = cursor.getLink())
if(cursor.getData().equals(target))
numOccurrences++;
return numOccurrences;
}
/**
* exist method Compares if the E object target is found. If found returns found.
* @param target
* @return found
*/
public boolean exists(E target)
{
boolean found = false;
Node<E> cursor = head;
while (cursor!= null && !found)
{
if(cursor.getData().equals(target))
found = true;
else
cursor = cursor.getLink();
}
return found;
}
/**
* remove method Receives E object and returns found if
* the the target is removed, false otherwise.
* @param target
* @return found
*/
public boolean remove(E target)
{
Node<E> cursor = head, previous = null;
boolean found = false;
while (cursor!= null && !found)
{
if(cursor.getData().equals(target))
found = true;
else
{
previous = cursor;
cursor = cursor.getLink();
}
}
if(found && cursor != null)
{
if(previous == null)
head = head.getLink();
else
previous.setLink(cursor.getLink());
numElements --;
}
return found;
}
/**
* toString method toString method display the content of the E object
* @return stringToReturn
*/
public String toString()
{
String stringToReturn = "";
Node<E> cursor;
if (numElements== 0)
stringToReturn= "empty";
else
for(cursor = head; cursor!= null; cursor = cursor.getLink())
{
stringToReturn = stringToReturn + cursor.getData();
if(cursor.getLink() != null)
stringToReturn = stringToReturn + ", ";
}
return stringToReturn;
}
/**
* The grab method looks for special items in the bag.
* @return returns.getData() the specific data from the bag.
*/
public E grab()
{
Random r = new Random();
E stringToReturn;
Node<E> cursor;
int count, randomNodeNum;
if(numElements == 0)
stringToReturn = null;
else
{
cursor = head;
randomNodeNum = r.nextInt(numElements);
for(count = 0; count < randomNodeNum; count++)
cursor = cursor.getLink();
stringToReturn = cursor.getData();
if (!remove((E) stringToReturn))
{
stringToReturn= null;
}
}
return stringToReturn;
}
public Node<E> getHeadOfList()
{
return head;
}
}
Car:
public class Car implements Comparable<Car>
{
private int yearModel;
private String make;
private int speed;
/**
Constructor`
@param newYearModel The year of the Car.
@param newMake The make of the Car.
*/
public Car(int newYearModel, String newMake)
{
yearModel = newYearModel;
make = newMake;
speed = 0;
}
/**
The getYearModel method returns a Car
object's yearModel.
@return The value in the yearModel field.
*/
public int getYearModel()
{
return yearModel;
}
/**
The getMake method returns a Car
object's make.
@return The value in the make field.
*/
public String getMake()
{
return make;
}
/**
The getSpeed method returns a Car
object's speed.
@return The value in the speed field.
*/
public int getSpeed()
{
return speed;
}
/**
The accelerate method increases speed.
*/
public void accelerate()
{
// i am ommitting a regulator
speed += 5;
}
/**
The brake method decreases speed.
*/
public void brake()
{
speed-=5;
if (speed < 0)
speed = 0;
}
/**
The equals method compares this Car to another Object .
@param The object to test for equality.
@return boolean with result of test for equality.
*/
public boolean equals(Object obj)
{
if (!(obj instanceof Car))
throw new ClassCastException("A Car object expected.");
Car otherCar = (Car) obj; // cast the Object to a Car
if (make.equalsIgnoreCase(otherCar.getMake())
&& yearModel == otherCar.getYearModel())
return true;
else
return false;
}
/**
The compareTo method compares this Car to another Object .
if this car is before other car, return a value < 0.
if this car is after other car, return a value > 0.
if this car is equal to other car, return 0.
@param The object to test for equality.
@return int with result of comparison.
*/
public int compareTo(Car anotherCar)
throws ClassCastException
{
if (!(anotherCar instanceof Car))
throw new ClassCastException("A Car object expected.");
if (getYearModel() < anotherCar.getYearModel())
return -1;
else if (getYearModel() > anotherCar.getYearModel())
return 1;
else
return make.compareToIgnoreCase(anotherCar.getMake());
}
/**
The toString method returns a Car
object's yearModel, make, speed as a String.
@return A String with values in the yearModel, make, speed fields.
*/
public String toString()
{
return "[Car " + yearModel +" "+ make+"]";
}
}
Tree:
public class Tree<E extends Comparable<E>>
{
private BTNode<E> root;
private int numItems;
/**
* Constructor Initialize the an null tree
*/
public Tree()
{
root = null;
numItems = 0;
}
/**
* add method Add new element
* @param element
*/
public void add( E element )
{
if(root == null)
root = new BTNode<E>(element, null, null);
else
{
BTNode<E> cursor = root;
boolean done = false;
while (!done)
{
if(element.compareTo(cursor.getData()) <= 0)
{
if(cursor.getLeft() == null)
{
BTNode<E> n = new BTNode<E>(element, null, null);
cursor.setLeft(n);
done = true;
}
else
cursor = cursor.getLeft();
}
else
{
if(cursor.getRight() == null)
{
BTNode<E> n = new BTNode<E>(element, null, null);
cursor.setRight(n);
done = true;
}
else
cursor = cursor.getRight();
}
}
}
numItems++;
}
/**
* remove method Remove one copy of a specified element from this bag.
* @param element
* @return found
*/
public boolean remove(E element)
{
// part 1 loop
boolean found = false;
BTNode<E> cursor = root;
BTNode<E> parent = null;
while(cursor!= null && !found)
{
if (element.equals(cursor.getData()))
{
found = true;
numItems--;
}
else if(element.compareTo(cursor.getData())<= 0)
{
parent = cursor;
cursor = cursor.getLeft();
}
else
{
parent = cursor;
cursor = cursor.getRight();
}
}
if(cursor == null)
found = false;
else if(cursor == root && cursor.getLeft() == null)
root = root.getRight();
else
{
if(cursor!= root && cursor.getLeft() == null)
if(element.compareTo(parent.getData())<= 0)
parent.setLeft(cursor.getRight());
else
parent.setRight(cursor.getRight());
else
{
cursor.setData(cursor.getLeft().getRightmostData());
cursor.setLeft(cursor.getLeft().removeRightMost());
}
}
return found;
}
/**
* size method determine the number of elements in the tree
* @return numItems
*/
public int size()
{
return numItems;
}
/**
* printTree method calls the in order print Method.
*/
public void printTree()
{
if(root == null)
System.out.println("Empty");
else
root.inOrderPrint();
}
}
BTNode:
public class BTNode<E>
{
private E data; // Declare E data
private BTNode<E> left; // Reference to the left child
private BTNode<E> right; // Reference to the right child
/**
* Constructor Initialize a node with a specified initial data and links to children.
* @param newData
* @param newLeft
* @param newRight
*/
public BTNode(E newData, BTNode<E> newLeft, BTNode<E> newRight)
{
data = newData;
left = newLeft;
right = newRight;
}
/**
* getData method, Get the data
* @return data
*/
public E getData()
{
return data;
}
/**
* getLeft method; Get the left child
* @return left
*/
public BTNode<E> getLeft()
{
return left;
}
/**
* getLeft method; Get the right child
* @return right
*/
public BTNode<E> getRight()
{
return right;
}
/**
* getRightmostdata method; Get the data
* from the leftmost node of the tree
* @return right or left return getRightmostData
*/
public E getRightmostData()
{
if (right == null)
return data;
else
return right.getRightmostData();
}
/**
* inOrderPrint method print the data
*/
public void inOrderPrint()
{
if (left != null)
left.inOrderPrint( );
System.out.println(data);
if (right != null)
right.inOrderPrint( );
}
/**
* removeRightMost method; Remove the rightMost node of
* the tree with this node as its root.
* @return this
*/
public BTNode<E> removeRightMost()
{
if (right == null)
{ // The leftmost node is at the root because there is no left child.
return left;
}
else
{ // A recursive call removes the leftmost node from my own left child.
right = right.removeRightMost( );
return this;
}
}
/**
* SetData method set the new E data
* @param newData
*/
public void setData(E newData)
{
data = newData;
}
/**
* SetLeft method set the new Left BTNode E
* @param newLeft
*/
public void setLeft (BTNode<E> newLeft)
{
left = newLeft;
}
/**
* SetLeft method set the new Right BTNode E
* @param newRight
*/
public void setRight (BTNode<E> newRight)
{
right = newRight;
}
}
I need help with first three points. Thank you.
Optional Extra Credit 5 points): when user picks u a specific item of Yourclass, an edge is establish o Example imagine if there is not an edge from back foyer to study, but if the Bugatti is picked up, then the edge from back foyer to study is established. optional Extra Credit(+10 points for grab method in Tree +5 points for logic in driver): after some condition, user can lose an item from their inventory Optional: user can put an item from inventory back into the current room (+5 for logic in driver +10 for grab method in Tree) itional Fun Exit conditions... no extra credit -just fun Game "ends" when some condition is met o example: if you enter into the back foyer without a model T in your inventory you lose or die game over! o example: if you enter into the study with a model T in your inventory you 'win the game' game over o A random number is generated when entering the pantry and if it is the unlucky number, the user suffers some consequence (death, loss, lawsuit) and the game end mple Run:Explanation / Answer
import java.util.Scanner;
public class Project4
{
* @param args the command line arguments
*
public static void main(String[ ] args) {
int userSelection;
Car grabbed;
Tree<Car> inventory = new Tree<Car>();
Scanner input = new Scanner(System.in);
Place frontFoyer = new Place("Front Foyer");
Place backFoyer = new Place("Back Foyer");
Place livingRoom = new Place("Living Room");
Place diningRoom = new Place("Dining Room");
Place kitchen = new Place("Kitchen");
Place pantry = new Place("Pantry");
Place study = new Place("Study");
frontFoyer.addNeighbor(diningRoom);
diningRoom.addNeighbor(frontFoyer);
frontFoyer.addNeighbor(livingRoom);
livingRoom.addNeighbor(frontFoyer);
frontFoyer.addNeighbor(backFoyer);
backFoyer.addNeighbor(frontFoyer);
kitchen.addNeighbor(pantry);
pantry.addNeighbor(kitchen);
pantry.addNeighbor(diningRoom);
diningRoom.addNeighbor(pantry);
kitchen.addNeighbor(backFoyer);
backFoyer.addNeighbor(kitchen);
backFoyer.addNeighbor(study);
study.addNeighbor(backFoyer);
diningRoom.addItem(new Car(1965,"Shelby Mustang GT 350"));
diningRoom.addItem(new Car(2017,"Zoom Zoom"));
livingRoom.addItem(new Car(1910,"Model T"));
pantry.addItem(new Car(2016,"Honda Accord"));
pantry.addItem(new Car(2016,"Toyota Camry"));
pantry.addItem(new Car(2017,"Dodge Asp"));
kitchen.addItem(new Car(1974,"Pontiac LeMans"));
kitchen.addItem(new Car(2016,"Kia Lemon"));
study.addItem(new Car(1958,"Ford Edsel"));
System.out.println("Graph Vertices are:");
displayLocationAndNeighbors(frontFoyer, "FF:");
displayLocationAndNeighbors(backFoyer, "BF:");
displayLocationAndNeighbors(livingRoom, "LR:");
displayLocationAndNeighbors(diningRoom, "DR:");
displayLocationAndNeighbors(pantry, "Pan:");
displayLocationAndNeighbors(kitchen, "Kit:");
displayLocationAndNeighbors(study, "Stu:");
Place currentLocation = frontFoyer;
do
{
displayLocationAndNeighbors(currentLocation, " You are currently at vertex "+ currentLocation );
System.out.print(" Inventory: ");
inventory.printTree();
System.out.print(" Where would you like to go? (-2 to pick up item / -1 to exit): ");
userSelection = input.nextInt();
if (userSelection == -1)
System.out.println("Bye bye");
else if(userSelection == -2)
{
grabbed = currentLocation.grabItem();
if(grabbed != null)
{
System.out.println("You Grabbed: " + grabbed);
inventory.add(grabbed);
}
else
System.out.print(" There is nothing in here ");
}
else if(userSelection < 0 || userSelection >= currentLocation.neighbors().length)
{
System.out.println(" " + userSelection + " is not a valid value ");
}
else
{
currentLocation = currentLocation.neighbors()[ userSelection ];
}
} while (userSelection != -1);
}
public static void displayLocationAndNeighbors(Place locationToDisplay, String header)
{
System.out.print(header);
if (locationToDisplay.neighbors().length == 0)
System.out.println(locationToDisplay + " has no Neighbors");
else
{
// display this place's neighbors
System.out.println("...neighbors of " + locationToDisplay + " are: ");
for (int i = 0; i < + locationToDisplay.neighbors().length; i++)
{
System.out.println(" " + i + " - " + locationToDisplay.neighbors()[i]);
}
}
System.out.println("Item Here: " + locationToDisplay.peekItem());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.