Finding length and angles of a triangle - java

My job is to compute the following properties of a given triangle: lengths of all sides, angles at all corners, the perimeter and the area. I have a Triangle class and a Triangle tester class. I THINK I have coded the perimeter and area correctly? But I am beginning to think instead of setting a constant variable side for my perimeter I should be using the lengths of all sides to find my perimeter as well. What I am stuck on is finding the lengths and the angles. For some reason when I run my tester class they all come out as 1.0. Any advice would be appreciated! Thank you!
import java.util.Scanner;
public class Triangle
{
Scanner in = new Scanner(System.in);
/**
* Variables
*/
double x1;
double x2;
double x3;
double y1;
double y2;
double y3;
double lengthA;
double lengthB;
double lengthC;
double angleA;
double angleB;
double angleC;
double area;
double perimeter;
double base;
double height;
double p;
/**
Constructs x and y coordinates
#param x1, x2, x3 to x1, x2, x3
#param y1, y2, y3 to y1, y2, y3
*/
public Triangle(double x1, double x2, double x3, double y1, double y2, double y3)
{
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
this.y1 = y1;
this.y2 = y2;
this.y3 = y3;
}
/**
*Find lengths of all sides
*/
public double getLengthA()
{
lengthA = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
return lengthA;
}
public double getLengthB()
{
lengthB = Math.sqrt(Math.pow((x3 - x2), 2) + Math.pow((y3 - y2), 2));
return lengthB;
}
public double getLengthC()
{
lengthC = Math.sqrt(Math.pow((x1 - x3), 2) + Math.pow((y1 - y3), 2));
return lengthC;
}
/**
* Find angles at all corners
#return angles at all corners
*/
public double getAngleA()
{
angleA = lengthA + lengthB + lengthC - (lengthB * lengthC);
return angleA;
}
public double getAngleB()
{
angleB = lengthB + lengthA + lengthC - (lengthA * lengthC);
return angleB;
}
public double getAngleC()
{
angleC = lengthC + lengthA + lengthB - (lengthA * lengthB);
return angleC;
}
/**
* Constant Variables
*/
public Triangle()
{
base = 5;
height = 15;
}
/**
* Find perimeter of triangle
*/
public double getPerimeter()
{
perimeter = lengthA + lengthB + lengthC;
return perimeter;
}
public double getHalfPerimeter()
{
p = perimeter / 2;
return p;
}
/**
* Find area of triangle
*/
public double getArea()
{
double area = Math.sqrt(p * (p - lengthA) * (p - lengthB) * (p - lengthC));
return area;
}
}
Here is my tester class:
import java.util.Scanner;
import javax.swing.JOptionPane;
public class TriangleSimulator
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
String input = JOptionPane.showInputDialog("Enter X coordinate for the first corner of the triangle: ");
double x1 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter Y coordinate for the first corner of the triangle: ");
double y1 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter X coordinate for the second corner of the triangle: ");
double x2 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter Y coordinate for the second corner of the triangle: ");
double y2 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter X coordinate for the third corner of the triangle: ");
double x3 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter Y coordinate for the third corner of the triangle: ");
double y3 = Double.parseDouble(input);
Triangle t = new Triangle(x1, x2, x3, y1, y2, y3);
System.out.println("Length A is: " + t.getLengthA());
System.out.println("Length B is: " + t.getLengthB());
System.out.println("Length C is: " + t.getLengthC());
System.out.println("Angle A is: " + t.getAngleA());
System.out.println("Angle B is: " + t.getAngleB());
System.out.println("Angle C is: " + t.getAngleC());
System.out.println("Area: " + t.getArea());
System.out.println("Perimeter: " + t.getPerimeter());
in.close();
}
}

You're showing JOptionPanes that prompt the user for input, but you're not getting the input and you're thus not using the input to set the state of your Triangle, creating a default Triangle object using its parameterless constructor. Understand that there's no magic in Java programming, and your Triangle object will not magically know what numbers the user has entered and change itself accordingly. Instead you must give that information into your Triangle.
What you must do is assign the results returned from the JOptionPanes, parse them into doubles, and then use those numbers to create a Triangle, using the constructor that takes numeric value parameters, not the default constructor. Do this, and you should be good.
e.g.
String input = JOptionPane.showInputDialog("Enter X coordinate for the first corner of the triangle: ");
double x1 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter Y coordinate for the first corner of the triangle:
double y1 = Double.parseDouble(input);
//.... etc repeat...
Triangle triangle = new Triangle(x1, x2, x3, y1, y2, y3);
Edit
This constructor ignores the values being passed in:
public Triangle(double x1, double x2, double x3, double y1, double y2, double y3)
{
x1 = 0;
x2 = 0;
x3 = 0;
y1 = 0;
y2 = 0;
y3 = 0;
}
A constructor like the one you'll need, should take the values passed in, and use those values to set class fields. For instance here:
public class Foo {
private int bar; // the bar field
public Foo(int bar) {
this.bar = bar;
}
}
Note that I use this.bar above so that Java knows that I want to set the bar field with the value held by the bar parameter (the bar without the this). You will need to do something similar only with 6 parameters, not one.
Then you do all your calculations in a separate block of code called an initializer block:
{
/**
* Find lengths of all sides
*/
lengthA = Math.pow(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2) * .05, lengthA);
lengthB = Math.pow(Math.pow((x3 - x2), 2) + Math.pow((y3 - y2), 2) * .05, lengthB);
lengthC = Math.pow(Math.pow((x1 - x3), 2) + Math.pow((y1 - y3), 2) * .05, lengthC);
}
This code gets called before your constructor, and so even if you set your Triangles fields correctly, this code wouldn't work. You don't want to use initializer blocks, and you can forget I even mentioned them other than to tell you not to use them. Instead do your calculations inside your constructor, and do so after setting all your fields.
Note that I've purposely not posted a solution to your problem because I firmly believe that what most of us need is to understand the concepts underlying any problems we are having, and then use that understanding to create our own code solutions.
Most important, read your texts, no better, study your texts, because the mistakes you're making involve foundational concepts, and show that you don't yet understand these concepts and have resorted to guessing. This will never work, as you need to understand all this and understand it well if you're going to be able to progress in this course.
Good luck!
Edit 2
For Tom, run this:
public class TestInitializerBlock {
public TestInitializerBlock() {
System.out.println("Inside of constructor");
}
{
System.out.println("Inside of initializer block");
}
public static void main(String[] args) {
new TestInitializerBlock();
}
}

Related

Distance between coordinates in Java

I'm trying to solve this problem.
Determine the distance between point (x1, y1) and point (x2, y2), and assign the result to pointsDistance.
I'll now present the original code along with my answer to the problem:
import java.util.Scanner;
public class CoordinateGeometry {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double x1;
double y1;
double x2;
double y2;
double pointsDistance;
double xDist;
double yDist;
pointsDistance = 0.0;
xDist = 0.0;
yDist = 0.0;
x1 = scnr.nextDouble();
y1 = scnr.nextDouble();
x2 = scnr.nextDouble();
y2 = scnr.nextDouble();
pointsDistance = Math.sqrt(x2 - Math.pow(x1, 2) + y2 - Math.pow(y1, 2)); //*Learning Square Rooots and powers*//
System.out.println(pointsDistance);
}
}
The desired outcomes from the inputs are not printing. For an example:
My value:
1
Expected value:
3
Where have I gone wrong?
Your formula is incorrect. It should be:
pointsDistance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
This is just an application of the Pythagorean theorem, and Java has a Math.hypot() convenience method that also protects against arithmetic overflow/underflow:
pointsDistance = Math.hypot(x2-x1, y2-y1);
Thanks to #LouisWasserman for the helpful comment.

How to convert double x1 = input.nextDouble(); into an array? [duplicate]

This question already has answers here:
how to take user input in Array using java?
(9 answers)
Closed 1 year ago.
/*
(Geometry: area of a triangle) Write a program that prompts the user to enter
three points (x1, y1), (x2, y2), (x3, y3) of a triangle and displays its area.
*/
import java.util.Scanner;
public class Exercise_02_19 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter three points
System.out.print("Enter three points for a triangle: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double x3 = input.nextDouble();
double y3 = input.nextDouble();
// Compute the area of a triangle
double side1 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
double side2 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);
double side3 = Math.pow(Math.pow(x1 - x3, 2) + Math.pow(y1 - y3, 2), 0.5);
double s = (side1 + side2 + side3) / 2;
double area = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
// Display result
System.out.println("The area of the triangle is " + area);
}
}
I am learning the Java coding language and currently stuck on a question. How can I convert double x1 = input.nextDouble(); into an array that does x1 to 3 and y1 to 3? We are currently using the Introduction to Java Programming and Data Structures Twelfth Edition and even taking the time to read and understand what is going on is hard for me to comprehend. If there is someone with Java knowledge to help what went wrong and point me in the right direction that would be great. Thank you!
double[] x = new double[3];
double[] y = new double[3];
Now you can access x1 to x3 as x[0] to x[2] and similarly for y.

Method from other class and use of toString()

I am trying to make this code work, receive 2 points (x, y) & (x1,y1) from user on MAIN and use a method from another class and get the calculation.
Also it would be very nice to receive an explanation about toString.
import java.util.Scanner;
public class Test{
public static void main(String[]args)
{
Scanner scan = new Scanner(System.in);
double x , y , x1 , y1;
Pytaguras Triangle = new Pytaguras();
System.out.println("Hello , this program is using Pytagoras formula" +
" on the 4 point that you enter");
System.out.println("Please enter 4 points to calculate the distance");
x = scan.nextDouble();
y = scan.nextDouble();
x1 = scan.nextDouble();
y1 = scan.nextDouble();
Triangle.setDouble( x, y, x1, y1);
System.out.println("the distance between 2 points is :" + Triangle.calculate() );
}
}
public class Pytaguras
{
private double x, y, x1 ,y1 ;
public void setDouble(double _x, double _y, double _x1, double _y1)
{ _x=x;
_y=y;
_x1=x1;
_y1=y1;}
public double calculate(double _x , double _y , double _x1 , double _y1 )
{ double s;
s = Math.sqrt((_x-_y)*(_x-_y)+(_x1-_y1)*(_x1-_y1));
return s;
}
}
If you would like to receive the distance-calculation via method toString() of your class Triangle, you just need to implement it same like method calculate:
Possible solution using toString
Your class would look like this:
public class Pytaguras {
private double x, y, x1 ,y1 ;
public void setDouble(double _x, double _y, double _x1, double _y1) {
_x=x;
_y=y;
_x1=x1;
_y1=y1;
}
public static double calculate(double _x , double _y , double _x1 , double _y1 ) {
return Math.sqrt((_x-_y)*(_x-_y)+(_x1-_y1)*(_x1-_y1));
}
public String toString() {
return "Distance is " + Math.sqrt((x - y)*(x -y)+(x1 - y1)*(x1 - y1));
}
}
What probably went wrong?
When you tried to implement toString() like this:
// code omitted for clarity
public double calculate(double _x , double _y , double _x1 , double _y1 ) {
double s; // variable declared locally, thus can be used only inside this method
s = Math.sqrt((_x-_y)*(_x-_y)+(_x1-_y1)*(_x1-_y1));
return s;
}
// code omitted for clarity
public String toString() {
return s; // will give compiler-error, since symbol/variable "s" is not visible here, but only inside method calculate
}
// code omitted for clarity
Refactoring = optimizing your code
use static on utility-method : Your calculation method does not depend on any variables (x,y,x1,y1) declared inside the class, but on parameters only. So it is independent and can be made static. Thus would make it a utility-method that can be called from outside like Pytaguras.calculate(...). See Java Tutorial on static.
use constructor for initialization of variables: Your 2 points can be directly initialized by using a constructor. Thus you do not need to call both new Pytaguras() and setDouble(x1, y1, x2, y2). You could simply call new Pytaguras(x1, y1, x2, y2). See Java Tutorial on constructor.
You can simply assign the values through the class constructor.
public class Triangle {
private double x, y, x1 , y1;
public Triangle(double new_x, double new_y, double new_x1, double new_y1) {
x = new_x;
y = new_y;
x1 = new_x1;
y1 = new_y1;
}
public double calculate() {
return Math.sqrt((x-y)*(x-y)+(x1-y1)*(x1-y1));
}
}
And then on your main:
public static void main(String[]args) {
Scanner scan = new Scanner(System.in);
double x , y , x1 , y1;
Pytaguras Triangle = new Pytaguras();
System.out.println("Hello , this program is using Pytagoras formula" +
" on the 4 point that you enter");
System.out.println("Please enter 4 points to calculate the distance");
x = scan.nextDouble();
y = scan.nextDouble();
x1 = scan.nextDouble();
y1 = scan.nextDouble();
Triangle triangle = new Triangle(x, y, x1, y1);
System.out.println("the distance between 2 points is :" + triangle.calculate());
}

How do I call the first area method from the main method?

I need the area method that contains the formula for the area of a circle to take in the radius that was found by the other area method that calculated the distance...So one of the area methods calculated the distance/radius then I want that answer to be used in the the radius of the area method for the area of a circle.
public class AreaCircle {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner reader = new Scanner (System.in);
double x1, x2, y1, y2, xc, yc;
System.out.println("x1: ");
x1 = reader.nextInt();
System.out.println("x2: ");
x2 = reader.nextInt();
System.out.println("y1: ");
y1 = reader.nextInt();
System.out.println("y2: ");
y2 = reader.nextInt();
double distance = area(x1, x2, y1, y2);
System.out.println("The radius of the circle is: ");
System.out.println(distance);
double answer = area(distance);
System.out.println("The area of the circle is: ");
System.out.println(answer);
}
public static double area (double distance) {
double areaCircle;
double powRadius;
double radius = distance;
powRadius = Math.pow(radius, 2);
areaCircle = (Math.PI *(powRadius));
return areaCircle;
}
public static double area (double x1, double x2, double y1, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
double dsquared = dx*dx + dy*dy;
double distance = Math.sqrt (dsquared);
return distance;
}
What you have to do is give your area() method an input variable that is the radius
Like so:
public static double area(double radius) {
//same as before
double areaCircle;
double powRadius;
powRadius = Math.pow(radius, 2);
areaCircle = (Math.PI *(powRadius));
return areaCircle;
}
Also rename your other area() method. As is, it should be called something like returnDistance.

How do I properly fix my function calling (java)?

This is just a small portion of the code, since I feel like it would be enough to get my point across.
public class Line {
private double x1;
private double x2;
private double y1;
private double y2;
public Project2(double a,double b,double c,double d) {
x1 = a;
x2 = b;
y1 = c;
y2 = d;
}
public double length () { //the length of a line
double step1 = Math.pow((x1-x2),2);
double step2 = Math.pow((y1-y2),2);
double step3 = step1+step2;
double step4 = Math.sqrt(step3);
return step4;}}
import java.util.Scanner;
public class Project2Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter starting coordinates for a line (starting with x then y): ");
double a = input.nextInt();
double b = input.nextInt();
System.out.println("Enter ending coordinates for the line (starting with x then y): ");
double c = input.nextInt();
double d = input.nextInt();
Line line1 = new Line(a,b,c,d);
System.out.println("The length of the line is: " + line1.length());
Whenever I try to run it, it just gives length of the line as 0.0.
At first I tried to pass line1 as an argument, but then I realized that I couldn't (or at least didn't know how to) call it since I couldn't do Line.length(line1) or line1.length(line1).
You state:
I tried with just a simple 1,1 and 2,2. Expected output was 1.41, what I got was 0.0
You're confusing the order of parameters in your method. Your method lists x1, x2, y1, y2, and with this, 1, 1, 2, 2, will check the length of line from point 1, 2 to 1, 2 which is 0.0. I think that you want to change your parameter order to x1, y1, x2, y2.
In fact, you should clarify your parameter variables to make them self commenting. Get rid of a, b, c, d and change to: x1, y1, x2, y2:
public class Line {
// order changed
private double x1;
private double y1;
private double x2;
private double y2;
// parameter names clarified and order changed
public Project2(double x1, double y1, double x2, double y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}

Categories