Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

Java- Accesing instances of a class in a method

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;
     }
 } 
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...