题目16
/* 定义PayrollSystem类,创建Employee变量数组并初始化,该数组存放各 类雇员对象的引用。利用循环结构遍历数组元素,输出各个对象的类 型,name,number,birthday,以及该对象生日。当键盘输入本月月份值时,如果本 月是某个Employee对象的生日,还要输出增加工资信息。 */
package com.jerry.exer; import com.sun.org.apache.bcel.internal.generic.NEW; import java.util.Date; /** * @author jerry_jy * @create 2022-09-30 15:44 */ public class Exer7 { public static void main(String[] args) { Employee3[] emps = new Employee3[2]; emps[0] = new PayrollSystem("jerry", 1001, new MyDate2(2022, 1, 1)); emps[0].earnings(); emps[1] = new PayrollSystem("tom", 1002, new MyDate2(2022, 9, 30)); for (int i = 0; i < emps.length; i++) { emps[i].toString(); } if (new Date().getMonth()+1==emps[0].getMonth()){ System.out.println("本月是你的生日,工资涨:"+emps[1].getSalary()+100); } } } class MyDate2 { private int year, month, day; public MyDate2() { } public MyDate2(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public String toDateString() { String str = this.getYear() + "年" + this.getMonth() + "月" + this.getDay() + "日"; return str; } } class Employee3 extends MyDate2 { private String name; private int number; private MyDate2 birthday; private double salary; public Employee3() { } public Employee3(String name, int number, MyDate2 birthday) { salary=2000; this.name = name; this.number = number; this.birthday = birthday; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } void earnings() { System.out.println("工资:"+this.getSalary()); } @Override public String toString() { return "Employee1{" + "name='" + name + '\'' + ", number=" + number + ", birthday=" + birthday.toDateString() + '}'; } } class PayrollSystem extends Employee3 { public PayrollSystem() { } public PayrollSystem(String name, int number, MyDate2 birthday) { super(name, number, birthday); } }
题目17
/* 定义一个接口用来实现两个对象的比较。 interface CompareObject{ public int compareTo(Object o); //若返回值是 0 , 代表相等; 若为正数,代表当 前对象大;负数代表当前对象小 } 定义一个Circle类,声明redius属性,提供getter和setter方法 定义一个ComparableCircle类,继承Circle类并且实现CompareObject接口。在 ComparableCircle类中给出接口中方法compareTo的实现体,用来比较两个圆的半 径大小。 定义一个测试类InterfaceTest,创建两个ComparableCircle对象,调用compareTo 方法比较两个类的半径大小。 */
package com.jerry.exer; /** * @author jerry_jy * @create 2022-09-30 16:46 */ public class Exer8 { public static void main(String[] args) { ComparableCircle c1 = new ComparableCircle(1.0); ComparableCircle c2 = new ComparableCircle(2.0); System.out.println(c1.compareTo(c2)); } } interface CompareObject { public int compareTo(Object o); } class Circle { private double radius; public Circle() { } public Circle(double radius) { this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } } class ComparableCircle extends Circle implements CompareObject { public ComparableCircle(double radius) { super(radius); } @Override public int compareTo(Object o) { if (o instanceof ComparableCircle) { ComparableCircle c = (ComparableCircle) o; if (this.getRadius() > c.getRadius()) { return 1; } else if (this.getRadius() < c.getRadius()) { return -1; } else { return 0; } } return 0; } }
题目18
/* 参 照 上 述 做 法 定 义 矩 形 类 Rectangle 和 ComparableRectangle 类 , 在 ComparableRectangle类中给出compareTo方法的实现,比较两个矩形的面积大小。 */
package com.jerry.exer; /** * @author jerry_jy * @create 2022-09-30 17:40 */ public class Exer9 { public static void main(String[] args) { ComparableRectangle c1 = new ComparableRectangle(1, 2); ComparableRectangle c2 = new ComparableRectangle(2, 3); System.out.println(c1.compareTo(c2)); } } interface CompareObject1 { public int compareTo(Object o); } class Rectangle { double length, width; public Rectangle() { } public Rectangle(double length, double width) { this.length = length; this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getArea() { return length * width; } } class ComparableRectangle extends Rectangle implements CompareObject1 { public ComparableRectangle(double length, double width) { super(length, width); } @Override public int compareTo(Object o) { if (o instanceof ComparableRectangle) { ComparableRectangle c = (ComparableRectangle) o; if (this.getArea() == c.getArea()) { return 0; } else if (this.getArea() < c.getArea()) { return -1; } else { return 1; } } return 0; } }
题目19
/* 编一个程序,包含以下文件 (1)Shape.java文件,在该文件中定义接口类Shape,该接口在shape包中。 属性:PI。 接口:求面积的方法area()。 (2)Circle.java文件,在该文件中定义圆类Circle,该类在circle包中,实现Shape接口类。 属性:圆半径radius。 方法:构造器;实现求面积方法area();求周长方法perimeter()。 (3)Cylinder.java文件,在该文件中定义圆柱体类Cylinder,该类在cylinder包中,继承圆类。 属性:圆柱体高度height。 方法:构造器;求表面积方法area();求体积方法volume()。 (4)X5_3_6.java文件,在该文件中定义主类X5_3_6,该类在默认包中,其中包含主方法main(),在主方法中创建两个圆类对象cir1和cir2,具体尺寸自己确定,并显示圆的面积和周长;再创建两个圆柱体类的对象cy1和cy2,具体尺寸自己确定,然后分别显示圆柱体cy1和cy2的底圆的面积和周长以及它们各自的体积和表面积。 【编程分析】本题主要考察接口、包、继承、封装等问题。 */
package com.jerry.exer1; /** * @author jerry_jy * @create 2022-09-30 18:28 */ public class Exer2 { public static void main(String[] args) { Circle c1 = new Circle(2); System.out.println("周长:" + c1.perimeter()); System.out.println("面积:" + c1.area()); Circle c2 = new Circle(3); System.out.println("周长:" + c2.perimeter()); System.out.println("面积:" + c2.area()); System.out.println("=========================="); Cylinder cyl1 = new Cylinder(4, 5); System.out.println("表面积:" + cyl1.area()); System.out.println("体积:" + cyl1.volume()); System.out.println("底面圆的周长:" + cyl1.perimeter()); System.out.println("底面圆的面积:" + cyl1.findArea()); System.out.println("============================"); Cylinder cyl2 = new Cylinder(6, 7); System.out.println("表面积:" + cyl2.area()); System.out.println("体积:" + cyl2.volume()); System.out.println("底面圆的周长:" + cyl2.perimeter()); System.out.println("底面圆的面积:" + cyl2.findArea()); } } interface Shape { final double PI = 3.14; double area(); } class Circle implements Shape { double radius; public Circle() { } public Circle(double radius) { this.radius = radius; } @Override public double area() { return PI * radius * radius; } public double perimeter() { return 2 * PI * radius; } } class Cylinder extends Circle { double height; public Cylinder() { } public Cylinder(double radius, double height) { super(radius); this.height = height; } public double findArea() { return super.area(); } public double area() {//求表面积方法 return super.area() * 2 + super.perimeter() * height; } public double volume() { return super.area() * height; } }
题目20
/* 鸭嘴兽属于脊椎动物中的哺乳动物,太阳花属于植物中的种子植物,而鸭嘴兽和太阳花都属于生物。完成如下要求(共50分,每小题10分): 1)设计一系列的接口来表示这些规范:生物动物脊椎动物哺乳动物;生物植物种子植物,并且按照箭头要求来继承; 2)生物都有呼吸方式,只有一个方法声明:void respirations();设计两个类,分别是鸭嘴兽和太阳花,分别实现生物接口的respirations方法,执行方法可以分别输出:“鸭嘴兽是动物,需要呼吸氧气”,“太阳花是植物,可以吸收二氧化碳,呼出氧气”; 3)动物接口拥有方法声明:void run(),鸭嘴兽类可以实现方法,输出信息:“鸭嘴兽只有两只脚,它可以用两只脚奔跑” 4)植物接口都有繁殖方法声明 void reproduction(),太阳花在实现reproduction方法时输出信息“太阳花可以用种子繁殖,也可以用枝条繁殖” 5)声明一个Test类作为测试,执行上述声明的所有方法; */
package com.jerry.exer1; /** * @author jerry_jy * @create 2022-09-30 18:50 */ public class Exer3 { public static void main(String[] args) { DuckMole duckMole = new DuckMole(); duckMole.respirations(); duckMole.run(); SunFlower sunFlower = new SunFlower(); sunFlower.reproduction(); sunFlower.respirations(); } } interface Creature { void respirations(); } interface Animal extends Creature { void run(); } interface Vertebrate extends Animal { } interface Mammal extends Vertebrate { } interface Plant extends Creature { void reproduction(); } interface SeedPlant extends Plant { } class DuckMole implements Creature, Animal { @Override public void respirations() { System.out.println("鸭嘴兽是动物,需要呼吸氧气"); } @Override public void run() { System.out.println("鸭嘴兽只有两只脚,它可以用两只脚奔跑"); } } class SunFlower implements Creature, Plant { @Override public void respirations() { System.out.println("太阳花是植物,可以吸收二氧化碳,呼出氧气"); } @Override public void reproduction() { System.out.println("太阳花可以用种子繁殖,也可以用枝条繁殖"); } }
题目21
/* 请使用接口编码实现如下需求:乐器(Instrument)分为:钢琴(Piano)、小提琴(Violin).各种乐器的弹奏( play )方法各不相同。编写一个测试类InstrumentTest,要求:编写方法testPlay,对各种乐器进行弹奏测试。要依据乐器的不同,进行相应的弹奏。在main方法中创建不同的乐器对象,通过testPlay的弹奏测试方法进行测试。 */
package com.jerry.exer1; /** * @author jerry_jy * @create 2022-09-30 19:01 */ public class Exer4 { public static void main(String[] args) { Piano piano = new Piano(); piano.play(); Violin violin = new Violin(); violin.play(); } } interface Instrument { void play(); } class Piano implements Instrument { @Override public void play() { System.out.println("弹钢琴......"); } } class Violin implements Instrument { @Override public void play() { System.out.println("拉小提琴......"); } }
题目22
/* 按要求实现下列问题: 1) 动物类Animal包含了抽象方法 abstract void shout(); 2) Cat类继承了Animal,并实现方法shout,打印“猫会喵喵叫” 3) Dog类继承了Animal,并实现方法shout,打印“狗会汪汪叫” 4) 在测试类中实例化对象Animal a1 =new Cat(),并调用a1的shout方法 5) 在测试类中实例化对象Animal a2 =new Dog(),并调用a2的shout方法 */
package com.jerry.exer1; /** * @author jerry_jy * @create 2022-09-30 19:05 */ public class Exer5 { public static void main(String[] args) { Animal1 a1 = new Cat(); a1.shout(); Animal1 a2 = new Dog(); a2.shout(); } } abstract class Animal1 { abstract void shout(); } class Cat extends Animal1 { @Override void shout() { System.out.println("猫会喵喵叫......"); } } class Dog extends Animal1 { @Override void shout() { System.out.println("狗会汪汪叫......"); } }
题目23
/* 有一个Car类,有属性temperature(温度),车内有Air(空调),有吹风的功能flow,Air会监视车内的温度,如果温度超过40度则吹冷气。如果温度低于0度则吹暖气,如果在这之间则关掉空调。实例化具有不同温度的Car对象,调用空调的flow方法,测试空调吹的风是否正确 */
package com.jerry.exer1; import com.sun.org.apache.bcel.internal.generic.NEW; /** * @author jerry_jy * @create 2022-09-30 19:32 */ public class Exer6 { public static void main(String[] args) { Car.Air air = new Car(42).new Air(); air.flow(); System.out.println("================"); Car.Air air1 = new Car(-2).new Air(); air1.flow(); System.out.println("================"); Car.Air air2 = new Car(16).new Air(); air2.flow(); } } class Car { static double temperature; public Car() { } public Car(double temperature) { Car.temperature = temperature; } class Air { void flow() { if (Car.temperature > 40) { System.out.println("吹冷风......"); } else if (Car.temperature < 0) { System.out.println("吹暖风......"); } else { System.out.println("关空调......"); } } } }
题目24
/* 编一个类A,在类中定义局部类B,B中有一个常量name,有一个方法show()打印常量name。进行测试 */
package com.jerry.exer1; /** * @author jerry_jy * @create 2022-09-30 19:49 */ public class Exer7 { public static void main(String[] args) { A.B jerry = new A().new B("jerry"); jerry.show(); } } class A { class B { String name; public B(String name) { this.name = name; } void show() { System.out.println("name:" + name); } } }
题目25
/* 设计公司类(Lenovo),有一个接口类Works,里面有接口work(),下面有2个部门(开发部和销售部),使用内部类分别实现开发部和销售部的工作。 */
package com.jerry.exer1; /** * @author jerry_jy * @create 2022-09-30 19:53 */ public class Exer8 { public static void main(String[] args) { Dev dev = new Dev(); dev.work(); Market market = new Market(); market.work(); } } interface Works { interface work { } } class Dev implements Works.work { void work() { System.out.println("开发部工作......"); } } class Market implements Works.work { void work() { System.out.println("市场部工作......"); } }
题目26
/* 定义一个抽象类Person,有name,age,sex三个属性,创建构造函数,给这三个属性赋值,重写打印描述信息方法,打印三个属性值;这个类中有两个抽象方法work和hello;定义两个子类Teacher,Student;教师的work实现是:教书育人,hello实现是:“同学好”;学生的work实现是:认真学习,hello实现是“老师好”。 */
package com.jerry.exer1; /** * @author jerry_jy * @create 2022-09-30 20:10 */ public class Exer11 { public static void main(String[] args) { Teacher teacher = new Teacher("jerry", 20, 'M'); System.out.println(teacher.toString()); teacher.hello(); teacher.work(); System.out.println("=================="); Student student = new Student("tom", 22, 'F'); System.out.println(student.toString()); student.work(); student.hello(); } } abstract class Person { String name; int age; char gender; public Person() { } public Person(String name, int age, char gender) { this.name = name; this.age = age; this.gender = gender; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", gender=" + gender + '}'; } abstract void work(); abstract void hello(); } class Teacher extends Person { public Teacher() { } public Teacher(String name, int age, char gender) { super(name, age, gender); } @Override void work() { System.out.println("教书育人"); } @Override void hello() { System.out.println("同学好"); } } class Student extends Person { public Student() { } public Student(String name, int age, char gender) { super(name, age, gender); } @Override void work() { System.out.println("认真学习"); } @Override void hello() { System.out.println("老师好"); } }
题目27
/* 定义Shape抽象类,包含私有属性color,创建构造器为color赋值;包含计算周长的方法celPerimeter();定义子类Triangle,包含三边;定义子类Circle,包含半径radius;子类分别实现父类的计算周长功能。 */
package com.jerry.exer1; /** * @author jerry_jy * @create 2022-09-30 20:23 */ public class Exer12 { public static void main(String[] args) { Triangle triangle = new Triangle("三角形", 3, 4, 5); triangle.celPerimeter(); System.out.println(triangle.toString()); Circle1 circle1 = new Circle1("三角形", 5.0); circle1.celPerimeter(); System.out.println(circle1.toString()); } } abstract class Shape1 { private String color; public Shape1() { } public Shape1(String color) { this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Shape1{" + "color='" + color + '\'' + '}'; } abstract void celPerimeter(); } class Triangle extends Shape1 { double a, b, c; public Triangle(String color, double a, double b, double c) { super(color); this.a = a; this.b = b; this.c = c; } @Override public String toString() { return "Triangle{" + "a=" + a + ", b=" + b + ", c=" + c + ", color=" + super.toString() + '}'; } @Override void celPerimeter() { System.out.println("三角形的周长:" + (a + b + c)); } } class Circle1 extends Shape1 { double radius; public Circle1(String color, double radius) { super(color); this.radius = radius; } @Override public String toString() { return "Circle1{" + "radius=" + radius + ", color=" + super.toString() + '}'; } @Override void celPerimeter() { System.out.println("圆的周长:" + Math.PI * radius * radius); } }
题目28
/* 有一个交通工具接口类Vehicles,有work接口,有Horse类和Boat类分别实现Vehicles,创建交通工具工厂类,有两个方法分别获得交通工具Horse和Boat;有Person类,有name和Vehicles属性,在构造器中赋值,实例化“唐僧”,一般情况下用Horse作为交通工具,遇到大河时用Boat作为交通工具。 */
package com.jerry.exer1; /** * @author jerry_jy * @create 2022-10-01 8:49 */ public class Exer13 { public static void main(String[] args) { Vehicles vehicles1 = VehiclesTransportation.getTransportation("平时"); vehicles1.work(); Vehicles vehicles2 = VehiclesTransportation.getTransportation("河"); vehicles2.work(); } } interface Vehicles { void work(); } class Horse implements Vehicles { @Override public void work() { System.out.println("马跑。。。"); } } class Boat implements Vehicles { @Override public void work() { System.out.println("划船。。。"); } } class VehiclesTransportation { String name; public VehiclesTransportation() { } public VehiclesTransportation(String name) { this.name = "唐僧"; } public static Vehicles getTransportation(String type) { if ("平时".equals(type)) { return new Horse(); } else if ("河".equals(type)) { return new Boat(); } else { return null; } } }
题目29
/* 有一个农场公司,专门向市场销售各类水果,主要包括下列水果:葡萄 Grape,草莓 Stuawberry,苹果 Apple;有一个Fruit接口,包含grow生长,harvest收获和plant方法,葡萄、草莓和苹果都实现了这个接口;有一个农场园丁类FruitGardener,具有采摘水果的方法getFruit(String fruitName);这时有人来果园玩,和园丁说介绍一下你的各种水果(创建一个People类进行测试,分别通过农场园丁类得到各种水果)。 */
package com.jerry.exer1; /** * @author jerry_jy * @create 2022-10-01 8:52 */ public class Exer14 { public static void main(String[] args) { Fruit fruit1 = FruitGardener.getFruit("葡萄"); fruit1.grow(); fruit1.harvest(); fruit1.plant(); Fruit fruit2 = FruitGardener.getFruit("草莓"); fruit2.plant(); fruit2.grow(); fruit2.harvest(); Fruit fruit3 = FruitGardener.getFruit("苹果"); fruit3.grow(); fruit3.harvest(); fruit3.plant(); } } interface Fruit { void grow(); void harvest(); void plant(); } class Grape implements Fruit { @Override public void grow() { System.out.println("葡萄生长。。。"); } @Override public void harvest() { System.out.println("葡萄收获。。。"); } @Override public void plant() { System.out.println("葡萄种植。。。"); } } class Stuawberry implements Fruit { @Override public void grow() { System.out.println("草莓生长。。。"); } @Override public void harvest() { System.out.println("草莓收获。。。"); } @Override public void plant() { System.out.println("草莓种植。。。"); } } class Apple implements Fruit { @Override public void grow() { System.out.println("苹果生长。。。"); } @Override public void harvest() { System.out.println("苹果收获。。。"); } @Override public void plant() { System.out.println("苹果种植。。。"); } } class FruitGardener { String fruitName; public static Fruit getFruit(String type) { if ("葡萄".equals(type)) { return new Grape(); } else if ("草莓".equals(type)) { return new Stuawberry(); } else if ("苹果".equals(type)) { return new Apple(); } else { return null; } } }
题目30
/* 有一个显示器接口Graphoscope,具有display方法,有两个类:台式显示器和液晶显示器都实现显示器接口,有一个显示器生产厂家能够生产这两种显示器;有Computer类,具有Graphoscope属性,生产两台电脑,分别配置台式显示器和液晶显示器。 */
package com.jerry.exer1; /** * @author jerry_jy * @create 2022-10-01 9:22 */ public class Exer15 { public static void main(String[] args) { Graphoscope produce1 = Computer.produce("台式"); produce1.display(); Graphoscope produce2 = Computer.produce("液晶"); produce2.display(); } } interface Graphoscope { void display(); } class TaiShi implements Graphoscope { @Override public void display() { System.out.println("台式显示器。。。"); } } class YeJin implements Graphoscope { @Override public void display() { System.out.println("液晶显示器。。。"); } } class Computer { public static Graphoscope produce(String type) { if ("台式".equals(type)) { return new TaiShi(); } else if ("液晶".equals(type)) { return new YeJin(); } else { return null; } } }