/*
* 1、创建一个球员类,并且该类最多只允许创建十一个对象。提示利用 static 和 封装性来完成
*/
public class Players {
private static int sum=1;
private Players() {System.out.println("创建了Players对象");}
public static Players create() {
Players p=null;
if(sum<=11) {
p=new Players();
sum++;
}else {
System.out.println("不能再创建对象了");
}
return p;
}
public static void main(String[] args) {
for(int i=0;i<12;i++) {
Players players=Players.create();
}
}
}
运行图:
/*2、设计2个类,要求如下:(知识点:类的继承 方法的覆盖)
2.1 定义一个汽车类Vehicle,
2.1.1 属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型)。
2.1.2 至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。
2.1.3 为属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。
2.1.4 定义一个一般方法run(),用打印语句描述汽车奔跑的功能
2.1.5 在main方法中创建一个品牌为“benz”、颜色为“black”的汽车。*/
public class Vehicle {
protected String brand;
protected String color;
protected double speed=0;
public Vehicle() {}
public Vehicle(String brand,String color) {
this.brand=brand;
this.color=color;
}
void run() {
System.out.println("品牌:"+this.brand+"颜色:"+this.color);
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
}
/*2.Car类定义*/
public class Car extends Vehicle {
private int loader;
public int getLoader() {
return loader;
}
public void setLoader(int loader) {
this.loader = loader;
}
public Car(String brand,String color,int loader) {
this.brand=brand;
this.color=color;
this.loader=loader;
}
void run() {
System.out.println("品牌:"+this.brand+"颜色:"+this.color+"载人数:"+loader);
}
}
在测试类的主函数中加入如下代码
public class Test {
public static void main(String[] args) {
Vehicle v=new Vehicle("benz","black");
v.run();
Car c=new Car("Honda","red",2);
c.run();
}
}
运行图:
/*3、设计三个类,分别如下:(知识点:抽象类及抽象方法)
3.1 设计Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。*/
public abstract class Shape {
protected String color;
protected double area;
protected double per;
public Shape() {}
public Shape(String color) {
this.color=color;
}
public abstract double getArea();
public abstract double getPer();
public abstract String showAll();
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
/*public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
public double getPer() {
return per;
}
public void setPer(double per) {
this.per = per;
}*/
}
/*3.2 设计 2个子类:
3.2.1 Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPer、getArea和showAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。*/
public class Rectangle extends Shape {
private double width;
private double length;
public Rectangle() {}
public Rectangle(double width,double length,String color) {
this.width=width;
this.length=length;
this.color=color;
}
@Override
public double getArea() {
return width*length;
}
@Override
public double getPer() {
return (width+length)*2;
}
@Override
public String showAll() {
return "Rectangle [width:"+width+",length:"+length+",area:"+getArea()+",per:"+getPer()+",color:"+this.color+"]";
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
}
/*3.2.2 Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和showAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)*/
public class Circle extends Shape {
private double radius;
public Circle() {}
public Circle(double radius,String color) {
this.radius=radius;
this.color=color;
}
@Override
public double getArea() {
return Math.PI*radius*radius;
}
@Override
public double getPer() {
return Math.PI*2*radius;
}
@Override
public String showAll() {
return "Circle [radius:"+radius+",area:"+getArea()+",per:"+getPer()+",color:"+this.color+"]";
}
}
/*3.3 在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。*/
public class TestShape {
public static void main(String[] args) {
Shape s1=new Rectangle(8.0,13.0,"blue");
System.out.println(s1.showAll());
Shape s2=new Circle(8.0,"green");
System.out.println(s2.showAll());
}
}
运行图:
/*4、 Cola公司的雇员分为以下若干类:(知识点:多态)
4.1 ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。*/
public abstract class ColaEmployee {
protected String name;
protected int month;
protected double monthSalary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public double getMonthSalary() {
return monthSalary;
}
public void setMonthSalary(double monthSalary) {
this.monthSalary = monthSalary;
}
public abstract double getSalary(int month);
}
/*4.2 SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。属性:月薪**/
public class SalariedEmployee extends ColaEmployee {
private double monthsal;
public double getMonthsal() {
return monthsal;
}
public void setMonthsal(double monthsal) {
this.monthsal = monthsal;
}
@Override
public String toString() {
return "SalariedEmployee [name="+ name +",monthsal=" + monthsal
+",salary="+this.monthSalary +"]\n";
}
@Override
public double getSalary(int month) {
if(this.month==month)
{
this.monthSalary+=100;
}
this.monthSalary+=monthsal;
return this.monthSalary;
}
}
/*4.3 HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数*/
public class HourEmplyees extends ColaEmployee {
private double hourSalary;
private int hours;
public double getHourSalary() {
return hourSalary;
}
public void setHourSalary(double hourSalary) {
this.hourSalary = hourSalary;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
@Override
public String toString() {
return "HourlyEmployee [name="+ name +",hourSalary=" + hourSalary + ", hours=" + hours
+",salary="+this.monthSalary +"]\n";
}
@Override
public double getSalary(int month) {
if(month==this.month)
{
this.monthSalary+=100;
}
if(this.hours>160)
{
this.monthSalary+=hourSalary*160+hourSalary*(this.hours-160)*1.5;
}else
{
this.monthSalary+=hourSalary*this.hours;
}
return this.monthSalary;
}
}
/*4.4 SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率*/
public class SalesEmployee extends ColaEmployee {
private double monthSales;
private double rate;
public double getMonthSales() {
return monthSales;
}
public void setMonthSales(double monthSales) {
this.monthSales = monthSales;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
@Override
public String toString() {
return "SalesEmployee [name="+ name +",monthSales=" + monthSales + ", rate=" + rate
+",salary="+this.monthSalary +"]\n";
}
@Override
public double getSalary(int month) {
if(this.month==month)
{
this.monthSalary+=100;
}
this.monthSalary+=this.monthSales*this.rate;
return this.monthSalary;
}
}
/*4.5 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,*/
import java.util.Arrays;
public class Company {
public void printSalary(ColaEmployee[] emps)
{
System.out.println(Arrays.toString(emps));
}
}
/*写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。*/
public class TestCompany {
public static void main(String[] args) {
ColaEmployee[] emps1=new HourEmplyees[3];
for (int i = 1; i <= emps1.length; i++) {
HourEmplyees e=new HourEmplyees();
e.setHours(158+i);
e.setHourSalary(30.0);
e.setMonth(i);
e.setName("员工"+i);
e.getSalary(8);//都算8月份的工资
emps1[i-1]=e;
}
ColaEmployee[] emps2=new SalesEmployee[5];
for (int i = 1; i <= emps2.length; i++) {
SalesEmployee e=new SalesEmployee();
e.setName("员工"+(i+3));
e.setMonth(i);
e.setMonthSales(20000.00+i);
e.setRate(0.2);
e.getSalary(8);
emps2[i-1]=e;
}
ColaEmployee[] emps4=new SalariedEmployee[2];
for (int i = 1; i <= emps4.length; i++) {
SalariedEmployee e=new SalariedEmployee();
e.setName("员工"+(i+8));
e.setMonth(i);
e.setMonthsal(20000.00+i);
e.getSalary(8);
emps4[i-1]=e;
}
//把三个数组合成一个数组
ColaEmployee[] emps3=new ColaEmployee[20];
System.arraycopy(emps1, 0, emps3, 0, emps1.length);
System.arraycopy(emps2, 0, emps3, emps1.length, emps2.length);
System.arraycopy(emps4, 0, emps3, emps1.length+emps2.length, emps4.length);
new Company().printSalary(emps3);
}
}
运行图:
/*5、利用接口实现动态的创建对象*/
public interface Fruits {
}
/*5.1 创建4个类:
苹果
5.2 在三种水果的构造方法中打印一句话.
以苹果类为例*/
public class Apple implements Fruits {
public Apple() {
System.out.println("创建了一个Apple对象");
}
}
/*香蕉*/
public class Bananas implements Fruits {
public Bananas() {
System.out.println("创建了一个Bananas对象");
}
}
/*葡萄*/
public class Grape implements Fruits {
public Grape() {
System.out.println("创建了一个Grape对象");
}
}
/*园丁
5.3 要求从控制台输入一个字符串,根据字符串的值来判断创建三种水果中哪个类的对象*/
import java.util.Scanner;
public class Gardener {
public static void main(String[] args) {
Gardener g=new Gardener();
g.create();
}
public Fruits create() {
Scanner scan=new Scanner(System.in);
String idx=scan.next();
Fruits fruits=null;
switch(idx) {
case "apple":
fruits=new Apple();
break;
case "banana":
fruits=new Bananas();
break;
case "grape":
fruits=new Grape();
break;
}
return fruits;
}
}
运行图:
/*6、编写三个系别的学生类:英语系,计算机系,文学系(要求通过继承学生类) [选做题]
6.1各系有以下成绩:
英语系: 演讲,期末考试,期中考试;
计算机系:操作能力,英语写作,期中考试,期末考试;
文学系: 演讲,作品,期末考试,期中考试;
6.2各系总分评测标准:
英语系: 演讲 50%
期末考试 25%
期中考试 25%
计算机系: 操作能力 40%
英语写作 20%
期末考试 20%
期中考试 20%
文学系: 演讲 35%
作品 35%
期末考试 15%
期中考试 15%
6.3定义一个可容纳5个学生的学生类数组,使用随机数给该数组装入各系学生的对象,然后按如下格式输出数组中的信息:
学号:XXXXXXXX 姓名:XXX 性别:X 年龄:XX 综合成绩:XX*/
public class Student {
int no;
String name;
char sex;
int age;
double score;
public Student(int no, String name, char sex, int age, double score) {
this.no = no;
this.name = name;
this.sex = sex;
this.age = age;
this.score = score;
}
public void Show() {
System.out.println(
"学号:" + this.no + " 姓名:" + this.name + " 性别:" + this.sex + " 年龄:" + this.age + " 综合成绩:" + this.score);
}
}
/*英语系: 演讲,期末考试,期中考试;*/
public class English extends Student {
public English(int no, String name, char sex, int age, double speechScore, double middleScore, double finalScore) {
super(no, name, sex, age, (0.5 * speechScore + 0.25 * middleScore + 0.25 * finalScore));
}
}
/*计算机系:操作能力,英语写作,期中考试,期末考试;*/
public class Computer extends Student {
public Computer(int no, String name, char sex, int age, double operateScore, double writeScore, double middleScore,
double finalScore) {
super(no, name, sex, age, (operateScore * 0.4 + writeScore * 0.2 + 0.2 * middleScore + 0.2 * finalScore));
}
}
/*文学系: 演讲,作品,期末考试,期中考试;*/
public class Chinese extends Student {
public Chinese(int no, String name, char sex, int age, double speechScore, double articleScore, double middleScore,
double finalScore) {
super(no, name, sex, age, (0.35 * speechScore + 0.35 * articleScore + 0.15 * middleScore + 0.15 * finalScore));
}
}
/*6.3定义一个可容纳5个学生的学生类数组,使用随机数给该数组装入各系学生的对象,然后按如下格式输出数组中的信息:
学号:XXXXXXXX 姓名:XXX 性别:X 年龄:XX 综合成绩:XX*/
public class TestStu {
public static void main(String[] args) {
Student[] stu = { new English(100, "李四", '男', 20, 80, 90, 90), new Computer(101, "王五", '男', 40, 20, 20, 20, 20),
new Chinese(102, "張三", '男', 30, 80, 100, 95, 95) };
Student[] stu1 = new Student[stu.length];
int[] t = { 1, 2, 3 };
for (int i = 0; i < stu1.length; i++) {
int num = (int) (Math.random() * stu.length);
while (stu[num] == null) {
num = (int) (Math.random() * stu.length);
}
stu1[i] = stu[num];
stu[num] = null;
}
for (int i = 0; i < stu1.length; i++) {
stu1[i].Show();
}
}
}
运行图:
说明比较详细的答案可以参考:https://blog.csdn.net/qq_37067955/article/details/81782571