学校小学期Java课程的练习题,留个档便于以后需要时候有例子可以回忆,写的烂的地方请多多包含
1、求两个数值之和
- 描述:定义一个计算器类,从键盘输入两个整数,每个整数按回车确认,调用计算器类中的add()方法计算两个整数之和,并输出结果。
- 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import java.util.Scanner; class Computer{
int add(int x,int y){ int re; re=x+y; return re; } } public class Example4_6 { public static void main(String args[]){ Scanner reader = new Scanner(System.in); Computer com = new Computer(); System.out.println("输入两个整数:"); int m = reader.nextInt(); int n = reader.nextInt(); int result; result=com.add(m,n); System.out.println("两数之和:"+result); } }
|
2、模拟收音机电池使用情况
- 描述:定义一个电池类和收音机类,电池类有电量这个成员,收音机类有耗电这个方法,当播放收音机就会耗电,现在模拟收音机电池使用情况,有一个满格的电池,让收音机使用,使用10个小时就会用完电池,输入一个整数,要使用几个小时,输出电池使用后的电量。
- 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import java.util.Scanner; public class Example4_7 { public static void main(String args[]) { Scanner reader = new Scanner(System.in); System.out.println("输入使用几个小时:"); int h = reader.nextInt(); Battery nanfu = new Battery(100); System.out.println("南孚电池的储电量是:"+nanfu.electricityAmount); Radio radio = new Radio(); System.out.println("收音机开始使用南孚电池"); radio.openRadio(nanfu,h); System.out.println("目前南孚电池的储电量是:"+nanfu.electricityAmount); } } class Battery { int electricityAmount;
Battery(int amount){ electricityAmount=amount; } } class Radio { void openRadio(Battery battery,int h){ if(h>=0 && h<=10){ battery.electricityAmount = battery.electricityAmount-(h*10); } else{ System.out.println("时间输入格式有误"); } } }
|
3、求圆锥的体积
- 描述:定义一个圆类和圆锥类,圆锥的底是一个圆,所以圆锥类有一个成员是圆类声明的对象,圆锥通过调用方法将某个圆的引用传递给圆锥的圆类型的成员变量,设置圆锥的底的半径和高,求出圆锥的体积。
- 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| import java.util.Scanner; public class Test_1 { public static void main(String args[]) { Scanner reader = new Scanner(System.in); Circle circle = new Circle(); System.out.println("输入圆锥底的半径:"); circle.setRadius(reader.nextDouble()); Circular circular = new Circular(); System.out.println("输入圆锥的高:"); circular.setHeight(reader.nextDouble()); circular.setBottom(circle); System.out.println("圆锥的体积:"+circular.getVolme()); } } class Circle { double radius,area; void setRadius(double r) { radius=r; } double getRadius() { return radius; }
double getArea(){ area=3.14*radius*radius; return area; } } class Circular { Circle bottom; double height; void setBottom(Circle c) { bottom = c; } void setHeight(double h) { height = h; } double getVolme() { if(bottom == null) return -1; else{ return bottom.getArea()*height/3.0; } }
double getBottomRadius() { return bottom.getRadius(); }
public void setBottomRadius(double r){ bottom.setRadius(r); } }
|
4、模拟手机和SIM卡的组合关系
- 描述:定义一个手机类和SIM卡类,一个手机可以安装任何SIM卡,手机类中有SIM卡的成员变量,并可以调用方法更改其中的SIM卡,现在创建一个手机,给手机安装了一个SIM卡,要换一个其他SIM卡,输出原来和更换后的卡号。
- 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import java.util.Scanner; public class Example4_9 { public static void main(String args[]) { Scanner reader = new Scanner(System.in); System.out.println("输入手机卡号:"); SIM simOne = new SIM(reader.nextLong()); MobileTelephone mobile = new MobileTelephone(); mobile.setSIM(simOne); System.out.println("原来的手机号码:"+mobile.lookNumber()); System.out.println("更换手机卡的号码为:"); SIM simTwo = new SIM(reader.nextLong()); mobile.setSIM(simTwo); System.out.println("更换后的手机号码:"+mobile.lookNumber()); } }
class SIM { long number; SIM(long tmp){ number=tmp; } void setNumber(long tmp){ number=tmp; } long getNumber(){ return number; } }
class MobileTelephone { SIM sim; void setSIM(SIM tmp){ sim=tmp; } long lookNumber(){ return sim.getNumber(); } }
|
5、梯形共享一个下底的问题
- 描述:定义一个Lader类,Lader类创建的梯形对象共享同一个下底,设置共享下底值,使用Lader类创建两个梯形,并输出两个梯形的下底。
- 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import java.util.Scanner; public class Example4_10 { public static void main(String args[]) { Scanner reader = new Scanner(System.in); System.out.println("输入梯形的共享下底:"); Lader1.bottom=reader.nextDouble(); Lader1 laderOne=new Lader1(); Lader1 laderTwo=new Lader1(); System.out.println("laderOne的下底:"+laderOne.getBottom()); System.out.println("laderTwo的下底:"+laderTwo.getBottom()); } } class Lader1 { double above,height; static double bottom;
void setAbove(double a) { above=a; }
void setBottom(double b) { bottom=b; }
double getAbove(){ return above; }
double getBottom(){ return bottom; } }
|
6、二分法查询
- 描述:定义一个int型数组并初始化,将数组从小到大排序,然后使用二分法判断用户从键盘输入的整数是否和数组中某个元素的值相同,即是否在数组中,最后打印输出和哪个元素值相同,或者不与数组中任何元素值相同。
- 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import java.util.*; public class Example4_11 { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); int [] a={12,34,9,23,45,6,45,90,123,19,34}; Arrays.sort(a); System.out.println(Arrays.toString(a)); System.out.println("输入整数,程序判断该整数是否在数组中:"); int number = scanner.nextInt();
int index = Arrays.binarySearch(a,number); if(index>=0) System.out.println(number+"和数组中索引为"+index+"的元素值相同"); else System.out.println(number+"不与数组中任何元素值相同"); } }
|
7、类中方法重载的实现
- 描述:定义一个B类,有两个相同名称的方法hello,参数不同,一个方法是将两个整数相加,另一个是将两个小数相乘,根据提示从键盘输入四个数值,调用B类中的hello方法,并将结果打印。
- 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import java.util.Scanner;
class B { int hello(int a, int b){ return a+b; } double hello(double a, double b){ return a*b; } } public class Example4_12 { public static void main(String args[]) { Scanner reader = new Scanner(System.in); System.out.println("请输入两个整数:"); int b = reader.nextInt(); int a = reader.nextInt(); System.out.println("输入两个小数:"); double c = reader.nextDouble(); double d = reader.nextDouble(); B tom=new B(); System.out.println("两个整数调用hello方法的结果:"); System.out.println(tom.hello(a,b)); System.out.println("两个小数调用hello方法的结果:"); System.out.println(tom.hello(c,d)); } }
|
8、求圆和梯形的面积
- 描述:定义学生类、梯形类、圆类,在梯形类和圆类中都有一个计算面积的方法,学生类中有两个获得圆面积和梯形面积的computerArea方法,测试创建一个学生zhang,设置圆的半径和梯形的上底、下底、高,调用zhang的computerArea方法,得到圆和梯形的面积,并打印输出。
- 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| import java.util.Scanner; public class Example4_13 { public static void main(String args[]) { Scanner reader = new Scanner(System.in); System.out.println("请输入圆的半径:"); Circle1 circle = new Circle1(); circle.setRadius(reader.nextDouble()); System.out.println("请输入梯形的上底、下底、高:"); Tixing lader = new Tixing(reader.nextDouble(),reader.nextDouble(),reader.nextDouble()); Student zhang = new Student(); System.out.println("zhang计算圆的面积:"); double result=zhang.computerArea(circle); System.out.println(result); System.out.println("zhang计算梯形的面积:"); result=zhang.computerArea(lader); System.out.println(result); } } class Circle1 { double radius,area;
void setRadius(double r) { radius=r; }
double getArea(){ area=3.14*radius*radius; return area; } } class Tixing { double above,bottom,height;
Tixing(double a,double b,double h) { above=a; bottom=b; height=h; }
double getArea() { return 0.5*(above+bottom)*height; } } class Student { double computerArea(Circle1 c) { double area=c.getArea(); return area; } double computerArea(Tixing t) { double area=t.getArea(); return area; } }
|
9、根据输入的名字初始化对象
- 描述:定义People1类,包含int型成员变量 leg、hand,String类型变量name,以及有参构造函数,和初始化的方法。获取键盘输入的名字,作为构造函数的参数,初始化对象,并打印对象属性值。
- 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import java.util.Scanner; public class People1{ int leg,hand; String name; People1(String s){ name=s; this.init(); } void init(){ leg=2; hand=2; System.out.println(name+"有"+hand+"只手"+leg+"条腿"); } public static void main(String args[]){ Scanner reader = new Scanner(System.in); System.out.println("请输入名字:"); People1 p=new People1(reader.next()); } }
|
10、大小写转换
- 描述:从键盘输入含有大小写字母的字符串,按回车结束,然后将字符串中的小写字母转换成大写,大写字母转换成小写,并将转换后的字符串输出。
- 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import java.util.Scanner; public class Example4_20 { public static void main(String args[ ]) { Scanner reader = new Scanner(System.in); System.out.println("请输入一串英文字母(回车确认):"); String str = reader.nextLine(); char a[]=new char[str.length()]; for(int i=0;i<str.length();i++) { if(Character.isLowerCase(str.charAt(i))) a[i]=Character.toUpperCase(str.charAt(i)); else if(Character.isUpperCase(str.charAt(i))) a[i]=Character.toLowerCase(str.charAt(i)); else System.out.println("第"+(i+1)+"个字母"+str.charAt(i)+"输入有误"); } System.out.println("大小写转换后的字母:"); for(int i=0;i<a.length;i++)
System.out.print(" "+a[i]); } }
|
11、求矩形和梯形的面积
- 描述:定义一个矩形类和梯形类,分别求矩形面积和梯形的面积,并输出结果。
- 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| import java.util.Scanner; public class Example4_5 { public static void main(String args[]) { Scanner reader = new Scanner(System.in); Rect ractangle=new Rect(); System.out.println("输入矩形的宽:"); ractangle.width = reader.nextDouble(); System.out.println("输入矩形的高:"); ractangle.height = reader.nextDouble(); double area=ractangle.getArea(); System.out.println("矩形的面积:"+area); Lader lader=new Lader(); System.out.println("输入梯形的上底:"); lader.above= reader.nextDouble(); System.out.println("输入梯形的下底:"); lader.bottom= reader.nextDouble(); System.out.println("输入梯形的高:"); lader.height= reader.nextDouble(); area=lader.getArea(); System.out.println("梯形的面积:"+area); } } class Rect { double width; double height;
double getArea() { return width*height; } } class Lader { double above; double bottom; double height;
double getArea() { return 0.5*(above+bottom)*height; } }
|
12、分数运算
- 描述:定义一个Rational类,编写方法对两个分数做加法减法乘法除法,返回结果为分数,从键盘输入两个数的分子和分母,对这两个数求加减乘除,并输出结果。
- 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| import java.util.Scanner; public class Example4_23 { public static void main(String args[]) { Scanner reader = new Scanner(System.in); Rational r1=new Rational(); r1.setNumerator(reader.nextInt()); r1.setDenominator(reader.nextInt()); Rational r2=new Rational(); r2.setNumerator(reader.nextInt()); r2.setDenominator(reader.nextInt()); Rational result=r1.add(r2); int a=result.getNumerator(); int b=result.getDenominator(); System.out.println("求和: "+a+"/"+b); result=r1.sub(r2); a=result.getNumerator(); b=result.getDenominator(); System.out.println("求差: "+a+"/"+b); result=r1.muti(r2); a=result.getNumerator(); b=result.getDenominator(); System.out.println("求积:"+a+"/"+b); result=r1.div(r2); a=result.getNumerator(); b=result.getDenominator(); System.out.println("求商: "+a+"/"+b); } } class Rational { int numerator = 1 ; int denominator = 1; void setNumerator(int a) { int c=f(Math.abs(a),denominator); numerator = a/c; denominator = denominator/c; if(numerator<0&&denominator<0) { numerator = -numerator; denominator = -denominator; } } void setDenominator(int b) { int c=f(numerator,Math.abs(b)); numerator = numerator/c; denominator = b/c; if(numerator<0&&denominator<0) { numerator = -numerator; denominator = -denominator; } } int getNumerator() { return numerator; } int getDenominator() { return denominator; } int f(int a,int b) { if(a==0) return 1; if(a<b) { int c=a; a=b; b=c; } int r=a%b; while(r!=0) { a=b; b=r; r=a%b; } return b; } Rational add(Rational r) { int a=r.getNumerator(); int b=r.getDenominator(); int newNumerator=numerator*b+denominator*a; int newDenominator=denominator*b; Rational result=new Rational(); result.setNumerator(newNumerator); result.setDenominator(newDenominator); return result; } Rational sub(Rational r) { int a=r.getNumerator(); int b=r.getDenominator(); int newNumerator=numerator*b-denominator*a; int newDenominator=denominator*b; Rational result=new Rational(); result.setNumerator(newNumerator); result.setDenominator(newDenominator); return result; } Rational muti(Rational r) { int a=r.getNumerator(); int b=r.getDenominator(); int newNumerator=numerator*a; int newDenominator=denominator*b; Rational result=new Rational(); result.setNumerator(newNumerator); result.setDenominator(newDenominator); return result; } Rational div(Rational r) { int a=r.getNumerator(); int b=r.getDenominator(); int newNumerator=numerator*b; int newDenominator=denominator*a; Rational result=new Rational(); result.setNumerator(newNumerator); result.setDenominator(newDenominator); return result; } }
|
13、求前n项之和
- 描述:从键盘输入一个为n的项数,按回车结束,计算2/1+3/2+4/3+…+(n+1)/n之和,结果用分数表示和小数,并输出。
- 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| import java.util.Scanner; public class Example4_24 { public static void main(String args[]) { Scanner reader = new Scanner(System.in); int n=reader.nextInt(),k=1,a=0,b=0; System.out.println("前"+n+"项求和:"); Rational sum=new Rational(); sum.setNumerator(0); Rational item=new Rational(); item.setNumerator(2); item.setDenominator(1); sum=sum.add(item); a=2; b=1; while(k<n) { item.setNumerator(a+b); item.setDenominator(a); sum=sum.add(item); int temp=a; a=a+b; b=temp; k++; } a=sum.getNumerator(); b=sum.getDenominator(); System.out.println("用分数表示:"); System.out.println(a+"/"+b); double doubleResult=(a*1.0)/b; System.out.println("用小数表示:"); System.out.print(doubleResult); } } class Rational { int numerator = 1 ; int denominator = 1; void setNumerator(int a) { int c=f(Math.abs(a),denominator); numerator = a/c; denominator = denominator/c; if(numerator<0&&denominator<0) { numerator = -numerator; denominator = -denominator; } } void setDenominator(int b) { int c=f(numerator,Math.abs(b)); numerator = numerator/c; denominator = b/c; if(numerator<0&&denominator<0) { numerator = -numerator; denominator = -denominator; } } int getNumerator() { return numerator; } int getDenominator() { return denominator; } int f(int a,int b) { if(a==0) return 1; if(a<b) { int c=a; a=b; b=c; } int r=a%b; while(r!=0) { a=b; b=r; r=a%b; } return b; } Rational add(Rational r) { int a=r.getNumerator(); int b=r.getDenominator(); int newNumerator=numerator*b+denominator*a; int newDenominator=denominator*b; Rational result=new Rational(); result.setNumerator(newNumerator); result.setDenominator(newDenominator); return result; } Rational sub(Rational r) { int a=r.getNumerator(); int b=r.getDenominator(); int newNumerator=numerator*b-denominator*a; int newDenominator=denominator*b; Rational result=new Rational(); result.setNumerator(newNumerator); result.setDenominator(newDenominator); return result; } Rational muti(Rational r) { int a=r.getNumerator(); int b=r.getDenominator(); int newNumerator=numerator*a; int newDenominator=denominator*b; Rational result=new Rational(); result.setNumerator(newNumerator); result.setDenominator(newDenominator); return result; } Rational div(Rational r) { int a=r.getNumerator(); int b=r.getDenominator(); int newNumerator=numerator*b; int newDenominator=denominator*a; Rational result=new Rational(); result.setNumerator(newNumerator); result.setDenominator(newDenominator); return result; } }
|
14、求分数评定的最后得分
就是不能通过,很申必,我觉得代码没问题就贴上来了
- 描述:用流水线完成分数评定,其中InputScore类的对象负责录入分数,InputScore类组合了DelScore类的对象;DelScore类的对象负责去掉一个最高分和一个最低分,DelScore类组合了ComputerAver类对象;ComputerAver类对象负责计算平均值;Line类组合了InputScore、DelScore和ComputerAver三个类的实例,从键盘输入评委数和每位评委的评分,根据规则计算出平均分,并输出。
- 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| import java.util.Scanner; public class SingGame { public static void main(String args[]){ Line line=new Line(); line.givePersonScore(); } }
class InputScore { DelScore del ; InputScore(DelScore del) { this.del=del; } public void inputScore() { System.out.println("请输入评委数"); Scanner read=new Scanner(System.in); int count = read.nextInt(); System.out.println("请输入各个评委的分数"); double []a = new double[count]; for(int i=0;i<count;i++) { a[i]=read.nextDouble(); } del.doDelete(a); } } class Line { InputScore one; DelScore two; ComputerAver three; Line(){ three=new ComputerAver(); two=new DelScore(three); one= new InputScore(two); } public void givePersonScore(){ one.inputScore(); } } class ComputerAver { public void giveAver(double [] b) { double sum=0; for(int i =0;i<b.length;i++) { sum = sum+ b[i]; } double aver=sum/b.length; System.out.print("选手最后得分"+aver); } } class DelScore { ComputerAver computer ; DelScore(ComputerAver computer) { this.computer = computer; } public void doDelete(double [] a) { java.util.Arrays.sort(a); System.out.print("去掉一个最高分:"+a[a.length-1]+","); System.out.print("去掉一个最低分:"+a[0]+"。"); double b[] =new double[a.length-2]; for(int i=0;i<a.length-2;i++) { b[i] = a[i+1]; } computer.giveAver(b); } }
|