Homework 5-8-2005 Temp
package lab7;public class Temperature
{
private double degree;
private String scale;
public void assumeScale ()
{
scale = "C" ;
}
public void assumeDegree ()
{
degree= 0 ;
}
public void assumeF (float degreeF)
{
degreeF= 0 ;
}
public void assumeC (float degreeC)
{
degreeC= 0 ;
}
// constructor 1
public double calDegreeF (double degreeInput,String scale )
{
if (scale.equalsIgnoreCase("C") )
{
this.degree = (9 * (degreeInput / 5)) + 32;
}
else
{
this.degree = (5*(degreeInput - 32)/9);
}
return (degree);
}
// public double calDegreeC (double degreeF,String scale )
//{
//return (degree);
public void setscaleC(String scaleInput)
{
this.scale = scaleInput;
}
public String getScale()
{
return(scale);
}
}
----------------------
package lab7;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TemperatureDemo
{
public static void main(String[] args)throws IOException
{
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System. in));
System.out.println("Enter degrees :");
Temperature temperature = new Temperature();
System.out.println("Equivalent Celsius temperature is "+ temperature.calDegreeF( 0.0,"C"));
// set the scale
temperature.setscaleC("F");
System.out.println("Equivalent Celsius temperature is "+ temperature.calDegreeF(32.0,temperature.getScale() ));
System.out.println("The scale is "+ temperature.getScale() );
System.out.println("Equivalent Celsius temperature is "+ temperature.calDegreeF( -40.0,"C"));
System.out.println("Equivalent Celsius temperature is "+ temperature.calDegreeF( 100.0,"C"));
}
}
0 Comments:
張貼留言
<< Home