




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、 實驗一1. 編寫一個類的方法,判斷某一年是否為閏年。閏年的條件是符合下面二者之一:能被 4 整除,但不能被 100 整除;能被 4 整除,又能被 100 整除。import java.util.Scanner;public class isLeapYear public static void main(String args) Scanner input = new Scanner(System.in); int num = input.nextInt(); if(num%400=0)|(num%100!=0&&num%4=0) System.out.println(num
2、+"是瑞年");else System.out.println(num+"不是瑞年"); 2. 編寫程序打印出所有的水仙花數(水仙花數是三位的整數)。public class shuiXianHua public static void main(String args) for(int i=100;i<=999;i+) int a=i/100; int b=i/10%10; int c=i%100%10; if(a*a*a+b*b*b+c*c*c=i) System.out.println(i+"是水仙花"); 3. 編寫程序
3、打印出100以內的素數。public class prime public static void main(String args) for(int i=2;i<=100;i+) if(is_prime(i) System.out.println(i); public static boolean is_prime(int num) for(int i=2;i<num;i+) if(num%i=0) return false; return true; 4. 編寫程序求1!+2!+20!import java.util.Scanner;public class jc public
4、 static void main(String args) int sum=0; int n=20; int ans=1; for(int i=1;i<=n;i+) ans=1; for(int j=1;j<=i;j+) ans*=j; sum+=ans; System.out.println(sum); 課后習題2.9,循環輸出某個圖形public class main_5 public static void main(String args) for(int i=1;i<=3;i+) for(int j=3-i+1;j>=1;j-) System.out.pri
5、nt(" "); for(int t=0;t<(i-1)*2;t+) System.out.print("*"); System.out.println("*"); for(int i=1;i<=3;i+) System.out.print("*"); System.out.print("+"); for(int i=1;i<=2;i+) System.out.print("*"); System.out.println("*"); fo
6、r(int i=1;i<=3;i+) for(int j=1;j<=i;j+) System.out.print(" "); for(int t=0;t<(3-i)*2;t+) System.out.print("*"); System.out.println("*"); 5. 編寫一個類的方法,其輸入參數為一個整數,輸出為該整數各個位上的最大數字。import java.util.Scanner;public class main_6 public static int judge(int num)int max=
7、-1;while(num!=0)int temp=num%10;if(temp>max)max=temp;num=num/10;return max;public static void main(String args) Scanner input = new Scanner(System.in); int num = input.nextInt(); System.out.println(judge(num); 6. 編寫程序輸出九九乘法表。public class main_7 public static void main(String args) for(int i=1;i&l
8、t;=9;i+) for(int j=1;j<i;j+) System.out.print(j+"*"+i+"="+i*j+" "); System.out.println(i+"*"+i+"="+i*i); 實驗二1、 設計一個名為figure的圖形軟件包(package)。包中包含矩形、圓。要求:(1)每個類都要構造方法并為成員設置get和set方法;(2)每個類都要有計算周長和面積的成員方法;(3)完成該軟件包后的編碼后,在另一個包的含有main方法的類中編寫代碼,分別使用圖形軟件包
9、中的類,生成對象,并打印出其周長和面積。package figure;public class circular private double r;public circular(double r)this.r=r;public double area()return 3.14*r*r;public double circle()return 3.14*2*r;package figure;public class rectangle private double width;private double length;public rectangle(double width,double
10、length)this.width=width; this.length=length; public double area()return width*length;public double circle()return (width+length)*2;package zuoye2;import figure.*;import java.util.Scanner;public class figure_main public static void main(String args)Scanner sc = new Scanner(System.in);System.out.print
11、ln("請輸入矩形的寬和長");double w =sc.nextDouble();double l =sc.nextDouble();rectangle r=new rectangle(w,l);System.out.println("矩形面積是"+r.area();System.out.println("矩形周長是"+r.circle();System.out.println("請輸入圓的半徑");double radii =sc.nextDouble();circular c=new circular(rad
12、ii);System.out.println("圓的面積是"+c.area();System.out.println("圓的周長是"+c.circle();2、 編寫類Factorial,為其添加兩個靜態方法(方法名自定義)。其中一個使用遞歸計算n的階乘,一個使用非遞歸計算n的階乘。構造main方法進行測試。package zuoye2;import java.util.Scanner;public class Factorial public static void main(String args)System.out.println("請
13、輸入n");Scanner sc = new Scanner(System.in);int n=sc.nextInt();System.out.println("遞歸求解"+digui(n);System.out.println("非遞歸求解"+xunhuan(n);public static long digui(int n)if(n<0)return -1;else if(n=0)|(n=1)return 1;elsereturn n*digui(n-1);public static long xunhuan(int n)int su
14、m=1;if(n<0)return -1;else if(n=0)|(n=1)sum=1;elsefor(int i=1;i<n+1;i+)sum=sum*i;return sum;3、 一個公司有三種不同類型的員工,他們的薪水分別按年(計算方法:年薪*工作年數)、按月(計算方法:月薪*工作月數)、按周(計算方法:周薪*工作周數)結算。編寫類Company,提供計算所有員工總薪水的方法getEarnings,該方法能夠根據輸入的一組員工(包含各類員工)返回這組員工的總薪水。package Company;public class Company public static int
15、sum_money=0; public static int getEarning(worker x) return sum_money+=x.qian(); public static int getEarnings() return sum_money; public static void main(String args) worker a=new worker(5,10000 ); worker_year b=new worker_year(5,1000); worker_month c=new worker_month(15,300); worker_week d=new work
16、er_week(25,100); getEarning(a); getEarning(b); getEarning(c); getEarning(d); System.out.println("總薪水為"+getEarnings(); package Company;public class worker int hour; int money;public worker(int hour_,int money_)this.hour=hour_;this.money=money_;public worker()public int qian()return hour*mon
17、ey; package Company;public class worker_year extends worker int year; int money;public worker_year(int year_,int money_)this.year=year_;this.money=money_;public int qian()return year* money; package Company;public class worker_month extends worker int month; int money;public worker_month(int month,i
18、nt money)this.month=month;this.money=money;public int qian()return month*money; package Company;public class worker_week extends worker int week; int money;public worker_week(int week,int money)this.week=week;this.money=money;public int qian()return week*money; 4、設計一個教師類Teacher(屬于.sdkd包),要求:1) 屬性有編號
19、(int no)、姓名(String name)、年齡(int age)、所屬學院(String seminary),為這些屬性設置相應的get和set方法。2) 為Teacher類重寫equals方法,要求:當兩個教師對象的no相同時返回true。3) 重寫Teacher類的toString方法,通過該方法可以返回“編號為*、姓名為*、年齡為*的*學院老師”形式的字符串。4) 再定義一個類TeacherManagement(屬于cn.sd包),提供方法search,方法可以在一組給定的教師中,根據姓名(或年齡)返回等于指定姓名(或年齡)的教師的字符串信息,信息格式為:“編號為*、姓名為*、年
20、齡為*的*學院老師”。如果沒有滿足條件的教師,則返回“沒有符合條件的教師”。5) 構造main方法進行測試。package .sdkd;public class Teacher private int no;private String name;private int age;private String seminary;public Teacher(int no,String name,int age,String seminary)this.no=no;=name;this.age=age;this.seminary=seminary;public boolean e
21、quals(Teacher t)if (this.getNo()=t.getNo()return true;elsereturn false;public String toString()return "編號為"+this.no+"、姓名為"++"、年齡為"+this.age+"的"+this.seminary+"學院的老師"package cn.sd;import .sdkd.Teacher;public class TeacherManagement public voi
22、d search(Teacher t,int age) int l = t.length; int i; for( i=0;i<l;i+) if(age=ti.getAge() System.out.println(ti.toString(); break; else if(i=l-1) System.out.println("沒有符合條件的老師"); public void search(Teacher t,String name) int l = t.length; int i;for( i = 0;i < l;i+)if(name=ti.getName()
23、 System.out.println(ti.toString(); break; else if(i=l-1)System.out.println("沒有符合條件的老師"); package .sdkd;import cn.sd.TeacherManagement;public class Teacher_main public static void main(String args) Teacher t = new Teacher4; t0=new Teacher(001,"趙",25,"信息"); t1=new Teacher
24、(002,"錢",26,"信息"); t2=new Teacher(002,"孫",27,"網絡"); t3=new Teacher(003,"李",27,"計算機");if (t1.equals(t2) System.out.println(t1.getName() + "老師和" + t2.getName() + "老師學工號相同");else System.out.println(t1.getName() + "老師和&
25、quot; + t2.getName() + "老師學工號不相同");if (t2.equals(t3) System.out.println(t2.getName() + "老師和" + t3.getName() + "老師學工號相同");else System.out.println(t2.getName() + "老師和" + t3.getName() + "老師學工號不相同"); System.out.println(t3.toString(); TeacherManagement a
26、= new TeacherManagement(); a.search(t,25); a.search(t,28); a.search(t,"趙"); a.search(t,"zhao");1、按照要求使用Java進行編碼。1) 編寫一個抽象類Shape,其中有抽象方法getArea()和getPerimeter()2) 在Shape類的基礎上派生出Rectangle和Circle類,二者都實現了計算面積的方法getArea()和計算周長的方法getPerimeter();3) 構造main函數,生成Rectangle和Circle對象,并用Shape類
27、型的變量調用Rectangle和Circle對象的getArea()和getPerim()方法。public class Circle extends Shapepublic double radius;Circle(double radius1)radius=radius1; public double getArea() return radius*radius*3.14; public double getPerimeter()return 2.0*radius*3.14;public class Rectangle extends Shape public double length;
28、 public double width; Rectangle(double length1,double width1) length=length1; width=width1; public double getArea() return length*width; public double getPerimeter() return 2.0*(length+width); public abstract class Shape public abstract double getArea();public abstract double getPerimeter();public s
29、tatic void main(String args)Rectangle rectangle = new Rectangle(2.0,1.0);Circle circle = new Circle(1.0);Shape shape;shape=rectangle;System.out.println(shape.getArea();System.out.println(shape.getPerimeter();shape=circle;System.out.println(shape.getArea();System.out.println(shape.getPerimeter();2、 卡
30、車要裝載一批貨物,貨物有三類,包括:電視、計算機、洗衣機。需要計算一個貨車裝載的所有貨物的總重量。要求有一個ComputeWeight接口,其中包含一個方法:public int computeWeight()。三種貨物分別實現該接口。為卡車類Car提供一個方法,能夠計算一個貨車所裝載的所有貨物的重量之和。package zuoye3;interface ComputeWeight public int computeWeight(); class TV implements ComputeWeightpublic int weight;public int quantity;public s
31、tatic int sum_weight;public TV(int weight,int quantity) this.weight=weight; this.quantity=quantity;public int computeWeight() return sum_weight += weight*quantity; class computer implements ComputeWeightpublic int weight;public int quantity;public static int sum_weight;public computer(int weight,int
32、 quantity) this.weight=weight; this.quantity=quantity;public int computeWeight() return sum_weight+= weight*quantity; class xiyiji implements ComputeWeightpublic int weight;public int quantity;public static int sum_weight;public xiyiji(int weight,int quantity) this.weight=weight; this.quantity=quant
33、ity;public int computeWeight() return sum_weight += weight*quantity;public class car public static int sum_weight=0; public static void main(String args) ComputeWeight weights=new ComputeWeight3; weights0=new TV(100,5); weights1=new computer(10,10); weights2=new xiyiji(200,5); for(int i=0;i<3;i+)
34、 sum_weight+=puteWeight(); System.out.println("總重量為"+sum_weight);3、 在實驗2中所實現的Teacher類的基礎上,修改Teacher類的代碼,要求:由多個Teacher對象所形成的數組可以以兩種方法排序(編號由低到高排序):1)使用Arrays.sort(Object a)方法;2)使用Arrays.sort(Object a, Comparator c)方法。package zuoye3;public class Teacher implements Comparable<Ob
35、ject>int no;private String name;int age;private String seminary;public Teacher(int no,String name,int age,String seminary)this.no=no;=name;this.age=age;this.seminary=seminary;public String toString()return "編號為"+this.no+"、姓名為"++"、年齡為"+this.age+"
36、;的"+this.seminary+"學院的老師"public int compareTo(Object o) Teacher t=(Teacher)o;if(this.no>t.no)return 1; if(this.no<t.no) return -1;return 0;package zuoye3;import java.util.Comparator;public class bycomparator implements Comparator<Object> public final int compare(Object t1,
37、 Object t2) if(Teacher)t1).age>(Teacher)t2).age) return 1; else if(Teacher)t1).age<(Teacher)t2).age) return -1; else return 0; package zuoye3;import java.util.Arrays;public class Teacher_main public static void main(String args) Teacher t = new Teacher4; t0=new Teacher(001,"趙",25,&qu
38、ot;信息"); t1=new Teacher(006,"錢",26,"信息"); t2=new Teacher(002,"孫",28,"網絡"); t3=new Teacher(003,"李",27,"計算機"); System.out.println("按學號排序前輸出"); for(int i=0;i<4;i+) System.out.println(ti.toString(); Arrays.sort(t); System.out.
39、println("按學號排序后 輸出"); for(int i=0;i<4;i+) System.out.println(ti.toString(); Arrays.sort(t,new bycomparator(); System.out.println("按年齡排序后 輸出"); for(int i=0;i<4;i+) System.out.println(ti.toString();1、 設計一個類,提供以下操作:(1)將一個含有n個元素的int型數組的元素按照從小到大的順序排列;(2)對排好序的數組使用折半查找(使用遞歸和非遞歸兩種形
40、式分別實現)查找某一個int元素。import java.util.Arrays;import java.util.Scanner;public class Ques1 public int checkRecursion(int a,int x,int start,int end)/System.out.println("=");if(start>=end)return -1;int mid=(start+end)/2;if(amid=x)return mid;if(amid<x)return checkRecursion(a,x,mid+1, end);els
41、ereturn checkRecursion(a,x,start,mid);public int checkUnRecursion(int a,int x,int start,int end)int mid;while(start<end)mid=(start+end)/2;if(amid=x)return mid;if(amid<x)start=mid+1;elseend=mid;return -1;public static void main(String args) int arr= 1,6,2,4,9,8,7,3; Arrays.sort(arr, 0, arr.leng
42、th); for(int i=0;i<arr.length;i+) System.out.print(arri+" "); Ques1 ques1= new Ques1(); System.out.println(ques1.checkUnRecursion(arr, 0, 0, arr.length);2、 使用一維數組編碼實現一個棧(Stack)類,要求提供以下操作:(1)boolean isEmpty():判斷棧當前是否為空;(2)入棧操作void push(obj):把數據元素obj插入堆棧;(3)出棧操作Object pop():出棧,并返回刪除的數據元素;
43、(4)Object getTop():取堆棧當前棧頂的數據元素并返回。編寫代碼測試所形成的Stack類,然后利用Stack類實現以下功能:輸入一個正整數,輸出該整數所對應的二進制數。package matrix;import java.util.Scanner;public class Stack private Object a;private int left;private static Scanner sc;Stack(int n) a=new Objectn;for(int i=0;i<n;i+)ai=null;left=n;boolean isEmpty()if(a.leng
44、th=left)return true;elsereturn false;void push(Object obj)if(left=0)System.out.println("ERROR");return;aleft-1=obj;left-;Object pop()Object o=aleft;aleft=null;left+;return o;Object getTop()return aleft;int erjinzhi()return Integer.parseInt(String) aleft, 2);public static void main(String a
45、rgs)Stack s=new Stack(10);sc = new Scanner(System.in); int num; System.out.print("請輸入一個正整數"); while (sc.hasNextInt() num = sc.nextInt(); while (num > 0) s.push(num % 2); num = num / 2; System.out.print("對應的二進制數為:"); while (!s.isEmpty() System.out.print(s.getTop(); s.pop(); Sys
46、tem.out.print("n"); 3、按照要求使用Java編碼。1) 以類型int聲明一個叫matrix的二維數組變量,將矩陣初始化為一個5個元素的數組。2) 以下列方式為matrix的內部元素賦值:matrix從零開始循環到其長度值;例如索引為i,在每次迭代中,將matrixi指向一個新的整數數組,其長度為i。然后用索引變量j,對數組中的每一個元素進行循環。在每次內部循環中,將matrixij賦值為(i*j)。3) 通過循環打印matrix中的所有元素,結果為:<><0><0 2><0 3 6><0 4 8 12
47、>package matrix;public class matrix public static void main(String args)int a=new int5;for(int i=0;i<a.length;i+)ai=new inti;for(int j=0;j<i;j+)aij=i*j;for(int i=0;i<a.length;i+)System.out.print("<");for(int j=0;j<ai.length;j+)if(j=0)System.out.print(aij);elseSystem.out.p
48、rint(" "+aij);System.out.println(">");4、 利用二維數組實現一個矩陣類:Matrix。要求提供以下操作:(1)set(int row, int col, double value):將第row行第col列的元素賦值為value;(2)get(int row,int col):取第row行第col列的元素;(3)width():返回矩陣的列數;(4)height():返回矩陣的行數;(5)Matrix add(Matrix b):返回當前矩陣與矩陣b相加后的結果矩陣;(6)Matrix multiply(Matr
49、ix b):返回當前矩陣與矩陣b相乘后的結果矩陣。(7)print():打印出當前矩陣的值。package matrix;public class Matrix2 public double a;public Matrix2(int row,int col)this.a=new doublerowcol;public void set(int row, int col, double value) if(row>=1&&row<=this.a.length&&col>=1&&col<=this.a0.length)this
50、.arow-1col-1=value;public double get(int row,int col)if(row>=1&&row<=this.a.length&&col>=1&&col<=this.a0.length)return this.arow-1col-1;return 0;public int width()return a0.length;public int height()return a.length;public Matrix2 add(Matrix2 m)Matrix2 matrix;int r
51、ow=this.a.length>m.a.length?this.a.length:m.a.length;int col=this.a0.length>m.a0.length?this.a0.length:m.a0.length; matrix=new Matrix2(row,col); for(int i=0;i<m.a.length;i+) for(int j=0;j<m.a0.length;j+) matrix.aij=m.aij; for(int i=0;i<this.a.length;i+) for(int j=0;j<this.a0.length;j+) matrix.aij+=this.aij;return matrix;public
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 企業內部如何建立高效的醫療信息安全事件處理機制
- 辦公自動化與AI加密區塊鏈技術的安全保障
- 醫療健康數據與疾病預測的精準性
- 健康數據驅動的醫療支出決策模式
- 從醫療事故看醫患信任的脆弱性
- 醫療行業數字化轉型數據驅動的醫療安全策略
- 以人為本探索更佳的醫療商業服務模式
- 醫療安全文化在專科護理教育中的重要性
- 八年級生物上冊知識點總結模版
- 醫療事故預防與風險控制教育
- 《神奇糖果店》教學課件
- 港口建設項目風險評估報告
- 中國傳統文化“二十四節氣”與美術學科的融合
- 傳媒公司主播離職協議書
- 環氧樹脂畢業設計
- 代謝性堿中毒護理課件
- 第四單元 舞蹈音樂天地-二拍子 四拍子舞曲 童聲合唱《閑聊波爾卡》 課件 2022-2023學年粵教版初中音樂九年級下冊
- 辦稅服務外包投標方案(完整版)
- 氫氧化鈉介紹msds
- 青甘大環線路線
- 通信基站維保投標方案(技術方案)
評論
0/150
提交評論