實 驗 報 告
( 2017 / 2018學年 第2學期)
課程名稱 |
JAVA語言程序設計 |
|||||
實驗名稱 |
Java集成開發環境的安裝與使用、 Java變量、表達式與控制結構 |
|||||
實驗時間 |
2018 |
年 |
4 |
月 |
11 |
日 |
指導單位 |
計算機學院軟件教學中心 |
|||||
指導教師 |
許棣華 |
學生姓名 |
王利國 |
班級學號 |
B160209 |
學院(系) |
電子與光學工程學院,微電子學院 |
專 業 |
微電子科學與工程 |
實驗名稱 |
方法、數組和類 |
指導教師 |
許棣華 |
|
|||||||
實驗類型 |
上機 |
實驗學時 |
2 |
實驗時間 |
2017.4.11 |
|
|||||
一、 實驗目的 1. 掌握方法的聲明和調用 2. 掌握數組的聲明、創建、初始化和使用 3. 掌握類的定義 4. 掌握對象的創建和使用 5. 掌握構造方法的使用 6. 掌握包的創建和使用 |
|
||||||||||
二、實驗環境(實驗設備) 1. 每位學生配備計算機一台 2. 計算機需安裝好JDK和Jcreator |
|
||||||||||
三、實驗內容 1. 尋找並輸出11~999之間的數m,它滿足m、m2、m3均為回文數。回文數是各位數字左右對稱的整數。判斷是否為回文要求通過編寫方法來完成。 package Test; /** * @Author liguo * @Description 尋找並輸出11~999之間的數m,它滿足m、m2、m3均為回文數。 * 回文數是各位數字左右對稱的整數。判斷是否為回文要求通過編寫方法來完成。 * @Data 2018-04-11 */ public class Test21 { //將數按照位數轉化為數組,然后比較數組首尾是否相同來判斷是否為回文 static boolean huiWen(int a) { boolean flag = true; int temp = String.valueOf( a ).length(); int b[] = new int[temp]; for (int i = 0; i <= temp - 1; i++) { b[i] = a % 10; a = a / 10; } int mid = temp / 2; for (int i = 0; i <= mid; i++) { if (b[i] != b[temp - 1 - i]) { flag = false; break; } } return flag; // for ( int number : b) // System.out.println( number ); } public static void main(String[] args) { for (int i = 11; i <= 999; i++) { if (huiWen( i )) { if (huiWen( i * i )) { if (huiWen( i * i * i )) System.out.println( i + " " ); } } } } }
2. 由鍵盤輸入10個整數,比較並輸出其中的最大值和最小值。 package Test; import java.util.Arrays; import java.util.Scanner; /** * @Author liguo * @Description 由鍵盤輸入10個整數,比較並輸出其中的最大值和最小值。 * @Data 2018-04-11 */ public class Test22 { public static void main(String[] args) { Scanner in = new Scanner( System.in ); int[] array = new int[10]; for (int i = 0; i < 10; i++) { array[i] = in.nextInt(); } Arrays.sort( array ); System.out.println( "最大值為" + array[9] + "最小值為" + array[0] ); } }
3. 隨機產生50個1-100之間的整數,存放於一個10´5的二維數組中,要求按照10´5的格式打印這個數組(即共顯示10行,每行5個數,數與數之間間隔一個空格),並求出該數組所有元素之package Test; /** * @Author liguo * @Description * @Data 2018-04-11 */ public class Test23 { public static void main(String[] args) { int[][] array = new int[10][5]; int i = 0; for (int[] row : array) { for (int value : row) { value = (int) (Math.random() * 100); System.out.print( value + " " ); int sum = 0; } System.out.println(); } } }
4. 學生類的創建和使用 ①創建一個學Student類,成員變量包括:學號、班號、姓名、性別、年齡等,且都是private類型。 ②聲明一個構造方法,初始化所有成員變量。 ③分別聲明獲得各屬性的public類型的成員方法,方法名要求以get開頭。 ④分別聲明修改各屬性的public類型的成員方法,方法名要求以set開頭。 ⑤聲明一個public類型的toString()方法,把該類中的所有域信息組合成一個字符串。 ⑥聲明統計創建Student對象的個數的私有域count和得到Student對象的個數的public方法。 ⑦將類Student放在子包student中。 ⑧在子包student外,創建測試類Student的主類。 在主類中:創建2個Student對象,輸出對象的所有域信息;修改對象的姓名和年齡,輸出修改后的姓名和年齡;比較兩個Student對象的年齡的大小,輸出年齡較大的Student對象package student; /** * @Author liguo * @Description * @Data 2018-04-11 */ public class Student { private int studentID; private int clssID; private String name; private String sex; private int age; private static int count; public static int getCount() { return count; } public Student(int studentID, int clssID, String name, String sex, int age) { this.studentID = studentID; this.clssID = clssID; this.name = name; this.sex = sex; this.age = age; count++; } @Override public String toString() { return "Student{" + "studentID=" + studentID + ", clssID=" + clssID + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}'; } public int getStudentID() { return studentID; } public void setStudentID(int studentID) { this.studentID = studentID; } public int getClssID() { return clssID; } public void setClssID(int clssID) { this.clssID = clssID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 地生院作業版本答案 import java.util.*; class Student { private String studentID; private String name; private String sex; private int age; private static int count; public static int getCount() { return count; } public Student(String studentID, String name, String sex, int age) { this.studentID = studentID; this.name = name; this.sex = sex; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void setCount(int count) { Student.count = count; } public static void main(String[] args) { //順序輸入兩個學生的學號、姓名、性別、年齡,一行輸入一個學生 // //輸出一下字符串“The older student is 年齡較大的學生姓名.” // B01 zhang male 20 // B02 li female 25 Scanner in = new Scanner( System.in ); String xuehao1 = in.next( ); String name1 = in.next( ); String sex1 = in.next( ); int age1 = in.nextInt(); String xuehao2 = in.next( ); String name2 = in.next( ); String sex2 = in.next( ); int age2 = in.nextInt(); Student s1 = new Student( xuehao1, name1, sex1, age1); Student s2 = new Student( xuehao2, name2, sex2, age2 ); if (s1.getAge() > s2.getAge()) System.out.println( "the older student is "+ s1.getName() ); else System.out.println( "the older student is "+ s2.getName() ); } } public static void main(String[] args) { Student one = new Student( 16020913, 9, "王利國", "男", 19 ); Student two = new Student( 16020912, 9, "王寧寧", "男", 20 ); System.out.println( one.toString() + "\n" + two.toString() ); one.setName( "lili" ); one.setAge( 20 ); two.setName( "dingding" ); two.setAge( 21 ); System.out.print( "兩個學生年齡較大者為:" ); System.out.println( one.getAge() > two.getAge() ? one.getAge() : two.getAge() ); System.out.println( "student對象的個數為"+getCount() ); }
}
|
|
||||||||||
四、實驗小結(包括問題和解決方法、心得體會等)
1:可以使用String.valueOf(數據).length方法來獲取int類型的長度。
2第一次在二維數組中使用foreach,挺好的,框架如下
for (int[] row : array) {
3:有些問題java也沒有封裝方法,查找api也沒有找到一個直接將int類型轉換成int數組的方法。最后只能手寫int類型轉數組。
4:有編譯器挺方便,直接可以生成第四題類中的Constructor和Getter,Setter方法。 |
|||||||||||
五、指導教師評語
|
|||||||||||
成 績 |
|
批閱人 |
|
日 期 |
|
||||||