2009年3月9日 星期一

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?

ASCII(American 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.