Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with the Java Person class. Completed Car class is shown below the Per

ID: 3598130 • Letter: N

Question

Need help with the Java Person class. Completed Car class is shown below the Person class.....Thanks.

import java.util.*;

class Person {

private String name;

private Car currentCar;

public Person(String name, Car currentCar) {

this.name = name;

this.currentCar = currentCar;

}

public String getName() {

//returns the name set in the constructor

//O(1)

return name;

}

public Car getCurrentCar() {

//returns the current car

//O(1)

return currentCar;

}

public boolean moveToCar(Car c) {

//Tries to move the person from their current car

//to the car passed in as a parameter. If the two

//cars are not adjacent, returns false (and the

//person remains in their current car). Returns

//true if the person was able to move.

//O(1)

return false;

}

public boolean equals(Object o) {

//two people are "equal" if they have the same name

//O(1)

return false;

}

public int hashCode() {

//returns a hashcode for a person...

//remember: if two objects are equal, they should

//have the same hashcode

//O(1)

return 0;

}

public String toString() {

//returns the person's name

//O(1)

return null;

}

//example test code... edit this as much as you want!

public static void main(String[] args) {

Car c1 = new Car("C1");

Car c2 = new Car("C2");

Car c3 = new Car("C3");

c1.setNext(c2);

c2.setPrevious(c1);

Person p1 = new Person("P1", c1);

if(p1.getCurrentCar().equals(c1) && p1.getName().equals("P1")) {

System.out.println("Yay 1");

}

if(p1.moveToCar(c2) && p1.getCurrentCar().equals(c2) && p1.moveToCar(c1) && p1.getCurrentCar().equals(c1)) {

System.out.println("Yay 2");

}

Person p1b = new Person("P1", c1);

if(p1.equals(p1b) && p1.hashCode() == p1b.hashCode()) {

System.out.println("Yay 3");

}

}

}

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

This is my completed Car.java class

class Car {

private String name;

private Car next;

private Car previous;

public Car(String name) {

this.name = name;

}

public Car getNext() {

//returns the next car after this one

//O(1)

return next;

}

public Car getPrevious() {

//returns the car before this one

//O(1)

return previous;

}

public void setNext(Car next) {

//sets the car after this one to next (the parameter)

//O(1)

this.next = next;

}

public void setPrevious(Car previous) {

//sets the car before this one to previous (the parameter)

//O(1)

this.previous = previous;

}

public String getName() {

//return's the car's name

//O(1)

return name;

}

public boolean equals(Object o) {

//two cars are equal if they have the same name

//O(1)

if(o instanceof Car)

{

return name.equals(((Car)o).name);

}

else

return false;

}

public int hashCode() {

//returns a hashcode for a car...

//remember: if two objects are equal, they should

//have the same hashcode

//O(1)

return name.hashCode();

}

public String toString() {

//return's the car's name

//O(1)

return name;

}

//example test code... edit this as much as you want!

public static void main(String[] args) {

Car c1 = new Car("C1");

Car c2 = new Car("C2");

c1.setNext(c2);

c2.setPrevious(c1);

if(c1.getName().equals("C1")) {

System.out.println("Yay 1");

}

if(c1.getNext().equals(c2) && c2.getPrevious().equals(c1)) {

System.out.println("Yay 2");

}

Car c1b = new Car("C1");

if(c1.equals(c1b) && c1.hashCode() == c1b.hashCode()) {

System.out.println("Yay 3");

}

c1.printAscii();

}

/*****************************************************************/

/****************** DO NOT EDIT BELOW THIS LINE ******************/

/*****************************************************************/

public void printAscii() {

/*

From: http://www.ascii-art.de/ascii/t/train.txt

_________

|O O O O|

-|_______|

o o

*/

Car current = this;

while(current != null) {

System.out.print(" _________");

current = current.getNext();

}

System.out.println();

current = this;

while(current != null) {

System.out.print(" | "+String.format("%-5s",current.getName())+" |");

current = current.getNext();

}

System.out.println();

current = this;

while(current != null) {

System.out.print("-|_______|");

current = current.getNext();

}

System.out.println();

current = this;

while(current != null) {

System.out.print(" o o ");

current = current.getNext();

}

System.out.println();

}

}

Explanation / Answer

========

public class Person {
   private String name;
   private Car currentCar;

   public Person(String name, Car currentCar) {
       this.name = name;
       this.currentCar = currentCar;
   }

   public String getName() {
       // returns the name set in the constructor //O(1)
       return name;
   }

   public Car getCurrentCar() {
       // returns the current car //O(1)
       return currentCar;
   }

   public boolean moveToCar(Car c) {
       // Tries to move the person from their current car
       // to the car passed in as a parameter. If the two
       // cars are not adjacent, returns false (and the
       // person remains in their current car). Returns //true if the person was able
       // to move. //O(1)
       Car prev = this.currentCar.getNext();
       Car next = this.currentCar.getPrevious();
       if ((prev != null && prev.equals(c)) || (next != null && next.equals(c))) {
           /* if (this.getCurrentCar().getNext().getName().equals(c.getName())) { */
           this.currentCar = c;
           return true;
       }
       return false;
   }

   public boolean equals(Object o) {
       // two people are "equal" if they have the same name
       // O(1)
       if (this.getName().equals(((Person) o).getName())) {
           return true;
       }
       return false;
   }

   public int hashCode() {
       // returns a hashcode for a person...
       // remember: if two objects are equal, they
       // should //have the same hashcode //O(1)
       return this.currentCar.hashCode() + this.name.hashCode();
   }

   public String toString() { // returns the person's name //O(1)
       return this.getName();
   }

   // example test code... edit this as much as you want!
   public static void main(String[] args) {
       Car c1 = new Car("C1");
       Car c2 = new Car("C2");
       Car c3 = new Car("C3");
       c1.setNext(c2);
       c2.setPrevious(c1);
       Person p1 = new Person("P1", c1);
       if (p1.getCurrentCar().equals(c1) && p1.getName().equals("P1")) {
           System.out.println("Yay 1");
       }
       if (p1.moveToCar(c2) && p1.getCurrentCar().equals(c2) && p1.moveToCar(c1) && p1.getCurrentCar().equals(c1)) {
           System.out.println("Yay 2");
       }
       Person p1b = new Person("P1", c1);
       if (p1.equals(p1b) && p1.hashCode() == p1b.hashCode()) {
           System.out.println("Yay 3");
       }
   }
}

=======

class Car {
   private String name;
   private Car next;
   private Car previous;

   public Car(String name) {
       this.name = name;
   }

   public Car getNext() {
       // returns the next car after this one //O(1)
       return next;
   }

   public Car getPrevious() { // returns the car before this one //O(1)
       return previous;
   }

   public void setNext(Car next) { // sets the car after this one to next (the parameter) //O(1)
       this.next = next;
   }

   public void setPrevious(Car previous) { // sets the car before this one to previous (the parameter) //O(1)
       this.previous = previous;
   }

   public String getName() { // return's the car's name //O(1)
       return name;
   }

   public boolean equals(Object o) { // two cars are equal if they have the same name //O(1)
       if (o instanceof Car) {
           return name.equals(((Car) o).name);
       } else
           return false;
   }

   public int hashCode() { // returns a hashcode for a car... //remember: if two objects are equal, they
                           // should //have the same hashcode //O(1)
       return name.hashCode();
   }

   public String toString() { // return's the car's name //O(1)
       return name;
   } // example test code... edit this as much as you want!

   public static void main(String[] args) {
       Car c1 = new Car("C1");
       Car c2 = new Car("C2");
       c1.setNext(c2);
       c2.setPrevious(c1);
       if (c1.getName().equals("C1")) {
           System.out.println("Yay 1");
       }
       if (c1.getNext().equals(c2) && c2.getPrevious().equals(c1)) {
           System.out.println("Yay 2");
       }
       Car c1b = new Car("C1");
       if (c1.equals(c1b) && c1.hashCode() == c1b.hashCode()) {
           System.out.println("Yay 3");
       }
       c1.printAscii();
   }

   /*****************************************************************/
   /****************** DO NOT EDIT BELOW THIS LINE ******************/
   /*****************************************************************/
   public void printAscii() { /*
                               * From: http://www.ascii-art.de/ascii/t/train.txt _________ |O O O O|
                               * -|_______| o o
                               */
       Car current = this;
       while (current != null) {
           System.out.print(" _________");
           current = current.getNext();
       }
       System.out.println();
       current = this;
       while (current != null) {
           System.out.print(" | " + String.format("%-5s", current.getName()) + " |");
           current = current.getNext();
       }
       System.out.println();
       current = this;
       while (current != null) {
           System.out.print("-|_______|");
           current = current.getNext();
       }
       System.out.println();
       current = this;
       while (current != null) {
           System.out.print(" o o ");
           current = current.getNext();
       }
       System.out.println();
   }
}


=====

OUTPUT:-

=========

Yay 1
Yay 2
Yay 3

=========

Thanks

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote