/*
* Tempurate.java
*
* Created on 2009年5月11日, 下午 2:41
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package homework;
/**
*
* @author KFN
*/
public class Temperature {
float degree ;
String scale ;
/** Creates a new instance of Tempurate */
public Temperature() {
degree = 0 ;
scale = "C";
}
public Temperature( float degree ) {
this.degree = degree;
scale = "C";
}
public Temperature( String scale ) {
degree = 0 ;
this.scale = scale ;
}
public Temperature( float degree, String scale ) {
this.degree = degree;
this.scale = scale ;
}
public float Change( String type ){
if ( this.scale.equals( type ) )
return degree; // 兩個種類相同的話直接傳入
else {
if ( type.equals( "F" ) )
return ( ( degree / 5 ) * 9 ) + 32 ;
else if( type.equals( "C" ) )
return 5 * ( degree - 32 ) / 9 ;
else
return 0;
}
}
public String toString(){
return this.degree + " degree " + this.scale ;
}
public boolean Equals( Temperature other ) {
if ( this.scale.equals( other.scale ) ) {
if ( this.degree == other.degree )
return true;
else
return false;
} else {
if ( this.degree == other.Change(this.scale) )
return true;
else
return false;
}
}
public boolean Greater( Temperature other ) {
if ( this.scale.equals( other.scale ) ) {
if ( this.degree > other.degree )
return true;
else
return false;
} else {
if ( this.degree > other.Change( this.scale ) )
return true;
else
return false;
}
}
public boolean Less( Temperature other ) {
if ( this.scale.equals( other.scale ) ) {
if ( this.degree <>
return true;
else
return false;
} else {
if ( this.degree <>
return true;
else
return false;
}
}
}
------------------------
/*
* Main.java
*
* Created on 2009年5月11日, 上午 11:40
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package homework;
/**
*
* @author KFN
*/
import java.util.Scanner;
public class Main {
/** Creates a new instance of Main */
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
/*
Scanner scan =new Scanner(System.in);
System.out.println("請輸入溫度");
float n = scan.nextFloat();
System.out.println("請選擇攝氏'C'或是華氏'F'");
String scale = scan.next();
if(scale.equals("C"))
{
float changeF = ((n/5)*9)+32;
System.out.println("您輸入攝氏"+n+"度");
System.out.println("轉換為華氏為"+changeF+"度");
}
else if(scale.equals("F"))
{
float changeC = 5*(n-32)/9 ;
System.out.println("您輸入華氏"+n+"度");
System.out.println("轉換為攝氏為"+changeC+"度");
}
else
{
System.out.println("請選擇攝氏'C'或是華氏'F'");
}
*/
Temperature t1 = new Temperature( 100, "C");
Temperature t2 = new Temperature( 212, "F" );
Temperature t3 = new Temperature( "C" );
Temperature t4 = new Temperature( "F" );
// question 1.
System.out.println("t1 " + t1.toString() );
System.out.println("t2 " + t2.toString() );
System.out.println("t3 " + t3.toString() );
System.out.println("t4 " + t4.toString() );
System.out.println("");
//question 2.
if ( t1.Equals( t2 ) )
System.out.println("t1 is equals to t2 " );
else
System.out.println("t1 is not equals t2 " );
if ( t2.Greater( t4 ) )
System.out.println("t2 is greater than t4 " );
else
System.out.println("t2 is not greater than t4 " );
if ( t2.Less( t4 ) )
System.out.println("t2 is Less than t4 " );
else
System.out.println("t2 is not Less than t4 " );
// TODO code application logic here
}
}