星期一, 5月 15, 2006

Complex

package Lab7;
public class Complex
{
private double real,imaginary ;
public Complex(double realN, double imaginaryN )
{
this.real=realN;
this.imaginary =imaginaryN;
}
public Complex()
{
real = 0 ;
imaginary = 0;
}
public Complex(double newValue )
{
this.real= newValue;
this.imaginary =0;
}
public static Complex add(Complex C1,Complex C2)
{
Complex C3 = new Complex(0,0);
C3.real = C1.real + C2.real ;
C3.imaginary = C1.imaginary+C2.imaginary;
return C3;
}
public static String toString(Complex C3)
{
return Integer.toString((int)C3.real)+"+"+Integer.toString((int)C3.imaginary )+"i";
}
public void writeOutput()
{
System.out.println(real+"+" + imaginary + "i");
}

}

-------
package Lab7;

public class ComplexDemo {
public static void main(String[] args) {
Complex C1 = new Complex(2,3);
C1.writeOutput();
Complex C2 = new Complex(4,5);
C2.writeOutput();
Complex C3 = Complex.add(C1,C2);
System.out.println("The value is : "+C3.toString(C3));

}
}



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"));
}
}

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 Taiwan License.