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.
程式:
import java.util.Scanner;
public class Homework0324
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter a list of nonnegative number:");
System.out.println("Mark the end with a negative number!");
System.out.println("I will show the max and min number!");
double number,max,min;
number=keyboard.nextDouble();
max=number;
min=number;
while (number>=0)
{
if(number>=max)
max=number;
if(number<=min)
min=number;
number=keyboard.nextDouble();
}
System.out.println("the max of three number is : "+max);
System.out.println("the min of three number is : "+min);
}
}
結果:
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.
程式:
import java.util.Scanner;
public class Homework0324_2
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter the limit number:");
int first,second,temp,max;
first=1;
second=1;
max=keyboard.nextInt();
System.out.print(first+" ");
System.out.print(second+" ");
while(second<=max)
{
temp=first+second;
first=second;
second=temp;
System.out.print(temp+" ");
}
}
}
結果:
2008年3月27日 星期四
2008年3月24日 星期一
Lab: Tax Calculation
import java.util.Scanner;
public class TaxCalculation
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter net income:");
double netIncome = keyboard.nextInt();
double tax;
if (netIncome<=370000)
tax=netIncome*0.06;
else if((netIncome>370000)&&(netIncome<=990000))
tax=netIncome*0.13-25900;
else if ((netIncome>990000)&&(netIncome<=1980000))
tax=netIncome*0.21-105100;
else if ((netIncome>1980000)&&(netIncome<=3720000))
tax=netIncome*0.30-283300;
else if (netIncome>3720000)
tax=netIncome*0.40-655300;
else
tax=0.0;
System.out.print("總共需要");
System.out.printf("%10.2f",tax);
System.out.println("元");
}
}
結果:
1000000的稅
public class TaxCalculation
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter net income:");
double netIncome = keyboard.nextInt();
double tax;
if (netIncome<=370000)
tax=netIncome*0.06;
else if((netIncome>370000)&&(netIncome<=990000))
tax=netIncome*0.13-25900;
else if ((netIncome>990000)&&(netIncome<=1980000))
tax=netIncome*0.21-105100;
else if ((netIncome>1980000)&&(netIncome<=3720000))
tax=netIncome*0.30-283300;
else if (netIncome>3720000)
tax=netIncome*0.40-655300;
else
tax=0.0;
System.out.print("總共需要");
System.out.printf("%10.2f",tax);
System.out.println("元");
}
}
結果:
1000000的稅
Lab Finding the max of three numbers
import java.util.Scanner;
public class MaxOfThreeNumbers
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter three number:");
int firstNumber = keyboard.nextInt();
int secondNumber = keyboard.nextInt();
int thirdNumber = keyboard.nextInt();
int temp=firstNumber;
if(secondNumber>=temp)
{temp=secondNumber;}
if(thirdNumber>=temp)
{temp=thirdNumber;}
System.out.println("the max of three number is : "+temp);
}
}
public class MaxOfThreeNumbers
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter three number:");
int firstNumber = keyboard.nextInt();
int secondNumber = keyboard.nextInt();
int thirdNumber = keyboard.nextInt();
int temp=firstNumber;
if(secondNumber>=temp)
{temp=secondNumber;}
if(thirdNumber>=temp)
{temp=thirdNumber;}
System.out.println("the max of three number is : "+temp);
}
}
結果:
2008年3月17日 星期一
Homework 3-17-2008_Project 3 of Chap. 2.
import java.util.Scanner;
public class Homework20080317Part3
{
public static void main(String[] args)
{
System.out.println("please enter two integers.");
Scanner keyboard = new Scanner(System.in);
int first=keyboard.nextInt();
int second=keyboard.nextInt();
System.out.println("sum :"+(first+second));
System.out.println("difference :"+(first-second));
System.out.println("product :"+(first*second));
}
}
public class Homework20080317Part3
{
public static void main(String[] args)
{
System.out.println("please enter two integers.");
Scanner keyboard = new Scanner(System.in);
int first=keyboard.nextInt();
int second=keyboard.nextInt();
System.out.println("sum :"+(first+second));
System.out.println("difference :"+(first-second));
System.out.println("product :"+(first*second));
}
}
Homework 3-17-2008_Project 1 of Chap. 2.
import java.util.Scanner;
public class Homework0317Part1
{
public static void main(String[] args)
{
System.out.println("please enter a number.");
Scanner keyboard = new Scanner(System.in);
double n=keyboard.nextDouble();
double myGuess=n/2;
double r=0.0;
for(int i=0;i<5;i++)
{
r=n/myGuess;
myGuess=(myGuess+r)/2;
}
System.out.print("my guess is ");
System.out.printf("%10.2f",myGuess);
System.out.println();
System.out.print("the real one is ");
System.out.printf("%10.2f",Math.sqrt(n));
}
}
結果:
1.使用較小的數字(誤差較小)
2.使用較大的數字(誤差較大)
public class Homework0317Part1
{
public static void main(String[] args)
{
System.out.println("please enter a number.");
Scanner keyboard = new Scanner(System.in);
double n=keyboard.nextDouble();
double myGuess=n/2;
double r=0.0;
for(int i=0;i<5;i++)
{
r=n/myGuess;
myGuess=(myGuess+r)/2;
}
System.out.print("my guess is ");
System.out.printf("%10.2f",myGuess);
System.out.println();
System.out.print("the real one is ");
System.out.printf("%10.2f",Math.sqrt(n));
}
}
結果:
1.使用較小的數字(誤差較小)
2.使用較大的數字(誤差較大)
Lab Keyboard input
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class LabKeyboardinput
{
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 of peas in the pod:");
String numberOfPods = keyboard.readLine();
String peasPerPod = keyboard.readLine();
double totalNumberOfPeas = Double.parseDouble (numberOfPods)*Double.parseDouble (peasPerPod);
System.out.print(numberOfPods+"pods and");
System.out.println(peasPerPod+"peas per pod.");
System.out.println("The total number of peas ="+totalNumberOfPeas);
}
}
結果:
import java.io.InputStreamReader;
import java.io.IOException;
public class LabKeyboardinput
{
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 of peas in the pod:");
String numberOfPods = keyboard.readLine();
String peasPerPod = keyboard.readLine();
double totalNumberOfPeas = Double.parseDouble (numberOfPods)*Double.parseDouble (peasPerPod);
System.out.print(numberOfPods+"pods and");
System.out.println(peasPerPod+"peas per pod.");
System.out.println("The total number of peas ="+totalNumberOfPeas);
}
}
結果:
Lab Scanner
import java.util.Scanner;
public class ScannerDemo
{
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 the pod:");
int numberOfPods = keyboard.nextInt();
int peasPerPod = keyboard.nextInt();
int totalNumberOfPeas = numberOfPods*peasPerPod;
System.out.print(numberOfPods+"pods and");
System.out.println(peasPerPod+"peas per pod.");
System.out.println("The total number of peas ="+totalNumberOfPeas);
}
}
public class ScannerDemo
{
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 the pod:");
int numberOfPods = keyboard.nextInt();
int peasPerPod = keyboard.nextInt();
int totalNumberOfPeas = numberOfPods*peasPerPod;
System.out.print(numberOfPods+"pods and");
System.out.println(peasPerPod+"peas per pod.");
System.out.println("The total number of peas ="+totalNumberOfPeas);
}
}
2008年3月10日 星期一
Homework 3-10-2008: String Processing
public class StringProcessing
{
public static void main(String[] args)
{
String original,newly,temp;
original="I hate you.";
int index;
index=original.indexOf("hate");
newly=original.substring(0,index)+"love"+original.substring(index+"hate".length(),original.length());
System.out.println("The line of text to be changed is:");
System.out.println(original);
System.out.println("I have rephrased that line to read:");
System.out.println(newly);
}
}
{
public static void main(String[] args)
{
String original,newly,temp;
original="I hate you.";
int index;
index=original.indexOf("hate");
newly=original.substring(0,index)+"love"+original.substring(index+"hate".length(),original.length());
System.out.println("The line of text to be changed is:");
System.out.println(original);
System.out.println("I have rephrased that line to read:");
System.out.println(newly);
}
}
Lab: Simple Calculation
public class SimpleCalculation
{
public static void main(String[] args)
{
double result6;
result6=6000/5280.0;
System.out.print("6000 feet 為");
System.out.printf("%10.2f",result6);
System.out.println("mile");
System.out.print("6000 feet 總共需要");
System.out.printf("%10.2f",5000*result6);
System.out.println("元");
double result4;
result4=4000/5280.0;
System.out.print("4000 feet 為");
System.out.printf("%10.2f",result4);
System.out.println("mile");
System.out.print("4000 feet 總共需要");
System.out.printf("%10.2f",5000*result4);
System.out.println("元");
}
}
2008年3月9日 星期日
Homework 3-3-2008
1. Explain bytecode, JVM
bytecode 是給 JVM(Java Virtual Machine) 下指令的一種虛擬機器碼,也就是說,JVM 是負責把Java原始碼編譯成為bytecode,為bytecode 的解譯程式。雖然 JVM 可能因平台而異,但是所有的 JVM 都能執行 bytecode ,所以不會被綁死在特定的硬體上。
2. Explain class, object
class是一種"類別"(抽象), 而object則為"物件"(實體)。
3. Reading Assignments:
bytecode 是給 JVM(Java Virtual Machine) 下指令的一種虛擬機器碼,也就是說,JVM 是負責把Java原始碼編譯成為bytecode,為bytecode 的解譯程式。雖然 JVM 可能因平台而異,但是所有的 JVM 都能執行 bytecode ,所以不會被綁死在特定的硬體上。
2. Explain class, object
class是一種"類別"(抽象), 而object則為"物件"(實體)。
java將每個程式視為一class, 包含許多object, 物件執行的動作就是method。java實際寫法如下:
public class (name of class)
{
object.method();
}
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;
Ans: 2, 4, 3
4.2 Write a Java program as follows:
Let i=2;
Print i;
Print 2 * (++i);
Print i;
Ans: 2, 6, 3
4.3 Write a Java program as follows:
Let m=7, n=2;
Print (double) m/n;
Print m/ (double)n;
Lab 2 Java for Scientific Computation
阿斯巴甜的每日安全劑量:每公斤體重50毫克。
小白鼠平均體重:20g
參考資料:
http://tw.search.yahoo.com/
http://tw.knowledge.yahoo.com/question/?qid=1405102414901
小白鼠平均體重:20g
參考資料:
http://tw.search.yahoo.com/
http://tw.knowledge.yahoo.com/question/?qid=1405102414901
2008年3月3日 星期一
2008年3月1日 星期六
HOMEWORK 2-25-2008
List at least 5 applications of Java. You must provide the references you used. We recommend Google Search engine.
1.手機遊戲
2.網路遊戲、動畫...應用在網際網路上的程式語言
3.資料庫
4.金融證卷
5.汽車資訊監控系統:http://cgi.taiwan.cnet.com/jpc/sp2-10.htm
Watch The Inside Story (Video), write your words on the development and inventor of Java.
Java,是一種物件導向的程式語言,它是由Sun Microsystems的James Gosling等人在1990年初開發的。它最初被命名為Oak,用來設定小型家電的程式語言,解決電器的控制和通訊問題。但是由於智慧型家電的市場需求沒有預期的高,最後被迫放棄。後來隨著網際網路的發展,Sun看到了Oak在電腦網路上的廣闊應用前景,於是改造了Oak,在1995年5月以「Java」的名稱正式發佈。Java伴隨著網際網路的迅猛發展而發展,逐漸成為重要的網路程式語言。
Java一開始是流行使用在瀏覽器中的,受到網景公司的重視,甚至成立了Java業務集團,但是幾年後它在瀏覽器的地位被逐步侵蝕。同時,Java也受到微軟的排擠,他們決定在新版本的IE和Windows中不再有Java平臺。看起來,未來的發展似乎是一片灰暗...但是近年來,Java傾被用在撰寫線上的一些小遊戲,還有資訊網的伺服器端以及手機設備上,特別是以手機設備上最被為重視,讓Java又開始流行起來。
參考文獻:http://zh.wikipedia.org/&再經過我吸收消化整理
1.手機遊戲
2.網路遊戲、動畫...應用在網際網路上的程式語言
3.資料庫
4.金融證卷
5.汽車資訊監控系統:http://cgi.taiwan.cnet.com/jpc/sp2-10.htm
Watch The Inside Story (Video), write your words on the development and inventor of Java.
Java,是一種物件導向的程式語言,它是由Sun Microsystems的James Gosling等人在1990年初開發的。它最初被命名為Oak,用來設定小型家電的程式語言,解決電器的控制和通訊問題。但是由於智慧型家電的市場需求沒有預期的高,最後被迫放棄。後來隨著網際網路的發展,Sun看到了Oak在電腦網路上的廣闊應用前景,於是改造了Oak,在1995年5月以「Java」的名稱正式發佈。Java伴隨著網際網路的迅猛發展而發展,逐漸成為重要的網路程式語言。
Java一開始是流行使用在瀏覽器中的,受到網景公司的重視,甚至成立了Java業務集團,但是幾年後它在瀏覽器的地位被逐步侵蝕。同時,Java也受到微軟的排擠,他們決定在新版本的IE和Windows中不再有Java平臺。看起來,未來的發展似乎是一片灰暗...但是近年來,Java傾被用在撰寫線上的一些小遊戲,還有資訊網的伺服器端以及手機設備上,特別是以手機設備上最被為重視,讓Java又開始流行起來。
參考文獻:http://zh.wikipedia.org/&再經過我吸收消化整理
訂閱:
文章 (Atom)