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

2008年5月19日 星期一

lab Fraction Addition

Write a program to implement a method that can do additions of 2 fractions. You will implement a class called Fraction consisting of a numerator and a denominator. The additions of2 fractions should be equal to a fraction.Use 1/2+1/3 as the test.Hints:Fraction f1, f2;f1.add(f2);





lab counter

Define a class called Counter whose objects count things. An object of this class records a count that is a nonnegative integer. Include methods to set the counter to 0, to increase the count by 1, and to decrease the count by 1. Include an accessor method that returns the current count value and a method that outputs the count to the screen. Write a program to test

counter.reset();
counter.inc();
counter.inc();
counter.dec();
counter.output();







2008年4月28日 星期一

lab class definition 2

Study Display 4.4 (2nd ed. and 3rd ed.) or Display 4.2 & Display 4.3 (1st ed.) and then
1. Comment out date.setDate(6, 17, year); by // date.setDate(6, 17, year);
2. At the next line below, add date.readInput();
3. Run the program again. Fix any problems you may encouter along the way.
4. At the last line of your program, add System.out.println(date.month);and see what happens. Why?

System.out.println()非CLASS的METHOD 故此無法純取CLASS的MEMBER



import java.util.Scanner;

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

public void setDate(int newMonth, int newDay, int newYear)
{
month = monthString(newMonth);
day = newDay;
year = newYear;
}
public String monthString(int monthNumber)
{
switch (monthNumber)
{
case 1:
return "January";
case 2:
return "Februay";
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 void writeOutput()
{
System.out.println(month + " " + day + ", "+ year);
}

}

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

public class DateThirdTryDemo

{
public static void main(String[] args)
{
DateThirdTry date = new DateThirdTry();
int year = 1882;
date.setDate(6, 17, year);

date.writeOutput();
}


}


-------------------------------------------------------------------------------------
public class DateThirdTryDemo

{
public static void main(String[] args)
{
DateThirdTry date = new DateThirdTry();

date.readInput();

System.out.println(date.month);

}

}

2008年4月14日 星期一

lab class definition

Display 4.1


Self-Test Exercise 1


public class DateFirstTry
{
public String month;
public int day;
public int year;
public void writeOutput()
{
System.out.println(month + "" + day + ", " + year);
}

public void makeItNewYears ()
{
month = "January";
day = 1;
}

}

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


public class DateFirstTryDemo
{
public static void main(String[] args)
{
DateFirstTry date1, date2;
date1 = new DateFirstTry();
date2 = new DateFirstTry();
date1.month = "December";
date1.day = 31;
date1.year = 2007;
System.out.println("date1:");
date1.writeOutput();

date2.month = "July";
date2.day = 4;
date2.year = 1776 ;
System.out.println("date2:");
date2.makeItNewYears();
date2.writeOutput();

}

Average income by gender




Write a program to calculate average income by gender based on the following data, where F stands for female and M for male.

F 62,000
M 25,000
F 38,000
F 43,000
M 65,000
M 120,000
F 80,000
M 30,100



You should be able to allow users to type in a whole line such as F 80,000 followed by next line M 30,100.

Without any change made to your program, your program should be able to process a new set of data, such as follows:

M 52,000
M 35,000
F 48,000
M 33,000
F 75,000
F 110,000
F 90,000
M 30,100

2008年4月7日 星期一

Lab 9*9


public class work
{
public static void main (String[] args)
{
int i,j;
for(i=1;i<10;i++)
{
for(j=1;j<10;j++)
{
System.out.printf("%2d*%2d=%2d", j,i,i*j);
}
System.out.println("");
}
}
}

"Lab Fibonacci numbers"


import java.util.Scanner;
public class work
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("輸入一個數字");
int a = keyboard.nextInt();
double i = 1,j = 0,k = 0,z;
for(z=0; z<=a; z++)
{
k = i+j;
if(z!= 100)

System.out.printf("%5.0e + %5.0e\t" ,j,i);

else

System.out.printf("%5.0f + %5.0f\t" ,j,i);
j = i;
i = k;

if (z!= 100)

System.out.printf("=\t%.0e\n" ,i);

else

System.out.printf("=\t%.0f\n", i);

double o=k, p=j, m=o/p;
System.out.printf("the ratio %5f\n", m);

}



}

}

Homework 3-24-2008


1. Based on your study of Display 3.8, write a code to find the max and min of a list of number.For example, given 1,3,5, and9, the max is 9 and the min is 1.Your program should be able to process a list of any length.


2. Write a program to generate the series 1, 1, 2, 3, 5, 8, 13, ...The series has a property that the third number is the sum of the first and second numbers. For example, 2=1+1, 3=1+2, and 5=2+3.

Lab Finding the max of three numbers



import java.util.Scanner;

public class work {
public static void main( String[] args )

{
Scanner keyboard = new Scanner (System.in);
System.out.println("請輸入三個數值");

double [] matrix = new double [3];
double maximum = 0;
for (int i = 0; i <>
{ matrix[i] = keyboard.nextDouble();} f
or (int i = 0; i <>
{ if (maximum < maximum =" matrix[i];">
System.out.println("最大值:" + maximum);
}
}

2008年3月24日 星期一

Lab: Tax Calculation



為 1,000,000時 ↓

為 2,000,000時↓






import java.util.Scanner;

public class work
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner(System.in);
double netIncome, tax;

System.out.println("Enter net income. \n" + "Do not include a dollar sign or commas");
netIncome = keyboard.nextDouble();

if (netIncome <= 370000) tax = 0.06*netIncome; else if (netIncome > 370000 && netIncome <= 990000) tax = 0.13*netIncome-25900; else if (netIncome > 990000 && netIncome <= 1980000) tax = 0.21*netIncome-105100; else if (netIncome > 1980000 && netIncome <= 3720000) tax = (0.3*netIncome-283300); else tax = 0.4*netIncome-655300; System.out.println("Tax of = " + tax); } }

2008年3月23日 星期日

Homework 3-17 2007











import java.util.Scanner;

public class project
{
public static void main(String[] args)
{
System.out.print("type the number you want:");
Scanner keyboard = new Scanner (System.in);
double h = keyboard.nextDouble();
double guess = h / 2;
double n;
for (int i=0 ;i <= 10 ; i++) { n = h/guess; guess =(guess+n)/2; } System.out.println(h+ "開根號:"+guess); }

}



import java.util.Scanner;


public class project2
{
public static void main(String[] args)
{
System.out.println("輸入兩個整數");
Scanner input = new Scanner (System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = a + b;
int d = a - b;
int e = a * b;
System.out.println(a+"+" +b+"="+ c);
System.out.println(a+"-" +b+"="+ d);
System.out.println(a+"*" +b+"="+e);
}

}


Lab Keyboard input


package homework;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;


public class ScannerDemo
{
public static void main(String[] args) throws IOException
{
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of pods followed by");
System.out.println("the number if peas in a pods");


String numberOfPods = keyboard.readLine();
String peasPerPod = keyboard.readLine();

int a,b;
a = Integer.parseInt(numberOfPods);
b = Integer.parseInt(peasPerPod);

int totalNumberOfPeas = a * b;

System.out.println(numberOfPods + " pods and ");
System.out.println(peasPerPod + " peas per pod ");
System.out.println("The tolal number of peas = " +totalNumberOfPeas);
}
}

2008年3月17日 星期一

Lab Scanner


import java.util.Scanner;

public class df
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter the number of pods followed by");
System.out.println("the number of peas in a pod");
int numberOfPods = keyboard.nextInt();
int peasPerPod = keyboard.nextInt();

int totalNumberOfPeas = numberOfPods*peasPerPod;

System.out.println(numberOfPods + "pods and");
System.out.println(peasPerPod + "peas per pod.");
System.out.println("The total number of peas = " + totalNumberOfPeas);
}

}