Java- Accesing instances of a class in a method

点点圈 提交于 2019-12-13 09:25:39

问题


I have to write a code for class that takes in a temperature value, converts it to kelvin, and then determines if this is greater than, equal to, less than, or whatever to another entered temp. It has to say true or false for each of the boolean possibilities. I can't get it to compare the temperature that is set as t1, with the temperature t that the boolean methods take as a parameter? Any suggestions? Also, quit downvoting me, I know I don't know what I'm doing thats why I asked the queston. I started learning Java last week, I know that I'm not good at it?

public class Temperature {

   public double degrees;
    public static void main(String[] args) {

}


    Temperature (){
   degrees=0;
}

Temperature (double enteredtemp){
    degrees = enteredtemp;
}
Temperature (double enteredtemp,char scale ){
   Temperature t1 = new Temperature (enteredtemp, scale);
   t1.set(enteredtemp,scale);


}
public void set (double enteredtemp, char scale){
       if (scale == 'r'|| scale == 'R'){ degrees = (enteredtemp/(9/5));}
       else if (scale == 'c'|| scale == 'C') {degrees = enteredtemp+273.15;}
       else if (scale =='F'|| scale == 'f'){degrees = ((enteredtemp+459.67)*9/5);}



}

public double get(){
    return degrees;
}
 public double get(char scale){
    if (scale == 'c'|| scale == 'C'){degrees = (degrees-273.15);}
    else if (scale == 'r'||scale == 'R'){degrees = (degrees*(9/5));}
    else if (scale == 'f'|| scale == 'F'){degrees = (degrees*(9/5)-459.67);}
    return (degrees);
            }


 public boolean isLessThan(Temperature t){

 if (t.get() < t1.get())
 return true;
 else {
 return false;
 }

 }
 public boolean isGreaterThan(Temperature t){
     if (t.get() > t1.get()) {
     return true;
     }
     else {
     return false;
     }
 }
 public boolean isEqual(Temperature t){
     if ((Math.abs(t.get() - t1.get()))<=10E-12){
     return true;
     }
     else {
     return false;
     }

 }

 public boolean isGreaterThanOrEqual(Temperature t){
     if (t.get() >= t1.get()){
     return true;
     }
     else {
     return false;
     }

 }
 public boolean isLessThanorEqual(Temperature t){
     if (t.get() <= t1.get()){
     return true;
     }
     else {
     return false;
     }
 } 
}

回答1:


In your functions of this form

public boolean isGreaterThan(Temperature t){
  if (t.get()>t1.get()) {return true;}
  else {return false;}
}

you are comparing t with a non-existent t1. You want to be comparing the current object (this) with t, the parameter you've been passed.

public boolean isGreaterThan(Temperature t){
  if (this.get()>t.get())
    return true;
  else
    return false;
}

Note also that in your constructor that takes two parameters, you're not setting your object properties at all: you're creating a new t1, setting its properties, and then discarding it. Call this.set rather than t1.set.




回答2:


Make it compile first

Your code as posted doesn't compile because you are using unknown variables like t1 here:

public boolean isGreaterThan(Temperature t){
     if (t.get()>t1.get()) {return true;}
     else {return false;}
 }

You want to drop the t1. there.

Don't make changes in get

It's counter intuitive and a bad practice to have a method named get that changes the fields of a class. So instead of this:

public double get(char scale) {
  if (scale == 'c' || scale == 'C') {
      degrees = (degrees - 273.15);
  } else if (scale == 'r' || scale == 'R') {
      degrees = (degrees * (9 / 5));
  } else if (scale == 'f' || scale == 'F') {
      degrees = (degrees * (9 / 5) - 459.67);
  }
  return (degrees);
}

Rewrite like this:

public double get(char scale) {
    if (scale == 'c' || scale == 'C') {
        return degrees - 273.15;
    } else if (scale == 'r' || scale == 'R') {
        return degrees * 9 / 5;
    } else if (scale == 'f' || scale == 'F') {
        return degrees * 9 / 5 - 459.67;
    }
    return degrees;
}

I also removed unnecessary parentheses.

Simplify

Also, most of your methods could be greatly simplified, for example:

public boolean isLessThan(Temperature t) {
    return get() < t.get();
}

public boolean isGreaterThan(Temperature t) {
    return get() > t.get();
}

public boolean isEqual(Temperature t) {
    return Math.abs(t.get() - get()) <= 10E-12;
}

public boolean isGreaterThanOrEqual(Temperature t) {
    return get() >= t.get();
}

public boolean isLessThanOrEqual(Temperature t) {
    return get() <= t.get();
}

Improve

(I know you commend your prof "doesn't like" switch (and he's wrong), but leaving this anyway for your reference.)

I also recommend rewriting set like this:

public void set(double enteredtemp, char scale) {
    switch (scale) {
        case 'r':
        case 'R':
            degrees = (enteredtemp / (9 / 5));
            break;
        case 'c':
        case 'C':
            degrees = enteredtemp + 273.15;
            break;
        case 'f':
        case 'F':
            degrees = ((enteredtemp + 459.67) * 9 / 5);
            break;
    }
}


来源:https://stackoverflow.com/questions/25702584/java-accesing-instances-of-a-class-in-a-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!