I'm supposed to make a class and a runner that takes in 4 variables and uses them to calculate the slope of the line. My problem is I don't know how to format my output so that I don't have such a long decimal.The formatting output I am supposed to use is: out.println(String.format("%.3f",dec)) Here is the code for my class.
public class Line
{
private int xOne;
private int yOne;
private int xTwo;
private int yTwo;
private double slope;
public Line(int x1, int y1, int x2, int y2)
{
int xOne = x1;
int yOne = y1;
int xTwo = x2;
int yTwo = y2;
}
public void setCoordinates(int x1, int y1, int x2, int y2)
{
xOne = (int) x1;
yOne = (int) y1;
xTwo = (int) x2;
yTwo = (int) y2;
}
public void calculateSlope( )
{
slope = (yTwo - yOne) / (xTwo - xOne);
}
public void print( )
{
System.out.println( "The slope is " + slope);
}
}
And here is the code for my runner.
public class LineRunner
{
public static void main( String[] args )
{
Line test = new Line(1,9,14,2);
test.calculateSlope();
test.print();
test.setCoordinates(1,7,18,3);
test.calculateSlope();
test.print();
}
}
Any help would be awesome!
You can use DecimalFormat:
DecimalFormat twoDForm = new DecimalFormat("#.##");
System.out.println(Double.valueOf(twoDForm.format(slope)));
You can even set how you want to do the rounding if needed, eg:
twoDForm.setRoundingMode(RoundingMode.CEILING);
Related
When I run it, it returns 0.0 which leads me to believe the calculation is not taking the user input.
I know I am missing something obvious but I've been looking at it too long.
Please help!
Input of x1 = -2, y1 = -3, x2 = -4, y2 = 4, should return 7.28
public class CalculateDistance {
private double x1, y1; //position of first point
private double x2, y2; //position of second point
public double getx1() {
return x1;
}
public void setx1(double x1){
this.x1 = x1;
}
public double gety1() {
return this.y1;
}
public void sety1(double y1){
this.y1 = y1;
}
public double getx2() {
return x2;
}
public void setx2(double x2){
this.x2 = x2;
}
public double gety2() {
return this.y2;
}
public void sety2(double y2){
this.y2 = y2;
}
public double calculateDistance; { //calculation of (x2-x1)squared + (y2-y1)squared
calculateDistance = Math.sqrt((((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)))); //formula which the square root of will be the distance
}
}
This is my main class
import java.util.Scanner;
public class TestCalculateDistance {
public static void main(String[] args) { //method
CalculateDistance position = new CalculateDistance();
Scanner input = new Scanner(System.in);
System.out.println("To calculate the distance between two points, please enter the following values; " +
"\nFor the first point, What is the value of x? ");//asking user to enter the x position
position.setx1(input.nextDouble());
System.out.println("What is the value of y? ");
position.sety1(input.nextDouble());
System.out.println("For the second point, What is the value of x? ");
position.setx2(input.nextDouble());
System.out.println("What is the value of y? ");
position.sety2(input.nextDouble());
System.out.println("The total distance between the two points is "
+ position.calculateDistance);
}
}
calculateDistance should be defined and called as a method and return the calculated result as follows:
In CalculateDistance class
public double calculateDistance() { //calculation of (x2-x1)squared + (y2-y1)squared
return Math.sqrt((((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)))); //formula which the square root of will be the distance
}
In the main class:
System.out.println("The total distance between the two points is "
+ position.calculateDistance());
Im trying understand what am I doing wrong here
import java.lang.Math
public class QuadraticEquation {
public static Roots findRoots(double a, double b, double c) {
double d = b*b-4*a*c;
double x1 = (-b + Math.sqrt(d))/2*a;
double x2 = (-b - Math.sqrt(d))/2*a;
Roots( x1, x2);
}
public static void main(String[] args) {
Roots roots = QuadraticEquation.findRoots(2, 10, 8);
System.out.println("Roots: " + roots.x1 + ", " + roots.x2);
}
}
class Roots {
public final double x1, x2;
public Roots(double x1, double x2) {
this.x1 = x1;
this.x2 = x2;
}
}
Obviously it gives me a error: cannot find symbol on last line of public static Roots findRoots but I don't understand what other way of calling the mutator is there
What's wrong with replacing
Roots(x1, x2);
with
return new Roots(x1, x2);
?
Also, I don't know what your understanding of a "mutator" is, but the keyword you might want to look up in your Java beginners guide is "constructor".
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;
}
So I have this triangle class I need to create using an abstract class. It will also be drawn by a tester class. I am part of the way through it but I am having serious issues with the math portion. I have set the coordinates in the tester class, I have no idea of how to get the pen to turn a certain degree to draw the next side of the triangle. Attached is all the classes and I have so far. Any help will be appreciated.
Tester class
import TurtleGraphics.*;
public class TestShapes1 {
public static void main (String[] args) {
// Declare and instantiate a pen, a circle and a wheel
Pen p = new StandardPen();
//Shape s1 = new Circle1 (20, 20, 20);
//Shape s2 = new Wheel1 (-20, -20, 20, 6);
Shape1 t2 = new Triangle1 (0, 0, 50, 0, 0, 30);
// Draw the circle and wheel
//s1.draw (p);
t2.draw (p);
}
}
Shape Class
import TurtleGraphics.Pen;
public interface Shape1 {
public double area();
public void draw (Pen p);
public double getXPos();
public double getYPos();
public void move (double xLoc, double yLoc);
public void stretchBy (double factor);
public String toString();
}
Triangle Class
import TurtleGraphics.Pen;
public class Triangle1 implements Shape1 {
private double x1, y1, x2, y2, x3, y3;
private double s1, s2, s3;
private double d1, d2;
//private double height, width;
public Triangle1() {
x1 = 0;
y1 = 0;
x2 = 1;
y2 = 0;
x3 = 0;
y3 = 1;
//height = 1;
//width = 1;
}
public Triangle1 (double xLoc1, double yLoc1, double xLoc2, double yLoc2, double xLoc3, double yLoc3) {
x1 = xLoc1;
y1 = yLoc1;
x2 = xLoc2;
y2 = yLoc2;
x3 = xLoc3;
y3 = yLoc3;
//height = h;
//width = w;
}
public double area() {
return (Math.abs(x1*y2-x2*y1+x2*y3-x3*y2+x3*y1-x1*y3))/2.0;
}
public void draw (Pen p) {
s1 = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
s2 = Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
s3 = Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
p.up();
p.move (x1, y1);
p.down();
p.setDirection (0);
p.move (s1);
d1 = (Math.acos((s2*s2+s3*s3-s1*s1)/(2.0*s2*s3)))*180/Math.PI;
p.turn (180 - d1);
p.move (s2);
d2 = (Math.acos((s3*s3+s1*s1-s2*s2)/(2.0*s3*s1)))*180/Math.PI;
p.turn (180 - d2);
p.move (s3);
p.turn (-90);
//p.move ();
}
public double getXPos() {
return x1;
}
public double getYPos() {
return y1;
}
public void move (double xLoc, double yLoc) {
x1 = x1 + xLoc;
y1 = y1 + yLoc;
x2 = x2 + xLoc;
y2 = y2 + yLoc;
x3 = x3 + xLoc;
y3 = y3 + yLoc;
}
public void stretchBy (double factor) {
x1 *= factor;
y1 *= factor;
}
public String toString() {
String str = "TRIANGLE\n";
// + "Width & Height: " + width + " & " + height +"\n"
// + "(X,Y) Position: (" + xPos + "," + yPos + ")\n"
// + "Area: " + area();
return str;
}
}
You don't need any math. Just pass the degrees to p.turn(). So use
p.turn(180);
instead of
d1 = (Math.acos((s2*s2+s3*s3-s1*s1)/(2.0*s2*s3)))*180/Math.PI;
p.turn (180 - d1);
See the documentation for reference:
The degrees can be an integer or floating-point number. Example:
pen.turn(-45); Rotate the pen 45 degrees clockwise.
I need to implement a Triangle class and im stuck on comparing the lengths of the sides to determine if the triangle is indeed an isosceles. Here is what I have so far:
public class TriangleIsosceles {
private Point cornerA;
private Point cornerB;
private Point cornerC;
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
public TriangleIsosceles(){
cornerA = new Point(0,0);
cornerB = new Point(10,0);
cornerC = new Point(5,5);
}
public TriangleIsosceles(int x1,int y1,int x2,int y2,int x3,int y3){
cornerA = new Point(x1,y1);
cornerB = new Point(x2,y2);
cornerC = new Point(x3,y3);
}
public String isIsosceles(String isIsosceles){
return isIsosceles;
}
}
The Point object im using is this:
public class Point {
private int x;
private int y;
public Point(){
this(0,0);
}
public Point(int x, int y){
this.x = x;
this.y = y;
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public void printPoint(){
System.out.println(x + y);
}
public String toString(){
return "x = "+x+" y = "+y;
}
}
In another class (LineSegment) I created a method length() that determines the distance of two points. Which looks like:
public double length() {
double length = Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2));
return length;
}
How can I use this method to help me find the lengths of the triangle in my TriangleIsosceles class?
I know I need to see if (lenghtAB == lengthBC || lengthBC == lenghtCA || lengthAB == lengthCA).
A quick, perfectly valid, solution would be to make your length method a static utility method, i.e.
public static double length(x1, y1, x2, y2)
{
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}
or
public static double length(Point p1, Point p2)
{
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
You could also add the method to Point itself, i.e. in the Point class add:
public double calcDistance(Point otherPoint)
{
return Math.sqrt(Math.pow(this.x - otherPoint.x, 2) + Math.pow(this.y - otherPoint.y, 2));
}
Assuming your LineSegment class has a constructor that takes two Point objects, you should create three LineSegment objects (which you can cache in the Triangle class). Then using LineSegment#getLength() you can determine if any two sides are the same length.
Since this looks like homework I won't give you the full solution.