Java實驗(04)

2020-4-1

一.汽車接口

  1. (20分)

接口與多態編程--汽車接口
題目描述
編程實現:
(1) 汽車接口(Car):有兩個方法, getName()、getPrice()
(2) BMW類,實現汽車接口
(3) 奇瑞QQ類,實現汽車接口
(4) 桑塔那類,實現汽車接口
(4) 汽車出售店(CarShop)類,有成員變量:count(售出數量) 和total(總價),有方法:
void sellCar(Car car); (調用方法獲取售出車的車型、售價;獲取售出數量及總價)
int getCount(); int getTotal();
(5) 編寫main()方法測試:賣出一輛BMW、一輛奇瑞QQ、一輛桑塔那,輸出結果

輸入描述

輸出描述
售出車型 單價
該店總收入 該店售出總數量

輸入樣例

輸出樣例
售出車型:BMW 單價:300000
該店總收入:300000 該店售出數量:1
售出車型:CheryQQ 單價:20000
該店總收入:320000 該店售出數量:2
售出車型:Santana 單價:280000
該店總收入:600000 該店售出數量:3

public class Main {

	public static void main(String[] args) {
		CarShop carShop = new CarShop();
		BMW bmw = new BMW("BMW",300000);
		CheryQQ cheryQQ = new CheryQQ("CheryQQ",20000);
		Santana santana = new Santana("Santana",280000);
		carShop.sellCar(bmw);
		System.out.println("該店總收入:"+carShop.getTotal()+
				" 該店售出數量:"+carShop.getCount());
		carShop.sellCar(cheryQQ);
		System.out.println("該店總收入:"+carShop.getTotal()+
				" 該店售出數量:"+carShop.getCount());
		carShop.sellCar(santana);
		System.out.println("該店總收入:"+carShop.getTotal()+
				" 該店售出數量:"+carShop.getCount());
	}

}
interface Car{
	//接口只能有常量
	//接口沒有函數體
	public abstract String getName();
	public abstract long getPrice();
}

class BMW implements Car{
	private String name;
	private long price;
	BMW(String name,long price){
		this.name = name;
		this.price = price;
		
	}
	public String getName() {
		return this.name;
	}
	public long getPrice() {
		return this.price;
	}
}

class CheryQQ implements Car{
	private String name;
	private long price;
	CheryQQ(String name,long price){
		this.name = name;
		this.price = price;
		
	}
	public String getName() {
		return this.name;
	}
	public long getPrice() {
		return this.price;
	}
}

class Santana implements Car{
	private String name;
	private long price;
	Santana(String name,long price){
		this.name = name;
		this.price = price;
	}
	public String getName() {
		return this.name;
	}
	public long getPrice() {
		return this.price;
	}
}


class CarShop {
	private int count;
	private long total;
	CarShop(){
		this.count = 0;
		this.total = 0;
	}
	public void sellCar(Car car) {
		this.total = this.total + car.getPrice();
		this.count++;
		System.out.println("售出車型:"+car.getName()+" 單價:"+
		car.getPrice());
	}
	public int getCount() {
		return this.count;
	}
	public long getTotal() {
		return this.total;
	}
}

二.用戶登錄界面

  1. (0分)

用戶登錄界面-swing
題目描述
編寫一個JFrame框架登錄應用程序,包括:
JLabel 用戶名
JLabel 密碼
JTextFiled 用於用戶輸入用戶名
JPasswordField 用於用戶輸入密碼
JButton 確定
JButton 取消
界面見學習通實驗4

輸出樣例
本題無測試樣例

import javax.swing.*;
import java.awt.*;

public class Main {
    public static void main(String[] args) {
        Login_GUI lg = new Login_GUI();
    }
}

class Login_GUI extends JFrame{
    public Login_GUI(){
        //定義組件
        JLabel label1,label2;
        //用戶名
        JTextField username_text;
        //密碼
        JPasswordField password_text;
        //按鈕
        JButton button1,button2;

        JPanel panel1,panel2,panel3;
        //創建組件
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        label1 = new JLabel("用戶名");
        label2 = new JLabel("密    碼");
        //要設置長度的
        username_text = new JTextField(10);
        password_text = new JPasswordField(10);

        button1 = new JButton("登錄");
        button2 = new JButton("取消");

        //設置佈局
        this.setLayout(new GridLayout(3,1,5,5));
        panel1.add(label1);
        panel1.add(username_text);

        panel2.add(label2);
        panel2.add(password_text);

        panel3.add(button1);
        panel3.add(button2);

        this.add(panel1);
        this.add(panel2);
        this.add(panel3);
//        this.add(button2);

        //設置窗口
        this.setTitle("用戶登錄");
        //窗口大小
        this.setSize(300,200);

        //設置窗體關閉時候,保證JVM要退出就是控制檯
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //獲取你的屏幕的寬和高
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        //然後設置你編寫的窗口的初始位置,也就是在中間,
        this.setLocation(width / 2 - 200, height / 2 - 150);

        //顯示
        this.setVisible(true);
    }
}

三.Student類與子類

  1. (20分)

繼承--Student類與子類
題目描述
編程實現:
(1)聲明Student類:
屬性包括:學號、姓名、英語成績、數學成績、Java成績、總成績(成績爲double型,學號、姓名爲String類型)
方法包括:構造方法、標準方法、toString方法、compare方法(比較兩個學生的總成績,結果分大於、等於、小於)、sum方法(計算總成績)、testScore方法(計算評測成績)
注:評測成績取三門課程成績的平均分,另外任何一門課成績的改變都需要對總成績重新進行計算,因此在每一個set方法中應調用sum()方法計算總成績
(2)聲明StudentSJ(參與綜合設計的學生)類爲Student的子類,增加任務屬性,並重寫testScore方法(計算評測成績,評測成績=三門課的平均分+3)
(3)聲明StudentZZ(綜合設計組長)類爲Student的子類,增加責任屬性,並重寫testScore方法(計算評測成績,評測成績=三門課的平均分+5)
(4)聲明測試類:在主方法中聲明Student類數組(含3個元素),生成3個對象存入數組,其中一個爲Student類的對象,一個StudentSJ類的對象,一個StudentZZ類的對象,將方法testScore()發送給數組中的每一個元素,輸出結果,並分析具體執行的是哪一個類中的方法。比較StudentSJ類對象和StudentZZ類對象的總成績,輸出結果。

輸入描述
一行上輸入一個學生信息:(中間一個空格間隔)
學號 姓名 英語成績 數學成績 Java成績 (任務或責任)

輸出描述
一行上輸出一個學生信息:(中間一個空格間隔,中文冒號)
學號 姓名 英語成績 數學成績 Java成績 評測成績 (任務或責任)
最後一行:第二位與第三位學生總成績比較結果

輸入樣例
1 黃源 85 81 78
2 趙子龍 89 83 84 數據查詢
3 關羽 84 89 83 組長

輸出樣例
學號:1 姓名:黃源 英語:85.0 數學:81.0 Java:78.0 評測成績:81.33333333333333
學號:2 姓名:趙子龍 英語:89.0 數學:83.0 Java:84.0 評測成績:88.33333333333333 任務:數據查詢
學號:3 姓名:關羽 英語:84.0 數學:89.0 Java:83.0 評測成績:90.33333333333333 責任:組長
趙子龍的總成績等於關羽

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Student[] studentArray = new Student[3];
		Scanner sc = new Scanner(System.in);
		String ID1 = sc.next();
		String name1 = sc.next();
		double englishScore1 = sc.nextDouble();
		double mathScore1 = sc.nextDouble();
		double JavaScore1 = sc.nextDouble();
		String ID2 = sc.next();
		String name2 = sc.next();
		double englishScore2 = sc.nextDouble();
		double mathScore2 = sc.nextDouble();
		double JavaScore2 = sc.nextDouble();
		String task = sc.next();
		String ID3 = sc.next();
		String name3 = sc.next();
		double englishScore3 = sc.nextDouble();
		double mathScore3 = sc.nextDouble();
		double JavaScore3 = sc.nextDouble();
		String responsibility = sc.next();
		sc.close();
		studentArray[0] = new Student(ID1,name1,englishScore1,
				mathScore1,JavaScore1);
		studentArray[1] = new StudentSJ(ID2,name2,englishScore2,
				mathScore2,JavaScore2,task);
		studentArray[2] = new StudentZZ(ID3,name3,englishScore3,
				mathScore3,JavaScore3,responsibility);
		System.out.println(studentArray[0].toString());
		System.out.println(studentArray[1].toString());
		System.out.println(studentArray[2].toString());
		System.out.print(studentArray[2].compare(studentArray[1], studentArray[2]));
	}

}
/*
1 黃源 85 81 78
2 趙子龍 55 55 66 數據查詢
3 關羽 33 55 44 組長
 */

class Student{
	private String ID;
	private String name;
	private double englishScore;
	private double mathScore;
	private double JavaScore;
	private double totalScore;
	Student() {}
	private void set(double englishScore,double mathScore,
			double JavaScore) {
		this.totalScore = this.sum();
		this.englishScore = englishScore;
		this.mathScore = mathScore;
		this.JavaScore = JavaScore;
	}
	Student(String ID,String name,
			double englishScore,double mathScore,
			double JavaScore){
		this.ID = ID;
		this.name = name;
		this.englishScore = englishScore;
		this.mathScore = mathScore;
		this.JavaScore = JavaScore;
	}
	public String toString() {
		return "學號:"+ID+" 姓名:"+name+" 英語:"+englishScore+
				" 數學:"+mathScore+" Java:"+JavaScore+
				" 評測成績:"+testScore();
	}
	//獲取平均分
	public double testScore() {
		return (englishScore+mathScore+JavaScore)/3.0;
	}
	public double sum() {
		this.totalScore = englishScore+mathScore+JavaScore;
		return this.totalScore;
	}
	public String getName() {
		return this.name;
	}
	//比較兩個學生的成績
	public String compare(Student a,Student b) {
		
		if(a.sum() > b.sum())
			return a.getName()+"的總成績大於"+b.getName();
		else if(a.sum() == b.sum())
			return a.getName()+"的總成績等於"+b.getName();
		else 
			return a.getName()+"的總成績小於"+b.getName();
	}
}

//參與綜合設計的學生
class StudentSJ extends Student{
	//增加任務屬性
	private String task;
	StudentSJ(String ID,String name,
			double englishScore,double mathScore,
			double JavaScore,String task){
		super(ID,name,englishScore,
				mathScore,JavaScore);
		this.task = task;
	}
	public double testScore() {
		return super.testScore() + 3;
	}
	public String toString() {
		return super.toString() + " 任務:"+task;
	}
}


//綜合設計組長
class StudentZZ extends Student{
	//增加責任屬性
	private String responsibility;
	StudentZZ(String ID,String name,
			double englishScore,double mathScore,
			double JavaScore,String responsibility){
		super(ID,name,englishScore,
				mathScore,JavaScore);
		this.responsibility = responsibility;
	}
	public double testScore() {
		return super.testScore() + 5;
	}
	public String toString() {
		return super.toString() + " 責任:"+responsibility;
	}
}

四.抽象類

  1. (20分)

抽象類-編程實現運算類
題目描述
編程實現運算類(類圖見學習通 實驗4 )
(1)定義抽象類Operation,有double型數據成員numberA、numberB,有抽象方法getResult()
(2)定義Operation的4個子類,分別實現加、減、乘、除四則運算
(3)定義類OperationFactory:有靜態方法Operation createOperate(String operate); 根據形參operate的值創建相應加、減、乘、除類的對象,賦給抽象類引用後返回
(4)定義測試類及主方法:用戶從鍵盤輸入運算數及運算符,根據運算符調用OperationFactory類的靜態方法,創建相應實例,設置運算數,輸出運行結果

輸入描述
樣例輸入(每次運行輸入一個式子:數據與運算符之間一個空格間隔)

輸出描述
一行輸出結果:冒號、感嘆號均爲中文標點

輸入樣例
//以下爲多組測試數據
56 + 12
34 / 17
45 / 0

輸出樣例
//以下爲多組數據的測試結果
結果是:68.0
結果是:2.0
除數不能爲0!

import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String operate = scanner.nextLine();

		OperationFactory operationFactory = new OperationFactory();
		Operation operation = operationFactory.createOperate(operate);
		if(operation != null) {
			double result = operation.getResult();
			System.out.println("結果是:"+result);
		}
		scanner.close();

	}

}

abstract class Operation{
	double numberA;
	double numberB;
	abstract double getResult();
}

class OperationAdd extends Operation{
	OperationAdd(double a,double b){
		this.numberA = a;
		this.numberB = b;
	}
	public double getResult() {
		return this.numberA + this.numberB;
	}
}


class OperationSub extends Operation{
	OperationSub(double a,double b){
		this.numberA = a;
		this.numberB = b;
	}
	public double getResult() {
		return this.numberA - this.numberB;
	}
}


class OperationMul extends Operation{
	OperationMul(double a,double b){
		this.numberA = a;
		this.numberB = b;
	}
	public double getResult() {
		return this.numberA * this.numberB;
	}
}


class OperationDiv extends Operation{
	OperationDiv(double a,double b){
		this.numberA = a;
		this.numberB = b;
	}
	public double getResult() {
		return this.numberA / this.numberB;
	}
}


class OperationFactory{
	
	public static Operation createOperate(String operate) {
		String []temp = operate.split(" ");
		double a = Double.parseDouble(temp[0]);
		double b = Double.parseDouble(temp[2]);
		if(temp[1].equals("+")) {
			OperationAdd operationAdd = new OperationAdd(a,b);
			return operationAdd;
		}
		else if(temp[1].equals("-")) {
			OperationSub operationSub = new OperationSub(a,b);
			return operationSub;
		}
		else if(temp[1].equals("*")) {
			OperationMul operationMul = new OperationMul(a,b);
			return operationMul;
		}
		else if(temp[1].equals("/")) {
			OperationDiv operationDiv = new OperationDiv(a,b);
			if(b == 0) {
				System.out.println("除數不能爲0!");
				return null;
			}
			return operationDiv;
		}
		return null;
	}
	
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章