2008年6月29日 星期日

期末報告

到圖書館挑選二本Java課本,寫下這些書名與作者出版社與出版日期,每本書各挑選一個習題進行個人研究,說明以下

1.你為什麼挑選這個習題(只有題目,沒有範例或解答),
2.這個習題讓你學到什麼概念,
3.請你製作一個講義說明這個習題。

Due: 6/29/2008 at 18:00

書名:Thinking in Java 2E中文版
作者:艾克爾 (Eckel, Bruce) 著
候 捷 譯
王 建興 譯
出版社:碁峰資訊
出版日期:2002[民91]

1.因為這個習題之前上課好像有做過,就想說拿來在練習一下
2.讓我重新學習回圈的概念
3.設計一個程式 列出數值1~100 並且加總
public class counter{
public static void main ( String [] args)
{
int num = 0;
int sum;
while(num < 100)
{
num ++ ;
System.out.println("number = " + number);
sum = sum + num;
}
System.out.println("the total is" + sum);
}
}


書名:JAVA程式設計藝術中文版
作者:H. M. Deitel、P. J. Deitel 著
陸茵、陳文敬 譯
出版社:全華
出版日期:2006[民95]

1.因為之前在寫C++有寫過這個程式,所以想用JAVA是寫看看
2.這個習題讓我了解C++與JAVA的不同之處
3.使用for迴圈的概念設計一個金字塔

public class pyramidal
{
public static void main ( String [] args)
int i,m;
for (i= 0; i<10 ;i++)
{
for(m = 0; m <= i: m++)
System.out.print("*");
System.out.println("");
}
}


2008年6月16日 星期一

Lab Static method

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



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.



2008年6月9日 星期一

Homework 6-02-2008

2. (30)

Write a complete Java program that uses a for loop to compute the sum of the even numbers and the sum of the odd numbers between 1 and 50.



public class Sum {
public static void main ( String [] args )
{
int sum_even = 0;
int sum_odd = 0;
int number = 1;

for (int i = 1; i <= 50; i++)
{
if(number % 2 == 0){
sum_even = sum_even + number;
}
if(number % 2 == 1){
sum_odd = sum_odd + number;
}
number ++;
}

System.out.println("Sum of even = " + sum_even);
System.out.println("Sum of odd = " + sum_odd);
}
}



3. (20) Rewrite the following for loop using a while expression.

int Sum=0;
for(i=0; i<=10; i++)
Sum=Sum+i;



public class SumUsingWhile {
public static void main ( String [] args )
{
int sum = 0;
int i = 0;
while (i <= 10)
{
sum = sum + i;
i++;
}
System.out.println("sum = " + sum);
}
}

2008年6月2日 星期一

Lab Overloading




Do Display 4.11
import java.util.Scanner;

public class DateSixthTry
{
private String month;
private int day;
private int year;

public void setDate(int monthInt, int day, int year)
{
if ( dateOK(monthInt, day , year))
{
this.month = monthString(monthInt);
this.year = year;
this.day = day;
}
else
{
System.out.println("Fatal Error");
System.exit(0);
}
}

public void setDate(String monthString, int day,int year)
{
if (dateOK(monthString, day , year) )
{
this.month = monthString;
this.year = year;
this.day = day;
}
else
{
System.out.println("Fatal Error");
System.exit(0);
}
}
public void setDate(int year)
{
setDate(1 ,1 ,year);
}
private boolean dateOK(int monthInt, int dayInt, int yearInt)
{
return ((monthInt >= 1) && (monthInt <= 12) && (dayInt >= 1) && (dayInt <= 31) && (yearInt >= 1000) && (yearInt <= 9999));

}
private boolean dateOK(String monthString, int dayInt , int yearInt)
{
return (monthOK(monthString) && (dayInt >= 1) && (dayInt <= 31) && (yearInt >= 1000) && (yearInt <= 9999));
}
private boolean monthOK(String month)
{
return(month.equals("January") || month.equals("February") || month.equals("March") || month.equals("April") || month.equals("May") || month.equals("June") || month.equals("July") || month.equals("August") || month.equals("September") || month.equals("October") || month.equals("November") || month.equals("December"));

}


public void readInput()
{
boolean tryAgain = true;
Scanner keyboard = new Scanner(System.in);
while (tryAgain)
{
System.out.println("Enter month, day, year.");
System.out.println("Do not use a coma.");
String monthInput = keyboard.next();
int dayInput = keyboard.nextInt();
int yearInput = keyboard.nextInt();
if (dateOK(monthInput , dayInput , yearInput))
{
setDate(monthInput, dayInput , yearInput);
tryAgain = false;
}
else
System.out.println("Illegal date. Reenter input.");

}
}

private String monthString(int monthInt)
{
switch (monthInt)
{
case 1:
return "January";
case 2:
return "Febuary";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
System.out.println("Fatal Error");
System.exit(0);
return "Error";
}
}
public String toString ()
{
return month + " " + day + "," + year;
}
}


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

public class OverloadingDemo
{
public static void main(String[] args)
{
DateSixthTry date1 = new DateSixthTry(),date2 = new DateSixthTry(),date3 = new DateSixthTry();

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

}



}

2008年5月26日 星期一

Lab ADT, accessor, mutator

Define a Complex class and write an object oriented program to compute (2+3i)+(4+5i) in Java.
The methods should include an access and a mutator.



2008年5月25日 星期日

lab Fraction equality test

Write a program to implement a method that can check whether 2 fractions are equal. You will implement a class called Fraction consisting of a numerator and a denominator. The equality test of 2 fractions should return a boolean value.Use the following as the tests.
1/2, 2/4
5/6, 6/7Hints:Fraction f1, f2;f1.equals(f2);

Photobucket

Photobucket