一.小汽车问题
1.父类
package Object;
public class Vehicles {
public Vehicles(String brand,String color) {
this.brand = brand;
this.color = color;
}
private String brand;//商标
private String color;//颜色
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void run() {
System.out.println("我已经开动了");
}
public void showinfo() {
System.out.print("商标: "+brand+" 颜色: "+color);
}
}
2. 子类一 Car
package Object;
public class Car extends Vehicles{
public Car(String brand, String color,int seats) {
super(brand,color);
//this.seats = seats;
}
private int seats;
public int getSeats() {
return seats;
}
public void setSeats(int seats) {
this.seats = seats;
}
public void showCar() {
showinfo();
System.out.println(" 座位数: "+ seats);
}
}
3. 子类二Trunk
package Object;
public class Truck extends Vehicles{
public Truck(String brand, String color,float load) {
super(brand, color);
this.load = load;
}
private float load;
public void showTruck() {
showinfo();
System.out.println(" 载重: "+load);
}
}
4.测试类
package Object;
public class Test1 {
public static void main(String[] args) {
Car c = new Car("奥迪","red",12);
c.showCar();
Truck t = new Truck("大众","black",23);
t.showTruck();
}
}
二. 编写一个类Calculate1,实现加、减两种运算,然后,编写另一个派生类Calculate2,实现乘、除两种运算。
package jicheng;
//编写一个类Calculate1,实现加、减两种运算,然后,
public class Calculate1 {
public int add(int a,int b) {
return a+b;
}
public double add(double a,double b) {
return a+b;
}
public int sub(int a,int b) {
return a-b;
}
public double sub(double a,double b) {
return a-b;
}
}
package jicheng;
//编写另一个派生类Calculate2,实现乘、除两种运算。
public class Calculate2 extends Calculate1{
public int multiply(int a,int b) {
return a*b;
}
public double multiply(double a,double b) {
return a*b;
}
public double divide(double a,double b) {
if(b==0) {
System.out.println("除数不能为0");
}
return a/b;
}
public int divide(int a,int b) {
if(b==0) {
System.out.println("除数不能为0");
}
return a/b;
}
public static void main(String []args) {
Calculate2 calculate2 = new Calculate2();
System.out.println(calculate2.add(1, 4));
System.out.println(calculate2.sub(1, 4));
System.out.println(calculate2.multiply(1, 4));
//System.out.println(calculate2.divide(1, 0));
System.out.println(calculate2.divide(5.0, 5));
}
}
三. 创建一个类A,在构造函数中输出“A”,再创建一个类B,在构造函数中输出“B”。
从A继承一个名为C的新类,并在C内创建一个成员变量B。不要为C创建构造函数。
在Main方法中创建类C的一个对象,写出运行程序后输出的结果。 A
如果在C中也创建一个构造函数输出“C”,整个程序运行的结果又是什么? A C
package jicheng;
public class A {
//创建一个类A,在构造函数中输出“A”,再创建一个类B,在构造函数中输出“B”。
//从A继承一个名为C的新类,并在C内创建一个成员变量B。不要为C创建构造函数。
//在Main方法中创建类C的一个对象,写出运行程序后输出的结果。
//如果在C中也创建一个构造函数输出“C”,整个程序运行的结果又是什么?
public A() {
System.out.println("A");
}
}
package jicheng;
public class B {
public B() {
System.out.println("B");
}
}
package jicheng;
public class C extends A{
B b;
public C() {
System.out.println("C");
}
}
package jicheng;
public class Yest {
public static void main(String[] args) {
C c = new C();
}
}
四. 建立三个类:居民、成人adult、官员Official。居民包含身份证号、姓名、出生日期,而成人继承自居民,多包含学历、职业两项数据;官员则继承自成人,多包含党派、职务两项数据。要求每个类的字段都以属性的方式对外提供数据输入输出的功能。Setter getter 方法
package jicheng;
//身份证号、姓名、出生日期
public class Resident {
private String id;
private String name;
private String born_date;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBorn_date() {
return born_date;
}
public void setBorn_date(String born_date) {
this.born_date = born_date;
}
}
package jicheng;
//成人继承自居民,多包含学历、职业
public class Adult extends Resident{
String xl; //学历
String profession;
public String getXl() {
return xl;
}
public void setXl(String xl) {
this.xl = xl;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
}
package jicheng;
//官员则继承自成人,多包含党派、职务
public class Official extends Adult{
String dp;
String job;
public String getDp() {
return dp;
}
public void setDp(String dp) {
this.dp = dp;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
五. 建立一个汽车Auto类,包括轮胎(wheel)个数,汽车颜色,车身重量、速度等成员变量。并通过不同的构造方法创建实例。至少要求: 汽车能够加速,减速,停车。 再定义一个小汽车类Car,继承Auto,并添加空调、CD等成员变量,覆盖(重写)加速,减速的方法
package jicheng;
/*
*轮胎(wheel)个数,汽车颜色,车身重量、速度等成员变量。并通过不同的构造方法创建实例。
* 汽车能够加速,减速,停车。
*再定义一个小汽车类Car,继承Auto,并添加空调、CD等成员变量,覆盖(重写)加速,减速的方法
*/
public class Auto {
public Auto() {
}
public Auto(int wheel,String color,double weight,double speed) {
this.wheel = wheel;
this.color = color;
this.weight = weight;
this.speed = speed;
}
private int wheel;
private String color;
private double weight;
private double speed;
public int getWheel() {
return wheel;
}
public void setWheel(int wheel) {
this.wheel = wheel;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public void speedUp(int s) {
speed += s;
}
public void speedDown(int s) {
speed -= s;
}
public void stop() {
speed = 0;
}
}
package jicheng;
public class Car extends Auto{
private String CD;
private String kongtiao;
@Override
public void speedUp(int s) {
super.speedUp(s);
System.out.println("我是小汽车加速速度: "+super.getSpeed());
}
@Override
public void speedDown(int s) {
super.speedDown(s);
System.out.println("我是小汽车加速速度: "+super.getSpeed());
}
}
package jicheng;
public class Yest {
public static void main(String[] args) {
Car c = new Car();
c.setSpeed(10);
c.speedUp(5);
c.speedDown(9);
}
}
六. 写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方法(getter和setter方法),返回月利率的方法getMonthlyInterest(),取款方法withdraw(),存款方法deposit()。
写一个用户程序测试Account类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%的Account对象。使用withdraw方法提款30000元,并打印余额。
再使用withdraw方法提款2500元,使用deposit方法存款3000元,然后打印余额和月利率。
package Object;
public class Account {
public Account (int id, double balance, double annualInterestRate ) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
//账号id,余额balance,年利率annualInterestRate;
private int id;
private double balance;
private double annualInterestRate;
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId( int id) {
this.id = id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//取款
public double withdraw (double outer) {
if(balance<outer) {
System.out.println("余额不足");
}else {
balance -= outer;
System.out.println("余额为: "+balance);
}
return balance;
}
//存款
public void deposit (double amount) {
balance += amount;
}
//返回月利率的方法getMonthlyInterest()
public double getMonthlyInterest() {
return annualInterestRate/12;
}
}
package Object;
public class CheckAccount extends Account{
public CheckAccount(int id, double balance, double annualInterestRate,double overdraft) {
super(id, balance, annualInterestRate);
this.overdraft = overdraft;
}
private double overdraft;
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
/*
计算需要透支的额度
判断可透支额overdraft是否足够支付本次透支需要,如果可以
将账户余额修改为0,冲减可透支金额
*/
public double withdraw (double outer) {
if(super.getBalance()<outer) {
if(overdraft + super.getBalance() <outer ) {
System.out.println("取款失败,可透支余额不足");
}else {
overdraft = (super.getBalance() +overdraft-outer);
super.setBalance(0) ;//注意顺序
System.out.println("使用了可透余额,取款成功");
}
}else {
super.setBalance(super.getBalance()-outer);
System.out.println("取款成功");
}
return super.getBalance();
}
}
package Object;
/*
*写一个用户程序测试CheckAccount类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%,可透支限额为5000元的CheckAccount对象。
使用withdraw方法提款5000元,并打印账户余额和可透支额。
再使用withdraw方法提款18000元,并打印账户余额和可透支额。
再使用withdraw方法提款3000元,并打印账户余额和可透支额。
*/
public class CheckAtest {
public static void main(String[] args) {
CheckAccount ca = new CheckAccount(1122,20000,0.045,5000);
ca.withdraw(5000);
//15000,5000
System.out.println("余额: "+ca.getBalance()+" 可透支额"+ca.getOverdraft());
ca.withdraw(18000);
//0 2000
System.out.println("余额: "+ca.getBalance()+" 可透支额"+ca.getOverdraft());
ca.withdraw(3000);
System.out.println("余额: "+ca.getBalance()+" 可透支额"+ca.getOverdraft());
}
}
多态
定义一个Animal类,方法有sing方法,定义这个类的三个子类(Dog,Cat,Bird),分别重写这个方法。利用多态,定义一个Animal类型的对象,Animal a;分别引用三个子类的对象,调用sing方法。为每个子类,增加额外的方法
package chouxiang;
public class Animal {
// public void eat() {
// System.out.println("正在吃饭");
// }
public void sing() {
System.out.println("正在唱歌");
}
}
package chouxiang;
public class Dog extends Animal{
public void eat() {
System.out.println("小狗正在吃饭");
}
@Override
public void sing() {
System.out.println("小狗正在唱歌");
}
}
package chouxiang;
public class Cat extends Animal{
public void eat() {
System.out.println("小猫正在吃饭");
}
public void sing() {
System.out.println("小猫正在唱歌");
}
}
}
package chouxiang;
public class Eagle extends Animal{
public void eat() {
System.out.println("小鸟正在吃饭");
}
@Override
public void sing() {
System.out.println("小鸟正在唱歌");
}
}
package chouxiang;
public class Test {
public static void main(String[] args) {
// Animal a1 = new Dog();
// Animal a2 = new Cat();
// Animal a3 = new Eagle();
// a1.eat();//小狗正在吃饭
// a2.eat();//小猫正在吃饭
// a3.eat();//小鸟正在吃饭
// if(a3 instanceof Eagle) {
// Eagle a4 = (Eagle)a3;
// a4.sing();
// }
/*
利用多态,定义一个Animal类型的对象,Animal a;分别引用三个子类的对象,调用sing方法。为每个子类,增加额外的方法。通过此例,练习向上转型(upCast 向上转换),
Animal a = new Dog();
向下转型(downCast 向下转换),及instanceof操作符。Dog dog =(Dog)a;
*/
Animal a1 = new Dog();
Animal a2 = new Cat();
Animal a3 = new Bird();
a1.sing();
a2.sing();
a3.sing();
if(a1 instanceof Dog) {
((Dog) a1).eat();
}
if(a2 instanceof Cat) {
((Cat) a2).eat();
}
if(a3 instanceof Bird) {
((Bird) a3).eat();
}
}
}
二. 定义一个(宠物)Pet类,类中有属性name,方法是follow(跟随);再定义三个子类,Cat,Dog,Eagle,分别重写follow方法;
再定义一个Hero类,这个类中有两个属性name和Pet类型的pet,一方法run(),再定义两个构造方法,Hero(String name,);Hero(String name,Pet pet)
run(Pet pet)方法的代码是Hero跑,并且他的宠物也跟着跑;
编写测试类来操作Hero
package chouxiang;
public class Dog extends Pet{
@Override
public void follow() {
System.out.println("小狗正在跟随");
}
}
package chouxiang;
public class Cat extends Pet{
@Override
public void follow() {
System.out.println("小猫正在跟随");
}
}
package chouxiang;
public class Eagle extends Pet{
@Override
public void follow() {
System.out.println("鹰正在跟随");
}
}
package chouxiang;
public class Test {
public static void main(String[] args) {
Pet p = new Dog();
p.setName("白龙马");
Hero h = new Hero("唐僧",p);
h.run(p);
}
}
package chouxiang;
public class Test {
public static void main(String[] args) {
Pet p = new Dog();
p.setName("白龙马");
Hero h = new Hero("唐僧",p);
h.run(p);
}
}
package chouxiang;
/*
* 二. 定义一个(宠物)Pet类,类中有属性name,方法是follow(跟随);再定义三个子类,Cat,Dog,Eagle,分别重写follow方法;
再定义一个Hero类,这个类中有两个属性name和Pet类型的pet,一方法run(),再定义两个构造方法,Hero(String name,);Hero(String name,Pet pet)
run(Pet pet)方法的代码是Hero跑,并且他的宠物也跟着跑;
编写测试类来操作Hero
*/
public class Pet {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void follow() {
System.out.println("正在跟随");
}
}
package chouxiang;
public class Hero {
public Hero(String name) {
this.name = name;
}
public Hero (String name,Pet pet) {
this.name = name;
this.pet = pet;
}
private String name;
private Pet pet;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
public void run(Pet pet) {
System.out.println(pet.getName()+"正在跟着"+name+"跑");
}
}
三. 图形问题
package chouxiang;
public class GeometricObject {
public GeometricObject(String color,double weight) {
this.color = color;
this.weight = weight;
}
private String color;
private double weight;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double findArea() {
return 0;
}
}
package chouxiang;
public class Circle extends GeometricObject{
public Circle(String color, double weight,double radius) {
super(color, weight);
this.radius = radius;
}
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double findArea() {
return 3.14*radius*radius;
}
}
package chouxiang;
public class MyRectangle extends GeometricObject{
public MyRectangle(String color,double weight,double width,double height) {
super(color,weight);
this.width = width;
this.height = height;
}
private double width;
private double height;
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public double findArea() {
return width*height;
}
}
package chouxiang;
public class Test {
//传入父类对象,传子类写死了
public boolean equalsAre(GeometricObject g1,GeometricObject g2) {
if(g1.findArea()==g2.findArea()) {
return true;
}else {
return false;
}
}
public void displayGeometricObject(GeometricObject g) {
System.out.println("面积为: "+ g.findArea());
}
public static void main(String[] args) {
GeometricObject g1 = new MyRectangle("red",10,3,2);
GeometricObject g2 = new Circle("esd",10,3);
Test t = new Test();
System.out.println(t.equalsAre(g1, g2));
t.displayGeometricObject(g1);
t.displayGeometricObject(g2);
}
}