实验内容:面向对象程序设计
1、定一个名为Person的类,其中含有一个String类型的成员变量name和一个int类型的成员变量age, 分别为这两个变量定义访问方法和修改方法,另外再为该类定义一个名为speak的方法, 在其中输出name和age的值。编写一应用程序,使用上面定义的Person类,实现数据的访问、修改。
package com.itheima.demo;public class Person {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void speak(){System.out.println("Name:"+getName());System.out.println("Age:"+getAge());}}class Persontest1{public static void main(String[] args) {Person P=new Person();P.setName("李四");P.setAge(21);P.speak();}}
正在上传…重新上传取消正在上传…重新上传取消正在上传…重新上传取消
2、定义一个名为Rectangle的类表示矩形,其中含有length、width两个double型的成员变量表示矩形的长和宽。 要求为每个变量定义访问方法和修改方法,定义求矩形周长的方法perimeter()和求面积的方法area()。 编写一个类测试这个矩形类。3、为上题(第2题)的Rectangle类编写一个带参数的构造方法,通过用户给出的长、宽创建矩形对象, 再编写一个默认构造方法(无参构造方法),使用默认构造方法创建矩形对象,然后通过setter方法为对象属性赋值。编写一个类测试这个矩形类。
package com.itheima.demo;public class Rectangle {private double length;private double width;public double getLength() {return length;}public void setLength(double length) {this.length = length;}public double getWidth() {return width;}public void setWidth(double width) {this.width = width;}public double perimeter(){return (getLength()+getWidth())*2;}public double area(){return getLength()*getWidth();}public Rectangle(double length,double width){this.length=length;this.width=width;}public Rectangle(){}}
public class Rectangletest {public static void main(String[] args) {Rectangle R=new Rectangle();R.setWidth(2.0);R.setLength(3.0);System.out.println("周长:"+R.perimeter());Rectangle M=new Rectangle(4,2);System.out.println("面积:"+M.area());}}
正在上传…重新上传取消
4、定义一个Triangle类表示三角形,其中包括3个double型变量a、b、c,用于表示3条边长。为该类定义两个构造方法 :默认构造方法(无参构造方法)设置三角形的3条边长都为0.0;带3个参数的构造方法通过传递3个参数创建三角形 对象。定义求三角形面积的方法area(),面积计算公式为:area=Math.sqrt(s*(s-a)(s-b)(s-c)),其中s=(a+b+c) /2。编写程序测试该类。
package com.itheima.demo;public class Triangle {private double a;private double b;private double c;public Triangle(double a, double b, double c) {this.a = a;this.b = b;this.c = c;}public Triangle() {}public double area(){double s=(a+b+c)/2;return Math.sqrt(s*(s-a)*(s-b)*(s-c));}}
package com.itheima.demo;public class Triengletest {public static void main(String[] args) {Triangle T=new Triangle();System.out.println(T.area());Triangle T1=new Triangle(10,10,10);System.out.println(T1.area());}}
正在上传…重新上传取消正在上传…重新上传取消正在上传…重新上传取消
5、定义一个表示二维平面上的点Point类,并在该类中定义一个计算两点之间距离的方法,其格式如下:
• public double getDistance(Point p)
编写一个类测试这个Point类。
package com.itheima.demo;public class Point {private double x;private double y;public Point(double x,double y){this.x=x;this.y=y;}public double getDistance(Point p){return Math.sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));}}
public class Pointtest {public static void main(String[] args) {Point P=new Point(2,3);Point P1=new Point(5,7);double part=P1.getDistance(P);System.out.println("两点相距:"+part);}}
正在上传…重新上传取消正在上传…重新上传取消正在上传…重新上传取消
- 定义一个名为Student的类,它继承Person类(Person类中包含String类型的name和int类型的age),其中定义sno(表示学号)和major(表示专业)两个成员变量和封装这两个变量的方法。编写主程序,检查新建类中的所有变量和方法。
package com.itheima.demo;public class Person {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Person(String name,int age){this.name=name;this.age=age;}public void speak(){System.out.println("Name:"+getName());System.out.println("Age:"+getAge());}}
class Student extends Person{private String sno;private String major;public Student(String name,int age,String sno,String major){super(name,age);this.sno=sno;this.major=major;}public void speak(){System.out.println("name="+getName()+"age="+getAge()+"sno="+sno+"major="+major);}}
public class PersonStudentStest {public static void main(String[] args) {Student S=new Student("张三",21,"6021203152","计算机");S.speak();}}
正在上传…重新上传取消正在上传…重新上传取消正在上传…重新上传取消
2、设计一个汽车类Auto,其中包含一个表示速度的double型的成员变量speed和表示启动的start()方法、表示加速的speedUp()方法以及表示停止的stop()方法。再设计一个Auto类的子类Bus表示公共汽车,在Bus类中定义一个int型的、表示乘客数量的成员变量passenger,另外定义两个方法gotOn()和gotOff()表示乘客上车和下车。编写一个应用程序,测试Bus类的使用。
package com.itheima.demo;public class Auto {private double speed;public void start(){System.out.println("启动");}public void speedUp(){System.out.println("加速");}public void stop(){System.out.println("停止");}}
public class Bus extends Auto{private int passenger;public void gotOn(){System.out.println("乘客上车");}public void gotOff(){System.out.println("乘客下车");}}
public class AutoBustest {public static void main(String[] args) {Bus B=new Bus();B.gotOff();B.gotOn();B.speedUp();B.start();B.stop();}}
正在上传…重新上传取消正在上传…重新上传取消正在上传…重新上传取消
3、定义一个名为Cuboid的长方体类,使其继承Rectangle类(Rectangle类中包含double类型的length和width),其中包含一个表示高度的double型成员变量height,定义一个构造方法Cuboid(double length,double width,double height)和一个求长方体体积的volume()方法。编写一个应用程序,在其中求一个长、宽、高分别为10、5、2的长方体的体积。
package com.itheima.demo;public class Rectangle {private double length;private double width;public double getLength() {return length;}public void setLength(double length) {this.length = length;}public double getWidth() {return width;}public void setWidth(double width) {this.width = width;}public double perimeter(){return (getLength()+getWidth())*2;}public double area(){return getLength()*getWidth();}public Rectangle(double length,double width){this.length=length;this.width=width;}public Rectangle(){}}
public class Cuboid extends Rectangle{private double height;public Cuboid(double length,double width,double height){super(length,width);this.height=height;}public double volume(){return getLength()*getWidth()*height;}}
public class CuboidRectangleTest {public static void main(String[] args) {Cuboid C=new Cuboid(10,5,2);System.out.println(C.volume());}}
正在上传…重新上传取消正在上传…重新上传取消正在上传…重新上传取消
4、定义一个表示圆的Circle类,其中有表示半径的double型的属性radius,计算圆周长的perimeter方法和计算面积的area方法。在Circle类的基础上,定义圆柱体Cylinder类和球体Sphere类,它们分别具备计算表面积的area方法和体积的volume方法,编写测试类进行测试。
package com.itheima.demo;public class Circle {private double radius;public double getRadius() {return radius;}public double Perimeter(){return 2*(Math.PI)*radius;}public double area(){return Math.PI*radius*radius;}public Circle(double radius){this.radius=radius;}}
public class Cylinder extends Circle{private double height;public Cylinder(double radius,double height){super(radius);this.height=height;}public double area() {return (Math.PI*getRadius()*getRadius()*2)+(Math.PI*2*getRadius()*height);}public double volume(){return Math.PI*getRadius()*getRadius()*height;}}
public class Sphere extends Circle{public double area(){return Math.PI*4*getRadius()*getRadius();}public double volume(){return (4/3.0)*Math.PI*getRadius()*getRadius()*getRadius();}public Sphere(double radius) {super(radius);}}
public class CircleTest {public static void main(String[] args) {Circle C=new Circle(2);System.out.println("圆");System.out.println("面积:"+C.area());System.out.println("周长:"+C.Perimeter());Cylinder cylinder = new Cylinder(2,3);System.out.println("圆柱体");System.out.println("面积:"+cylinder.area());System.out.println("体积:"+cylinder.volume());Sphere sphere=new Sphere(3);System.out.println("球体");System.out.println("面积:"+sphere.area());System.out.println("体积:"+sphere.volume());}}
正在上传…重新上传取消正在上传…重新上传取消正在上传…重新上传取消
5、编写一个银行账户类,类的构成包括:
(1)数据成员:用户的账号名称、用户的账户余额;
(2)方法包括:开户(设置账户名称及余额),利用构造方法完成;
(3)查询余额。
package com.itheima.demo;public class Bank {private String name;private int account;public Bank(String name,int account){this.name=name;this.account=account;}public void search(){System.out.println("姓名:"+name);System.out.println("账户余额:"+account);}}
public class Banktest {public static void main(String[] args) {Bank bank=new Bank("李四",3000);bank.search();}}
正在上传…重新上传取消正在上传…重新上传取消正在上传…重新上传取消
6、接口应用:接口继承与接口实现
具体要求:
(1)Biology生物接口中定义了breathe()抽象方法;
(2)Animal动物接口继承了Biology接口,增加eat()和sleep()两个抽象方法;
(3)Human人类接口继承了Animal接口,增加think()和learn()两个抽象方法;
(4)定义一个普通人类Person实现Human接口,并进行测试。
package com.itheima.demo;public interface Biology {public void breathe();}
public interface Animal extends Biology{public abstract void eat();public abstract void sleep();}
public interface Human extends Animal {public abstract void think();public abstract void learn();}
public class Person1 implements Human{@Overridepublic void breathe() {System.out.println("呼吸");}@Overridepublic void eat() {System.out.println("吃");}@Overridepublic void sleep() {System.out.println("睡觉");}@Overridepublic void think() {System.out.println("认为");}@Overridepublic void learn() {System.out.println("学习");}}
public class PersonTest {public static void main(String[] args) {Person1 person1=new Person1();person1.breathe();person1.eat();person1.learn();person1.sleep();person1.think();}}
正在上传…重新上传取消正在上传…重新上传取消正在上传…重新上传取消
7、动态多态应用:企业员工工资管理
具体要求:某公司的雇员分为以下若干类:
(1)Employee员工类,包含受保护的属性name(姓名)、birthDate(出生日期),public double getSalary(Employee e)方法(该方法根据传入不同类别的员工,工资计算方法则返回其本月的工资);
(2)SalariedEmployee类继承Employee类,代表领取固定工资的员工,包含私有属性salary(固定工资);
(3)HourlyEmployee类继承Employee类,代表按小时拿工资的员工,包含私有属性hourSalary(每小时的工资)、hours(每月工作的小时数)。工资计算方法:月工资=hourSalary * hours,每月工作超出160小时的部分,按照1.5倍小时工资发放。
(4)SalesEmployee类继承Employee类,代表销售人员,包含受保护的属性sale(月销售额),commissionRate(提成率)。工资计算方法:月工资=sale * commissionRate。
(5)BasePlusSalesEmployee类继承SalesEmployee类,代表有固定底薪的销售人员,工资由底薪加上销售提成部分组成。其私有属性basicSalary(底薪)。工资计算方法:月工资=basicSalary + sale * commissionRate。
(6)根据要求创建SalariedEmployee、HourlyEmployee、SalesEmployee、BasePlusSalesEmployee类的对象各一个,并计算某个月这4个对象的工资。
package com.itheima.demo;public class Employee {protected String name;protected String birthDate;public double getSalary(Employee e){return 0;}public Employee(String name, String birthDate) {this.name = name;this.birthDate = birthDate;}}
public class SalariedEmployee extends Employee {private double salary;public double getSalary(){return salary;}public SalariedEmployee(String name, String birthDate, double salary) {super(name, birthDate);this.salary = salary;}}
public class HourlyEmployee extends Employee {private double hourSalary;private double hours;public double getSalary(){if(hours>160){return hourSalary*160+hourSalary*(hours-160)*1.5;}else {return hourSalary * hours;}}public HourlyEmployee(String name, String birthDate, double hourSalary, double hours) {super(name, birthDate);this.hourSalary = hourSalary;this.hours = hours;}}
public class SalesEmployee extends Employee {protected double sale;protected double commissionRate;public double getSalary(){return sale*commissionRate;}public SalesEmployee(String name, String birthDate, double sale, double commissionRate) {super(name, birthDate);this.sale = sale;this.commissionRate = commissionRate;}}
public class BasePulsSalesEmployee extends SalesEmployee {private double basicSalary;public double getSalary() {return basicSalary+sale*commissionRate;}public BasePulsSalesEmployee(String name, String birthDate, double sale, double commissionRate, double basicSalary) {super(name, birthDate, sale, commissionRate);this.basicSalary = basicSalary;}}
public class EmployeeTest {public static void main(String[] args) {SalariedEmployee E=new SalariedEmployee("张三","2021",20000);System.out.println("固定工资员工:"+E.name+" "+E.birthDate+" "+E.getSalary());HourlyEmployee H=new HourlyEmployee("李四","2001-12-31",20,300);System.out.println("小时工资员工:"+H.name+" "+H.birthDate+" "+H.getSalary());SalesEmployee S=new SalesEmployee("王五","2003-03-01",1000,2.5);System.out.println("销售员工:"+S.name+" "+S.birthDate+" "+S.getSalary());BasePulsSalesEmployee B=new BasePulsSalesEmployee("杨六","2003-08-23",62,1.9,2000);System.out.println("固定底薪销售员工:"+B.name+" "+B.birthDate+" "+B.getSalary());}}
正在上传…重新上传取消正在上传…重新上传取消正在上传…重新上传取消