2009年3月31日 星期二

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?
執行過程

程式碼

class程式碼
------------------------------------------------<程式碼開始>--------------------------------------

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 "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 void writeOutput(){
System.out.println(month + " " + day + ", " + year);
}
public void readInput(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter month, day and year.");
System.out.println("Do not use a comma.");
month = keyboard.next();
day = keyboard.nextInt();
year = keyboard.nextInt();
}
}

------------------------------------------------<程式碼結束>--------------------------------------
主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class class_definition_2 {
    public static void main (String args[]) {
        DateThirdTry date = new DateThirdTry();
int year = 1882;
//date.setDate(6, 17, year);
date.readInput();
date.writeOutput();
//System.out.println(date.month);
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

lab class definition

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

class程式碼
------------------------------------------------<程式碼開始>--------------------------------------

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

------------------------------------------------<程式碼結束>--------------------------------------

主程式碼
------------------------------------------------<程式碼開始>--------------------------------------


import java.util.*;
public class Class_definition 
{
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.writeOutput();
}
}


------------------------------------------------<程式碼結束>--------------------------------------

附註:今天上課完全被這段程式給打敗,只是一個小錯誤,讓我搞到整節課都跑不出來。
在class程式碼時把void寫成Void而無法判讀,而由於軟體的問題,在修改過後一直無法正確的編譯過去。

2009年3月25日 星期三

Homework: 3/23/2009

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.

------------------------------------------------<程式碼開始>--------------------------------------

import java.util.*;
public class Homework03232009 {
    public static void main (String args[]) {
Scanner Keyboard = new Scanner(System.in);
        int mn=0, fn=0, le=1;
double mTotal=0, fTotal=0;
String sex;
do{
System.out.print("enter:");
sex = Keyboard.next();
if(sex.equals("M")){
mTotal = Keyboard.nextDouble();
mn++;
}
else if(sex.equals("F")){
fTotal = Keyboard.nextDouble();
fn++;
}
else if(sex.equals("exit")){
System.out.println("End");
le = 0;
}
else
System.out.println("error.\n");
}while(le==1);
System.out.println("Total people: " + (mTotal + fTotal));
System.out.println("Male's Avg = " + mTotal/mn);
System.out.println("Female's Avg = " + fTotal/fn);
    }
}

------------------------------------------------<程式碼結束>--------------------------------------
附註:本次也是參考以繳交之同學作業改寫,新增了偵錯輸入這部份。這次學到更多的字串應用方式。

2009年3月24日 星期二

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!...


------------------------------------------------<程式碼開始>--------------------------------------

import java.util.*;
import java.util.Scanner;
public class Cosine {
    public static void main (String args[]) {
Scanner Keyboard = new Scanner(System.in);
        int x, n=0, i;
double answer=1, up=1, down=1;
System.out.print("Cos(x)=1 - x^2/2! + x^4/4! - x^6/6!...\nx:");
x = Keyboard.nextInt();
System.out.print("n:");
n = Keyboard.nextInt();
n*=2;
for(i=2 ; i<n+1 ; i+=2){
up = (-1) * up * x * x;
down = down * (i-1) * i;
answer = answer + up / down;
}
System.out.println("Cos(" + x + ") = " + answer);
    }
}

------------------------------------------------<程式碼結束>--------------------------------------
附註:本次程式碼是參考同學的方法來寫,因為發覺自己原本的方式太複雜了。

2009年3月23日 星期一

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

------------------------------------------------<程式碼開始>--------------------------------------

import java.util.*;
public class Fibonacci {
    public static void main (String args[]) {
int n;
double a=0, b=1, f=0;
System.out.println("Fibonacci List:");
for(n=1; n<101; n++){
f = a + b;
System.out.println("n = " + n + " => \t" + f + " => " + f/a);
b = a;
a = f;
}
    }
}

------------------------------------------------<程式碼結束>--------------------------------------
附註:本程式碼經過老師的提點,將不必要的判斷式給拿掉改寫。

2009年3月19日 星期四

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

1.

------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;




import java.util.Scanner;
public class Homework03162009{
static double Function(int x, int n){
double i, j=1, k, h, answer=0;
for(i=n;i>1;i--){
for(k=0, h=1;k<i;k++){
h = h * (k+1);
}
answer = answer + (Math.pow(x, i)) / h;
}
return answer + 1;
}
    public static void main (String args[]) {
Scanner Keyboard = new Scanner(System.in);
int x;
System.out.println("Find e^x.");
System.out.println("e^x = 1 + x^2/2! + ... + x^n/n!\nenter x");
x = Keyboard.nextInt();
System.out.println("n =   1, e^" + x + " = " + Function(x, 1));
System.out.println("n =  10, e^" + x + " = " + Function(x, 10));
System.out.println("n =  50, e^" + x + " = " + Function(x, 50));
System.out.println("n = 100, e^" + x + " = " + Function(x, 100));
    }
}

------------------------------------------------<程式碼開始>--------------------------------------

附註:這部份程式碼驗證部份不知怎麼做,但是就程式來說應該沒有錯誤,若有問題請給予指教。
程式碼修正中...... 2009/03/20

2.

------------------------------------------------<程式碼開始>--------------------------------------

import java.util.*;
public class Homework03162009 {
    public static void main (String args[]) {
        for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
{
if(i*j>=10)
System.out.print(i+"X"+j+"="+i*j+"  ");
else
System.out.print(i+"X"+j+"= "+i*j+"  ");
}
System.out.println();
}
    }
}
------------------------------------------------<程式碼結束>--------------------------------------

附註:久久九九乘法算是基礎的程式練習題目,這部份比較大的問題應該屬於排版吧!因為如果沒有調整看起來會不整齊。


後來發現使用/t的方式即可定位好來,這樣就不必另外寫判斷式。



if(i*j>=10)
System.out.print(i+"X"+j+"="+i*j+"  ");
else
System.out.print(i+"X"+j+"= "+i*j+"  ");

所以上列的程式就可以改寫成:


System.out.print(i+"X"+j+"="+i*j+"/t");

這樣也可以達到排版的效果。
2009/03/23

2009年3月17日 星期二

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.


------------------------------------------------<程式碼開始>--------------------------------------

import java.util.Scanner;
public class s20090316
{
public static void main(String[] args)
{
Scanner Keyboard = new Scanner(System.in);
int i, q, p, temp=0;
System.out.println("write a code to find the max and min of a list of number.");
System.out.println("list?");
q = Keyboard.nextInt();
int[] Number = new int[q];
for(i=0;i<q;i++)
{
p=i+1;
System.out.println("The " + p + " number:");
Number[i] = Keyboard.nextInt();   
}
/*for(i=0, p=0;i<q;i++)
{
p=i+1;
System.out.println(p + " number:"+Number[i]);
}*/
for(int j=0;j<q;j++)
{
for(int k=0;k<q-1;k++)
{
if(Number[k]<Number[k+1])
{
temp = Number[k];
Number[k] = Number[k+1];
Number[k+1] = temp;
}
}   
}
System.out.println("Max = " + Number[0]); 
System.out.println("min = " + Number[q-1]); 
}
}
------------------------------------------------<程式碼結束>--------------------------------------

2009年3月16日 星期一

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 ScannerDemo
{
public static void main(String[] args)
{
Scanner Keyboard = new Scanner(System.in);

System.out.println("輸入三個整數找最大值與最小值");
int i;
int[] Number = new int[4];
System.out.println("請輸入第一個數值:");
Number[0] = Keyboard.nextInt();
System.out.println("請輸入第二個數值:");
Number[1] = Keyboard.nextInt();
System.out.println("請輸入第三個數值:");
Number[2] = Keyboard.nextInt();
for(int j=0;j<3;j++)
{
for(i=0;i<2;i++)
{
if(Number[i]<Number[i+1])
{
Number[i] = Number[3];
Number[i] = Number[i+1];
Number[i+1] = Number[3];
}
}
}
System.out.println("最大值為:" + Number[0]);
}
}

------------------------------------------------<程式碼結束>--------------------------------------

附註:其實這個程式碼是大才小用,本來使用if來比較即可,並不需要做排序。

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 ScannerDemo
{
 public static void main(String[] args)
 {
  Scanner Keyboard = new Scanner(System.in);
 
  System.out.println("查詢應繳稅額");
 
  double Tax=0;
  double TaxP=0;
  for(int i=2;i>0;i--)
  {
   System.out.println("請輸入您的所得淨額:");
   Tax = Keyboard.nextInt();
   System.out.println("您輸入的所得淨額為:"+ Tax);
   if(Tax<=370000)
    TaxP = Tax*0.06;
   else if(Tax>=370001 && Tax<=990000)
    TaxP = Tax*0.13-25900;
   else if(Tax>=990001 && Tax<=1980000)
    TaxP = Tax*0.21-105100;
   else if(Tax>=1980001 && Tax<=3720000)
    TaxP = Tax*0.3-283300;
   else
    TaxP = Tax*0.4-655300;
  
   System.out.println("您應繳納稅額為:" + TaxP);
   Tax=0;
  }
 }
}

/*
應納稅額=所得淨額*適用稅率-累進差額
綜合所得稅淨額 稅率級距 累進差額
0~370,000元 6 % 0元
370,001~990,000元 13% 25,900元
990,001~1,980,000元 21% 105,100元
1,980,001~3,720,000元 30 % 283,300元
>3,720,001元 40% 655,300元
 */
------------------------------------------------<程式碼結束>--------------------------------------

Homework 3-9-2009

1. Do Project 4 in Chapter 2
2. Do Project 5 in Chapter 2
3. Do Project 6, in Chapter 2
4. Do Project 7, in Chapter 2
wait......

2009年3月9日 星期一

Lab Keyboard precessing

Project 3 of Chap. 2

------------------------------------------------<程式碼開始>--------------------------------------

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class S952621502 extends JApplet {
public static void main( String[] args ) throws IOException
{
System.out.println("Enter two number to sum, difference, and product.");
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
String n1 = keyboard.readLine();
String n2 = keyboard.readLine();
int N1 = Integer.parseInt(n1);
int N2 = Integer.parseInt(n2);
int N3 = N1 + N2;
int N4 = N1 - N2;
int N5 = N1 * N2;
System.out.println(N1 + "+" + N2 + "=" + N3);
System.out.println(N1 + "-" + N2 + "=" + N4);
System.out.println(N1 + "*" + N2 + "=" + N5);
}
}
------------------------------------------------<程式碼結束>--------------------------------------

Lab Keyboard input

enter


------------------------------------------------<程式碼開始>--------------------------------------


import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class S952621502 extends JApplet {
public static void main( String[] args ) throws IOException
{
System.out.println("Enter two number to add");
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
String n1 = keyboard.readLine();
String n2 = keyboard.readLine();
int N1 = Integer.parseInt(n1);
int N2 = Integer.parseInt(n2);
int N3 = N1 + N2;
System.out.println(N1 + "+" + N2 + "=" + N3);
}
}

------------------------------------------------<程式碼結束>--------------------------------------

Lab Scanner

enter

------------------------------------------------<程式碼開始>--------------------------------------

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.util.Scanner;
public class S9526215 extends JApplet {
public static void main( String[] args )
{
Scanner Keyboard = new Scanner(System.in);
System.out.println("Enter two number to add");
int number1 = Keyboard.nextInt();
int number2 = Keyboard.nextInt();
int number3 = number1 + number2;
System.out.println(number1 + "+" + number2 + "=" + number3);
}
}


------------------------------------------------<程式碼結束>--------------------------------------