2005/12/19

Homework 12-5-2005

package welcome;

public class Temp
{


private double value;
private char scale;

public void writeout()
{
System.out.println(value + " " + scale);
}


public Temp()
{
this.value = 0;
this.scale = 'C';
}

public Temp(double value)
{
this.value = value;
this.scale = 'C';
}

public Temp(char scale)
{
this.value = 0;
if (scale == 'f' || scale == 'F')
{ this.scale = 'F'; }
else if (scale == 'c' || scale == 'C')
{ this.scale = 'C'; }
else
{
System.out.println("Error ");
this.scale = 'C';
}
}

public Temp(double value, char scale)
{
this.value = value;
if (scale == 'f' || scale == 'F')
{ this.scale = 'F'; }
else if (scale == 'c' || scale == 'C')
{ this.scale = 'C'; }
else
{
System.out.println("Error ");
this.scale = 'C';
}
}



public double getC()
{
double Tc = 0;
if (scale == 'C')
{ Tc = value; }
else if (scale == 'F')
{ Tc = (value - 32) * 5 / 9; }
return Tc;
}

public double getF() {
double Tf = 0;
if (scale == 'F')
{ Tf = value; }
else if (scale == 'C')
{ Tf = (value * 9 / 5) + 32; }
return Tf;
}

public void setValue(double value)
{ this.value = value; }

public void setScale(char scale)
{

if (scale == 'f' || scale == 'F')
{ this.scale = 'F'; }
else if (scale == 'c' || scale == 'C')
{ this.scale = 'C'; }
else
{ System.out.println("Error "); }

}

public void setTemp(double value, char scale)
{
if (scale == 'f' || scale == 'F')
{ this.scale = 'F'; }
else if (scale == 'c' || scale == 'C')
{ this.scale = 'C'; }
else
{ System.out.println("Error "); }
this.value = value;
}

public boolean equals(Temp otherTemp)
{
if ( this.scale == 'C' && this.value == otherTemp.getC() )
{ return true; }
else if ( this.scale == 'F' && this.value == otherTemp.getF() )
{ return true; }
else
{ return false; }
}

public boolean greater(Temp otherTemp)
{
if ( this.scale == 'C' && this.value >= otherTemp.getC() )
{ return true; }
else if ( this.scale == 'F' && this.value >= otherTemp.getF() )
{ return true; }
else
{ return false; }
}

public boolean smaller(Temp otherTemp)
{
if ( this.scale == 'C' && this.value <= otherTemp.getC() )
{ return true; }
else if ( this.scale == 'F' && this.value <= otherTemp.getF() )
{ return true; }
else
{ return false; }
}


}







package welcome;


public class TempDisplay
{

public static void main(String[] args)
{

Temp Tc_mp = new Temp(0.0, 'C');
Temp Tf_mp = new Temp(32.0, 'F');

Temp Tc_bp = new Temp(100.0);
Tc_bp.setScale('C');

Temp Tf_bp = new Temp(212.0);
Tf_bp.setScale('F');

Temp Tc_cold = new Temp();
Tc_cold.setTemp( -40.0, 'C');

Temp Tf_cold = new Temp();
Tf_cold.setScale('F');
Tf_cold.setValue( -40.0);

System.out.print("The Tc of melt point = ");
Tc_mp.writeout();
System.out.print("The Tf of melt point = ");
Tf_mp.writeout();
System.out.print("The Tc of boil point = ");
Tc_bp.writeout();
System.out.print("The Tf of boil point = ");
Tf_bp.writeout();
System.out.print("The Tc of cold = ");
Tc_cold.writeout();
System.out.print("The Tf of cold = ");
Tc_cold.writeout();

System.out.print("\nTest equals.\n");
System.out.println("Tf_bp" + " = " + "Tc_bp" + " ? " + Tf_bp.equals(Tc_bp));
System.out.println("Tc_mp" + " = " + "Tf_mp" + " ? " + Tc_mp.equals(Tf_mp));
System.out.println("Tc_cold" + " = " + "Tc_cold" + " ? " + Tf_cold.equals(Tc_cold));
System.out.println("Tf_mp" + " = " + "Tc_mp" + " ? " + Tf_mp.equals(Tc_mp));

System.out.print("\nTest smaller.\n");
System.out.println("Tc_mp" + " <= " + "Tc_cold" + " ? " + Tc_mp.smaller(Tc_cold));
System.out.println("Tc_mp" + " <= " + "Tf_bp" + " ? " + Tc_mp.smaller(Tf_bp));
System.out.println("Tf_mp" + " <= " + "Tc_cold" + " ? " + Tf_mp.smaller(Tc_cold));
System.out.println("Tf_mp" + " <= " + "Tc_bp" + " ? " + Tf_mp.smaller(Tc_bp));

System.out.print("\nTest geater\n");
System.out.println("Tc_mp" + " >= " + "Tf_bp" + " ? " + Tc_mp.greater(Tc_bp));
System.out.println("Tc_mp" + " >= " + "Tf_cold" + " ? " + Tc_mp.greater(Tf_cold));
System.out.println("Tf_mp" + " >= " + "Tf_bp" + " ? " + Tf_mp.greater(Tf_bp));
System.out.println("Tf_mp" + " >= " + "Tf_cold" + " ? " + Tf_mp.greater(Tf_cold));
}

}


0 Comments:

張貼留言

<< Home