学校小学期Java课程的练习题,留个档便于以后需要时候有例子可以回忆,写的烂的地方请多多包含
1、计算输入学号、数字的和
- 描述:定义一个接口Computable,包含静态变量MAX=46和方法f()。定义类China、Japan实现接口。类China中实现的f方法为 求1到x的数字之和,类Japan中实现的f方法为 求MAX和x的和。输入实例zhang、henlu的学号和数字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
| import java.util.Scanner; interface Computable { int MAX = 46; int f(int x); } class China implements Computable { int number;
public int f(int x) { int sum=0; for(int i=1;i<=x;i++){ sum=sum+i; } return sum; } } class Japan implements Computable { int number;
public int f(int x) { return MAX+x; } } public class Test{ public static void main(String args[]) { China zhang; Japan henlu; zhang=new China(); henlu=new Japan(); System.out.println("输入zhang的学号、数字n(计算1..n的和):"); System.out.println("输入henlu的学号、数字n(计算n+MAX的和):"); Scanner scanner = new Scanner(System.in); int a,b; a=scanner.nextInt(); b=scanner.nextInt(); int c,d; d = scanner.nextInt(); c = scanner.nextInt(); zhang.number=a+Computable.MAX; henlu.number=c+Computable.MAX; System.out.println("----------输出-----------");
System.out.println("zhang的学号"+zhang.number+",zhang求和结果"+zhang.f(b)); System.out.println("henlu的学号"+henlu.number+",henlu求和结果"+henlu.f(d)); } }
|
2、对输入的字符串进行打印输出
- 描述:定义一个接口ShowMessage,包含有show方法。利用接口回调技术,两个不同的类(TV/PC)实现同一个接口的方法,显示商品的品牌。
- 代码
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
| import java.util.Scanner; interface ShowMessage { void show(String s); } class TV implements ShowMessage { public void show(String s) { System.out.println(s); } } class PC implements ShowMessage { public void show(String s) { System.out.println(s); } } public class test{ public static void main(String args[]) { ShowMessage sm; sm=new TV(); Scanner scanner = new Scanner(System.in); System.out.println("请输入TV的商标");
sm.show(scanner.next()); sm=new PC(); System.out.println("请输入PC的商标");
sm.show(scanner.next()); } }
|
3、对输入的字符串进行拼接打印输出
- 描述:定义一个MotorVehicles抽象类,包含抽象方法brake。定义接口MoneyFare,包含方法charge,接口ControlTemperature包含方法ControlTemperature。定义Bus类继承抽象类MotorVehicles并且实现接口MoneyFare,Taxi类继承抽象类MotorVehicles并且实现接口MoneyFare和ControlTemperature。Cinema类实现接口MoneyFare和ControlTemperature。主函数中根据不同的对象,打印输出自身的属性值。
- 代码
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
| import java.util.Scanner; abstract class MotorVehicles { abstract void brake(String s); } interface MoneyFare { void charge(String c); } interface ControlTemperature { void controlAirTemperature(String t); } class Bus extends MotorVehicles implements MoneyFare { void brake(String s) { System.out.println("公共汽车使用"+s+"刹车技术"); } public void charge(String c) { System.out.println("公共汽车:"+c+"元/张,不计算公里数"); } } class Taxi extends MotorVehicles implements MoneyFare,ControlTemperature { void brake(String s) { System.out.println("出租车使用"+s+"刹车技术"); } public void charge(String c) { System.out.println("出租车:"+c+"元/公里,起价3公里"); } public void controlAirTemperature(String t) { System.out.println("出租车安装了"+t+"空调"); } } class Cinema implements MoneyFare,ControlTemperature { public void charge(String s) { System.out.println("电影院:门票,"+s+"元/张"); } public void controlAirTemperature(String t) { System.out.println("电影院安装了"+t+"空调"); } } public class Example6_3 { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); Bus bus101 = new Bus(); Taxi buleTaxi = new Taxi(); Cinema redStarCinema = new Cinema(); MoneyFare fare; ControlTemperature temperature; fare = bus101; System.out.println("请输入公交车采用刹车技术和车票价(元):");
bus101.brake(scanner.next()); fare.charge(scanner.next()); fare = buleTaxi; temperature = buleTaxi; System.out.println("请输入出租车采用刹车技术、车票价(元)、安装空调样式:");
buleTaxi.brake(scanner.next()); fare.charge(scanner.next()); temperature.controlAirTemperature(scanner.next()); fare = redStarCinema; temperature = redStarCinema; System.out.println("请输入电影院票价(元)、安装空调样式:");
fare.charge(scanner.next()); temperature.controlAirTemperature(scanner.next()); } }
|
4、计算俩个数的算数平均值和几何平均值
- 描述:定义接口CompurerAverage包含有average方法。定义类A实现接口,实现average方法的功能为计算算术平均数,类B实现接口,实现average方法的功能为计算几何平均数。获取输入的两个数,调用不同的方法,求平均数并打印输出。
- 代码
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
| import java.util.Scanner; interface CompurerAverage { public double average(double a,double b); } class A implements CompurerAverage { public double average(double a,double b) { double aver=(a+b)/2; return aver; } } class B implements CompurerAverage { public double average(double a,double b) { double aver=Math.sqrt(a*b); return aver; } } public class Example6_4 { public static void main(String args[]) { CompurerAverage computer; double a,b,result; computer = new A(); System.out.println("请输入俩个数:"); Scanner scanner = new Scanner(System.in);
a=scanner.nextDouble(); b=scanner.nextDouble(); result=computer.average(a,b); System.out.printf("%5.2f和%5.2f的算术平均值:%5.2f\n",a,b,result); computer = new B(); result=computer.average(a,b); System.out.printf("%5.2f和%5.2f的几何平均值:%5.2f",a,b,result); } }
|
5、根据提示语句,输入字符串,打印输出
- 描述:定义接口SpeakHello包含方法speakHello,类Chinese和English实现接口。定义类KindHello包含有方法lookHello,方法的参数为SpeakHello接口,方法的实现为调用接口的speakHello方法。主函数中调用KindHello类的lookHello方法,参数分别为Chinese和English对象以及Scanner,最后打印输出结果。
- 代码
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
| import java.util.Scanner; interface SpeakHello { void speakHello(Scanner scanner); } class Chinese implements SpeakHello { public void speakHello(Scanner scanner) { System.out.println("中国人习惯问候语:"); System.out.println("中国人习惯问候语:"+scanner.nextLine()); } } class English implements SpeakHello { public void speakHello(Scanner scanner) { System.out.println("英国人习惯问候语:"); System.out.println("英国人习惯问候语:"+scanner.nextLine()); } } class KindHello { public void lookHello(SpeakHello hello,Scanner scanner) { hello.speakHello(scanner); } } public class Example6_5 { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); KindHello kindHello=new KindHello(); Chinese ch=new Chinese(); English en=new English(); kindHello.lookHello(ch,scanner); kindHello.lookHello(en,scanner); } }
|
6、实现多态
- 描述:编写基类Shape,派生类Circle、Square、Triangle都继承基类Shape,并覆写基类的方法。从键盘输入一个0-2的整数,输入三次分别按回车结束,创建Circle、Square、Triangle对象,调用方法draw,打印内容。
- 代码
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
| import java.util.Scanner;
class Shape { void draw() { } void erase() { } }
class Circle extends Shape { @Override void draw() { System.out.println("Circle.draw()"); } @Override void erase() { System.out.println("Circle.erase()"); } } class Square extends Shape { @Override void draw() { System.out.println("Square.draw()"); } @Override void erase() { System.out.println("Square.erase()"); } } class Triangle extends Shape { @Override void draw() { System.out.println("Triangle.draw()"); } @Override void erase() { System.out.println("Triangle.erase()"); } }
public class Example7_100 { public static void main(String[] args) { Scanner reader = new Scanner(System.in); Shape[] shape = new Shape[3]; int n; for (int i = 0; i < shape.length; i++) { n = reader.nextInt(); switch (n) { case 0: shape[i] = new Circle(); break; case 1: shape[i] = new Square(); break; case 2: shape[i] = new Triangle(); } } for (Shape oneS : shape) { oneS.draw(); } } }
|
7、抽象类的运用
- 描述:定义抽象类Driver,包含抽象方法drives()。定义类FemaleDriver,MaleDriver继承Driver,并重写方法drives(),从键盘输入一个数,打印输出两个对象调用drives()方法的内容。
- 代码
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
| import java.util.Scanner; public class Example7_101 { static public void main(String[] args) { Scanner reader = new Scanner(System.in); Driver a = new FemaleDriver(reader.nextInt()); Driver b = new MaleDriver(); a.drives(); b.drives(); } } class FemaleDriver extends Driver { public FemaleDriver(int a) { } @Override public void drives() { System.out.println("A Female driver drives a vehicle."); } } class MaleDriver extends Driver { public MaleDriver() { } @Override public void drives() { System.out.println("A male driver drives a vehicle."); } } abstract class Driver { public Driver() { } public abstract void drives(); }
|