1

So this is a rather simple question I'm sure for java and been answered before, I just can't seem to exactly find this answer. Prefer if someone could just comment the answer.

If you have

class Triangle{
private double x1,x2,x3,y1,y2,y3;
public Triangle(Point point1, Point point2, Point point3) 
{ 
x1=point1.getX();
y1=point1.getY();
x2=point2.getX();
y2=point2.getY();
x3=point3.getX();
y3=point3.getY();

//Trying to get x and y values of point1-point3

}

double width=x1-x2;
double length=y3-y2;

public double area() 
{ 
return (length * width)/2; 
} 

So basically I have points defined to take two variables x and y and I'm trying to calculate this area of a triangle. So someone gives 3 points to make this triangle and I'm trying to get those values from the points and I do have getters for my points but I'm just ending up with nothing for length and width.

3
  • 1
    add a getter: public int getId() { return id;} Commented Nov 15, 2014 at 2:36
  • Did you mean to write: this.newid = id;? Commented Nov 15, 2014 at 2:36
  • I updated to what it should look like. Commented Nov 15, 2014 at 3:00

1 Answer 1

1

If you want code outside of A to be able to access newid, then you need to add getter and/or setter methods to A.

For example:

public class A {

    private int newid;

    public A(int id){
        this.newid = id;
    }

    public int getNewid() {
        return this.newid;
    }

    public void setNewid(int id) {
        this.newid = id;
    }
}

Note that the this. qualification is not necessary in this particular example.

On the other hand, if you want to access newId within the class A, then ...

public class A {
    private int newid;

    ...

    public double b() {
       weight = newid * 5;
       // or 
       weight = this.newid * 5;
       ...
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

B is OPs another method, not a class.
Ok so I just put the main code I was trying to ask in an abstract way and I don't know if the way I asked made sense and hopefully clarified things.
In that case, the OP's teacher should be taken out the back and shot :-). You should NEVER start a Java method name with an uppercase letter, and if the OP's teacher hasn't taught him that in the first lesson, he needs to review his Java course design.
Now fix the classname. Classnames should ALWAYS start with an uppercase letter. Seriously dude, ignoring the Java style rules is the single best way to annoy people who are trying to read your code.
Oh nevermind, I thought you said they shouldn't be capitalized... I had it capitalized in the first place, I misread your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.