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

I\'m new to java and my professor throws us into the dark with these projects. I

ID: 3630174 • Letter: I

Question

I'm new to java and my professor throws us into the dark with these projects. I had to create a parameterless constructor and a parameterized constructor for a triangle. All the methods in the class are the only methods allowed in it. I tried my best with this project but I can't get it to work. After I enter the coordinates of the vertices for the triangle, it automatically makes all the coordinates of the vertices (0,0) which then makes everything else equal 0. I think it has something to do with the coordinate instance variables in my class but I can't figure out what. So what is wrong with it? Also, as a side note, if there is anything that is unconventional or messy, please let me know how I can clean it up.

Here's my triangle class:

package Triangle;

import java.awt.*;

public class Triangle {

private Point vertexA;
private Point vertexB;
private Point vertexC;
private int xA;
private int yA;
private int xB;
private int yB;
private int xC;
private int yC;
private float side1 = (float) Math.sqrt(Math.pow((xA-xB), 2)+Math.pow(yA-yB, 2));
private float side2 = (float) Math.sqrt(Math.pow((xB-xC), 2)+Math.pow(yB-yC, 2));
private float side3 = (float) Math.sqrt(Math.pow((xC-xA), 2)+Math.pow(yC-yA, 2));;

public Triangle() {
vertexA = new Point(0,0);
vertexB = new Point(0,0);
vertexC = new Point(0,0);
}


public Triangle(Point vertexA, Point vertexB, Point vertexC){
vertexA = new Point(xA,yA);
vertexB = new Point(xB,yB);
vertexC = new Point(xC,yC);
}

public void translate(int dX, int dY){
vertexA = new Point(xA+dX,yA+dY);
vertexB = new Point(xB+dX,yB+dY);
vertexC = new Point(xC+dX,yC+dY);
}

public float getPerimeter(){
return side1+side2+side3;
}

public float getArea(){
float s = (side1+side2+side3)/2;
float area = (float) Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return area;
}

public boolean isEquilateral(){
if (side1 == side2 && side2 == side3)
return true;
else
return false;
}

public boolean isIsosceles(){
if (side1 == side2 || side2 == side3 || side1 == side3)
return true;
else
return false;
}

public boolean isScalene(){
if (side1 != side2 && side2 != side3 && side1 != side3)
return true;
else
return false;
}

public boolean isRight(){
if (Math.pow(side1, 2)+Math.pow(side2, 2) == Math.pow(side3, 2) || Math.pow(side2, 2)+Math.pow(side3, 2) == Math.pow(side2, 2) || Math.pow(side1, 2)+Math.pow(side3, 2) == Math.pow(side2, 2))
return true;
else
return false;
}

public String toString(){
return "{("+xA+","+yA+"),("+xB+","+yB+"),("+xC+","+yC+")}";
}

}


Here's the implementation of that class:

package triangletester;

import java.awt.*;
import Triangle.*;
import java.util.*;

public class TriangleTester {

public static void main(String[] args) {

System.out.print("Enter the x- and y- coordinates of the first vertex> ");
Scanner keyb = new Scanner(System.in);
int xA = keyb.nextInt();
int yA = keyb.nextInt();
Point vertexA = new Point(xA,yA);
System.out.print("Enter the x- and y- coordinates of the second vertex> ");
int xB = keyb.nextInt();
int yB = keyb.nextInt();
Point vertexB = new Point(xB,yB);
System.out.print("Enter the x- and y- coordinates of the third vertex> ");
int xC = keyb.nextInt();
int yC = keyb.nextInt();
Point vertexC = new Point(xC,yC);
Triangle tri1 = new Triangle(vertexA,vertexB,vertexC);
System.out.println("Here are the details of the triangle, "+tri1.toString()+":");
System.out.print("Enter the number of units to translate horizontally: ");
int dX = keyb.nextInt();
System.out.print("Enter the number of units to translate vertically: ");
int dY = keyb.nextInt();
tri1.translate(dX,dY);
System.out.println("Here are the details of the translated triangle, "+tri1.toString()+":");
System.out.println("Perimeter = "+tri1.getPerimeter());
System.out.println("Area = "+tri1.getArea());
System.out.println("Classification: ");
System.out.print("Right: ");
if (tri1.isRight())
System.out.println("Y");
else
System.out.println("N");
System.out.print("Isosceles: ");
if (tri1.isIsosceles())
System.out.println("Y");
else
System.out.println("N");
System.out.print("Scalene: ");
if (tri1.isScalene())
System.out.println("Y");
else
System.out.println("N");
System.out.print("Equilateral: ");
if (tri1.isEquilateral())
System.out.println("Y");
else
System.out.println("N");


}
}

Explanation / Answer

I have corrected the code and cleaned it. //Triangle .java package triangletester; import java.awt.Point; public class Triangle { private Point vertexA, vertexB, vertexC; private float side1, side2, side3; private int xA,yA, xB,yB, xC,yC; public Triangle() { vertexA = new Point(0,0); vertexB = new Point(0,0); vertexC = new Point(0,0); side1 = side2 = side3 = 0; xA = yA = xB = yB = xC = yC =0; } public Triangle(Point pVertexA, Point pVertexB, Point pVertexC) { vertexA = pVertexA; vertexB = pVertexB; vertexC = pVertexC; xA = pVertexA.x; yA = pVertexA.y; xB = pVertexB.x; yB = pVertexB.y; xC = pVertexC.x; yC = pVertexC.y; side1 = (float) Math.sqrt(Math.pow((xA-xB), 2)+Math.pow(yA-yB, 2)); side2 = (float) Math.sqrt(Math.pow((xB-xC), 2)+Math.pow(yB-yC, 2)); side3 = (float) Math.sqrt(Math.pow((xC-xA), 2)+Math.pow(yC-yA, 2)); } public void translate(int dX, int dY) { vertexA = new Point(xA+dX,yA+dY); vertexB = new Point(xB+dX,yB+dY); vertexC = new Point(xC+dX,yC+dY); xA = xA+dX; yA = yA+dY; xB = xB+dX; yB = yB+dY; xC = xC+dX; yC = yC+dY; } public float getPerimeter() { return side1+side2+side3; } public float getArea() { float s = (side1+side2+side3)/2; float area = (float) Math.sqrt(s*(s-side1)*(s-side2)*(s-side3)); return area; } public boolean isEquilateral() { if (side1 == side2 && side2 == side3) return true; else return false; } public boolean isIsosceles() { if (side1 == side2 || side2 == side3 || side1 == side3) return true; else return false; } public boolean isScalene() { if (side1 != side2 && side2 != side3 && side1 != side3) return true; else return false; } public boolean isRight() { if (Math.pow(side1, 2)+Math.pow(side2, 2) == Math.pow(side3, 2) || Math.pow(side2, 2)+Math.pow(side3, 2) == Math.pow(side2, 2) || Math.pow(side1, 2)+Math.pow(side3, 2) == Math.pow(side2, 2)) return true; else return false; } @Override public String toString() { return "{("+xA+","+yA+"),("+xB+","+yB+"),("+xC+","+yC+")}"; } } //TriangleTester .java package triangletester; import java.awt.Point; import java.util.Scanner; public class TriangleTester { public static void main(String[] args) { System.out.print("Enter the x- and y- coordinates of the first vertex> "); Scanner keyb = new Scanner(System.in); int xA = keyb.nextInt(); int yA = keyb.nextInt(); Point vertexA = new Point(xA,yA); System.out.print("Enter the x- and y- coordinates of the second vertex> "); int xB = keyb.nextInt(); int yB = keyb.nextInt(); Point vertexB = new Point(xB,yB); System.out.print("Enter the x- and y- coordinates of the third vertex> "); int xC = keyb.nextInt(); int yC = keyb.nextInt(); Point vertexC = new Point(xC,yC); Triangle tri1 = new Triangle(vertexA,vertexB,vertexC); System.out.println("Here are the details of the triangle, "+tri1.toString()+":"); System.out.print("Enter the number of units to translate horizontally: "); int dX = keyb.nextInt(); System.out.print("Enter the number of units to translate vertically: "); int dY = keyb.nextInt(); tri1.translate(dX,dY); System.out.println("Here are the details of the translated triangle, "+tri1.toString()+":"); System.out.println("Perimeter = "+tri1.getPerimeter()); System.out.println("Area = "+tri1.getArea()); System.out.println("Classification: "); System.out.print("Right: "); if (tri1.isRight()) System.out.println("Y"); else System.out.println("N"); System.out.print("Isosceles: "); if (tri1.isIsosceles()) System.out.println("Y"); else System.out.println("N"); System.out.print("Scalene: "); if (tri1.isScalene()) System.out.println("Y"); else System.out.println("N"); System.out.print("Equilateral: "); if (tri1.isEquilateral()) System.out.println("Y"); else System.out.println("N"); } }

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