一、实验目的
1.掌握类的继承机制
2.掌握抽象类的定义方法
2.熟悉类中成员变量和方法的访问控制
3.熟悉成员方法或构造方法的多态性
二、实验内容
1. Circle类及其子类
【问题描述】
实现类Circle,半径为整型私有数据成员
1)构造方法:参数为半径(整型数据)。
2)成员方法:标准方法;计算周长的方法、计算面积的方法;toString()方法返回将半径、周长和面积连接的长字符串。
派生类Circle获得圆柱体类,要求:
1)添加属性成员圆柱的高(整型数据)。
2)添加标准方法、构造方法、计算圆柱体体积的方法。
定义测试类,输入半径和高,分别实例化Circle类和圆柱体类,按要求输出圆和圆柱体的信息。结果保留2位小数
【输入形式】
两行,第一行为圆类对象的半径,第二行为圆柱体类对象的半径和高
【输出形式】
两行,第一行输出圆类对象的相关信息,第二行输出圆柱体对象的相关信息
运行结果:
源码:
package circle1;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Circle1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int rad = in.nextInt();
Cir1 a1 = new Cir1(rad);
a1.show();
int c = in.nextInt();
int h = in.nextInt();
Vol a2 = new Vol(c, h);
a2.show();
in.close();
}
}
class Cir1 {
private int rad;
DecimalFormat df = new DecimalFormat("#.00");
public double perimeter() {
return Math.PI * 2 * rad;
}
public double area() {
return rad * rad * Math.PI;
}
public String toString() {
return "radium=" + rad + " perimeter=" + df.format(perimeter()) + " area=" + df.format(area());
}
public void show() {
System.out.printf(toString());
}
public Cir1(int rad) {
this.rad = rad;
}
}
class Vol extends Cir1 {
private int h;
public Vol(int rad, int h) {
super(rad);
this.h = h;
}
public double volume(int h) {
return area() * h;
}
public String toString() {
return super.toString()+" volume=" + df.format(volume(h));
}
public void show() {
System.out.printf(toString());
}
}
2.抽象类Shape
【问题描述】
定义抽象类Shape,圆形Circle、Rectangle为子类。
Shape类有一个数据成员(double型),定义带参构造方法、抽象方法calArea()(返回值类型:double)
在Circle、Square两个类中,实现calArea()方法。
在Rectangle类中,增加一个数据成员(double型),实现calArea()方法。
编写测试类:定义一个Shape类引用shape,分别指向一个Circle类、Rectangle类对象,利用多态,计算各平面图形的面积并输出。(结果保留2位小数)
要求:
1.数据成员全部为私有成员;
2.数据从键盘输入,调用带参构造方法创建子类对象。
提示:使用Math类的PI
【输入形式】两行:分别是为圆的半径、长方形的长和宽
【输出形式】两种平面图形的面积
运行结果:
源码:
package area;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Area {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("#.00");
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
double a1 = in.nextDouble();
Shape x1 = new Circle(a1);
System.out.printf(df.format(x1.calArea()));
double a2 = in.nextDouble();
double a3 = in.nextDouble();
Shape x2 = new Rectangle(a2,a3);
System.out.printf(df.format(x2.calArea()));
in.close();
}
}
abstract class Shape{
protected double rad;
public Shape(double rad) {
this.rad = rad;
}
abstract public double calArea();
}
class Circle extends Shape{
public Circle(double rad) {
super(rad);
}
public double calArea() {
return Math.PI*rad*rad;
}
}
class Rectangle extends Shape{
private double k;
public Rectangle(double c ,double k) {
super(c);
this.k = k;
}
public double calArea() {
return rad*k;
}
}
3. 抽象类-员工类
【问题描述】
编写抽象类-员工类,有姓名、工号(int型)、基本工资(double型)属性,计算工资为抽象方法,由员工类派生出教师、实验员和行政人员三个类。教师有课酬标准(整型,元/小时)和月课时(整型),月工资=基本工资+课酬标准月课时;实验员有月在岗小时(整型),月工资=基本工资+50每月在岗小时;行政人员有岗贴(double型),月工资=基本工资+岗贴。请编写各类定义,并编写测试类(主动类),在主动类中定义一个打印方法,形参为员工类引用,在主方法中分别创建三个对象,调用打印方法,根据不同的实参打印出不同类对象的信息。
【输入形式】
三行,第一行为教师信息,第二行为实验员信息,第三行为行政人员信息
【输出形式】
三类人员的相关信息
运行结果:
源码:
package person_inform;
import java.util.Scanner;
public class Person_Inform {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String name = in.next();
int empNo = in.nextInt();
double baseSalary = in.nextDouble();
int hourlyFee = in.nextInt();
int monthlyHour = in.nextInt();
String name1 = in.next();
int empNo1 = in.nextInt();
double baseSalary1 = in.nextDouble();
int hourlyFee1 = in.nextInt();
String name2 = in.next();
int empNo2 = in.nextInt();
double baseSalary2 = in.nextDouble();
double allowance = in.nextDouble();
System.out.println("teacher:");
Pers t = new Teacher(name, empNo, baseSalary, hourlyFee, monthlyHour);
System.out.println(t.toString());
System.out.println("lab worker:");
Pers l = new lab_Worker(name1, empNo1, baseSalary1, hourlyFee1);
System.out.println(l.toString());
System.out.println("staff:");
Pers s = new Staff(name2, empNo2, baseSalary2, allowance);
System.out.println(s.toString());
in.close();
}
}
abstract class Pers {
private String name;
private int empNo;
protected double baseSalary;
public Pers(String name, int empNo, double baseSalary) {
this.name = name;
this.empNo = empNo;
this.baseSalary = baseSalary;
}
abstract public double salary();
public String toString() {
return "name:" + name + "\n" + "empNo:" + empNo + "\n" + "baseSalary:"
+ baseSalary;
}
}
class Teacher extends Pers {
private int hourlyFee;
private int monthlyHour;
public Teacher(String name, int empNo, double baseSalary, int hourlyFee, int monthlyHour) {
super(name, empNo, baseSalary);
this.hourlyFee = hourlyFee;
this.monthlyHour = monthlyHour;
}
public double salary() {
return baseSalary + hourlyFee * monthlyHour;
}
public String toString() {
return super.toString()+"\n"+"hourlyFee:"+hourlyFee+"\n"+"monthlyHour:"+monthlyHour
+"\n"+"salary:"+salary()+"\n";
}
}
class lab_Worker extends Pers {
private double hourlyFee;
public lab_Worker(String name, int empNo, double baseSalary, int hourlyFee) {
super(name, empNo, baseSalary);
this.hourlyFee = hourlyFee;
}
public double salary() {
return baseSalary + hourlyFee * 50;
}
public String toString() {
return super.toString()+"\n"+"hourlyFee:"+hourlyFee+"\n"+"salary:"+salary()+"\n";
}
}
class Staff extends Pers {
private double allowance;
public Staff(String name, int empNo, double baseSalary, double allowance) {
super(name, empNo, baseSalary);
this.allowance = allowance;
}
public double salary() {
return baseSalary + allowance;
}
public String toString() {
return super.toString()+"\n"+"allowance:"+allowance+"\n"+"salary:"+salary()+"\n";
}
}
END:熬夜也要敲完,这是一场发际线与手速的竞争。