java作业大综合_第1页
java作业大综合_第2页
java作业大综合_第3页
java作业大综合_第4页
java作业大综合_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论