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

Java computer science Please leave The Line Class method outside of the main met

ID: 3861307 • Letter: J

Question

Java computer science Please leave The Line Class method outside of the main method. Please make sure that this will compile and execute in jGrasp and make sure to include every aspect, please!

Points and Lines

Computer graphics systems make use of two general rendering systems – rasters and vectors -- to produce images. Raster systems keep track of collections of individual pixels and vector systems keep track of lines. (Lines are more accurate, but require more computer resources.)

This assignment calls for a Line object that represents a graphically depicted image on the screen. This object must store this data:

Endpoint objects (x and y coordinates as an integer). There will be two of these.

The line color as a String

The line width as an integer

Note that the Line object must be composed of two Point objects.

The Point Class

Create a Point class that contains x and y coordinates (x and y are integers) that represents a point in two-dimensional space. A point can lie only in the first quadrant in the x-y coordinate axis. Include the following methods

two constructors: a default that initializes x and y to 0, and another that accepts the x and y values as passed-in parameters

get and set methods to retrieve and assign coordinate values

a toString method that will return a String containing the coordinates of the point formatted as (x, y) an equals method (returns a boolean value) that compares two Points for equality. This method takes a single parameter of type Object and will cast the Object into a Point for comparison. anything else you deem necessary

The Line Class

Create a Line class that contains variables and/or methods to represent

the end Points of the line (be sure your Point objects are declared as private)

the length of the line (represent this as a return value from a method)

color

width

This Line class should have the following methods

A default constructor that creates Points that represent a zero-length black line at the origin with a width of 1 pixel

A constructor that accepts four ints – x and y for point 1 and x and y for point 2, a color and a width A constructor that accepts four ints (color defaults to black and width to 1 pixel)

get and set methods to retrieve values and assign values as you deem necessary

A toString method that prints the coordinates of the line endpoints, the length, the color and width of the line (nicely formatted, please)

an equals method (returns a boolean value) that will compare two lines for equality. Two lines are equal if their endpoints, width and color are the same. Note that Points do not need to be in same order. Be sure and use the equals method you wrote from your Point class to compare Points. Your equals method should override the equals method in Object.

a validateLine method that will make sure the points are in the first quadrant. This method must be declared as static -- so that it can be called without an instance of the class.

Create a driver class called LineDriver that will work with two lines. Create both lines as zero-length at the origin. Then provide a menu that is displayed repetitively with the following options:

1 - Enter coordinates, width and color for first line (line should be validated as part of this process -- use the validateLine method -- do not continue until valid coordinates and width are obtained from the user)

2 - Enter coordinates, width and color for second line (ditto)

3 - Compare the two lines (specify if lines are equal/not equal after comparison)

4 - Display coordinates, width, length and color for first line (the length should be derived from the coordinates using the distance formula and is represented as a real number with 2 post-decimal positions of precision.)

5 - Display coordinates, width, length and color for second line (the length should be derived from the coordinates using the distance formula and is represented as a real number with 2 post-decimal positions of precision.)

6 - Quit

Explanation / Answer

Hi,

The solution goes as follows:

Point.java

package coordinategeometry;

public class Point {

   private int x;
   private int y;
  
   public Point() {
       this.x = 0;
       this.y = 0;
   }
  
   public Point(int x, int y) {
       super();
  this.x = x;
       this.y = y;
   }
  
   /**
   * @return the x
   */
   public int getX() {
       return x;
   }
   /**
   * @param x the x to set
   */
   public void setX(int x) {
       this.x = x;
   }
   /**
   * @return the y
   */
   public int getY() {
       return y;
   }
   /**
   * @param y the y to set
   */
   public void setY(int y) {
       this.y = y;
   }

   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Point [x=" + x + ", y=" + y + "]";
   }

   /* (non-Javadoc)
   * @see java.lang.Object#hashCode()
   */
   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + x;
       result = prime * result + y;
       return result;
   }

   /* (non-Javadoc)
   * @see java.lang.Object#equals(java.lang.Object)
   */
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Point other = (Point) obj;
       if (x != other.x)
           return false;
       if (y != other.y)
           return false;
       return true;
   }
  
}

Line.java

package coordinategeometry;

public class Line {

   private String color;
   private int width;
   private Point startPoint;
   private Point endPoint;
  
   public Line (int x1, int y1, int x2, int y2, String color, int width) {
       this.startPoint = new Point(x1, y1);
       this.startPoint = new Point(x2, y2);
       this.color = color;
       this.width = width;
   }
  
   public Line(int x1, int y1, int x2, int y2) {
       this.startPoint = new Point(x1, y1);
       this.startPoint = new Point(x2, y2);
       this.color = "BLACK";
       this.width = 1;
      
   }
  
   public Line() {
       this.startPoint = new Point();
       this.endPoint = new Point();
       this.color = "BLACK";
       this.width = 1;
   }

   /**
   * @return the color
   */
   public String getColor() {
       return color;
   }

   /**
   * @param color the color to set
   */
   public void setColor(String color) {
       this.color = color;
   }

   /**
   * @return the width
   */
   public int getWidth() {
       return width;
   }

   /**
   * @param width the width to set
   */
   public void setWidth(int width) {
       this.width = width;
   }

   /**
   * @return the startPoint
   */
   public Point getStartPoint() {
       return startPoint;
   }

   /**
   * @param startPoint the startPoint to set
   */
   public void setStartPoint(Point startPoint) {
       this.startPoint = startPoint;
   }

   /**
   * @return the endPoint
   */
   public Point getEndPoint() {
       return endPoint;
   }

   /**
   * @param endPoint the endPoint to set
   */
   public void setEndPoint(Point endPoint) {
       this.endPoint = endPoint;
   }

   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Line [color=" + color + ", width=" + width + ", startPoint=" + startPoint + ", endPoint=" + endPoint
               + "]";
   }

   /* (non-Javadoc)
   * @see java.lang.Object#hashCode()
   */
   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + ((color == null) ? 0 : color.hashCode());
       result = prime * result + ((endPoint == null) ? 0 : endPoint.hashCode());
       result = prime * result + ((startPoint == null) ? 0 : startPoint.hashCode());
       result = prime * result + width;
       return result;
   }

   /* (non-Javadoc)
   * @see java.lang.Object#equals(java.lang.Object)
   */
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Line other = (Line) obj;
       if (color == null) {
           if (other.color != null)
               return false;
       } else if (!color.equals(other.color))
           return false;
       if (endPoint == null) {
           if (other.endPoint != null)
               return false;
       } else if (!endPoint.equals(other.endPoint))
           return false;
       if (startPoint == null) {
           if (other.startPoint != null)
               return false;
       } else if (!startPoint.equals(other.startPoint))
           return false;
       if (width != other.width)
           return false;
       return true;
   }
  
   /**
   * Validates whether the line is valid or not, in case any of the point has negative co-ordinates
   * @param line
   * @return
   */
   public static boolean validateLine(final Line line) {
      
       boolean isValidLine = false;
      
       final Point startPoint = line.getStartPoint();
       final Point endPoint = line.getEndPoint();
      
       isValidLine = startPoint.getX() > 0 && startPoint.getY() > 0 && endPoint.getX() > 0 && endPoint.getY() > 0;
      
       return isValidLine;
   }
}

LineDriver.java

package coordinategeometry;

import java.util.Scanner;

public class LineDriver {
  
   private static Line firstLine;
   private static Line secondLine;

   public static void main(String args[]) {
      
       System.out.println("---------Welcome to Line Driver-------------");
       System.out.println("The menu options are as follows:");
       System.out.println("1 -> Enter coordinates, width and color for first line");
       System.out.println("2 -> Enter coordinates, width and color for first line");
       System.out.println("3 -> Compare two Lines");
       System.out.println("4 -> Display coordinates, width, length and color for first line");
       System.out.println("5 -> Display coordinates, width, length and color for second line");
       System.out.println("6 -> Exit");
      
       Scanner scan = new Scanner(System.in);
       int choice = scan.nextInt();
      
       switch(choice) {
      
       case 1 :
           createLine(true);
       break;
      
       case 2:
           createLine(false);
       break;
      
       case 3:
           compareLines();
       break;
      
       case 4:
           displayLine(true);
       break;
      
       case 5:
           displayLine(false);
       break;
      
       case 6:
           System.exit(0);
       break;
      
       default:
           System.out.println("Invalid choice");
       break;  
       }
      
   }

   private static void displayLine(boolean isStartLine) {
      
       String s = isStartLine ? "first" : "second" ;
      
       System.out.println("Details for " + s + " line are as: ");
      
       final String lineDisplay = isStartLine ? firstLine.toString() : secondLine.toString();
      
       System.out.println(lineDisplay);
      
   }

   private static void compareLines() {
      
       final boolean areLinesEqual = (firstLine.getWidth() == secondLine.getWidth());
       System.out.println("The equality of two line is as: " + areLinesEqual);
      
   }

   private static void createLine(boolean isStartLine) {
      
       String s = isStartLine ? "first" : "second" ;
       System.out.println("Enter co-ordinates for " + s + " line");
      
       System.out.println("Enter x1 co-ordinate for " + s + " line");
       Scanner scan = new Scanner(System.in);
       int x1 = scan.nextInt();
      
       System.out.println("Enter y1 co-ordinate for " + s + " line");
       int y1 = scan.nextInt();
      
       System.out.println("Enter x2 co-ordinate for " + s + " line");
       int x2 = scan.nextInt();
      
       System.out.println("Enter y2 co-ordinate for " + s + " line");
       int y2 = scan.nextInt();
      
       System.out.println("Enter Color for " + s + " line");
       String color = scan.next();
       System.out.println("Enter width for " + s + " line");
       int width = scan.nextInt();
      
       final Line line = new Line(x1, y1, x2, y2, color, width);
      
       if(Line.validateLine(line)) {
           if (isStartLine) {
               firstLine = line;
           } else {
               secondLine = line;
           }
       } else{
           System.out.println("Invalid co-ordinates entered the given line is invalid");
       }
   }
}

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