简体   繁体   中英

Programs without a main method in java and variables

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?

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;
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM