Hello, I need help for this homework. It is for Java language : Composition and
ID: 3802838 • Letter: H
Question
Hello, I need help for this homework. It is for Java language :
Composition and Encapsulation – The Line and Point Class
Points and Lines ( Here is the Image's Link : http://e.top4top.net/p_4407a55b1.png ).
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
----
Please I do not want you to copy other work, I need a new work. If you can not do it. Do not copy other people work.
Thank you
Explanation / Answer
Point.java
public class Point {
private int x;
private int y;
public Point()
{
this.x=0;
this.y=0;
}
public Point(int x,int y)
{
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String toString(){
return "("+x+","+ y+")";
}
public boolean equals(Object o) {
if (!(o instanceof Point)) {
return false;
}
Point q = (Point)o;
return Integer.compare(x,q.x) == 0
&& Integer.compare(y,q.y) == 0;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------
Line.java
public class Line {
private Point startPointOfLine;
private Point endPointOfLine;
private double lengthOfLine;
private String color;
private float width;
public Line()
{
this.startPointOfLine.setX(0);
this.startPointOfLine.setY(0);
this.endPointOfLine.setX(0);
this.endPointOfLine.setY(0);
this.color="black";
this.lengthOfLine=0;
this.width=1;
}
public Line(int a, int b, int c, int d, String color, Float width)
{
this.startPointOfLine.setX(a);
this.startPointOfLine.setY(b);
this.endPointOfLine.setX(c);
this.endPointOfLine.setY(d);
this.color=color;
this.width=width;
}
public Line(int a, int b, int c, int d)
{
this.startPointOfLine.setX(a);
this.startPointOfLine.setY(b);
this.endPointOfLine.setX(c);
this.endPointOfLine.setY(d);
this.color="black";
this.width=1;
}
public Point getStartPointOfLine() {
return startPointOfLine;
}
public void setStartPointOfLine(Point startPointOfLine) {
this.startPointOfLine = startPointOfLine;
}
public Point getEndPointOfLine() {
return endPointOfLine;
}
public void setEndPointOfLine(Point endPointOfLine) {
this.endPointOfLine = endPointOfLine;
}
public double getLengthOfLine() {
return lengthOfLine;
}
public void setLengthOfLine(double lengthOfLine) {
this.lengthOfLine = lengthOfLine;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public String toString(){
double length = Math.hypot((this.endPointOfLine.getX() - this.startPointOfLine.getX()),( this.endPointOfLine.getY() - this.startPointOfLine.getY()));
return "Start Point:"+this.startPointOfLine+" End Point:"+this.endPointOfLine+" Length:"+length+" Color:"+this.color+" Width:"+this.width;
}
public boolean equals(Object o) {
if (!(o instanceof Line)) {
return false;
}
Line q = (Line)o;
return startPointOfLine.equals(q.startPointOfLine) && endPointOfLine.equals(q.endPointOfLine) && Float.compare(width,q.width) == 0 && color.equals(q.color);
}
public static boolean validateLine(int a,int b, int x, int y)
{
if(a>0 && x>0)
{
if(b>0 && y>0)
return true;
}
return false;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------
LineDriver.java
import java.util.Scanner;
public class LineDriver {
public static void main(String[] args) {
Line l1=new Line();
Line l2=new Line();
int a1=0,b1=0,c1=0,d1=0;
int a2=0,b2=0,c2=0,d2=0;
String color1="",color2="";
float width1=0,width2=0;
Scanner scan=new Scanner(System.in);
int flag1=0,flag2=0;
while(flag1==0)
{
System.out.println("Enter coordinates, width and color for First line ");
a1=scan.nextInt();
b1=scan.nextInt();
c1=scan.nextInt();
d1=scan.nextInt();
color1=scan.next();
width1=scan.nextFloat();
boolean rel=Line.validateLine(a1,b1,c1,d1);
if(rel==true)
{
flag1=1;
}
}
while(flag2==0)
{
System.out.println("Enter coordinates, width and color for Second line ");
a2=scan.nextInt();
b2=scan.nextInt();
c2=scan.nextInt();
d2=scan.nextInt();
color2=scan.next();
width2=scan.nextFloat();
boolean rel=Line.validateLine(a2,b2,c2,d2);
if(rel==true)
{
flag2=1;
}
}
Point P1= new Point(a1,b1);
Point p2= new Point(c1,d1);
l1.setStartPointOfLine(P1);
l1.setEndPointOfLine(p2);
l1.setColor(color1);
l1.setWidth(width1);
Point P3= new Point(a2,b2);
Point p4= new Point(c2,d2);
l2.setStartPointOfLine(P3);
l2.setEndPointOfLine(p4);
l2.setColor(color2);
l2.setWidth(width2);
if (l1.equals(l2)) {
System.out.println("Lines are Equal ");
} else {
System.out.println("Lines are Not Equal ");
}
System.out.println("Line1"+" "+l1);
System.out.println("Line2"+" "+l2);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.