2009年5月25日 星期一

Lab Magic Parking Tower

A parking tower is out of order someday. If you park a Benz, you will end up with a Torben. Write a program to simulate this scenario. First create a class called CarParked which has a static method called outOfOrder. Name an object called yourCar, which happens to be a Benz. Your program should contain a class called CarParked and a test program called CarParkedDemo which test the method by CarParked.outOfOrder(yourCar).

Hint: You may study Display 5.14 to get some ideas.



-----


public class CarParked {
private String car;  //p275 3 name改car
public CarParked( String car )
{
this.car = car;
}
public CarParked()
{
car = " No car yet. ";
}
void setCar(String car)
{
this.car = car;
}
public static CarParked outOfOrder( CarParked car )
{
car.setCar( "Torben" ) ;
return car ;
}
public String toString()
{
return car;
}

}

----

public class CarParkedDemo {
public static void main (String args[]) {
CarParked car = new CarParked( "Benz" ) ;
 
System.out.println("進入停車場的車為" + car);
 
CarParked.outOfOrder( car );
 
System.out.println("離開時的車為" + car);
}

}

小考成績

全錯

連我覺得應該有分的也沒拿到

也不是沒看也不是沒念

但仍然會有點沮喪

仿佛在浪費時間似的

就像微積分

題庫全做完了

那又如何

還不是被當了

說實話還蠻DOWN的

2009年5月11日 星期一

Lab Static Method

Define a Complex class with a static method for computing complex addition. Use (2+3i)+(4+5i) in your test.




-------------
public class Complex {
int real;
int image;
public Complex()
{}
public Complex( int real , int image)
{
this.real = real;
this.image = image;
}
public static Complex add(Complex a, Complex b) {
Complex ans = new Complex();
ans.real = a.real + b.real;
ans.image = a.image + b.image;
return ans;
}
public String toString() {
return real + "+" + image + "i";
}
}

---------------
public class LabStaticMethod {
        public static void main (String args[]) {
 
 Complex f1, f2;
 f1 = new Complex(2, 3);
 f2 = new Complex(4, 5);
 
 System.out.println("f1 = " + f1.toString());
 System.out.println("f2 = " + f2.toString());
 System.out.println("f1 + f2 = " + Complex.add(f1, f2).toString());
   }
}

Lab Math methods

Compute the following mathematical functions.

Math.round(3.2)
Math.round(3.6)
Math.floor(3.2)
Math.floor(3.6)
Math.ceil(3.2)
Math.ceil(3.6)



----
public class LabMath {

/**
* @param args
*/
public static void main(String args[]) {
System.out.println("Math.round(3.2) = " + Math.round(3.2) );
System.out.println("Math.round(3.6) = " + Math.round(3.6) );
System.out.println("Math.floor(3.2) = " + Math.floor(3.2) );
System.out.println("Math.floor(3.6) = " + Math.floor(3.6) );
System.out.println("Math.ceil(3.2) = " + Math.ceil(3.2) );
System.out.println("Math.ceil(3.2) = " + Math.ceil(3.6) );
// TODO 自動產生方法 Stub

}

}

Lab Finding the max of three numbers

Write a static method that computes the maximum of three float numbers.

--------

import java.util.Scanner;
public class Maximum {
public static void main (String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("請輸入三個數字比大小");
float a = scan.nextFloat();
float b = scan.nextFloat();
float c = scan.nextFloat();
System.out.println("最大者為"+Maximum.Max(a, b, c));
}
public static float Max( float a,float b,float c )  
{
if( a > b && a > c)
return a;
else if( b > c && b >a )
return b;
else
return c;
}

}

Lab Finding the max of three numbers

Write a static method that computes the maximum of three float numbers.





HOMEWORK2

/*
* 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
}
}