But, since you posted it here I couldn't resist adding/tweaking it a bit
Among the useless things I did was renaming getC and GetF, mostly
because getC rings like an age old io function to me...
Code:import java.util.*; import java.text.*; /** * A sortable, formatted and printable temperature class * */ public class Temperature implements Comparator { //Class name /** * format for printing temperature values */ static DecimalFormat df=new DecimalFormat("0.0"); double temp; //Variable char scale; //Varible public Temperature() { //Default Constructor temp = 0; scale = 'C'; } public Temperature(char tempScale) { //Scale Constructor temp = 0; scale = tempScale; } public Temperature(double tempValue) { //Temp Constructor temp = tempValue; scale = 'C'; } public Temperature(double tempValue, char tempScale) { //Constructor temp = tempValue; scale = tempScale; } public double getCelsius() { //Used to get degrees C double value; if (scale == 'C') { return temp; } else { return ( (double) (Math.round( (5 * (temp - 32.0) / 9.0) * 10.0)) / 10); } } public double getFarenheit() { //Used to get degrees F if (scale == 'F') { return temp; } else { return (Math.round( (9 * (temp / 5) + 32) * 10) / 10); } } public void setTemp(double newTemp) { //set temp temp = newTemp; } public void setScale(char newScale) { //set scale scale = newScale; } public void setTempScale(double newTemp, char newScale) { //set both temp = newTemp; scale = newScale; } /** * return a formatted temperature string * @return */ public String toString () { return df.format(temp)+" "+scale; } /** * the equals method should be implemented as an override of the * Object class' equals method, parameter should be an Object. * Note: the two next metods define the Comparator interface * Among othjer things this enables sorting. * @param obj * @return */ public boolean equals(Object obj) { //compares for equality if (!(obj instanceof Temperature)) { return false; } Temperature aTemp=(Temperature)obj; return (getCelsius() == aTemp.getCelsius()); } /** * Method used by Collections.sort * @param o1 * @param o2 * @return */ public int compare(Object o1, Object o2) { Temperature t1=(Temperature)o1; Temperature t2=(Temperature)o2; // switch t1 & t2 here to get descending sort return (int)(t1.getCelsius()*10.0d-t2.getCelsius()*10.0d); } // This is comparing statements to see if the first is greater than the second public boolean greaterThan(Temperature obj) { return (getCelsius() > obj.getCelsius()); } // This is comparing statements to see if the first is less than the second public boolean lessThan(Temperature obj) { return (getCelsius() < obj.getCelsius()); } // Returns temp value public double getTemperatureTemp() { return temp; } // Returns scale character public char getTemperatureScale() { return scale; } public static void main(String[] args) { Temperature t = new Temperature(100.5, 'C'); System.out.println("\n\nt:"+t); // stuff an arrayList with temperatures and sort them on actual heat value ArrayList tList=new ArrayList(); tList.add(t); tList.add(new Temperature(140.5,'F')); tList.add(new Temperature(60,'C')); tList.add(new Temperature(90,'F')); tList.add(new Temperature(70.3,'C')); tList.add(new Temperature(.7,'C')); // the 'new Temperature()' parameter here is just a dummy for using the // Temperature class' Comparator Collections.sort(tList,new Temperature()); System.out.println("Sorted temperatures"); for (int i=0; i<tList.size(); i++) { System.out.println(tList.get(i)); } } }



Reply With Quote


Bookmarks