/*
* 1帆疟、創(chuàng)建一個(gè)球員類,并且該類最多只允許創(chuàng)建十一個(gè)對(duì)象鞍时。提示利用 static 和 封裝性來(lái)完成
*/
public class Players {
private static int sum=1;
private Players() {System.out.println("創(chuàng)建了Players對(duì)象");}
public static Players create() {
Players p=null;
if(sum<=11) {
p=new Players();
sum++;
}else {
System.out.println("不能再創(chuàng)建對(duì)象了");
}
return p;
}
public static void main(String[] args) {
for(int i=0;i<12;i++) {
Players players=Players.create();
}
}
}
運(yùn)行圖:
/*2笙纤、設(shè)計(jì)2個(gè)類宵蛀,要求如下:(知識(shí)點(diǎn):類的繼承 方法的覆蓋)
2.1? 定義一個(gè)汽車類Vehicle,
2.1.1 屬性包括:汽車品牌brand(String類型)瘟斜、顏色color(String類型)和速度speed(double類型)缸夹。
2.1.2 至少提供一個(gè)有參的構(gòu)造方法(要求品牌和顏色可以初始化為任意值,但速度的初始值必須為0)螺句。
2.1.3 為屬性提供訪問(wèn)器方法虽惭。注意:汽車品牌一旦初始化之后不能修改。
2.1.4 定義一個(gè)一般方法run()蛇尚,用打印語(yǔ)句描述汽車奔跑的功能
2.1.5 在main方法中創(chuàng)建一個(gè)品牌為“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+"載人數(shù):"+loader);
}
}
在測(cè)試類的主函數(shù)中加入如下代碼
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();
}
}
運(yùn)行圖:
/*3、設(shè)計(jì)三個(gè)類匆笤,分別如下:(知識(shí)點(diǎn):抽象類及抽象方法)
3.1 設(shè)計(jì)Shape表示圖形類研侣,有面積屬性area、周長(zhǎng)屬性per炮捧,顏色屬性color庶诡,有兩個(gè)構(gòu)造方法(一個(gè)是默認(rèn)的、一個(gè)是為顏色賦值的)咆课,還有3個(gè)抽象方法末誓,分別是:getArea計(jì)算面積、getPer計(jì)算周長(zhǎng)傀蚌、showAll輸出所有信息基显,還有一個(gè)求顏色的方法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 設(shè)計(jì) 2個(gè)子類:
3.2.1? Rectangle表示矩形類善炫,增加兩個(gè)屬性撩幽,Width表示長(zhǎng)度、height表示寬度箩艺,重寫getPer窜醉、getArea和showAll三個(gè)方法,另外又增加一個(gè)構(gòu)造方法(一個(gè)是默認(rèn)的艺谆、一個(gè)是為高度榨惰、寬度、顏色賦值的)静汤。*/
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個(gè)屬性,radius表示半徑虫给,重寫getPer藤抡、getArea和showAll三個(gè)方法,另外又增加兩個(gè)構(gòu)造方法(為半徑抹估、顏色賦值的)*/
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方法中缠黍,聲明創(chuàng)建每個(gè)子類的對(duì)象,并調(diào)用2個(gè)子類的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());
}
}
運(yùn)行圖:
/*4瓷式、 Cola公司的雇員分為以下若干類:(知識(shí)點(diǎn):多態(tài))?
4.1? ColaEmployee :這是所有員工總的父類,屬性:?jiǎn)T工的姓名,員工的生日月份语泽。方法:getSalary(int month) 根據(jù)參數(shù)月份來(lái)確定工資贸典,如果該月員工過(guò)生日,則公司會(huì)額外獎(jiǎng)勵(lì)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 的子類,按小時(shí)拿工資的員工蔬充,每月工作超出160 小時(shí)的部分按照1.5 倍工資發(fā)放蝶俱。屬性:每小時(shí)的工資、每月工作的小時(shí)數(shù)*/
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? 定義一個(gè)類Company积蜻,在該類中寫一個(gè)方法,調(diào)用該方法可以打印出某月某個(gè)員工的工資數(shù)額彻消,*/
import java.util.Arrays;
public class Company {
public void printSalary(ColaEmployee[] emps)
{
System.out.println(Arrays.toString(emps));
}
}
/*寫一個(gè)測(cè)試類TestCompany,在main方法竿拆,把若干各種類型的員工放在一個(gè)ColaEmployee 數(shù)組里,并單元出數(shù)組中每個(gè)員工當(dāng)月的工資宾尚。*/
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;
}
//把三個(gè)數(shù)組合成一個(gè)數(shù)組
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);
}
}
運(yùn)行圖:
/*5丙笋、利用接口實(shí)現(xiàn)動(dòng)態(tài)的創(chuàng)建對(duì)象*/
public interface Fruits {
}
/*5.1? 創(chuàng)建4個(gè)類:
蘋果
5.2? 在三種水果的構(gòu)造方法中打印一句話.
以蘋果類為例*/
public class Apple implements Fruits {
public Apple() {
System.out.println("創(chuàng)建了一個(gè)Apple對(duì)象");
}
}
/*香蕉*/
public class Bananas implements Fruits {
public Bananas() {
System.out.println("創(chuàng)建了一個(gè)Bananas對(duì)象");
}
}
/*葡萄*/
public class Grape implements Fruits {
public Grape() {
System.out.println("創(chuàng)建了一個(gè)Grape對(duì)象");
}
}
/*園丁
5.3 要求從控制臺(tái)輸入一個(gè)字符串,根據(jù)字符串的值來(lái)判斷創(chuàng)建三種水果中哪個(gè)類的對(duì)象*/
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;
}
}
運(yùn)行圖:
/*6煌贴、編寫三個(gè)系別的學(xué)生類:英語(yǔ)系御板,計(jì)算機(jī)系,文學(xué)系(要求通過(guò)繼承學(xué)生類) [選做題]
6.1各系有以下成績(jī):
??英語(yǔ)系:?演講牛郑,期末考試怠肋,期中考試;
??計(jì)算機(jī)系:操作能力淹朋,英語(yǔ)寫作笙各,期中考試,期末考試础芍;
??文學(xué)系:?演講杈抢,作品,期末考試者甲,期中考試;
?6.2各系總分評(píng)測(cè)標(biāo)準(zhǔn):
??? 英語(yǔ)系:????演講 50%
???????????????? 期末考試 25%
???????????????? 期中考試 25%
??? 計(jì)算機(jī)系:? 操作能力?40%
??????????????? 英語(yǔ)寫作??20%
??????????????? 期末考試??20%
??????????????? 期中考試?20%
文學(xué)系:??演講??35%
?????????????? 作品??35%
?????????????? 期末考試?15%
?????????????? 期中考試??15%
6.3定義一個(gè)可容納5個(gè)學(xué)生的學(xué)生類數(shù)組春感,使用隨機(jī)數(shù)給該數(shù)組裝入各系學(xué)生的對(duì)象砌创,然后按如下格式輸出數(shù)組中的信息:
學(xué)號(hào):XXXXXXXX 姓名:XXX 性別:X 年齡:XX 綜合成績(jī):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(
"學(xué)號(hào):" + this.no + " 姓名:" + this.name + " 性別:" + this.sex + " 年齡:" + this.age + " 綜合成績(jī):" + this.score);
}
}
/*英語(yǔ)系: 演講虏缸,期末考試,期中考試嫩实;*/
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));
}
}
/*計(jì)算機(jī)系:操作能力刽辙,英語(yǔ)寫作,期中考試甲献,期末考試宰缤;*/
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));
}
}
/*文學(xué)系: 演講,作品,期末考試慨灭,期中考試;*/
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定義一個(gè)可容納5個(gè)學(xué)生的學(xué)生類數(shù)組朦乏,使用隨機(jī)數(shù)給該數(shù)組裝入各系學(xué)生的對(duì)象,然后按如下格式輸出數(shù)組中的信息:
學(xué)號(hào):XXXXXXXX 姓名:XXX 性別:X 年齡:XX 綜合成績(jī):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();
}
}
}
運(yùn)行圖:
說(shuō)明比較詳細(xì)的答案可以參考:https://blog.csdn.net/qq_37067955/article/details/81782571