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

Homework 5-4-2009

Do Project 7 of Chapter 4.

1.
2.
3.4.




---



2009年5月4日 星期一

Lab Method Overloading

依據Class definition 3,修改程式使其接受三種setDate

date1.setDate(1,2,2008);
date2.setDate("February",2, 2008);
date3.setDate(2008);



Lab Java Constructor

Write constructors in the lab Fraction Addition.





------程式碼------


public class FractionAddtion {

public static void main(String[] args)
{
Fraction f1 = new Fraction(1 ,2);
Fraction f2 = new Fraction(1 ,3);

System.out.println("f1 = " + f1.outputstring() + "\nf2 = " + f2.outputstring());
System.out.println("f1 + f2 = "+ f1.add(f2).outputstring());

}

}

---------------------------

public class Fraction {
private int numerator;
private int denominator;

public void number( int numerator, int denominator)
{
this.numerator = numerator;
this.denominator = denominator;
}
public String outputstring() {

return (numerator + "/" + denominator); //分數的表示形式
}

public Fraction(int x, int y) {
this.numerator = x;
this.denominator = y;
}

public Fraction add(Fraction Fraction2)
{
Fraction countOfTemp = new Fraction(numerator ,denominator);
countOfTemp.numerator = numerator*Fraction2.denominator + denominator*Fraction2.numerator;
countOfTemp.denominator = denominator*Fraction2.denominator;
//交叉相乘
return countOfTemp;
}





}

小考資訊

http://javaatcycu.blogspot.com/2009/05/5-18-2009.html

Homework 4-27-2009

Do project 2 of Chapter 4.

強烈要求同學一定要親自動手做,鼓勵同學將理論與實作密切配合。
相信只要同學真正的努力用功,縱使資質稍差,應該都可以學到Java程式設計之知識



homework

/*
* Fraction.java
*
* Created on 2009年5月4日, 下午 5:18
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package fraction;

/**
*
* @author KFN
*/
public class Fraction
{
private int numerator;
private int denominator;


public void number( int numerator, int denominator)
{
this.numerator = numerator;
this.denominator = denominator;
}
/**
* Creates a new instance of Fraction
*/
public String outputstring()
{

return (numerator + "/" + denominator); //分數的表示形式
}
public Fraction multiply(Fraction Fraction2)
{
Fraction countOfTemp = new Fraction();
countOfTemp.numerator = numerator*Fraction2.numerator;
countOfTemp.denominator = denominator*Fraction2.denominator;

int d = 0;
int a = countOfTemp.numerator;
int b = countOfTemp.denominator;
int c = 0;
do {

c = a % b;
int temp = a%b;
a = b;
b = temp;
d = a;

} while ( c != 0 );// end while
countOfTemp.numerator = countOfTemp.numerator / d;
countOfTemp.denominator = countOfTemp.denominator / d;
return countOfTemp;
}
public Fraction()
{
}

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Fraction f1 = new Fraction();
Fraction f2 = new Fraction();
f1.number(1, 2);
f2.number(1, 3);
System.out.println("f1 = " + f1.outputstring() + "\nf2 = " + f2.outputstring());
System.out.println("f1 * f2 = "+ f1.multiply(f2).outputstring());
// TODO code application logic here
}

}

Homework 4-13-2009 Fraction Multiplication

Write a program to implement a method that can multiply 2 fractions. You will implement a class called Fraction consisting of a numerator and a denominator. The multiplication of
2 fractions should be equal to a fraction.
Use 1/2 * 1/3 as the test.

Hints:
Fraction f1, f2;
f1.multiply(f2);





改成1/2 * 2/3


due date: 4/27/2009

沒有在期限內做出來
卡在通分卡太久
又死撐著不去看別人怎麼寫
不算藉口的藉口...