Question

This is my code that i need to finish. In BoxRegion.java I have no idea how...

This is my code that i need to finish.
In BoxRegion.java
I have no idea how to create the constructor.
I tried to use super(x,y) but It is hard to apply. 
And Also In BoxRegionHashTable,
I don't know how to create displayAnnotation 



BoxRegion.java
------------------------------------------------
public final class BoxRegion {
    final Point2D p1;
    final Point2D p2;

    /**
     * Create a new 3D point with given x, y and z values
     *
     * @param x1, y1 are the x,y coordinates for point
     *            defining a corner of the BoxRegion
     * @param x2, y2 are coordinates for the other corner
     */
    public BoxRegion(int x1, int y1, int x2, int y2) {
        // TODO: assign values to p1 and p2
       
    }

    /**
     * Return a string describing the 3D point
     */
    public String toString() {
        return "(" + p1.x + ", " + p1.y + "), (" + +p2.x + ", " + p2.y + ")";
    }


    /**
     * Override the default hashcode method
     */
    // TODO: write hashCode method
    int hashcode() {
        int hash = 17;
        hash = hash * 31 + p1.hashCode();
        hash = hash * 31 + p2.hashCode();
        return hash;

    }

    /**
     * Override the default equals method.
     * Two BoxRegion objects equal if the coordinates
     * of their opposing points are equal.
     */
    // TODO: Write equals method
    public boolean equals(Object obj) {
        if (obj instanceof Point2D == false){
            return false;
        }
        BoxRegion b = (BoxRegion) obj;
        if ( p1!= b.p1 || p2 != b.p2) {
            return false;
        }
        return true;
    }
}

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

Point2D

** 
 * Object for storing a 2D point.
 * 
 * @author rlsummerscales
 *
 */
public final class Point2D {
    final int x;
    final int y;
    
    /** Create a new 2D point with given x and y values 
      * 
      * @param xValue is the value for the x-coordinate
      * @param yValue is the value for the y-coordinate
      */
    public Point2D(int xValue, int yValue) {
        x = xValue;
        y = yValue;
    }
    
    /** Return a string describing the 2D point */
    public String toString() {
        return "(" + x + ", " + y + ")";
    }
    
    
    /** Override the default hashcode method */
    public int hashCode(){
        int hash = 13;
        hash = 31*hash + ((Integer) x).hashCode();
        hash = 31*hash + ((Integer) y).hashCode();
        return hash;
    }
    
    /** Override the default equals method.
      * Two Point2D objects equal if their values are equal.
      */
    public boolean equals(Object obj) {
        if (obj instanceof Point2D == false){
            return false;
        }
        Point2D p = (Point2D) obj;
        if (x != p.x || y != p.y) {
            return false;
        }
        return true;
    }
    
}

BoxRegionHashTable

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

public class BoxRegionHashTable {
    /** Output the annotation for a given BoxRegion
      * 
      * @param key is the BoxRegion
      * @param annotationMap is a map of annotation string for a set of points
      */
    // TODO: Write function displaAnnotation.
    // It should output the annotation for a given key for a given HashMap.
    // if point is not in hash table, display message stating that it has no annotation

    
    /** Test the equals and hashCode implementations in BoxRegion
      * @param args is an array of command line arguments
      */
    public static void main(String[] args) {
        // TODO: Create hash map of BoxRegion objects with their annotations as keys
        
        // TODO: Insert BoxRegions and corresponding strings into table
        
        // the following should be successful
        displayAnnotation(new BoxRegion(45, 237, 60, 300), annotationMap);
        displayAnnotation(new BoxRegion(1175, 140, 350, 418), annotationMap);
        displayAnnotation(new BoxRegion(2000, 0, 1700, 3000), annotationMap);
        displayAnnotation(new BoxRegion(1460, 1030, 819, 1420), annotationMap);
        
        // the following should fail
        displayAnnotation(new BoxRegion(237, 45, 300, 60), annotationMap);
        displayAnnotation(new BoxRegion(0, 0, 0, 0), annotationMap);
    }
    
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • I have pasted the code and screenshot of successful run below.
  • In case of any doubts just comment down.

--------------------------------------Screenshot--------------------------------------

ksmukta:4. - javac *.java && java BoxRegionHashTable Annotation: Box1 It has no annotation It has no annotation It has no ann

--------------------------------------BoxRegion.java--------------------------------------

public class BoxRegion {
final Point2D p1;
final Point2D p2;

/**
* Create a new 3D point with given x, y and z values
*
* @param x1, y1 are the x,y coordinates for point
* defining a corner of the BoxRegion
* @param x2, y2 are coordinates for the other corner
*/
public BoxRegion(int x1, int y1, int x2, int y2) {
p1 = new Point2D(x1,y1);
p2 = new Point2D(x2,y2);
}

/**
* Return a string describing the 3D point
*/
public String toString() {
return "(" + p1.x + ", " + p1.y + "), (" + +p2.x + ", " + p2.y + ")";
}


/**
* Override the default hashcode method
*/
// TODO: write hashCode method
public int hashCode() {
int hash = 17;
hash = hash * 31 + p1.hashCode();
hash = hash * 31 + p2.hashCode();
return hash;
}

/**
* Override the default equals method.
* Two BoxRegion objects equal if the coordinates
* of their opposing points are equal.
*/
// TODO: Write equals method
public boolean equals(Object obj) {
if (obj instanceof Point2D == false){
return false;
}
BoxRegion b = (BoxRegion) obj;
if ( p1!= b.p1 || p2 != b.p2) {
return false;
}
return true;
}
}

--------------------------------------Point2D.java--------------------------------------

/**
* Object for storing a 2D point.
*
* @author rlsummerscales
*
**/
public class Point2D {
final int x;
final int y;
  
/** Create a new 2D point with given x and y values
*
* @param xValue is the value for the x-coordinate
* @param yValue is the value for the y-coordinate
*/
public Point2D(int xValue, int yValue) {
x = xValue;
y = yValue;
}
  
/** Return a string describing the 2D point */
public String toString() {
return "(" + x + ", " + y + ")";
}
  
  
/** Override the default hashcode method */
public int hashCode() {
int hash = 13;
hash = 31*hash + ((Integer) x).hashCode();
hash = 31*hash + ((Integer) y).hashCode();
return hash;
}
  
/** Override the default equals method.
* Two Point2D objects equal if their values are equal.
*/
public boolean equals(Object obj) {
if (obj instanceof Point2D == false){
return false;
}
Point2D p = (Point2D) obj;
if (x != p.x || y != p.y) {
return false;
}
return true;
}
  
}

--------------------------------------BoxRegionHashTable.java--------------------------------------

import java.util.*;

public class BoxRegionHashTable {
/** Output the annotation for a given BoxRegion
*
* @param key is the BoxRegion
* @param annotationMap is a map of annotation string for a set of points
**/

// TODO: Write function displayAnnotation.
public static void displayAnnotation(BoxRegion b, HashMap<Integer, String> annotationMap){
if(annotationMap.containsKey(b.hashCode())){
System.out.println("Annotation: " + annotationMap.get(b.hashCode()));
}else{
System.out.println("It has no annotation");
}
}

// It should output the annotation for a given key for a given HashMap.
// if point is not in hash table, display message stating that it has no annotation

  
/** Test the equals and hashCode implementations in BoxRegion
* @param args is an array of command line arguments
**/
public static void main(String[] args) {
// TODO: Create hash map of BoxRegion objects with their annotations as keys
HashMap<Integer, String> annotationMap = new HashMap<Integer, String>();
// TODO: Insert BoxRegions and corresponding strings into table
annotationMap.put((new BoxRegion(45, 237, 60, 300)).hashCode(), "Box1");
// the following should be successful
displayAnnotation(new BoxRegion(45, 237, 60, 300), annotationMap);
displayAnnotation(new BoxRegion(1175, 140, 350, 418), annotationMap);
displayAnnotation(new BoxRegion(2000, 0, 1700, 3000), annotationMap);
displayAnnotation(new BoxRegion(1460, 1030, 819, 1420), annotationMap);
  
// the following should fail
displayAnnotation(new BoxRegion(237, 45, 300, 60), annotationMap);
displayAnnotation(new BoxRegion(0, 0, 0, 0), annotationMap);
}
  
}

Add a comment
Know the answer?
Add Answer to:
This is my code that i need to finish. In BoxRegion.java I have no idea how...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need Help PLZ ( Java Code ). Today 14.06.2019 I have to hand in my...

    I need Help PLZ ( Java Code ). Today 14.06.2019 I have to hand in my homework. reachability Actually, you find flying very good, but you do not trust the whole new-fangled flying stuff and the infrastructure it has built up. As a diehard medieval metal fan you prefer to travel from A to B but rather the good old catapult. Since one can not easily take the favorite cat on vacation with it (cats do not get drafts, which...

  • I need Help PLZ ( Java Code ). Tomorrow 05.06.2019 I have to hand in my homework. reachability Actually, you find f...

    I need Help PLZ ( Java Code ). Tomorrow 05.06.2019 I have to hand in my homework. reachability Actually, you find flying very good, but you do not trust the whole new-fangled flying stuff and the infrastructure it has built up. As a diehard medieval metal fan you prefer to travel from A to B but rather the good old catapult. Since one can not easily take the favorite cat on vacation with it (cats do not get drafts, which...

  • PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need...

    PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need help. import java.util.LinkedList; public class TwoDTree { private TwoDTreeNode root; private int size; public TwoDTree() {    clear(); } /** * Returns true if a point is already in the tree. * Returns false otherwise. * * The traversal remains the same. Start at the root, if the tree * is not empty, and compare the x-coordinates of the point passed * in and...

  • Java/LinkedList Need help with a few of the TODO parts, more info below in comments in...

    Java/LinkedList Need help with a few of the TODO parts, more info below in comments in bold. Thanks, package lab4; import java.util.IdentityHashMap; public class IntNode implements Cloneable {    private int data;    private IntNode next;       public IntNode(int d, IntNode n) {        data = d;        next = n;    }       public IntNode getNext() {        return next;    }          /// Override methods from Object       @Override   ...

  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...

  • I need help with my homework. The task: Actually, you find flying very good, but you...

    I need help with my homework. The task: Actually, you find flying very good, but you do not trust the whole new-fangled flying stuff and the infrastructure it has built up. As a diehard medieval metal fan you prefer to travel from A to B but rather the good old catapult. Since one can not easily take the favorite cat on vacation with it (cats do not get drafts, which is why ICEs are also eliminated), they let themselves be...

  • (file ToDo.java) Add code for an equals method that checks the description of a ToDo item...

    (file ToDo.java) Add code for an equals method that checks the description of a ToDo item with a passed ToDo item’s description. The method should return true if they are identical and false if the descriptions are not identical. public class Lab8Interfaces {    /**    * @param args    * the command line arguments    */    public static void main(String[] args) {        ToDoItem[] arr = new ToDoItem[5];        ToDoItem toDo1 = new ToDoItem("Hospital Project");   ...

  • Susceptible.java interface Susceptible { public boolean infect(Disease disease); public void forceInfection(Disease disease); public Disease getCurrentDisease(); public...

    Susceptible.java interface Susceptible { public boolean infect(Disease disease); public void forceInfection(Disease disease); public Disease getCurrentDisease(); public void setImmune(); public boolean isImmune(); public Point getPosition(); } Movable.java interface Movable { public void move(int step); } public class Point { private int xCoordinate; private int yCoordinate; /** * Creates a point at the origin (0,0) and colour set to black */ public Point(){ xCoordinate = 0; yCoordinate = 0; } /** * Creates a new point at a given coordinate and colour...

  • PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class...

    PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class definition. * * @author David Brown * @version 2019-01-22 */ public class Movie implements Comparable { // Constants public static final int FIRST_YEAR = 1888; public static final String[] GENRES = { "science fiction", "fantasy", "drama", "romance", "comedy", "zombie", "action", "historical", "horror", "war" }; public static final int MAX_RATING = 10; public static final int MIN_RATING = 0; /** * Converts a string of...

  • I need help getting my game to select a random plant to paint here is the code for Actor and the ...

    I need help getting my game to select a random plant to paint here is the code for Actor and the two plants and the paintComponent that paints the objects. If you need anything else please comment and I can add it. The comments in paintComponent is where and what I think I need to change. package ale import java.awt.geom. Point2D public class Plant extends Actor e public Plant ( Point 2D . Double startingPosition, Point2D·Double initHit box, Buffered Image...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT