2008年6月29日 星期日
期末報告
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
Lab Magic Parking Tower
Hint: You may study Display 5.14 to get some ideas.
2008年6月9日 星期一
Homework 6-02-2008
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
2008年5月25日 星期日
lab Fraction equality test
1/2, 2/4
5/6, 6/7Hints:Fraction f1, f2;f1.equals(f2);
2008年5月19日 星期一
lab Fraction Addition
lab counter
counter.reset();
counter.inc();
counter.inc();
counter.dec();
counter.output();
2008年4月28日 星期一
lab class definition 2
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
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 Fibonacci numbers"
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.
Lab Finding the max of three numbers
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;
2008年3月24日 星期一
Lab: Tax Calculation
為 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
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);
}
}
2008年3月10日 星期一
Lab: String Processing
package gf;
public class S1
{ public static void main(String[] args)
{
int a; int b; String part_a = "", part_b = "";
String sentence = "I hate you";
String keyword = "love";
sentence.indexOf("hate");
a = sentence.indexOf("hate");
b = a + 4; part_a= sentence.substring(0,a);
part_b= sentence.substring(b);
System.out.println(part_a+keyword+part_b);
}
}
Lab: Simple Calculation
2008年3月6日 星期四
Homework 3-3-2008
1. Explain bytecode, JVM
bytecode:是一種中間語言,可以稱為Java byte-code 或是簡稱為 byte-code,當初之所以要發展的原因是為了讓這語言能在不同的機械中運作,而它具有許多的特性:很小, 很容易上手, 而且並不貴等因素,而他負責編譯機械語言,讓使用者變於撰寫
JVM:JVM是一個虛構出來的電腦,並且屏蔽了原有的處理系統的指令,讓JAVA可以有自己的CPU等相關指令,使JAVA可以在任何平台都可以修改
2. Explain class, object
class: A Java program is divided into smaller parts called classes, and normally each class definition is in a separate file and is compiled separately.
object: To make sure it is clear which program we mean, we call the input program, which in our case will be a Java program, the source program, or source code, and call the translated low-level-language program that the compiler produces the object program, or object code.
3. Reading Assignments
Read 1.1, 1.2, 1.3 of Textbook
4.1 Write a Java program as follows:
Let i=2;Print i;
Print 2 * (i++);
Print i;
public class I
{
public static void main(String[] args)
{
int i;/*虛告一個i的函數*/
i = 2;
System.out.println(i);/*輸出i值*/
System.out.println(2*(i++));/*輸出2乘與i++後的值*/
System.out.println(i);
}
}
4.2 Write a Java program as follows:
Let i=2;
Print i;
Print 2 * (++i);
Print i;
package homework;
public class I
{
public static void main(String[] args)
{
int i;/*虛告一個i的函數*/
i = 2;
System.out.println(i);/*輸出i值*/
System.out.println(2*(++i));/*輸出2乘與++i後的值*/
System.out.println(i);
}
}
4.3 Write a Java program as follows:
Let m=7, n=2;
Print (double) m/n;
Print m/ (double)n;
public class I
{
public static void main(String[] args)
{
int m,n;/*虛告m和n的函數*/
m = 7; n = 2;
System.out.println((double)m/n);
System.out.println(m/(double)n);
}
}
2008年3月3日 星期一
Lab 2 Java for Scientific Computation
public class JAVA {
public static void main (String[] args)
{
int weight_of_mouse = 400;/*各個的體重*/
int weight_of_human = 75000;
int weight_of_strating = 75000;
int weight_of_desired = 70000;
double ratio_of_sweetener_to_soda = 0.001;
double sweetener_to_kill_a_mouse = 1;
double ratio_of_sweetener_to_mouse;
double sweetener_to_human;
double diet_soda_drink_in_safely_range;
System.out.println("某人想要減重所需減肥可樂的量"+"但是過多的代糖會導致死亡!!!");/*輸出"某人想要減重所需減肥可樂的量"+"但是過多的代糖會導致死亡!!!"*/
System.out.println("如果某人"+weight_of_strating/1000+"Kg");/*如果某人xx公斤*/
System.out.println("使用此方法所減的重量");
System.out.println("如果我們知道"+sweetener_to_kill_a_mouse+" g 可殺死1隻"+""+weight_of_mouse+"克的老鼠");
ratio_of_sweetener_to_mouse = sweetener_to_kill_a_mouse / weight_of_mouse;
sweetener_to_human = weight_of_human * ratio_of_sweetener_to_mouse;
diet_soda_drink_in_safely_range = sweetener_to_human / ratio_of_sweetener_to_soda;
System.out.println("一個人所能喝的量為"+sweetener_to_human+" g 的"+ "人工代糖");
System.out.println("如果一個成年人想使用減肥可樂,那他的量不應該超過:"+diet_soda_drink_in_safely_range/1000 + "公升");
}
}
Lab Get familiar with JBuilder
2008年3月2日 星期日
Homework 2-25-2008
Java,是一種物件導向的程式語言,它是由Sun Microsystems的James Gosling等人在1990年初開發的。它最初被命名為Oak,用來設定小型家電的程式語言,解決電器的控制和通訊問題。但是由於智慧型家電的市場需求沒有預期的高,最後被迫放棄。後來隨著網際網路的發展,Sun看到了Oak在電腦網路上的廣闊應用前景,於是改造了Oak,在1995年5月以「Java」的名稱正式發佈。Java伴隨著網際網路的迅猛發展而發展,逐漸成為重要的網路程式語言。
2. List at least 5 applications of Java. You must provide the references you used. We recommend Google Search engine.
1.手機遊戲
2.某些應用程式
3.IC卡
4.網頁
5.電腦系統