2009年3月30日 星期一

MEMO

import java.util.Scanner;

public class Ch4lab2 {
private String month;
private int day;
private int year;
public void readInput()
{
System.out.println(month + " " + day + ", " + 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 "February";
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 class DateThirdTryDemo {
public static void main(String[] args)
{
Ch4lab2 date = new Ch4lab2();
int year = 1882;
date.setDate(6, 17, year);
date.readInput();
System.out.println(date.newDay());
System.out.println(date.newMonth());
System.out.println(date.newYear());
}

}


MEMO

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

}
------------


public class ch4 {
public static void main (String[] args)
{
DateFirstTry date1, date2;
date1 = new DateFirstTry();
date2 = new DateFirstTry();
date1.month = "January";
date1.day = 1;
date1.year = 2007;
System.out.println("date1:");
date1.writeOutput();
date2.month = "July";
date2.day = 4;
date2.year = 1776;
System.out.println("date2:");
date2.writeOutput();
System.out.println("Make It In New Years:");
date2.makeItNewYears();
date2.writeOutput();
}

}

lab class definition

Study Display 4.1 and then do Self-Test Exercise 1.


2009年3月27日 星期五

Lab Cosine

Write a Java program to calculate the triangular function as follows:
Cos(x)=1 - x 2 /2!+ x 4/4!- x 6/ 6!...



要往前兩階
就乘以你要往前的那兩階的數字就好了
也就是下個數字和下下個數字....
何苦想要把奇數階層消去勒
我是笨蛋...


-------


public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double a=1,b=1,n,s=1,c = 0;
double active = 1; // 分子
double denominator = 1; // 分母
double multiple = 2 ;
System.out.println( "請輸入x" );
double x = keyboard.nextInt();
System.out.println( "請給予精確度n;n>0" );
n = keyboard.nextInt();
double result = 1.0;
for(int i = 1 ; i < n + 1 ; i ++ ){
active = active * x * x;
System.out.println( "x^: "+active );
denominator = -1*denominator * multiple * ( multiple-1 ); // 分母
System.out.println( "n^: "+denominator );
result += active / denominator;
multiple = multiple + 2;
}
System.out.println( "cos("+x+")="+ result );

// TODO code application logic here
}

}

TEMP

package javaapplication14;
import java.util.Scanner;
/**
*
* @author KFN
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double a=1,b=1,n,s=1,c = 0;
double active = 1; // 分子
double denominator = 1; // 分母
double multiple = 2 ;
double x = keyboard.nextInt();
n = keyboard.nextInt();
double result = 1.0;
for(int i = 1 ; i < n + 1 ; i ++ ){
active = active * x * x;
System.out.println( active );
denominator = denominator * multiple * ( multiple-1 ); // 分母
System.out.println( denominator );
result += active / denominator;
multiple = multiple + 2;
}
System.out.println( result );

// TODO code application logic here

2009年3月23日 星期一

[090330]

執行效能
避免做多餘動作
class 開頭大寫


Hint:

n!

i=0i n;i++

c=1

c*(i+1)



-----

c=x*c;

TEMP

import java.util.Scanner;
public class Lab0330 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double i, a=1,b=1,n,s,c;
System.out.println("請問要Fibonacci數列的第幾項?");
n = keyboard.nextInt();

for(i=1; i < n-1 ; i ++ ){

s=a+b;
b=a;
a=s;
c=a/b;
System.out.println(a+"/"+b+"="+c);

}
System.out.println("上列為後項除以前項之數列");
System.out.println("第"+n+"項為"+a);

}
}

Lab Fibonacci



List the first 100 numbers and the ratio of
a number to its previous number, such as 1/1 = 1, 2/1 = 2, 3/2 = 1·5, 5/3 = 1·666..., 8/5 = 1·6, 13/8 = 1·625, 21/13 = 1·61538....

Want to know more about Fibonacci number


------
超過能列出來的數字就怪怪的...
就算改成LONG也只能列到第93項...
之後會出現1.618033988749895的規律
------
import java.util.Scanner;
public class Lab0330 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
long i, a=1,b=1,n,s;
System.out.println("請問要Fibonacci數列的第幾項?");
n = keyboard.nextInt();

for(i=1; i < n-1 ; i ++ ){

s=a+b;
b=a;
a=s;
double c=(double)a/b;
System.out.println(a+"/"+b+"="+c);

}
System.out.println("上列為後項除以前項之數列");
System.out.println("第"+n+"項為"+a);

}
}

TEMP

/*
* Main.java
*
* Created on 2009年3月23日, 下午 4:31
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication13;

import java.util.Scanner;
/**
*
* @author KFN
*/
public class Main {

/** Creates a new instance of Main */

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int x, n, s;
System.out.println("Please input a value 'x'");
x = keyboard.nextInt();
if ( x < 0)
{
System.out.println( "x should > 0" );
}
System.out.println("Please input a value 'n'.");
n = keyboard.nextInt();
if ( n < 0 )
{
System.out.println( "n should > 0" );
}
do{

double ans = 0.0;
ans = tailor( x, n );
System.out.println("The answer of e^"+x+"="+ans);
System.out.println("Please input '0' to stop this program or other number to continue.");
s = keyboard.nextInt();
}while( s != 0 );


// TODO code application logic here
}
public static double tailor( int x, int n ) {

double sum = 0.0;
for( int i = 0 ; i <= n ; i++ ) {
System.out.println( "x^i:" + comp(x,i) ) ;
System.out.println( "n!:" + comp1(i) ) ;
sum = sum + comp( x, i )/comp1( i );
}
return sum;
}
public static double comp( int x , int n )
{
if ( n == 0 )
return 1.0;
double sum = 1.0;
for(int i = 0 ; i < n ; i++)
sum = sum * x;
return sum ;
}
public static double comp1( int n )
{
if(n == 0 )
return 1;

return n * comp1( n-1 );
}
}

Homework 3-16-2009

1. Project 7 of Chap. 3

2. Write a program to generate the following table of arithmetic expressions

1*1=1 1*2=2 1*3=3 ... 1*9=9
2*1=2 2*2=4 2*3=6 ... 2*9=19
...
9*1=9 9*2=18 9*3=27 ... 9*9=81

2009年3月16日 星期一

Lab Finding the max of a list of numbers

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.



主要概念是創造一個arrary,並宣告一個變數令array會往前跑(arrary[0]變arrary[1],在這裡宣告可使用arrary[0]~[99]),也就是將每次key入的數字儲存在這個arrary中(從0開始放)

而while是繼續執行的條件
.
.
.
最後就是大ㄧ有敎過的泡沫排序法...

Lab Finding the max of a list of numbers

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

Write a program to decide the max number of the three input number.




 
import java.util.Scanner;

public class LAB02 {    
    public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double n[] = new double[100];
int count = 0;
System.out.println( "請輸入數字,輸入\"-1\"為停止") ;
                while( count <>
                  n[count] = keyboard.nextDouble();
                  count++;  
                }
                // 讀入INPUT數字
                
                for( int i = 0 ; i <>
                  for( int j = i+1 ; j <>
                    if( n[i] > n[j] ) {
                      double temp = n[i];
                      n[i] = n[j];
                      n[j] = temp;
                    }
               // 上面是網路上COPY的泡沫排序法 
               System.out.println( "min: "+n[0] + " & Max: " + n[count-1] ) ;
                
    }
    
}

Lab Tax Calculation

Study Display 3.1. Based on the income tax rate in Taiwan,
calculate the income tax of a person whose annual income is 1,000,000 or 2,000,000.





import java.util.Scanner;

public class IncomeTax {
public static void main(String[] args)
{      
Scanner keyboard = new Scanner(System.in);
double netIncome = 0.0,tax = 0.0;
System.out.println("請輸入所得淨額");
netIncome = keyboard.nextDouble();
if(netIncome <= 370000)
tax = netIncome*0.06;
else if((netIncome >= 370001)&&(netIncome <= 990000))
tax = netIncome*0.13-25900;
else if ((netIncome>=990001)&&(netIncome<=1980000))
tax = netIncome*0.21-105100;
else if ((netIncome>=1980001)&&(netIncome<=3720000))
tax = netIncome*0.3-283300;
else if( netIncome >= 3720001 )
   tax = netIncome*0.4-655300;
System.out.printf("應繳所得稅為"+ tax + "元" );
}  
}


2009年3月15日 星期日

Homework 3-9-2009

1. Do Project 4 in Chapter 2

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//new class為配置一個記憶體空間
System.out.println("Enter the distance of the commute in miles.");
double a = keyboard.nextDouble();
System.out.println("Enter the feul consumption rate in miles per gallon.");
double b = keyboard.nextDouble();
System.out.println("Enter the price of a gallon.");
double c = keyboard.nextDouble();
System.out.println("The cost of the commute is \n" + ( a / b ) * c );
// TODO code application logic here
}
2. Do Project 5 in Chapter 2

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

System.out.println("Enter the purchase price of an item.");
double P = keyboard.nextDouble();
System.out.println("Enter the exceptd number of years of service.");
double Y = keyboard.nextDouble();
System.out.println("Enter the expected salvage value.");
double S = keyboard.nextDouble();
System.out.println("The yearly depreciation for the item is \n" + (P - S ) / Y );
// TODO code application logic here
}
3. Do Project 6, in Chapter 2

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

System.out.println("Enter the amount of artificial sweetener needed to kill a mouse.(g)");
double S = keyboard.nextDouble();
System.out.println("Enter the weight of the mouse.(g)");
double m = keyboard.nextDouble();
System.out.println("Enter the desired weight of the dieter.(g)");
double M = keyboard.nextDouble();
System.out.println("The diet soda pop it is possible to dink is\n" + (M / m ) * S *1000 + "ml or" + (M / m ) * S + "L");
// TODO code application logic here
}
4. Do Project 7, in Chapter 2

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

System.out.println("Enter the price of item \n (from 25 cents to a dollar, in 5-cent increments)");
int item = keyboard.nextInt();
System.out.println("You buoght an item for"+ item +"cents and gave me a dollar");

int q = (100 - item)/25;
int n = ((100 - item)%25)/5;

System.out.println("So your change is \n"+ q +" quarters and "+ n +" nickels.");

// TODO code application logic here
}

2009年3月9日 星期一

Lab Keyboard precessing


Project 3 of Chap. 2.

Lab Keyboard Input

Rewrite Display 2.6 using BufferedReader.

You need to import the following packages in the first place.

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

Change

Scanner keyboard= new Scanner(System.in);

into

BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));

String inputString = keyboard.readLine();

Note the Main method needs IOException handling as follows:

public static void main (String[] args) throws IOException


Lab Scanner

Do Display 2.6.



What is ASCII?

ASCIIAmerican Standard Code for Information Interchange,美國資訊互換標準代碼)是基於拉丁字母的一套電腦編碼系統。它主要用於顯示現代英語和其他西歐語言。它是現今最通用的單位元組編碼系統,並等同於國際標準ISO/IEC 646

ASCII第一次以規範標準的型態發表是在1967年,最後一次更新則是在1986年,至今為止共定義了128個字元,其中33個字元無法顯示(這是以現今作業系統為依歸,但在DOS模式下可顯示出一些諸如笑臉、撲克牌花式等8-bit符號),且這33個字元多數都已是陳廢的控制字元,控制字元的用途主要是用來操控已經處理過的文字,在33個字元之外的是95個可顯示的字元,包含用鍵盤敲下空白鍵所產生的空白字元也算1個可顯示字元(顯示為空白)。



轉自WIKI
http://zh.wikipedia.org/w/index.php?title=ASCII&variant=zh-tw

Homework 3-2-2009

1. Write a program that can reverse the order of an input string. For example, if you input "ab", it will output "ba". If you input "abcdefg", it should return "gfedcba".

概念:
比如我有6個字,所以.length()=6
但是我要從編號5開始倒著排回去(5 4 3 2 1 0)
所以.length()-1並取for迴圈美執行ㄧ次減一個編號這樣(--)

public static void main(String[] args) {
String word = "ab";
System.out.println(word);
for(int i=word.length()-1;i>=0;i--)
{
System.out.print(word.charAt(i));
}

String word2 = "abcdefg";
System.out.println("\n" + word2);
for(int i=word2.length()-1;i>=0;i--)
{
System.out.print(word2.charAt(i));
}

// TODO code application logic here
}


2. Write a program that can print names alternatively. The names are in the format of "First Name + Last Name" or "Last Name, First Name". For example, if you input "Walt Savitch", your program will output "Savitch, Walt". If you input "Savitch, Walt", your program will output "Walt Savitch".


用,來取位置來交換前半段和後半段

public static void main(String[] args) {
String name = "Dispy,Jhung";
System.out.println(name);
int i = name.indexOf(",");
String start = name.substring(i+1);
String end = name.substring(0,i);
name = start+" "+end;
System.out.println(name);

2009年3月2日 星期一

Lab String Processing

Do Project 5 of Chap. 1 on Page 56.

Write a program that starts with a line of text and then outputs that line of text with the first occurrence of "hate" changed to "love". For example, a possible sample output might be

The line of text to be changed is:
I hate you.
I have rephrased that line to read:
I love you.

Hint: You may consider use the methods: indexOf(A_String) and substring(Start, End) in your program.



用抄的

Lab Java casting

Write a Java program as follows:

Let m=1, n=2;
Print m/n
Print m/ (double)n;
Print (double) m/n;


Lab Java operators

a) Write a Java program as follows:

Let i=2;
Print i;
Print 2 * (i++);
Print i;

Ans: 2, 4, 3


b) Write a Java program as follows:

Let i=2;
Print i;
Print 2 * (++i);
Print i;

Ans: 2, 6, 3



Homework 2-23-2009

1. Suppose you are a landscape architect who charges $5,000 per mile to landscape a highway, and suppose you know the length in feet of the high way you are working on. Write a Java program to calculate the price you charge when the length is 6000 and 4000, respectively.

Hint: There are 5280 feet in a mile.

2. Write a Java program that displays the results of the expressions 15/4, 4/15, 15%4, 4%15. Calculate the values of these expressions manually to verify that the displayed values are correct.