1.The Java library contains an interface called Comparable that is defined as fo
ID: 3665820 • Letter: 1
Question
1.The Java library contains an interface called Comparable that is defined as follows: public interface Comparable { intcompareTo(T o); } The variable T is called a generic type and can represent any object type (e.g., String, Cat, whatever). The only thing that it can’t represent is primitive datatypes. This is exactly the same concept as we saw when we studied the ArrayList class. The point of this interface is to define a uniform way of designing classes in which any two object can be compared (i.e. for any two objects x and y of the class, either x is less than y, x is greater than y, or x is equal to y). A class should implement this method so that whenever one calls x.compareTo(y)for objects x and y, the method returns a negative integer if x is less than y, zero if x is equal to y, and a positive integer is x is greater than y. Given an array of typeT (or an ArrayList), calling Arrays.sort(T[] array) (or respectively Collections.sort(ArrayList list) for an ArrayList) will invoke the compareTo method of the underlying class type to compare elements of the array/ArrayList pairwise in order to perform the sorting. You can check out Oracle’s documentation on this interface at https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html#compareTo-T- for more information. For this exercise, I want you to take the Bug class from HW1 and modify it so that it implements the Comparable interface. The compareTo method should compare two bugs Bug1 and Bug2 as follows: Bug1 is considered larger than Bug2 if either (1) Bug1’s Manhattan distance from the origin, i.e. |xpos| + |ypos| where |.| is the absolute value operation, is larger than Bug2’s Manhattan distance from the origin, OR (2) Bug1’s Manhattan distance from the origin is equal to Bug2’s Manhattan distance from the origin and Bug1’s direction is closer to North than Bug2’s direction moving counter-clockwise (i.e., North > East > South > West). Bug1 and Bug2 are considered the same if their Manhattan distances from the origin are equal and their directions are the same. (Note that this does not necessarily imply that Bug1 and Bug2 are in the same exact position. For example, a Bug at (xpos = 1, ypos = 1, dir = North) and a Bug at (xpos = -1, ypos = 1, dir = North) are considered equivalent according to the metric above. a. Rewrite your Bug class from Homework 1 so that it implements the Comparable interface as described above. b. Write a tester class with a main method that initializes an ArrayList of Bug objects with randomly selected positions and directions. Then use the Collections.sort method (described here: http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html) to sort this ArrayList. Use enhanced for loops to print out the original ArrayList and the sorted ArrayList.
previous program code:
import java.lang.*;
import java.io.*;
import java.util.*;
class Bug
{
private int curIntX;
private int curIntY;
private String bugDirection;
/*constructor*/
public Bug(int intX,int intY)
{
curIntX=intX;
curIntY=intY;
bugDirection="North";
}
/*TURN 90 DEGREE RIGHT*/
public void turnRight()
{
if(bugDirection.equals("North"))
{
bugDirection="East";
}
else if(bugDirection.equals("East"))
{
bugDirection="South";
}
else if(bugDirection.equals("South"))
{
bugDirection="West";
}
else if(bugDirection.equals("West"))
{
bugDirection="North";
}
}
/*TURN 90 DEGREE LEFT*/
public void turnLeft()
{
if(bugDirection.equals("North"))
{
bugDirection="West";
}
else if(bugDirection.equals("West"))
{
bugDirection="South";
}
else if(bugDirection.equals("South"))
{
bugDirection="East";
}
else if(bugDirection.equals("East"))
{
bugDirection="North";
}
}
/*MOVE 1 UNIT FROM CURRENT POSITION*/
public void move()
{
curIntX=curIntX+1;
curIntY=curIntY+1;
}
public int getPositionX() // get X position
{
return curIntX;
}
public int getPositionY() // get Y position
{
return curIntY;
}
public String getDirection()
{
return bugDirection;
}
/*OVERRIDDEN toString*/
public String toString()
{
/*RETURN STRING*/
return "Bug Position:("+ getPositionX() +","+getPositionY()+"), it is currently facing:"+bugDirection;
}
}
public class BUGDriver
{
public static void main(String[] args)
{
/* CREATING NEW BUG*/
Bug b=new Bug(10,20);
System.out.println("My Bug: "+b);
b.move();
b.turnLeft(); //TURN LEFT
b.turnLeft();
b.turnRight();//TURN RIGHT
b.move();
System.out.println("My Bug: "+b);
}}
Explanation / Answer
Bug.java
import java.lang.*;
import java.io.*;
import java.util.*;
class Bug implements Comparable<Bug>
{
private int curIntX;
private int curIntY;
private String bugDirection;
/*constructor*/
public Bug(int intX,int intY)
{
curIntX=intX;
curIntY=intY;
bugDirection="North";
}
/*TURN 90 DEGREE RIGHT*/
public void turnRight()
{
if(bugDirection.equals("North"))
{
bugDirection="East";
}
else if(bugDirection.equals("East"))
{
bugDirection="South";
}
else if(bugDirection.equals("South"))
{
bugDirection="West";
}
else if(bugDirection.equals("West"))
{
bugDirection="North";
}
}
/*TURN 90 DEGREE LEFT*/
public void turnLeft()
{
if(bugDirection.equals("North"))
{
bugDirection="West";
}
else if(bugDirection.equals("West"))
{
bugDirection="South";
}
else if(bugDirection.equals("South"))
{
bugDirection="East";
}
else if(bugDirection.equals("East"))
{
bugDirection="North";
}
}
/*MOVE 1 UNIT FROM CURRENT POSITION*/
public void move()
{
curIntX=curIntX+1;
curIntY=curIntY+1;
}
public int getPositionX() // get X position
{
return curIntX;
}
public int getPositionY() // get Y position
{
return curIntY;
}
public String getDirection()
{
return bugDirection;
}
/*OVERRIDDEN toString*/
public String toString()
{
/*RETURN STRING*/
return "Bug Position:("+ getPositionX() +","+getPositionY()+"), it is currently facing:"+bugDirection;
}
//implemeting the compareTo method for interface comparable
@Override
public int compareTo(Bug o) {
int manhattanDistBug1=Math.abs(this.getPositionX())+Math.abs(this.getPositionX());
int manhattanDistBug2=Math.abs(o.getPositionX())+Math.abs(o.getPositionX());
String directionBug1=this.getDirection();
String directionBug2=o.getDirection();
if(manhattanDistBug1>manhattanDistBug2)
{
return 1;
}
else if(manhattanDistBug1==manhattanDistBug2)
{
//Both the bugs are equal
if(directionBug1.equals(directionBug2))
{
return 0;
}
else if(directionBug1.equals("North"))
{
return 1;
}
else if(directionBug2.equals("East"))
{
if(directionBug2.equals("North"))
{
return -1;
}
else
{
return 1;
}
}
else if(directionBug1.equals("South"))
{
if(directionBug2.equals("West"))
{
return 1;
}
else
{
return -1;
}
}
else
{
return -1;
}
}
return -1;
}
}
BugDriver.java(for testing the bug class and sorting an arraylist of bugs and printing it)
import java.util.ArrayList;
import java.util.Collections;
public class BugDriver
{
public static void main(String[] args)
{
/* CREATING NEW BUG*/
Bug b1=new Bug(10,20);
Bug b2=new Bug(11,22);
Bug b3=new Bug(10,20);
Bug b4=new Bug(11,23);
Bug b5=new Bug(5,5);
b3.turnLeft(); //TURN LEFT
ArrayList<Bug> bugs=new ArrayList<Bug>();
bugs.add(b1);
bugs.add(b2);
bugs.add(b3);
bugs.add(b4);
bugs.add(b5);
Collections.sort(bugs);
for(int i=0;i<bugs.size();++i)
{
System.out.println(bugs.get(i).toString());
}
}}
Sample output:
Bug Position:(5,5), it is currently facing:North
Bug Position:(10,20), it is currently facing:West
Bug Position:(10,20), it is currently facing:North
Bug Position:(11,22), it is currently facing:North
Bug Position:(11,23), it is currently facing:North
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.