Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've never used Java, one of my programming friends gave me this to try and get used to Java.

He gave me the test.java file along with some pointers for making the line class. However, I still don't know how to get the slope or y_int. I assume for vert I continue to use a Boolean and if/else statement but I am a bit lost.

this is the Test.java:
Java
public class Test {
public static void main(String [] args){
    Line line1 = new Line(0,1);
    Line line2 = new Line(0.5,2);
    Line line3 = new Line(1);
    System.out.println("The slope of "+ "line is ")+line1.getSlope();
    System.out.println("It is"+line3.vert)+ "that line3 is vertical"();
    System.out.println("the slope of line3")+ "is "+line3.getSlope();
}
}



this is the Line.java Class
Java
public class Line {

    public class Line {

    public double slope;
    public double y_int;
    public double x_int;
    public boolean vert;

    public Line(double slope, double y_int){
        this.slope = slope;
        this.y_int = y_int;
        vert = false;
    }

    public Line(double x_int) {
        slope = Double.NaN;
        y_int = Double.NaN;
        vert = true;
        this.x_int = x_int;
    }

    public double getSlope() {
        return slope;
    }

    public double getY_int() {
        return y_int;
    }

    public boolean isVert() {
        return vert;
    }
}


What I have tried:

I suppose it comes down to me not being the best at math. I obviously understand the slope formula but translating to code has proven difficult.
Posted
Updated 17-Sep-17 14:38pm
v2

1 solution

 
Share this answer
 
Comments
Member 13414583 17-Sep-17 20:43pm    
I did actually search google vigorously and found those two pages prior to asking on here, I just didn't quite follow how each were arriving at the solution. Partially because of how differently they were attacking the problem, and also due to me seemingly being inept. I have used python in the past... 3 years ago. Haven't touched code since aside from some CSS (which really isn't coding at all)
Graeme_Grant 17-Sep-17 20:50pm    
They're both working solutions. Use your debugger, set a breakpoint and step through the code to see what they are doing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900