我快迪底部助老師的絕招啦~\"~ 請求各路好手相助!!
1. 有一程式碼,其變數的初值宣告如下:
char ch=’P’;
short s=54;
float f=9.6f;
int i=24;
double d=2.98;
試撰寫完整的程式,將下列各小題的運算結果印出,並印出其最終資料型態
(1) ch/(s+f)-(i%d)
(2) ch*(s-f)/i-d
(3) i+(f-ch)*s%(i+ch)
(4) 25-(ch+s)/18
2. 某一維陣列的內容如後int arr[]={4,7,2,9,2,8,7,4,6,3}。試寫一method,傳回本陣列中最大值與最小值的差值。
3. 設計一CBox類別用來表示立體的箱子。此類別內含長(length)、寬(width)與高(height)三個資料成員
(1) 請完成setL()、setW()這兩個method以用來設定CBox物件的length與width成員的值
(2) 再加入setBox(int w, int h) method, 使得它可以同時設定CBox物件的width及height
(3) 承上題,再多載setBox() method,使得它可以同時設定CBox物件的length、width、及height三個資料成員
2006-07-12 05:22:34 · 1 個解答 · 發問者 NICK 1 in 電腦與網際網路 ➔ 程式設計
最快速回答完全者 送20點給這位高手喔~~在此感謝各位大大喔!!
2006-07-12 05:24:04 · update #1
1.public class A { public static void main(String[] args) { char ch = 'P'; short s = 54; float f = 9.6f; int i = 24; double d = 2.98; double result1 = ch/(s+f)-(i%d); double result2 = ch*(s-f)/i-d; float result3 = i+(f-ch)*s%(i+ch); float result4 = 25-(ch+s)/18; System.out.println("(1) ch/(s+f)-(i%d) = " + result1 + " (Type: double)"); System.out.println("(2) ch*(s-f)/i-d = " + result2 + " (Type: double)"); System.out.println("(3) i+(f-ch)*s%(i+ch) = " + result3 + " (Type: float)"); System.out.println("(4) 25-(ch+s)/18 = " + result4 + " (Type: float)"); }}2.public class B { public static void main(String[] args) { int arr[] = {4,7,2,9,2,8,7,4,6,3}; System.out.println("The diff between max and min in {4,7,2,9,2,8,7,4,6,3} is " + diff(arr)); } public static int diff(int[] ary) { int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for (int x: ary) { if (x > max) max = x; if (x < min) min = x; } return (max - min); }}3.public class CBox { private int length, width, height; public void setL(int x) {length = x;} public void setW(int x) {width = x;} public void setBox(int w, int h) { width = w; height = h; } public void setBox(int l, int w, int h) { length = l; width = w; height = h; } public String toString() { return "length = " + length + " | width = " + width + " | height = " + height; } public static void main(String[] args) { CBox cb1 = new CBox(); System.out.println("CBox 1: " + cb1); cb1.setL(5); System.out.println("After setL(5)..."); System.out.println("CBox 1: " + cb1); cb1.setW(6); System.out.println("After setW(6)..."); System.out.println("CBox 1: " + cb1); cb1.setBox(3, 4); System.out.println("After setBox(3, 4)..."); System.out.println("CBox 1: " + cb1); cb1.setBox(1, 2, 3); System.out.println("After setBox(1, 2, 3)..."); System.out.println("CBox 1: " + cb1); }}
2006-07-12 08:10:33 · answer #1 · answered by ? 7 · 0⤊ 0⤋