一、上机目的
1.掌握类的声明,对象的创建以及方法的定义和调用。
2.掌握包机制。
3.掌握类的继承。
4. 掌握多态机制。
5.掌握抽象类与接口的使用。
二、上机内容
1.(1)设计一个矩形类Rectangle,该类包含2个私有实例变量:矩形的长(length)和宽(width),它还有一个方法,计算长方形的面积getArea()。在Rectangle类中创建构造方法,初始化类中的各个变量;
(2) 设计一个长方体类Cuboid,该类包含2个私有实例变量:长方体的底面矩形(rectangle)和高(height),它还有一个方法,计算长方体的体积getVolume()。在Cuboid类中创建构造方法,初始化类中的各个变量;
(3)设计一个测试类Test,该类中分别创建类Rectangle和Cuboid的实例,求矩形的面积和长方体的体积。
Rectangle类:
public class Rectangle {
private double length;
private double width;
public Rectangle() {
this.length=0;
this.width=0;
}
public Rectangle(double length,double width) {
this.length=length;
this.width=width;
}
public double getArea() {
return length*width;
}
}
Cuboid类:
public class Cuboid {
private double rectangle;
private double height;
public Cuboid() {
this.rectangle=0;
this.height=0;
}
public Cuboid(double rectangle,double height) {
this.rectangle=rectangle;
this.height=height;
}
public double getVolume() {
return rectangle*height;
}
}
Test2类:
public class Test2 {
public static void main(String[] args) {
Rectangle rectangle=new Rectangle(3.6,2.0);
Cuboid cuboid=new Cuboid(10,4.2);
System.out.println("矩形的面积:"+rectangle.getArea());
System.out.println("长方形的体积:"+cuboid.getVolume());
}
}
2. 编写一个完整的Java Application程序,包含接口Shape,Circle类和Square类实现接口shape,Test类测试Circle和Square,具体要求如下:
1)接口Shape:包含方法
double getArea():求一个形状的面积
double getPerimeter():求一个形状的周长
public interface Shape {
double getArea();
double getPerimeter();
}
2)Circle类:具有以下属性和方法:
属性radius:double类型,表示圆的半径。
方法:Circle(double r):构造函数
toString(): 输出圆的相关信息(半径等)。
public class Circle implements Shape{
double radius;
public Circle(double radius) {
this.radius=radius;
}
@Override
public double getArea() {
return radius*radius*Math.PI;
}
@Override
public double getPerimeter() {
return radius*2*Math.PI;
}
@Override
public String toString() {
return "Circle{"+
"radius="+radius+
'}';
}
}
3)Square类:具有以下属性和方法:
属性length,width:均是double类型,表示长方形的长和宽。
方法:Square(double x,double y):构造函数
toString(): 输出长方形的相关信息(长 宽等)
public class Square implements Shape{
private double length;
private double width;
public Square(double length,double width) {
this.length=length;
this.width=width;
}
@Override
public double getArea() {
return length*width;
}
@Override
public double getPerimeter() {
return (length+width)*2;
}
@Override
public String toString() {
return "Square{"+
"length="+length+
",width="+width+
'}';
}
}
4)Test类作为主类完成测试功能:
输出圆的周长和面积。
输出长方形的周长和面积
产生随机数决定生成形状的类型,偶数就生成一个Circle对象,奇数就生成一个Square对象,将生成的对象赋值给接口句柄,计算并输出该形状的周长和面积。
import java.util.Random;
public class Test {
public static void main(String[] args) {
Random random=new Random();
final int nextInt=random.nextInt(100);
if(nextInt%2==0) {
final Circle circle=new Circle(3.0);
System.out.println("圆的面积:"+circle.getArea());
System.out.println("圆的周长:"+circle.getPerimeter());
}else {
final Square square=new Square(4.0, 2.5);
System.out.println("长方形的面积:"+square.getArea());
System.out.println("长方形的周长:"+square.getPerimeter());
}
}
}
3. 设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声,要求如下:
(1)编写抽象类Animal()
该类有两个抽象方法cry()和getAnimalName(),即要求各种具体的动物给出自己的叫声和种类名称。
public abstract class Animal {
public abstract void cry();
public abstract String getAnimalName();
}
(2)编写模拟器类Simulator
该类有一个playSound(Animal animal)方法,该方法的参数是Animal类型。即参数animal可以调用Animal的子类重写cry()方法播放具体动物的声音,调用子类重写getAnimalName()方法显示动物种类的名称。
public class Simulator {
public void playSound(Animal animal) {
System.out.println("现在播放"+animal.getAnimalName()+"类的声音");
animal.cry();
}
}
(3)编写Animal类的子类:Dog和Cat类
public class Dog extends Animal{
public void cry() {
System.out.println("汪汪汪");
}
public String getAnimalName() {
return "狗";
}
}
public class Cat extends Animal{
public void cry() {
System.out.println("喵喵喵");
}
public String getAnimalName() {
return "猫";
}
}
(4)编写主类Application(用户程序)
public class Cat extends Animal{
public void cry() {
System.out.println("喵喵喵");
}
public String getAnimalName() {
return "猫";
}
}
4. 将第3题中的抽象类改为接口实现。
Animal类:
public interface Animal {
void cry();
String getAnimalName();
}
Simulator类:
public class Simulator {
public void playSound(Animal animal) {
System.out.println("现在播放"+animal.getAnimalName()+"类的声音");
animal.cry();
}
}
Dog类:
public class Dog implements Animal{
public void cry() {
System.out.println("汪汪汪");
}
public String getAnimalName() {
return "狗";
}
}
Cat类:
public class Cat implements Animal{
public void cry() {
System.out.println("喵喵喵");
}
public String getAnimalName() {
return "猫";
}
}
Application类:
public class Application {
public static void main(String[] args) {
Simulator simulator=new Simulator();
simulator.playSound(new Dog());
simulator.playSound(new Cat());
}
}



