Java课程实验(二)包含接口,类的定义,抽象等

Java课程实验二

本次实验涉及知识点

1、掌握类的定义
2、掌握构造函数的概念和使用
3、掌握类及其成员的访问控制符的使用
4、掌握类的继承关系和派生方法
5、理解的多态的概念与使用
6、掌握抽象函数的定义及使用
7、掌握接口的定义和使用

1、类的定义与实例化

定义一个表示学生的类Student。这个类的属性有“学号”、“班号”、“姓名”、“性别”、“年龄”,方法有“获得学号”、“获得班号”、 “获得姓名”、“获得性别”、“获得年龄”、构造函数、printInfo()(实现将学生的信息输出显示),并要求实例化一个学生,并输出学生信息。

package 实验报告2;

public class PrintInfo {
	public static void main(String[] args) {
		Student s=new Student(202004201634L,420,"C小只","女",20);
		s.printInfo();
	}

}

package 实验报告2;

public class Student {
	private long ID;
	private int classid;
	private String name;
	private String sex;
	private int age;
	
	public Student(long I,int c,String n,String s,int a) {
		this.setID(I);
		this.setClassid(c);
		this.setName(n);
		this.setSex(s);
		this.setAge(a);

	}
	
	public Student() {

	}
	
	public void printInfo(){
		System.out.println("学生"+this.getName()+"的学号为"+this.getID()+",班号为"+this.getClassid()+",性别为"+this.getSex()+",年龄为"+this.getAge());
	}
	
	public long getID() {
		return ID;
	}

	public void setID(long iD) {
		ID = iD;
	}

	public int getClassid() {
		return classid;
	}

	public void setClassid(int classid) {
		this.classid = classid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

2、编写支付程序

假定要为某个公司编写雇员工资支付程序,这个公司有各种类型的雇员(Employee),不同类型的雇员按不同的方式支付工资:
(1)经理(Manager)-每月获得一份固定的工资(Salary);
(2)销售人员(Salesman-在基本工资的基础上每月还有销售提成(baseSalary+SalesCommission)
(3)一般工人( Worker)-则按他每月工作的天数计算工资(days*preSalary)
根据上述要求试用类的继承和相关机制描述这些功能,并编写一个Java Application程序,演示这些类的用法。(提示:应设计一个抽象类Employee描述所有雇员的共同特性(姓名,员工号),这个类应提供一个计算工资的抽象方法ComputerSalary(),经理、销售人员和一般工人对应的类都应该继承这个类,并重新定义计算工资的方法,给出它的具体实现)。

package 实验报告2;

public class Application {

	public static void main(String[] args) {
		Manager m=new Manager("C小只",202004192146L,8000);
		Salesman s= new Salesman("C大只",202004192148L,5000,1530);
		Worker w=new Worker("C小小",202004192149L,150);
		m.ComputerSalary();
		s.ComputerSalary();
		w.ComputerSalary();

	}

}
package 实验报告2;

public abstract class Employee {
	private String name;
	private long id;
	public Employee(String n,long i) {
		this.setName(n);
		this.setId(i);
	}
	public Employee() {
		
	}
	public abstract void ComputerSalary();
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	

}
package 实验报告2;

public class Manager extends Employee {
	private int Salary;
	public Manager(String n,long i,int S) {
		super(n,i);
		this.setSalary(S);
	}
	public Manager(){
		
	}
	public void ComputerSalary() {
		System.out.println(this.getName()+"经理的每月工资为"+this.getSalary()+"元,工号:"+this.getId());
	}
	public int getSalary() {
		return Salary;
	}
	public void setSalary(int salary) {
		Salary = salary;
	}

}
package 实验报告2;

public class Salesman extends Employee {
	private int baseSalary;
	private int SalesCommission;
	private int Salary;
	public Salesman(String n,long i,int b,int S) {
		super(n,i);
		this.setBaseSalary(b);
		this.setSalesCommission(S);
	}
	public Salesman(){
		
	}
	public void ComputerSalary() {
		Salary=baseSalary+SalesCommission;
		System.out.println("销售人员"+this.getName()+"的每月工资为"+Salary+"元,工号:"+this.getId());
	}

	public int getBaseSalary() {
		return baseSalary;
	}

	public void setBaseSalary(int baseSalary) {
		this.baseSalary = baseSalary;
	}

	public int getSalesCommission() {
		return SalesCommission;
	}

	public void setSalesCommission(int salesCommission) {
		SalesCommission = salesCommission;
	}

}
package 实验报告2;

public class Worker extends Employee {
	private int preSalary;
	private int Salary;
	public static int days=30;
	public Worker(String n,long i,int p) {
		super(n,i);
		this.setPreSalary(p);
	}
	public Worker(){
		
	}
	public void ComputerSalary() {
		Salary=days*preSalary;
		System.out.println("工人"+this.getName()+"的每月工资为"+Salary+"元,工号:"+this.getId());
	}
	public int getPreSalary() {
		return preSalary;
	}
	public void setPreSalary(int preSalary) {
		this.preSalary = preSalary;
	}
	

}

3、计算圆的周长和面积

设计一个形状Shapes接口,在其中定义计算机面积的getArea()方法和计算周长的方法getPerimeter(),和输出信息方法printInfo(). 然后设计一个Circle类以实现Shapes接口中的方法, 最后以Java Application程序方式测试前述接口及其实现类, 输出其测试结果。

package 实验报告2;

public class Main3_Application {
	public static void main(String[] args) {
		Circle c=new Circle(8);
		c.printInfo();
	}

}
package 实验报告2;

public interface Shapes {
	public static double pi=3.14;
	public void getArea();//计算面积
	public void getPerimeter();//计算周长
	public void printInfo();
}
package 实验报告2;

public class Circle implements Shapes{
    private double r; 
    private double s;
    private double c;
    public Circle(){
    	
    }
    public Circle(double r){
    	this.setR(r);
    }
	public void getArea() {
		this.s=pi*r*r;
	}
	public void getPerimeter() {
		this.c=2*pi*r;
	}
	public void printInfo() {
		this.getArea();
		this.getPerimeter();
		System.out.println("半径为"+r+"的圆的面积为"+s+",周长为"+c);
	}
	public double getR() {
		return r;
	}
	public void setR(double r) {
		this.r = r;
	}
}

你可能感兴趣的:(Java题库)