1、设计类Shape, Rectangle和Circle,要求使用继承,方法包括计算周长和面积。
package test6;
abstract class Shape {//抽象类
public Shape(String name) {
System.out.println("此形状为:" + name);
}
abstract public double area();//抽象方法
abstract public double circumference();
}
class Circle extends Shape {//圆形
private double radius;
public Circle(double radius) {//构造函数
super("圆形");//对父类构造函数进行初始化
this.radius=radius;
}
public double area() { //实现父类抽象方法
return Math.PI*radius*radius;
}
public double circumference() {
return 2*Math.PI*radius;
}
}
class Rect extends Shape {//方形
private double width,height;
public Rect(double w,double h) {
super("长方形");
this.width=w;
this.height=h;
}
public double area() {
return width*height;
}
public double circumference() {
return 2*(width+height);
}
}
public class two {
public static void main(String[] args) {
Shape rect = new Rect(0.6, 1.2);
System.out.println("高为1.2,宽为0.6的矩形面积:" + String.format("%.2f", rect.area()));//保留小数点后两位
System.out.println("高为1.2,宽为0.6的矩形周长:" +String.format("%.2f", rect.circumference()));
Circle circle = new Circle(3.5);
System.out.println("半径为3.5的圆的面积:" + String.format("%.2f",circle.area()));
System.out.println("半径为3.5的圆的周长:" + String.format("%.2f",circle.circumference()));
}
}
输出:
2. 假设在银行定期存款分半年期和一年期两种。如果是半年定期存款,利息率为1.75%;如果是一年定期存款,利息率为2.25%。无论哪种存款,在得到利息后还要交利息所得税5%。根据不同的存款金额,分别计算存款半年和一年相应的利息。
要求:
(1)设计一个存款抽象类Cash,成员变量包括存款金额amount、利息率interest和利息所得税tax(为最终类),成员方法只有抽象方法calculate(),用来计算利息;
(2)定义两个类HalfCash和FullCash,分别用来完成计算和显示定期存款半年和一年相应的利息;
(3)给出程序,当存款金额为1000元时,运行出其结果。
package test7;
abstract class Cash {
double amount,interest,tax;
abstract double calculate(double amount);
}
class HalfCash extends Cash {
double calculate(double amount) {
interest=0.0175/2;
tax=0.05;
double interest_money=amount*interest;//利息
double tax_money=interest_money*tax; //利息所得税
return interest_money-tax_money;//最终所获利息
}
}
class FullCash extends Cash {
double calculate(double amount) {
interest=0.0225;
tax=0.05;
double interest_money=amount*interest;//利息
double tax_money=interest_money*tax; //利息所得税
return interest_money-tax_money;//最终所获利息
}
}
public class one {
public static void main(String[] args) {
HalfCash halfcash=new HalfCash();
halfcash.calculate(1000);
System.out.println("半年定期存款所得利息:"+halfcash.calculate(1000));
FullCash fullcash=new FullCash();
fullcash.calculate(1000);
System.out.print("一年定期存款所得利息:"+fullcash.calculate(1000));
}
}
输出:
3. 编写程序,要求如下:
(1)定义Biology(生物)、Animal(动物)和Mankind(人)3个接口;
(2)接口Biology声明breath()抽象方法;
(3)接口Animal继承Biolog并声明move()和eat()抽象方法;
(4)接口Mankind继承Animal并声明study()和think()抽象方法;
(5)定义类NormalMan实现接口Mankind,仅显示相应的功能信息来实现它们声明的抽象方法;
(6)在类NormalMan中定义私有变量name,实现无参构造方法和有参构造方法初始化该变量。
package test7;
interface Biology {
void breath();
}
interface Animal extends Biology {
void move();
void eat();
}
interface Mankind extends Animal{
void study();
void think();
}
class NormalMan implements Mankind{
String name;
public NormalMan(){ //无参构造函数
}
public NormalMan(String name){//有参构造函数
this.name=name;
System.out.println("大家好:我叫"+this.name);
}
public void breath() {
System.out.println("我正在呼吸。。。。");
}
public void move() {
System.out.println("我正在移动。。。。");
}
public void eat() {
System.out.println("我正在吃。。。。");
}
public void study() {
System.out.println("我正在学习。。。。");
}
public void think() {
System.out.println("我正在思考。。。。");
}
}
public class three {
public static void main(String[] args) {
// TODO Auto-generated method stub
NormalMan kevin = new NormalMan("Kevin");
kevin.breath();
kevin.move();
kevin.eat();
kevin.study();
kevin.think();
}
}
输出: