這次的練習(xí)主要是一些類的高階操作,像繼承、接口和內(nèi)部類這些,但其實還是挺簡單的
?
目錄
7-1 jmu-Java-03面向?qū)ο蠡A(chǔ)-04-形狀-繼承
前言
本題描述
思考
輸入樣例:
輸出樣例:
?7-3 jmu-Java-04面向?qū)ο筮M階-03-接口-自定義接口ArrayIntegerStack
main方法說明
思考
輸入樣例
輸出樣例
?7-3 教師類
輸入格式:
輸出格式:
輸入樣例:
輸出樣例:
?7-4 教師類-2
輸入格式:
輸出格式:
輸入樣例:
輸出樣例:
?7-5 編寫一個類Shop(商店)、內(nèi)部類InnerCoupons(內(nèi)部購物券)
輸入格式:
輸出格式:
輸入樣例:
輸出樣例:
7-6 jmu-Java-04面向?qū)ο筮M階-01-接口-Comparable
1.編寫PersonSortable類
2.main方法中
輸入樣例:
輸出樣例:
?7-7 jmu-Java-03面向?qū)ο?06-繼承覆蓋綜合練習(xí)-Person、Student、Employee、Company
main方法說明
輸入樣例:
輸出樣例:
?7-8 定義接口(Biology、Animal)、類(Person)、子類(Pupil)
輸入格式:
輸出格式:
輸入樣例:
輸出樣例:
7-9 Employee類的層級結(jié)構(gòu)
輸入格式:
輸出格式:
輸入樣例:
輸出樣例:
7-1 jmu-Java-03面向?qū)ο蠡A(chǔ)-04-形狀-繼承
前言
前面題目形狀中我們看到,為了輸出所有形狀的周長與面積,需要建立多個數(shù)組進行多次循環(huán)。這次試驗使用繼承與多態(tài)來改進我們的設(shè)計。
本題描述
1.定義抽象類Shape
屬性:不可變靜態(tài)常量double PI
,值為3.14
,
抽象方法:public double getPerimeter()
,public double getArea()
2.Rectangle與Circle類均繼承自Shape類。
Rectangle類(屬性:int width,length)、Circle類(屬性:int radius)。
帶參構(gòu)造方法為Rectangle(int width,int length)
,Circle(int radius)
。toString
方法(Eclipse自動生成)
3.編寫double sumAllArea
方法計算并返回傳入的形狀數(shù)組中所有對象的面積和與double sumAllPerimeter
方法計算并返回傳入的形狀數(shù)組中所有對象的周長和。
4.main方法
4.1 輸入整型值n,然后建立n個不同的形狀。如果輸入rect,則依次輸入寬、長。如果輸入cir,則輸入半徑。
4.2 然后輸出所有的形狀的周長之和,面積之和。并將所有的形狀信息以樣例的格式輸出。?提示:使用Arrays.toString
。
4.3 最后輸出每個形狀的類型與父類型.使用類似shape.getClass()
?//獲得類型,?shape.getClass().getSuperclass()
?//獲得父類型;
注意:處理輸入的時候使用混合使用nextInt
與nextLine
需注意行尾回車換行問題。
思考
- 你覺得sumAllArea和sumAllPerimeter方法放在哪個類中更合適?
- 是否應(yīng)該聲明為static?
輸入樣例:
4
rect
3 1
rect
1 5
cir
1
cir
2
輸出樣例:
38.84
23.700000000000003
[Rectangle [width=3, length=1], Rectangle [width=1, length=5], Circle [radius=1], Circle [radius=2]]
class Rectangle,class Shape
class Rectangle,class Shape
class Circle,class Shape
class Circle,class Shape
?
import java.math.*;
import java.util.*;
abstract class Shape{
final double PI = 3.14;
public abstract double getPerimeter();
public abstract double getArea();
}
class Rectangle extends Shape{
private int length = 0;
private int width = 0;
public Rectangle(int _length,int _width){
this.length = _length;
this.width = _width;
}
public double getArea(){
return this.width * this.length;
}
public double getPerimeter(){
return (this.width + this.length)*2;
}
public String toString(){
return "Rectangle [width=" + this.length + ", length=" + this.width + "]";
}
}
class Circle extends Shape{
private int radius = 0;
public Circle(int _radius){
this.radius = _radius;
}
public double getPerimeter(){
return this.radius * PI * 2;
}
public double getArea(){
return this.radius * PI * this.radius;
}
public String toString(){
return "Circle [radius=" + this.radius + "]";
}
}
public class Main {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int n = in.nextInt();
ArrayList<Shape> shape = new ArrayList<Shape>();
double sumArea = 0;
double sumPerimeter = 0;
for(int i=0;i<n;i++){
String op = in.next();
if(op.equals("rect")){
int width = in.nextInt();
int length = in.nextInt();
shape.add(new Rectangle(width,length));
}
if(op.equals("cir")){
int radius = in.nextInt();
shape.add(new Circle(radius));
}
}
for(int i=0;i<n;i++){
sumArea += shape.get(i).getArea();
sumPerimeter += shape.get(i).getPerimeter();
}
System.out.println(sumPerimeter);
System.out.println(sumArea);
System.out.println(shape.toString());
for(int i=0;i<n;i++){
System.out.println(shape.get(i).getClass() + "," + shape.get(i).getClass().getSuperclass());
}
}
}
?7-3 jmu-Java-04面向?qū)ο筮M階-03-接口-自定義接口ArrayIntegerStack
定義IntegerStack
接口,用于聲明一個存放Integer元素的棧的常見方法:
public Integer push(Integer item); //如果item為null,則不入棧直接返回null。如果棧滿,也返回null。如果插入成功,返回item。 public Integer pop(); //出棧,如果為空,則返回null。出棧時只移動棧頂指針,相應(yīng)位置不置為null public Integer peek(); //獲得棧頂元素,如果為空,則返回null. public boolean empty(); //如果為空返回true public int size(); //返回棧中元素個數(shù)
定義IntegerStack的實現(xiàn)類ArrayIntegerStack
,內(nèi)部使用數(shù)組實現(xiàn)。創(chuàng)建時,可指定內(nèi)部數(shù)組大小。
main方法說明
- 輸入n,建立可包含n個元素的ArrayIntegerStack對象
- 輸入m個值,均入棧。每次入棧均打印入棧返回結(jié)果。
- 輸出棧頂元素,輸出是否為空,輸出size
- 使用Arrays.toString()輸出內(nèi)部數(shù)組中的值。
- 輸入x,然后出棧x次,每次出棧均打印。
- 輸出棧頂元素,輸出是否為空,輸出size
- 使用Arrays.toString()輸出內(nèi)部數(shù)組中的值。
思考
如果IntegerStack接口的實現(xiàn)類內(nèi)部使用ArrayList來存儲元素,怎么實現(xiàn)?測試代碼需要進行什么修改?
輸入樣例
5
3
1 2 3
2
輸出樣例
1
2
3
3,false,3
[1, 2, 3, null, null]
3
2
1,false,1
[1, 2, 3, null, null]
import java.math.*;
import java.util.*;
interface IntegerStack{
public Integer push(Integer item);
public Integer pop();
public Integer peek();
public boolean empty();
public int size();
}
class ArrayIntegerStack implements IntegerStack{
Integer[] array;
int len = 0;
int length = 0;
public ArrayIntegerStack(int _len){
this.array = new Integer[_len];
this.length = _len;
}
public Integer push(Integer item){
if(this.array == null){
return null;
}
if(this.len == this.length){
return null;
}
array[this.len] = item;
this.len++;
return item;
}
public Integer pop(){
if(this.len == 0){
return null;
}
this.len--;
return this.array[this.len];
}
public Integer peek(){
if(this.len == 0){
return null;
}
return this.array[this.len-1];
}
public boolean empty(){
if(this.len == 0){
return true;
}
return false;
}
public int size(){
return this.len;
}
public String toString(){
String res = "[";
for(int i=0;i<length - 1;i++){
res += this.array[i];
res += ", ";
}
res += this.array[this.length - 1];
res += "]";
return res;
}
}
public class Main {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int n = in.nextInt();
ArrayIntegerStack ais = new ArrayIntegerStack(n);
int m = in.nextInt();
for(int i=0;i<m;i++){
int tmp = in.nextInt();
System.out.println(ais.push(tmp));
}
System.out.println(ais.peek() + "," + ais.empty() + "," + ais.size());
System.out.println(ais.toString());
int x = in.nextInt();
for(int i=0;i<x;i++){
System.out.println(ais.pop());
}
System.out.println(ais.peek() + "," + ais.empty() + "," + ais.size());
System.out.println(ais.toString());
}
}
?7-3 教師類
?
設(shè)計一個教師類Teacher,要求:
屬性有編號(int no)、姓名(String name)、年齡(int age)、所屬學(xué)院(String seminary),為這些屬性設(shè)置相應(yīng)的get和set方法。
為Teacher類重寫equals方法,要求:當(dāng)兩個教師對象的no相同時返回true。
重寫Teacher類的toString方法,通過該方法可以返回“no:?, name:, age: **, seminary: **”形式的字符串。
輸入格式:
兩個教師對象的編號,姓名,年齡,學(xué)院
輸出格式:
教師的信息
兩個教師是否相等
輸入樣例:
在這里給出一組輸入。例如:
1 Linda 38 SoftwareEngineering
2 Mindy 27 ComputerScience
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
no: 1, name:Linda, age: 38, seminary: SoftwareEngineering
no: 2, name:Mindy, age: 27, seminary: ComputerScience
false
?
import java.math.*;
import java.util.*;
class Teacher{
private int no;
private String name;
private int age;
private String seminary;
public Teacher(int _no,String _name,int _age,String _seminary){
this.age = _age;
this.name = _name;
this.no = _no;
this.seminary = _seminary;
}
public boolean equals(Teacher a){
return this.age == a.age && this.no == a.no && this.seminary.equals(a.seminary) && this.name.equals(a.name);
}
public String toString(){
return "no: " + this.no + ", name:" + this.name + ", age: " + this.age + ", seminary: " + this.seminary;
}
}
public class Main {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int no = in.nextInt();
String name = in.next();
int age = in.nextInt();
String seminary = in.next();
Teacher t1 = new Teacher(no,name,age,seminary);
no = in.nextInt();
name = in.next();
age = in.nextInt();
seminary = in.next();
Teacher t2 = new Teacher(no,name,age,seminary);
System.out.println(t1.toString());
System.out.println(t2);
System.out.println(t1.equals(t2));
}
}
?7-4 教師類-2
修改題目143
- 修改教師類,使得由多個Teacher對象所形成的數(shù)組可以排序(編號由低到高排序),并在main函數(shù)中使用Arrays.sort(Object[] a)方法排序
- 定義一個類TeacherManagement,包含教師數(shù)組,提供方法add(Teacher[]),使其可以添加教師,提供重載方法search,方法可以在一組給定的教師中,根據(jù)姓名或年齡返回等于指定姓名或年齡的教師的字符串信息,信息格式為:“no:?, name:, age: **, seminary: **”。如果沒有滿足條件的教師,則返回“no such teacher”。
輸入格式:
教師個數(shù)
教師信息
待查找教師的姓名
待查找教師的年齡
輸出格式:
排序后的信息
按姓名查找的老師信息
按年齡查找的老師信息
輸入樣例:
在這里給出一組輸入。例如:
4
3 Linda 38 SoftwareEngineering
1 Mindy 27 ComputerScience
4 Cindy 28 SoftwareEngineering
2 Melody 27 ComputerScience
Cindy
27
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
no: 1, name: Mindy, age: 27, seminary: ComputerScience
no: 2, name: Melody, age: 27, seminary: ComputerScience
no: 3, name: Linda, age: 38, seminary: SoftwareEngineering
no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering
search by name:
no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering
search by age:
no: 1, name: Mindy, age: 27, seminary: ComputerScience
no: 2, name: Melody, age: 27, seminary: ComputerScience
import java.math.*;
import java.util.*;
class Teacher{
private int no;
private String name;
private int age;
private String seminary;
public Teacher(int _no,String _name,int _age,String _seminary){
this.age = _age;
this.name = _name;
this.no = _no;
this.seminary = _seminary;
}
public int getAge(){
return this.age;
}
public String getName(){
return this.name;
}
public int getNo(){
return this.no;
}
public boolean equals(Teacher a){
return this.age == a.age && this.no == a.no && this.seminary.equals(a.seminary) && this.name.equals(a.name);
}
public String toString(){
return "no: " + this.no + ", name: " + this.name + ", age: " + this.age + ", seminary: " + this.seminary;
}
}
class TeacherManagement{
public ArrayList<Teacher> teachers = new ArrayList<Teacher>();
public void add(Teacher t){
this.teachers.add(t);
}
public void search(int _age){
int flag = 1;
System.out.println("search by age:");
for(int i=0;i<teachers.size();i++){
if(_age == teachers.get(i).getAge()){
flag = 0;
System.out.println(teachers.get(i));
}
}
if(flag == 1){
System.out.println("no such teacher");
}
}
public void search(String _name){
int flag = 1;
System.out.println("search by name:");
for(int i=0;i<teachers.size();i++){
if(_name.equals(teachers.get(i).getName())){
flag = 0;
System.out.println(teachers.get(i));
}
}
if(flag == 1){
System.out.println("no such teacher");
}
}
public void sort(){
for(int i=0;i<teachers.size();i++){
for(int j=0;j<teachers.size()-1;j++){
if(teachers.get(i).getNo() < teachers.get(j).getNo()){
Teacher tmp = teachers.get(i);
teachers.set(i,teachers.get(j));
teachers.set(j,tmp);
}
}
}
}
}
public class Main {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int num = in.nextInt();
TeacherManagement tm = new TeacherManagement();
for(int i=0;i<num;i++){
int no = in.nextInt();
String name = in.next();
int age = in.nextInt();
String seminary = in.next();
Teacher tmp = new Teacher(no,name,age,seminary);
tm.add(tmp);
}
tm.sort();
for(int i=0;i<tm.teachers.size();i++){
System.out.println(tm.teachers.get(i));
}
String s = in.next();
int nm = 0;
try{
nm = Integer.valueOf(s);
tm.search(nm);
} catch (Exception e){
tm.search(s);
}
s = in.next();
try{
nm = Integer.valueOf(s);
tm.search(nm);
} catch (Exception e){
tm.search(s);
}
}
}
?7-5 編寫一個類Shop(商店)、內(nèi)部類InnerCoupons(內(nèi)部購物券)
?
編寫一個類Shop
(商店),該類中有一個成員內(nèi)部類InnerCoupons
(內(nèi)部購物券),可以用于購買該商店的牛奶(假設(shè)每箱牛奶售價為50元)。要求如下:
(1)Shop類中有私有屬性milkCount
(牛奶的箱數(shù),int類型)、公有的成員方法setMilkCount( )
和getMilkCount( )
分別用于設(shè)置和獲取牛奶的箱數(shù)。
(2)成員內(nèi)部類InnerCoupons,有公有屬性value
(面值,int類型),一個帶參數(shù)的構(gòu)造方法可以設(shè)定購物券的面值value,一個公有的成員方法buy( )
要求輸出使用了面值為多少的購物券進行支付,同時使商店牛奶的箱數(shù)減少value/50。
(3)Shop類中還有成員變量coupons50
(面值為50元的內(nèi)部購物券,類型為InnerCoupons)、coupons100
(面值為100元的內(nèi)部購物券,類型為InnerCoupons)。
(4)在Shop類的構(gòu)造方法中,調(diào)用內(nèi)部類InnerCoupons的帶參數(shù)的構(gòu)造方法分別創(chuàng)建上面的購物券coupons50、coupons100。
在測試類Main
中,創(chuàng)建一個Shop類的對象myshop,從鍵盤輸入一個整數(shù)(大于或等于3),將其設(shè)置為牛奶的箱數(shù)。假定有顧客分別使用了該商店面值為50的購物券、面值為100的購物券各消費一次,分別輸出消費后商店剩下的牛奶箱數(shù)。
輸入格式:
輸入一個大于或等于3的整數(shù)。
輸出格式:
使用了面值為50的購物券進行支付
牛奶還剩XX箱
使用了面值為100的購物券進行支付
牛奶還剩XX箱
輸入樣例:
在這里給出一組輸入。例如:
5
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
使用了面值為50的購物券進行支付
牛奶還剩4箱
使用了面值為100的購物券進行支付
牛奶還剩2箱
import java.math.*;
import java.util.*;
class Shop{
private int milkCount;
public InnerCoupons coupons50 = new InnerCoupons(50);
public InnerCoupons coupons100 = new InnerCoupons(100);
public void setMilkCount(int nums){
this.milkCount = nums;
}
public int getMilkCount(){
return this.milkCount;
}
public Shop(int nums){
this.milkCount = nums;
}
public class InnerCoupons{
public int value;
public InnerCoupons(int _value){
this.value = _value;
}
public void buy(){
System.out.println("使用了面值為" + value + "的購物券進行支付");
milkCount -= value/50;
System.out.println("牛奶還剩" + milkCount + "箱");
}
}
}
public class Main {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int n = in.nextInt();
Shop shop = new Shop(n);
shop.coupons50.buy();
shop.coupons100.buy();
}
}
7-6 jmu-Java-04面向?qū)ο筮M階-01-接口-Comparable
?
編寫實現(xiàn)Comparable接口的PersonSortable
類,使其按name以及age排序
1.編寫PersonSortable類
屬性:private name(String)
、private age(int)
有參構(gòu)造函數(shù):參數(shù)為name,age
toString函數(shù):返回格式為:name-age
實現(xiàn)Comparable接口:實現(xiàn)先對name升序排序,如果name相同則對age進行升序排序
2.main方法中
- 首先輸入n
- 輸入n行name age,并創(chuàng)建n個對象放入數(shù)組
- 對數(shù)組進行排序后輸出。
- 最后一行使用
System.out.println(Arrays.toString(PersonSortable.class.getInterfaces()));
輸出PersonSortable所實現(xiàn)的所有接口
輸入樣例:
5
zhang 15
zhang 12
wang 14
Wang 17
li 17
輸出樣例:
Wang-17
li-17
wang-14
zhang-12
zhang-15
//這行是標識信息
import java.math.*;
import java.util.*;
class PersonSortable implements Comparable<PersonSortable>{
private String name;
private int age;
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public PersonSortable(String _name,int _age){
this.name = _name;
this.age = _age;
}
public String toString(){
return this.name + "-" + this.age;
}
public int compareTo(PersonSortable person){
if(this.name.equals(person.getName())){
return this.age - person.getAge();
}
return this.name.compareTo(person.getName());
}
}
public class Main {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int n = in.nextInt();
PersonSortable[] ps = new PersonSortable[n];
for(int i=0;i<n;i++){
String name = in.next();
int age = in.nextInt();
ps[i] = new PersonSortable(name,age);
}
Arrays.sort(ps);
for(PersonSortable p : ps){
System.out.println(p);
}
System.out.println(Arrays.toString(PersonSortable.class.getInterfaces()));
}
}
?7-7 jmu-Java-03面向?qū)ο?06-繼承覆蓋綜合練習(xí)-Person、Student、Employee、Company
?
定義Person抽象類,Student類、Company類,Employee類。
Person類的屬性:String name, int age, boolean gender
Person類的方法:
public Person(String name, int age, boolean gender);
public String toString(); //返回"name-age-gender"格式的字符串
public boolean equals(Object obj);//比較name、age、gender,都相同返回true,否則返回false
Student類繼承自Person
,屬性:String stuNo, String clazz
Student類的方法:
//建議使用super復(fù)用Person類的相關(guān)有參構(gòu)造函數(shù)
public Student(String name, int age, boolean gender, String stuNo, String clazz);
public String toString(); //返回 “Student:person的toString-stuNo-clazz”格式的字符串
public boolean equals(Object obj);//首先調(diào)用父類的equals方法,如果返回true,則繼續(xù)比較stuNo與clazz。
Company類屬性:String name
Company類方法:
public Company(String name);
public String toString(); //直接返回name
public boolean equals(Object obj);//name相同返回true
Employee類繼承自Person
,屬性:Company company, double salary
Employee類方法:
//建議使用super復(fù)用Person類的相關(guān)有參構(gòu)造函數(shù)
public Employee(String name, int age, boolean gender, double salary, Company company);
public String toString(); //返回"Employee:person的toString-company-salary"格式的字符串
public boolean equals(Object obj);//首先調(diào)用父類的equals方法,如果返回true。再比較company與salary。
//比較salary屬性時,使用DecimalFormat df = new DecimalFormat("#.#");保留1位小數(shù)
編寫equals方法重要說明:
- 對Employee的company屬性的比較。要考慮傳入為
null
的情況。如果company不為null且傳入為null,返回false - 對所有String字符類型比較時,也要考慮
null
情況。
提示
- 排序可使用Collections.sort
- equals方法要考慮周全
main方法說明
- 創(chuàng)建若干Student對象、Employee對象。
輸入s
,然后依次輸入name age gender stuNo clazz
創(chuàng)建Student對象。
輸入e
,然后依次輸入name age gender salary company
創(chuàng)建Employee對象。
然后將創(chuàng)建好的對象放入List<Person> personList
。輸入其他字符,則結(jié)束創(chuàng)建。
創(chuàng)建說明:?對于String類型,如果為null
則不創(chuàng)建對象,而賦值為null
。對于company屬性,如果為null則賦值為null
,否則創(chuàng)建相應(yīng)的Company對象。
-
對personList中的元素實現(xiàn)先按照姓名升序排序,姓名相同再按照年齡升序排序。提示:可使用
Comparable<Person>
或Comparator<Person>
-
接受輸入,如果輸入為
exit
則return
退出程序,否則繼續(xù)下面步驟。 -
將personList中的元素按照類型分別放到stuList與empList。注意:不要將兩個內(nèi)容相同的對象放入列表(是否相同是根據(jù)equals返回結(jié)果進行判定)。
-
輸出字符串
stuList
,然后輸出stuList中的每個對象。 -
輸出字符串
empList
,然后輸出empList中的每個對象。
1-3
為一個測試點4-6
為一個測試點
輸入樣例:
s zhang 23 false 001 net15
e wang 18 true 3000.51 IBM
s zhang 23 false 001 net15
e bo 25 true 5000.51 IBM
e bo 25 true 5000.52 IBM
e bo 18 true 5000.54 IBM
e tan 25 true 5000.56 IBM
e tan 25 true 5000.51 IBM
s wang 17 false 002 null
s wang 17 false 002 null
e hua 16 false 1000 null
s wang 17 false 002 net16
e hua 16 false 1000 null
e hua 18 false 1234 MicroSoft
!
continue
輸出樣例:
Employee:bo-18-true-IBM-5000.54
Employee:bo-25-true-IBM-5000.51
Employee:bo-25-true-IBM-5000.52
Employee:hua-16-false-null-1000.0
Employee:hua-16-false-null-1000.0
Employee:hua-18-false-MicroSoft-1234.0
Employee:tan-25-true-IBM-5000.56
Employee:tan-25-true-IBM-5000.51
Student:wang-17-false-002-null
Student:wang-17-false-002-null
Student:wang-17-false-002-net16
Employee:wang-18-true-IBM-3000.51
Student:zhang-23-false-001-net15
Student:zhang-23-false-001-net15
stuList
Student:wang-17-false-002-null
Student:wang-17-false-002-net16
Student:zhang-23-false-001-net15
empList
Employee:bo-18-true-IBM-5000.54
Employee:bo-25-true-IBM-5000.51
Employee:hua-16-false-null-1000.0
Employee:hua-18-false-MicroSoft-1234.0
Employee:tan-25-true-IBM-5000.56
Employee:tan-25-true-IBM-5000.51
Employee:wang-18-true-IBM-3000.51
import java.math.*;
import java.util.*;
import java.text.*;
class Person implements Comparable<Person>{
public String name;
public int age;
public boolean gender;
public Person(String name, int age, boolean gender){
this.name = name;
this.gender = gender;
this.age = age;
}
public String toString(){
return this.name + "-" + this.age + "-" + this.gender;
}
public boolean equals(Object obj){
Person ob = (Person) obj;
return this.name.equals(ob.name) && this.age == ob.age && this.gender == ob.gender;
}
public int compareTo(Person p){
if(this.name.equals(p.name)){
return this.age - p.age;
}
return this.name.compareTo(p.name);
}
}
class Student extends Person{
String stuNo;
String clazz;
//建議使用super復(fù)用Person類的相關(guān)有參構(gòu)造函數(shù)
public Student(String name, int age, boolean gender, String stuNo, String clazz){
super(name,age,gender);
this.stuNo = stuNo;
this.clazz = clazz;
}
public String toString(){
return "Student:" + super.toString() + "-" + this.stuNo + "-" + this.clazz;
} //返回 “Student:person的toString-stuNo-clazz”格式的字符串
public boolean equals(Object obj){
if(super.equals(obj)==true){
Student ob = (Student)obj;
if(this.clazz.equals(ob.clazz) && this.stuNo.equals(ob.stuNo)){
return true;
}
}
return false;
}//首先調(diào)用父類的equals方法,如果返回true,則繼續(xù)比較stuNo與clazz。
}
class Company{
String name;
public Company(String name){
this.name = name;
}
public String toString(){
return this.name;
}
public boolean equals(Object obj){
Company ob = (Company) obj;
return this.name.equals(ob.name);
}//name相同返回true
}
class Employee extends Person{
public Company company;
public double salary;
//建議使用super復(fù)用Person類的相關(guān)有參構(gòu)造函數(shù)
public Employee(String name, int age, boolean gender, double salary, Company company){
super(name,age,gender);
this.salary = salary;
this.company = company;
}
public String toString(){
return "Employee:" + super.toString() + "-" + this.company + "-" + this.salary;
}
public boolean equals(Object obj){
if(super.equals(obj)){
Employee ob = (Employee)obj;
String a = new DecimalFormat("#.#").format(this.salary);
String b = new DecimalFormat("#.#").format(ob.salary);
return this.company.equals(ob.company) && a.equals(b);
}
return false;
}//首先調(diào)用父類的equals方法,如果返回true。再比較company與salary。
//比較salary屬性時,使用DecimalFormat df = new DecimalFormat("#.#");保留1位小數(shù)
}
public class Main {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
List<Person> ps = new ArrayList<>();
List<Student> st = new ArrayList<>();
List<Employee> em = new ArrayList<>();
while(true){
String op = in.next();
if(op.equals("s")){
String name = in.next();
int age = in.nextInt();
boolean gender = in.nextBoolean();
String stuNo = in.next();
String clazz = in.next();
Student s = new Student(name,age,gender,stuNo,clazz);
ps.add(s);
st.add(s);
}
else if(op.equals("e")){
String name = in.next();
int age = in.nextInt();
boolean gender = in.nextBoolean();
double salary = in.nextDouble();
String company = in.next();
Employee e = new Employee(name,age,gender,salary,new Company(company));
ps.add(e);
em.add(e);
}
else{
Collections.sort(ps);
for(int i=0;i<ps.size();i++) {
System.out.println(ps.get(i));
}
String tempString = in.next();
if(tempString.compareTo("exit")==0||tempString.compareTo("return")==0) {
return;
}
List<Student> tst = new ArrayList<>();
List<Employee> tem = new ArrayList<>();
System.out.println("stuList");
for(int i=0;i<st.size();i++) {
boolean flag = true;
for(int j=0;j<tst.size();j++){
if(st.get(i).equals(tst.get(j))){
//System.out.println(1);
flag = false;
}
}
if(flag){
tst.add(st.get(i));
}
}
Collections.sort(tst);
for(int i=0;i<tst.size();i++){
System.out.println(tst.get(i));
}
System.out.println("empList");
//System.out.println(em.size());
for(int i=0;i<em.size();i++) {
boolean flag = true;
for(int j=0;j<tem.size();j++){
if(em.get(i).equals(tem.get(j))){
flag = false;
}
}
if(flag){
tem.add(em.get(i));
}
}
Collections.sort(tem);
//System.out.println(tem.size());
for(int i=0;i<tem.size();i++){
System.out.println(tem.get(i));
}
}
}
}
}
?7-8 定義接口(Biology、Animal)、類(Person)、子類(Pupil)
(1)定義Biology
(生物)、Animal
(動物)2個接口,其中Biology聲明了抽象方法breathe( )
,Animal聲明了抽象方法eat( )
和sleep( )
。
(2)定義一個類Person
(人)實現(xiàn)上述2個接口,實現(xiàn)了所有的抽象方法,同時自己還有一個方法think( )
。breathe()、eat()、sleep()、think()四個方法分別輸出:我喜歡呼吸新鮮空氣
我會按時吃飯
早睡早起身體好
我喜歡思考
(3)定義Person類的子類Pupil
(小學(xué)生),有私有的成員變量school
(學(xué)校),公有的成員方法setSchool( )
、getSchool( )
分別用于設(shè)置、獲取學(xué)校信息。
(4)在測試類Main
中,用Pupil類創(chuàng)建一個對象zhangsan。嘗試從鍵盤輸入學(xué)校信息給zhangsan,獲取到該信息后輸出該學(xué)校信息,格式為“我的學(xué)校是XXX”;依次調(diào)用zhangsan的breathe()、eat()、sleep()、think()方法。
輸入格式:
從鍵盤輸入一個學(xué)校名稱(字符串格式)
輸出格式:
第一行輸出:我的學(xué)校是XXX(XXX為輸入的學(xué)校名稱)
第二行是breathe()方法的輸出
第三行是eat()方法的輸出
第四行是sleep()方法的輸出
第五行是think()方法的輸出
輸入樣例:
在這里給出一組輸入。例如:
新余市逸夫小學(xué)
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
我的學(xué)校是新余市逸夫小學(xué)
我喜歡呼吸新鮮空氣
我會按時吃飯
早睡早起身體好
我喜歡思考
?
import java.math.*;
import java.util.*;
interface Biology{
abstract void breathe();
}
interface Animal{
abstract void eat();
abstract void sleep();
}
class Person implements Animal,Biology{
public void think(){
System.out.println("我喜歡思考");
}
public void eat(){
System.out.println("我會按時吃飯");
}
public void sleep(){
System.out.println("早睡早起身體好");
}
public void breathe(){
System.out.println("我喜歡呼吸新鮮空氣");
}
}
class Pupil extends Person{
String school;
public void setSchool(String _school){
this.school = _school;
}
public String getSchool(){
return this.school;
}
}
public class Main {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
String sc = in.next();
System.out.println("我的學(xué)校是" + sc);
Pupil p = new Pupil();
p.breathe() ;
p.eat();
p.sleep();
p.think();
}
}
7-9 Employee類的層級結(jié)構(gòu)
定義四個類,分別為Employee類、SalariedEmployee類、HourlyEmployee類和CommissionEmployee類。其中Employee類是其他三個類的父類。Employee類包含姓名和身份證號。除此之外,SalariedEmployee類還應(yīng)包含每月工資;HourlyEmployee類還應(yīng)包含每小時工資數(shù)和工作時間數(shù);CommissionEmployee還應(yīng)包含提成比例和銷售總額。其中HourlyEmployee的工資為:每小時工資數(shù)×工作時間數(shù),CommissionEmployee的工資為:提成比例×銷售總額。每個類都應(yīng)有合適的構(gòu)造方法、數(shù)據(jù)成員的設(shè)置和讀取方法。編寫一個測試程序,創(chuàng)建這些類的對象,并輸出與對象相關(guān)的信息。注意子類有時需調(diào)用父類的構(gòu)造方法和被覆蓋的方法,成員變量定義為private,對有些方法實現(xiàn)重載。測試程序如下所示:
public static void main(String [] args){
Scanner in=new Scanner(System.in);
Employee [] e=new Employee[3];
e[0]=new SalariedEmployee(in.next(),in.next(),in.nextDouble());
e[1]=new HourlyEmployee(in.next(),in.next(),in.nextDouble(),in.nextDouble());
e[2]=new CommissionEmployee(in.next(),in.next(),in.nextDouble(),in.nextDouble());
for(int i=0;i<e.length;i++)
{
System.out.println(e[i].getSalary());
System.out.println(e[i]);
}
}
輸入格式:
輸入三行。第一行為一個SalariedEmployee對象的姓名,身份證號和每月工資。第二行為一個HourlyEmployee對象的姓名、身份證號、每小時工資數(shù)、工作時間。第三行為一個CommissionEmployee對象的姓名、身份證號、提成比例和銷售總額。
輸出格式:
輸出三個對象的工資和對象的其他信息。每一個對象輸出兩行,第一行為工資,第二行為對象的信息。文章來源:http://www.zghlxwxcb.cn/news/detail-743518.html
輸入樣例:
Mike 0001 5000
Jack 0002 20 300
Tom 0003 0.2 50000
輸出樣例:
5000.0
SalariedEmployee[name=Mike,id=0001][monthSalary=5000.0]
6000.0
HourlyEmployee[name=Jack,id=0002][hourSalary=20.0,workhour=300.0]
10000.0
CommissionEmployee[name=Tom,id=0003][commissionRatio=0.2,sale=50000.0]
import java.math.*;
import java.util.*;
class Employee{
String name;
String id;
public Employee(String _name, String _id){
this.name = _name;
this.id = _id;
}
public String toString(){
return "[name=" + this.name + ",id=" + this.id + "]";
}
public double getSalary(){
return 0.0;
}
}
class SalariedEmployee extends Employee{
double monthSalary;
public double getSalary(){
return this.monthSalary;
}
public String toString(){
return "SalariedEmployee" + super.toString() + "[monthSalary=" + this.monthSalary + "]";
}
public SalariedEmployee(String _name, String _id, double _monthSalary){
super(_name,_id);
this.monthSalary = _monthSalary;
}
}
class HourlyEmployee extends Employee{
double hourSalary;
double workhour;
public HourlyEmployee(String _name, String _id, double _hourSalary, double _workhour){
super(_name,_id);
this.hourSalary = _hourSalary;
this.workhour = _workhour;
}
public double getSalary(){
return this.hourSalary * this.workhour;
}
public String toString(){
return "HourlyEmployee" + super.toString() + "[hourSalary=" + this.hourSalary + ",workhour=" + this.workhour + "]";
}
}
class CommissionEmployee extends Employee{
double commissionRatio;
double sale;
public CommissionEmployee(String _name,String _id, double _commissionRatio, double _sale){
super(_name,_id);
this.commissionRatio = _commissionRatio;
this.sale = _sale;
}
public double getSalary(){
return this.sale * this.commissionRatio;
}
public String toString(){
return "CommissionEmployee" + super.toString() + "[commissionRatio=" + this.commissionRatio + ",sale=" + this.sale + "]";
}
}
public class Main {
public static void main(String [] args){
Scanner in = new Scanner(System.in);
Employee [] e = new Employee[3];
e[0] = new SalariedEmployee(in.next(),in.next(),in.nextDouble());
e[1] = new HourlyEmployee(in.next(),in.next(),in.nextDouble(),in.nextDouble());
e[2] = new CommissionEmployee(in.next(),in.next(),in.nextDouble(),in.nextDouble());
for(int i=0;i<e.length;i++){
System.out.println(e[i].getSalary());
System.out.println(e[i]);
}
}
}
?文章來源地址http://www.zghlxwxcb.cn/news/detail-743518.html
到了這里,關(guān)于Java程序設(shè)計2023-第三次上機練習(xí)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!