Java面向?qū)ο蟾呒?jí)特性習(xí)題

/*

* 1、創(chuàng)建一個(gè)球員類,并且該類最多只允許創(chuàng)建十一個(gè)對(duì)象。提示利用 static 和 封裝性來完成

*/

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 為屬性提供訪問器方法。注意:汽車品牌一旦初始化之后不能修改。

2.1.4 定義一個(gè)一般方法run(),用打印語句描述汽車奔跑的功能

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);

}

}

在測試類的主函數(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、周長屬性per,顏色屬性color,有兩個(gè)構(gòu)造方法(一個(gè)是默認(rèn)的、一個(gè)是為顏色賦值的),還有3個(gè)抽象方法,分別是:getArea計(jì)算面積、getPer計(jì)算周長、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表示長度、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 :這是所有員工總的父類,屬性:員工的姓名,員工的生日月份。方法:getSalary(int month) 根據(jù)參數(shù)月份來確定工資,如果該月員工過生日,則公司會(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è)測試類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ù)字符串的值來判斷創(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é)生類:英語系,計(jì)算機(jī)系,文學(xué)系(要求通過繼承學(xué)生類) [選做題]

6.1各系有以下成績:

??英語系:?演講,期末考試,期中考試;

??計(jì)算機(jī)系:操作能力,英語寫作,期中考試,期末考試;

??文學(xué)系:?演講,作品,期末考試,期中考試;

?6.2各系總分評(píng)測標(biāo)準(zhǔn):

??? 英語系:????演講 50%

???????????????? 期末考試 25%

???????????????? 期中考試 25%

??? 計(jì)算機(jī)系:? 操作能力?40%

??????????????? 英語寫作??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 綜合成績: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 + " 綜合成績:" + 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));

}

}

/*計(jì)算機(jī)系:操作能力,英語寫作,期中考試,期末考試;*/

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 綜合成績: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)行圖:

說明比較詳細(xì)的答案可以參考:https://blog.csdn.net/qq_37067955/article/details/81782571

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子長到第三個(gè)月后每個(gè)月又生一對(duì)兔...
    開心的鑼鼓閱讀 3,393評(píng)論 0 9
  • 50道經(jīng)典Java編程練習(xí)題,將數(shù)學(xué)思維運(yùn)用到編程中來。抱歉哈找不到文章的原貼了,有冒犯的麻煩知會(huì)聲哈~ 1.指數(shù)...
    OSET我要編程閱讀 7,284評(píng)論 0 9
  • Java經(jīng)典問題算法大全 /*【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子...
    趙宇_阿特奇閱讀 2,075評(píng)論 0 2
  • DAY 05 1、 public classArrayDemo { public static void mai...
    周書達(dá)閱讀 822評(píng)論 0 0
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,142評(píng)論 0 2

友情鏈接更多精彩內(nèi)容