2009年5月25日 星期一

Lab: Static Method II

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

class程式碼
------------------------------------------------<程式碼開始>--------------------------------------
public class ADTClass {
private int realNum = 0, imaginaryNum = 0;
public ADTClass(int realNum, int imaginaryNum) {
this.realNum = realNum;
this.imaginaryNum = imaginaryNum;
}
public ADTClass multiplication(ADTClass f) {
ADTClass ans = new ADTClass(1, 1);
ans.realNum = this.realNum * f.realNum - this.imaginaryNum * f.imaginaryNum;
ans.imaginaryNum = this.imaginaryNum * f.realNum + f.imaginaryNum * this.realNum;
return ans;
}
public String toString() {
return realNum + "+" + imaginaryNum + "i";
}
}
------------------------------------------------<程式碼結束>--------------------------------------

主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class StaticMethodII {
    public static void main (String args[]) {
ADTClass f1, f2;
f1 = new ADTClass(2, 3);
f2 = new ADTClass(4, 5);
    System.out.println("f1 = " + f1.toString());
System.out.println("f2 = " + f2.toString());
System.out.println("f1 x f2 = " + f1.multiplication(f2).toString());
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

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.

class程式碼

------------------------------------------------<程式碼開始>--------------------------------------
public class CarParked {
private String name;
public CarParked(String YourCar) {
this.name = YourCar;
}
void setCar (String YourCar) {
this.name = YourCar;
}
static CarParked outOfOrder(CarParked name) {
name.setCar("Torben");
return name;
}
public String toString() {
return name;
}
}
------------------------------------------------<程式碼結束>--------------------------------------

主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class CarParkedDemo {
public static void main (String args[]) {
CarParked Car = new CarParked("Benz");
        System.out.println("parking " + Car);
CarParked.outOfOrder(Car);
System.out.println("out " + Car );
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

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.



------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
class ADTClass {
private int realNum = 0, imaginaryNum = 0;
public ADTClass(int realNum, int imaginaryNum) {
this.realNum = realNum;
this.imaginaryNum = imaginaryNum;
}
public static ADTClass add(ADTClass a, ADTClass b) {
ADTClass ans = new ADTClass(1, 1);
ans.realNum = a.realNum + b.realNum;
ans.imaginaryNum = a.imaginaryNum + b.imaginaryNum;
return ans;
}
public String toString() {
return realNum + "+" + imaginaryNum + "i";
}
}
public class LabStaticMethod {
    public static void main (String args[]) {
ADTClass f1, f2;
f1 = new ADTClass(2, 3);
f2 = new ADTClass(4, 5);
System.out.println("f1 = " + f1.toString());
System.out.println("f2 = " + f2.toString());
System.out.println("f1 + f2 = " + ADTClass.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)


------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
import java.math.*;
public class LabMath {
    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.6) = " + Math.ceil(3.6) );
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

Lab Finding the max of three numbers

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



------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
import java.util.Scanner;
public class LabFindingmax {
public static float maximun(float n1, float n2, float n3) {
if(n1 > n2 && n1 > n3)
return n1;
else if(n2 > n1 && n2 > n3)
return n2;
else
return n3;
}
    public static void main (String args[]) {
Scanner Keyboard = new Scanner(System.in);
float a, b, c; 
System.out.println("Finding the max of three numbers\nNumber 1:");
a = Keyboard.nextFloat();
System.out.println("Number 2:");
b = Keyboard.nextFloat();
System.out.println("Number 3:");
c = Keyboard.nextFloat();
System.out.println("Max = " + maximun(a, b, c) );
    }
}


------------------------------------------------<程式碼結束>--------------------------------------
附註:手動輸入三個float數,找出最大值。

2009年5月4日 星期一

Lab Java Constructor

Write constructors in the lab Fraction Addition.



class程式碼
------------------------------------------------<程式碼開始>--------------------------------------
public class Fractions {
private int numerator, denominator, red;
public Fractions(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public Fractions add(Fractions f) {
int x, a, b;
Fractions ans = new Fractions(1, 1);
ans.numerator = f.denominator * this.numerator + f.numerator * this.denominator;
ans.denominator = f.denominator * this.denominator;
red = reduction (ans.numerator, ans.denominator);
ans.numerator = ans.numerator / red;
ans.denominator = ans.denominator / red;
return ans;
}
public Fractions multiply(Fractions f) {
Fractions ans = new Fractions(1, 1);
ans.numerator = f.numerator * this.numerator;
ans.denominator = f.denominator * this.denominator;
red = reduction (ans.numerator, ans.denominator);
ans.numerator = ans.numerator / red;
ans.denominator = ans.denominator / red;
return ans;
}
private int reduction(int a, int b) {
int x;
if(a<b) {
x = a;
a = b;
b = x;
}
while (b != 0) {
x = a % b;
a = b;
b = x;
}
return a;
}
public String toString()
{
return (numerator + "/" + denominator);
}
}
------------------------------------------------<程式碼結束>--------------------------------------
主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class JavaConstructor {
    public static void main (String args[]) {
Fractions f1 = new Fractions(4, 2), f2 = new Fractions(1, 3);
        
System.out.println("f1 = " + f1.toString());
System.out.println("f2 = " + f2.toString());
System.out.println("f1 + f2 = " + f1.add(f2).toString());
System.out.println("f1 * f2 = " + f1.multiply(f2).toString() + "\n");
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

Lab Method Overloading

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

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



class程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class MethodOverloading {
    public static void main (String args[]) {
DateFourthTry date1 = new DateFourthTry();
DateFourthTry date2 = new DateFourthTry(); 
DateFourthTry date3 = new DateFourthTry();
date1.setDate(1,2,2008);
date2.setDate("February",2, 2008);
date3.setDate(2008);
System.out.println(date1);
System.out.println(date2);
System.out.println(date3);
}
}
------------------------------------------------<程式碼結束>--------------------------------------
主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.Scanner;
public class DateFourthTry {
private int month, day, year;
public void setDate(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
public void setDate(String stringmonth, int day, int year){
this.month = monthString(stringmonth);
this.day = day;
this.year = year;
}
public void setDate(int year) {
setDate(1, 1, year);
}
public int monthString(String month){
if(month == "January")
return 1;
else if(month == "February")
return 2;
else if(month == "March")
return 3;
else if(month == "April")
return 4;
else if(month == "May")
return 5;
else if(month == "June")
return 6;
else if(month == "July")
return 7;
else if(month == "August")
return 8;
else if(month == "September")
return 9;
else if(month == "October")
return 10;
else if(month == "November")
return 11;
else if(month == "December")
return 12;
else {
System.out.println("Fatal Error");
System.exit(0);
return 0;
}
}
public String toString() {
return (month + " " + day + ", " + year);
}
public int getDay() {
return day;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public boolean equals(DateFourthTry otherDate) {
return( (month == otherDate.month) && (day == otherDate.day) && (year == otherDate.year) );
}
public boolean precedes(DateFourthTry otherDate) {
return ( (year < otherDate.year) || 
(year == otherDate.year && getMonth() < otherDate.getMonth() ) || 
(year == otherDate.year && month == otherDate.month && day < otherDate.day) );
}
}
------------------------------------------------<程式碼結束>--------------------------------------
附註: