0

Here is a portion of the program I am writing:

public class Triangle {
double a, b, c;
private boolean t,s,r,e,i;
String Triangle, Scalene, Right;
public Triangle(double a, double b, double c) {
    //I dont know what to put here...
    System.out.print(a + " " + b + " " + c);

}
public boolean isTriangle() {
    t = true;
    System.out.print(a + " " + b + " " + c);
    if(a + b > c) {
        if(a + c > b) {
            if(b + c > a) {
                //boolean remains true
                t = true;
            }
        }
    }
    else {
        //boolean is false
        t = false;
    }
    return t;
}

The problem I am having is that my variable data wont transfer over from one method to the other even though the variables are declared in the class. Part of my assignment is to work with bluej (The awful program that it is) which is how I am entering the data. The print statement shows that the data I entered is stored in the variables in the method: Triangle, but not in the method: isTriangle. The compiler also wont let me use a return a; or anything like that. What am I doing wrong?

1
  • Yes because the triangle method is using variables inside triangle constructor but the variable in istriangle are global variables defined at the top. Best would be assigning the variables the value to your global variables like this. a = a; this.b = b Commented Apr 6, 2016 at 22:50

3 Answers 3

1

change your constructor code as below

public Triangle(double a, double b, double c) {

    this.a = a;
    this.b = b;
    this.c = c;

    //I dont know what to put here...
    System.out.print(a + " " + b + " " + c);

}

You need to set the value in class variable once received in constructor.

Sign up to request clarification or add additional context in comments.

7 Comments

Why? You need to explain what the OP is doing wrong. That's what he actually asked.
See explanation above.
didn't I explained what is wrong in my answer after the code?
No. You didn't. My downvote stands. Please read the >>text<< of the question to understand what the OP is actually asking. Simply giving a solution does not cut it. You need to explain what what he was doing was wrong.
@StephenC I am not sure what are you expecting in explanation. It is straightforward. Data from constructor need to be set in class variables. If there is anything else which I missed please state that in your answer and I will upvote it
|
1

If you set your global class fields from the constructor you have declared, a,b,c will be used by every method referring to them.

public class Triangle {
double a = 0.0;
double b = 0.0;
double c = 0.0;
private boolean t,s,r,e,i;
String Triangle, Scalene, Right;
public Triangle(double a, double b, double c) {
    this.a = a;
    this.b = b;
    this.c = c;
    System.out.print(a + " " + b + " " + c);

}
public boolean isTriangle() {
    t = true;
    System.out.print(a + " " + b + " " + c);
    if(a + b > c) {
        if(a + c > b) {
            if(b + c > a) {
                //boolean remains true
                t = true;
            }
        }
    }
    else {
        //boolean is false
        t = false;
    }
    return t;
}

2 Comments

And still -1, that's sweet :P
This answer has the same problem. And worse still, you have duplicated his other major mistake without comment.
0

The problem I am having is that my variable data wont transfer over from one method to the other even though the variables are declared in the class.

Lets look at the code

double a, b, c;

public Triangle(double a, double b, double c) {
    //I dont know what to put here...
    System.out.print(a + " " + b + " " + c);
}

In the above version, the values of a, b and c in the constructor won't transfer to the a, b and c fields in the constructor.

Why? Because they are different variables. The parameters of a constructor are local to the constructor (just like the parameters of a method). They go away when the constructor ends. In this case, you have used the same names for the two sets of variables, but that doesn't make them the same variables.

To make the values transfer across you need to assign them. But that raises a second issue. The way you have named the constructor parameters, they are shadowing the corresponding field names. In other words, within the constructor, the identifier a refers to the parameter not the field. So a = a; would actually being the value of the a parameter to itself. Instead you need to use this to qualify references to the fields that have been shadowed. Thuss

public Triangle(double a, double b, double c) {
    this.a = a;
    this.b = b;
    this.c = c;
}

The print statement shows that the data I entered is stored in the variables in the method: Triangle, but not in the method: isTriangle.

Actually, that isn't what it is showing. The print statement in the constructor is actually printing the values of the parameters a, b and c. Shadowing again.


The compiler also wont let me use a return a; or anything like that. What am I doing wrong?

It is not clear where (or why) you are trying to put return a;, but it won't work in this class.

  • If you are trying to put that statement into the constructor, a constructor can never return a value.

  • If you are trying to put it into the isTriangle method, that won't work because the return type for that method is boolean not double.


Now I want to talk about some other mistakes in your code (as written)

double a, b, c;

This is a poor design choice. Unless you have a good reason for doing otherwise, you should be declaring all of your variables as private. If you don't then your Triangle class will be a "leaky abstraction". It doesn't matter much in a tiny application, but in a larger problem you can get other classes reading or modifying the values of these fields of your Triangle.

  • Modifying the fields from another class is liable to break the Triangle, and in a large and complex class it can be hard to track down where the breakage happened.
  • Even if some other class just reads the non-private fields, that means that you have an unwanted dependency (a "coupling") between the Triangle class and the other class or classes.

Next:

String Triangle, Scalene, Right;

Same problem as above, and another one (actually two). The names of variables / fields should start with a lower case letter. Names like Triangle, Scalene and Right should only be used for classes. (And the other problem is that you already have a class called Triangle ... which would make life "interesting".)

Finally:

private boolean t ...

This is actually very wrong. In your code, t is being used within the isTriangle method to hold a working variable. It should not be declared as a field. It should be declared as a local variable in that method.

Why? Two reasons:

  1. By making t a field rather than a local variable declared in the isTriangle method, you are causing that field to be reserved for the lifetime of the Triangle object. That is a (small) waste of space, since the variable is only (currently) needed for the duration of an isInstance call.

  2. This is the more important reason. By using a field to hold this local variable, you have made the isTriangle method non-reentrant.

    • If the code was a bit different and isTriangle called itself (in a useful way), then the nested call to isTriangle would trample the caller's copy of t.

    • If the code was a bit different and isTriangle could be called from multiple threads, then one thread's calls could trample on the t values being used by another thread.

    The fact that these things could happen means that your simple Triangle class suddenly becomes more difficult to understand. You need to consider more things before you can be sure that your application is correct, and will stay that way.

1 Comment

Thank you for that in depth analysis of my code. I've learned s few things from what you posted. In the case of the variable t I was using it in a few place in my code and it all needed to be the same variable no matter the method. I ended up changing it later on and I was able to eliminate the use for t all together. Many of the other errors I wouldn't have made if it wasn't for the assignment parameters. Others I wont be making again because of your post.

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.