版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
1、第一章 實驗一package ch01;import java.text.SimpleDateFormat;import java.util.Date;class Timer extends Thread private SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH:mm:ss");public void run() while (true) System.out.print("r 現(xiàn)在時間是: "); Date now = new Date(); System.out.
2、print(sdf.format(now); try sleep(1000); catch (InterruptedException e) e.printStackTrace();public class Clock public static void main(String args) Timer timer = new Timer(); timer.start();實驗二package ch01;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.util.Random;impo
3、rt javax.swing.JButton; import javax.swing.JFrame;public class MagicButton extends MouseAdapter JFrame win;JButton button = new JButton(" 你點不到我 "); Random rand = new Random();void initUI() win = new JFrame(); win.setLayout(null);button.setSize(100, 40);button.addMouseListener(this);win.add
4、(button); win.setSize(400, 300); win.setResizable(false); win.setLocationRelativeTo(null); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.setVisible(true);public static void main(String args) MagicButton demo = new MagicButton(); demo.initUI();public void mouseEntered(MouseEvent e) int mous
5、eX = button.getX() + e.getX(); int mouseY = button.getY() + e.getY();while (true) int buttonX = rand.nextInt(win.getWidth() - button.getWidth(); int buttonY = rand.nextInt(win.getHeight() - button.getHeight(); button.setLocation(buttonX, buttonY);if (!button.getBounds().contains(mouseX, mouseY) brea
6、k;第二章 實驗一/*2. 交換兩個變量的值(不允許使用中間變量) 。*/ package ch03;public class Exp2_2 public static void main(String args) int a = 2, b = 3;int s = a * b;a = s / a;b = s / a;System.out.println("a=" + a + ", b=" + b);實驗二/*3. 逆序輸出一個 7位整數(shù),如 8639427 輸出為 7249368(不允許使用循環(huán)語句) 。*/ package ch03;public cl
7、ass Exp2_3 public static void main(String args) long a = 8639427; System.out.print(a % 10); System.out.print(a / 10 % 10); System.out.print(a / 100 % 10); System.out.print(a / 1000 % 10); System.out.print(a / 10000 % 10); System.out.print(a / 100000 % 10); System.out.print(a / 1000000 % 10); 實驗三 /*4
8、. 對于int型變量a,以最快的速度計算 34 x a的值。 */package ch03;public class Exp2_4 public static void main(String args) int a = 3;int b = (a << 5) + (a << 1);System.out.println(a + "*34=" + b);實驗四/*ch 的值轉(zhuǎn)為5. 字符型變量 ch 中存放著一個大小寫未知的英文字母,判斷其大小寫后,將 小寫或大寫字母(不允許使用加減運算符和 if 語句)。*/ package ch03;public c
9、lass Exp2_5 public static void main(String args) char ch = 'E'ch = (char) (ch & 32) = 0 ? ch | 32 : ch & (Integer.MAX_V ALUE - 32); System.out.println("ch1=" + ch); 實驗 5 /*6. 使用嵌套的條件運算符,求 a、b、c 中的最大者。 */package ch03;public class Exp2_6 public static void main(String args) i
10、nt a = 2, b = 4, c = 3;int max = (a > b ? a : b) > c ? (a > b ? a : b) : c; System.out.println("max=" + max);第三章 實驗一/*2. 使用循環(huán)結(jié)構(gòu)逆序輸出任意位數(shù)的整數(shù)。*/ package ch04;import java.util.Scanner;public class Exp3_2 public static void main(String args) Scanner s = new Scanner(System.in); System.o
11、ut.println(" 輸入整數(shù): "); long n = s.nextLong();while (n > 0) System.out.print(n % 10);n /= 10; 實驗二/*3. 輸出以下由數(shù)字組成的菱形(要求將輸出行數(shù)存放于變量中以便隨時更改)12*/ package ch04;import java.util.Scanner;public class Exp3_3 public static void main(String args) int rows;Scanner s = new Scanner(System.in); System.o
12、ut.print(" 輸入行數(shù) :"); rows = s.nextInt();for (int i = -rows / 2; i <= rows / 2; i+) System.out.printf("%-" + (3 * Math.abs(i) + 1) + "s", "");for (int j = Math.abs(i) - rows / 2; j <= rows / 2 - Math.abs(i); j+) System.out.printf("%-3d", rows /
13、2 + 1 - Math.abs(i) - Math.abs(j);System.out.println(); 實驗三/*4. 124輸出以上由數(shù)字組成的三角形(要求將輸出行數(shù)存放于變量中以便隨時更改)3581269131810 15 2114 2019171116*/ package ch04;import java.util.Scanner;public class Exp3_4 public static void main(String args) int rows;Scanner s = new Scanner(System.in); System.out.print("
14、輸入行數(shù) :"); rows = s.nextInt();int firstNumOfRow = 1, nextNumOfRow;for (int i = 1; i <= rows; i+) firstNumOfRow += i - 1; int firstStepOfRow = i + 1; nextNumOfRow = firstNumOfRow; for (int j = 1; j <= rows + 1 - i; j+) System.out.printf("%-4d", nextNumOfRow); nextNumOfRow += first
15、StepOfRow+;System.out.println(); 實驗四/*5. 計算多項式 8+88+888+8888+88888+. 的前 8 項之和。 輸出結(jié)果:98765424*/ package ch04;public class Exp3_5 public static void main(String args) long sum = 0;for (int i = 1; i <= 8; i+) long num = 0; for (int j = 1; j <= i; j+) num = num * 10 + 8;sum += num;System.out.print
16、ln(sum);第四章 實驗一/*1.產(chǎn)生 10個 100 以內(nèi)的隨機整數(shù)以填充一維數(shù)組,實現(xiàn)以下功能。 找出最大以及最小值。查找給定整數(shù) a 在數(shù)組中最后一次出現(xiàn)的位置,若不存在則提示。 判斷數(shù)組是否呈非遞減排列。將數(shù)組元素翻轉(zhuǎn)存放。*/ package ch05;import java.util.Random;import java.util.Scanner;public class Exp4_1 int init() int a = new int10; Random r = new Random(); for (int i = 0; i < a.length; i+) ai =
17、r.nextInt(100); return a;void print(int a) for (int i = 0; i < a.length; i+) System.out.printf("%-5d", ai);System.out.println();int findMax(int a) int max = a0;for (int i = 1; i < a.length; i+) if (max < ai) max = ai;return max;int findMin(int a) int min = a0;for (int i = 1; i <
18、; a.length; i+) if (min > ai) min = ai; return min;int findLastLocation(int a, int x) for (int i = a.length - 1; i >= 0; i-) if (ai = x) return i; return -1;boolean isAsc(int a) for (int i = 0; i < a.length - 1; i+) if (ai > ai + 1) return false;return true;void reverse(int a) for (int i
19、 = 0; i < a.length / 2; i+) int temp = ai;ai = aa.length - i - 1; aa.length - i - 1 = temp;public static void main(String args) Exp4_1 t = new Exp4_1(); int a = t.init(); t.print(a);System.out.println("max=" + t.findMax(a); System.out.println("min=" + t.findMin(a); System.out.
20、print(" 輸入要查找的數(shù): "); Scanner s = new Scanner(System.in); int x = s.nextInt();int i = t.findLastLocation(a, X); if (i = -1) System.out.println(X + " 在數(shù)組中不存在。 else System.out.printf("Last location of %d: %d ");。n", X, i);if (t.isAsc(a) System.out.println(" 數(shù)組是非遞減排列!
21、else System.out.println(" 數(shù)組不是非遞減排列! ");");t.reverse(a);System.out.println(" 翻轉(zhuǎn)后的數(shù)組: "); t.print(a);實驗二/*2. 將 a 插入到一個長度不小于 10 且元素呈遞增排列的一維數(shù)組中,并保證插入之后的數(shù) 組依然遞增(若 a 在插入前的數(shù)組中存在,則輸出提示并忽略) 。*/package ch05;import java.util.Scanner;public class EXp4_2 int a = 2, 4, 5, 7, 9, 11, 15, 1
22、7, 20, 22, Integer.MAX_V ALUE ;void print(boolean isAfterInsert) int end = isAfterInsert ? a.length : a.length - 1; for (int i = 0; i < end; i+) System.out.printf("%-5d", ai);System.out.println();int findInsertLocation(int X) int i = 0;for (; i < a.length - 1; i+) if (ai = X) return
23、-1; else if (ai > X) return i;return i;void insert(int i, int x) for (int j = a.length - 2; j >= i; j-) aj + 1 = aj;ai = x;public static void main(String args) Exp4_2 t = new Exp4_2(); t.print(false);System.out.print(" 輸入要插入的數(shù): ");Scanner s = new Scanner(System.in); int x = s.nextInt
24、();int i = t.findInsertLocation(x); if (i = -1) System.out.println(x + " 在數(shù)組中已經(jīng)存在,放棄插入! "); else t.insert(i, x); t.print(true); 實驗三/*,若無3. 找出階數(shù)不小于 8 的方陣的鞍點值及位置(鞍點值在該行上最大、該列上最小) 鞍點則提示。*/ package ch05;import java.util.ArrayList;import java.util.List;import java.util.Random; import java.util.
25、Scanner;/* 鞍點對象類*/class AnDian private int row; / 鞍點所在行下標 private int col; / 鞍點所在列下標 private int value; / 鞍點值/ 完全構(gòu)造方法public AnDian(int row, int col, int value) this.row = row;this.col = col; this.value = value;/ getters and setters public int getRow() return row;public void setRow(int row) this.row
26、= row;public int getCol() return col;public void setCol(int col) this.col = col;public int getValue() return value;public void setValue(int value) this.value = value;/* 測試類(整體上是若干個并列的 2 重循環(huán),時間復雜度較 3 重循環(huán)低) */public class Exp4_3 int a; / 矩陣int maxOfRows; / 存放每行的最大值int minOfCols; / 存放每列的最小值final int LI
27、MIT = 3; / 矩陣元素值的上限(為測試方便此處寫死,也可在運行時由用戶 輸入)/ 初始化矩陣void initArray() Scanner scanner = new Scanner(System.in);System.out.print(" 輸入矩陣行數(shù) :"); int m = scanner.nextInt(); / 矩陣行數(shù) System.out.print(" 輸入矩陣列數(shù) :"); int n = scanner.nextInt(); / 矩陣列數(shù)/ 構(gòu)造各數(shù)組 a = new intmn; maxOfRows = new intm
28、; minOfCols = new intn;/ 以隨機數(shù)填充矩陣Random random = new Random();for (int i = 0; i < a.length; i+) for (int j = 0; j < ai.length; j+) aij = random.nextInt(LIMIT);/ 記錄每行的最大值int max;for (int i = 0; i < a.length; i+) max = ai0;for (int j = 1; j < ai.length; j+) if (max < aij) max = aij;maxO
29、fRowsi = max;/ 記錄每列的最小值int min;for (int j = 0; j < a0.length; j+) min = a0j;for (int i = 1; i < a.length; i+) if (min > aij) min = aij;minOfColsj = min;/ 打印矩陣 void printArray() System.out.println(" 得到的矩陣為: "); for (int i = 0; i < a.length; i+) for (int j = 0; j < ai.length;
30、j+) System.out.printf("%-8d", aij);System.out.println();/ 找鞍點(可能有多個) ,返回類型為線性表類型( List ),尖括號語法為泛型,表示線 性表的元素為 AnDian 對象,具體看教材List<AnDian> findAnDian() List<AnDian> anDians = new ArrayList<AnDian>(); /構(gòu)造線性表對象for (int i = 0; i < a.length; i+) / 掃描矩陣中的每個元素for (int j = 0; j
31、 < ai.length; j+) / 是當前行最大且當前列最小 if (aij = maxOfRowsi && aij = minOfColsj) AnDian p = new AnDian(i, j, aij); / 構(gòu)造 AnDian 對象 anDians.add(p); / 加入 AnDian 對象到線性表return anDians; / 返回線性表/ 測試入口public static void main(String args) Exp4_3 o = new Exp4_3(); o.initArray(); o.printArray();List<An
32、Dian> anDians = o.findAnDian();System.out.println("if (anDians.size() = 0) / 返回的線性表元素個數(shù)為 0System.out.println(" 沒有鞍點。 "); else int i = 0;for (AnDian e : anDians) / 迭代性 for 循環(huán)的語法也可用于線性表類型 System.out.printf(" 鞍點 %-4d:a%-3d%-3d = %-8dn", +i, e.getRow(), e.getCol(), e.getValue
33、(); 實驗四/*4. 編寫如下圖所示的程序以模擬命令行的 copy 命令。*/ package ch05;public class Exp4_4 public static void main(String args) if (args.length != 2) System.err.println(" 命令語法不正確,使用格式為: java Exp4_4 要復制的文 件 復制到的路徑 ");return; System.out.println(" 成功將 "" + args0 + "" 復制到 ""
34、+ args1 + "" 。 ");實驗五 /*5. 輸出如上圖所示的循環(huán)移位方陣(第一行存于一維數(shù)組,循環(huán)右移該行元素一個位置以 產(chǎn)生下一行,以此類推)19847984757 5198*/ package ch05;public class Exp4_5 void shift(int a) int last = aa.length - 1;for (int i = a.length - 2; i >= 0; i-) ai + 1 = ai;a0 = last; void print(int a) for (int i = 0; i < a.lengt
35、h; i+) System.out.printf("%-5d", ai);System.out.println();public static void main(String args) Exp4_5 t = new Exp4_5(); int a = 7, 4, 8, 9, 1, 5 ; t.print(a);for (int i = 0; i < a.length - 1; i+) t.shift(a); t.print(a);第五章 實驗一/*2. 在例 6.17 的基礎上,將 draw 方法改為計算形狀自身面積的 calcArea 方法并在測試類中 測試。S
36、quare 類提示:可根據(jù)需要在 Shape 的每個子類中增加用以計算面積所必須的字段,如為 增加邊長輸出結(jié)果: s1 的面積: s2 的面積: s3 的面積:字段、 Circle 類增加半徑字段等,然后編寫含相應字段的構(gòu)造方法以構(gòu)造具體的形狀對象。4.06.012.566370614359172*/ package ch06;class Shape public double calcArea() return 0;class Square extends Shape float width;public Square(int width) this.width = width;public
37、 double calcArea() return width * width;class Triangle extends Shape float a, b, c;public Triangle(float a, float b, float c) this.a = a;this.b = b;this.c = c;public double calcArea() float s = (a + b + c) / 2;return Math.sqrt(s * (s - a) * (s - b) * (s - c);class Circle extends Shape float radius;p
38、ublic Circle(float radius) this.radius = radius;public double calcArea() return Math.PI * radius * radius; public class Exp5_2 public static void main(String args) Shape s1 = new Square(2); Shape s2 = new Triangle(3, 4, 5); Shape s3 = new Circle(2); System.out.println("s1 System.out.println(&qu
39、ot;s2 System.out.println("s3 實驗二 /*3. 編寫 TimeCounter 類,其靜態(tài)方法 秒數(shù) begin 在命令行顯示倒計時(每隔 中調(diào)用 startCount 方法。的面積: 的面積: 的面積:" + s1.calcArea();" + s2.calcArea();" + s3.calcArea();startCount(int begin) 方法根據(jù)指定的初始1 秒刷新,時間為 0 時退出程序) ,然后在 main 方法*/ package ch06;public class Exp5_3 static void s
40、tartCount(int begin) while (begin > 0) System.out.println(begin); long init = System.currentTimeMillis(); while (true) long now = System.currentTimeMillis(); if (now - init >= 1000) begin-;break;System.out.println(" 時間到! ");public static void main(String args) startCount(5);實驗三/*4.編寫
41、 Complex 類表示數(shù)學上的復數(shù)概念,具體包括: real 和 image 字段,分別表示復數(shù)的實部和虛部。 讀取和設置 real/image 字段的 get 和 set 方法。 根據(jù)實部和虛部參數(shù)構(gòu)造復數(shù)對象的構(gòu)造方法。 打印當前復數(shù)對象內(nèi)容以及與另一復數(shù)相加的方法,原型為:void printInfo();Complex add(Complex anotherComplex); 重寫父類 Object 的 equals 方法,相等邏輯為“若 2 個復數(shù)對象的實部和虛部分別對應相 等,則這 2 個復數(shù)相等” 。最后編寫一個帶 main 方法的測試類 ComplexTest ,分別測試 C
42、omplex 中的各個方法。 */package ch06;class Complex float real; float image;public float getReal() return real;public void setReal(float real) this.real = real;public float getImage() return image;public void setImage(float image) this.image = image;public Complex(float real, float image) this.real = real;
43、this.image = image;void printInfo() H:HSystem.out.println(real + (image > 0 ? "+" : "-") + Math.abs(image) + Complex add(Complex anotherComplex) Complex c = new Complex(0, 0); c.setReal(this.real + anotherComplex.getReal(); c.setImage(this.image + anotherComplex.getImage(); re
44、turn c;public boolean equals(Object obj) Complex c = (Complex) obj;return this.real = c.getReal() && this.image = c.getImage(); public class Exp5_4 public static void main(String args) Complex c1 = new Complex(1, -2); Complex c2 = new Complex(2, 6); Complex c3 = c1.add(c2); Complex c4 = c2.a
45、dd(c1);System.out.print("c1=");c1.printInfo();System.out.print("c2=");c2.printInfo();System.out.print("c3=");c3.printInfo();System.out.print("c4=");c4.printInfo();System.out.println(c1.equals(c2) ? "c1=c2" : "c1!=c2");System.out.println(c3.
46、equals(c4) ? "c3=c4" : "c3!=c4"); 實驗四/*5. 查閱 java.util 包下的 GregorianCalendar 類的 API 文檔,編寫 MyDate 類繼承該類,并實 現(xiàn)以下方法:MyDate(int year, int month, int day); / 根據(jù)指定的年月日構(gòu)造日期對象/ 得到當前日期的年份/ 得到當前日期的月份/ 得到當前日期是本年的第幾天/ 得到當前日期是本月的第幾天/ 得到當前日期是本周的第幾天(即星期幾)int getYear();int getMonth();int getDayOf
47、Year();int getDayOfMonth();MyDate getBeforeDate(int beforeDays);/ 得到當前日期之前若干天對應的日期對象/ 得到當前日期之后若干天對應的日期對象 / 得到當前日期與指定日期 d 相隔多少天int getDayOfWeek();MyDate getAfterDate(int afterDays); int daysBetweenWith(MyDate d); 最后編寫一個帶 main 方法的測試類 MyDateTest ,分別測試 MyDate 中的各個方法。 提示:可通過父類GregorianCalendar相應方法的組合以實現(xiàn)上
48、述各方法。 注意父類中根據(jù)年月日創(chuàng)建日歷對象的構(gòu)造方法中,月份參數(shù)是從 0 開始的。 */package ch06;import java.util.GregorianCalendar;class MyDate extends GregorianCalendar MyDate(int year, int month, int day) super(year, month - 1, day);int getYear() return super.get(YEAR);int getMonth() return super.get(MONTH) + 1;int getDayOfYear() retu
49、rn super.get(DAY_OF_YEAR); int getDayOfMonth() return super.get(DAY_OF_MONTH);int getDayOfWeek() return super.get(DAY_OF_WEEK); MyDate getBeforeDate(int beforeDays) long time = super.getTimeInMillis() - beforeDays * 24L * 60 * 60 * 1000; GregorianCalendar gc = new GregorianCalendar();gc.setTimeInMil
50、lis(time);return new MyDate(gc.get(YEAR), gc.get(MONTH) + 1, gc.get(DAY_OF_MONTH); MyDate getAfterDate(int afterDays) long time = super.getTimeInMillis() + afterDays * 24L * 60 * 60 * 1000; GregorianCalendar gc = new GregorianCalendar();gc.setTimeInMillis(time);return new MyDate(gc.get(YEAR), gc.get
51、(MONTH) + 1, gc.get(DAY_OF_MONTH); int daysBetweenWith(MyDate d) long t1 = super.getTimeInMillis(); long t2 = d.getTimeInMillis(); return (int) (Math.abs(t1 - t2) / (24 * 60 * 60 * 1000);void print(String name) %d-%d-%dn", name, getYear(), getMonth(),System.out.printf("%-4s: getDayOfMonth(
52、);public class Exp5_5 public static void main(String args) MyDate d1 = new MyDate(2014, 3, 12);MyDate d2 = new MyDate(2012, 5, 5);MyDate d3 = new MyDate(2013, 3, 12);d1.print("d1");d2.print("d2");d3.print("d3");System.out.println("d3System.out.println("d3System.out.println("d3是該星期的第 " + d3.getDayOfWeek() + " 天"); 是該月的第 " + d3.getDayOfMonth() + " 天"); 是該年的第 " + d3.getD
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 戰(zhàn)友聚會發(fā)言稿合集15篇
- 成人禮學生發(fā)言稿(范文15篇)
- 感恩父母倡議書(15篇)
- 建筑工地質(zhì)量安全會議
- 土地職業(yè)培訓平臺
- 插花入門基礎知識
- 數(shù)據(jù)專員培訓課件
- 安全健康伴我行班會
- 2025年中考復習必背歷史措施類試題答題模板
- 陰囊積液的高頻彩色多普勒超聲特征分析
- 2024年全國各地中考試題分類匯編:作文題目
- 《糖拌西紅柿 》 教案()
- 彈性力學數(shù)值方法:解析法:彈性力學中的變分原理
- 《零售學第二版教學》課件
- 廣東省珠海市香洲區(qū)2023-2024學年四年級下學期期末數(shù)學試卷
- 房地產(chǎn)行業(yè)職業(yè)生涯規(guī)劃
- 河南省鄧州市2023-2024學年八年級上學期期末語文試題
- 江蘇省建筑與裝飾工程計價定額(2014)電子表格版
- MOOC 數(shù)字電路與系統(tǒng)-大連理工大學 中國大學慕課答案
- 八年級英語上冊語法填空練習題(含答案)
- 東北三省三校2024年高三二模(第二次聯(lián)合模擬考試)英語試卷(含標準答案)
評論
0/150
提交評論