定義考題類,子類單選題和多選題繼承考題類

#定義考題類(Question)及其子類
完成考題類(Question),單選題(SingleChoice)和多選題(MultiChoice)是其子類
要求:
1)Question包含題幹屬性(text)
2)Question包含檢測標準答案的方法 boolean check(int[] answers)
參數answers:用戶提供的答案(注意:單選題只有一個答案)
3)單選題(SingleChoice)和多選

  • List item

題(MultiChoice)是Question的子類
MultiChoice包含屬性:
選項:String[] options
多選標準答案:int[] answers
SingleChoice包含屬性:
選項:String[] options
單選標準答案:int answers
4)在MultiChoice實現參數爲(String text,String[] options,int[] answers)的構造方法
參數text:題幹
參數options:選項
參數answers:多選標準答案(正確選項的序號)
5)在SingleChoice實現參數爲(String text,String[] options,int answers)的構造方法
參數text:題幹
參數options:選項
參數answers:單選標準答案(正確選項的序號)
6)在SingleChoice和MultiChoice類中重寫Question類中的check方法
方法中提供具體的檢查用戶答案的邏輯
1、定義父類,包含text屬性和check方法

public class Question {//定義父類,考題類
private String text;
//題幹,字符串用來存儲題目
public String getText() {
	//text的get方法
	return text;
}

public void setText(String text) {
	//text的set方法
	this.text = text;
}


boolean  check(int answers) {
	//父類的check方法,用來子類繼承並重寫
	return true;
}
}

2、單選題類包含選項和答案兩個屬性,還要重寫check方法進行答案對比

public class SingleChoice extends Question{
	public SingleChoice(String text,String[] options, int answers) {
		//定義有參構造器,依次包含題幹,選項,標準答案這些參數
		super();
		this.options = options;
		this.answers = answers;
		this.setText(text);//set方法調用父類text屬性
	}
	private String[] options= {"1、爺爺","2、奧特曼","3、滅爸","4、變形金剛"};
	private int answers;
	//定義answers屬性

	public String[] getOptions() {
		return options;
	}
	public void setOptions(String[] options) {
		this.options = options;
	}
	public int getAnswers() {
		return answers;
	}
	public void setAnswers(int answers) {
		this.answers = answers;
	}
	public SingleChoice() {
		super();
	}
	public boolean check(int answers) {
		if(answers==1) {
		//選項裏面的答案是1,所以當輸入的答案是1時,返回true
			return true;
		}else {
			return false;//否則返回false
		}
	}
	
	}
	

在這裏插入代碼片
3、多選題類也包含選項和答案兩個屬性,也要重寫check方法進行答案對比

	private String[] options= {"1、外公","2、外婆","3、葫蘆娃","4、蠍子精"};
	//定義並初始化options屬性
	private int[] answers=new int [2];
	//定義並初始化answers屬性
	public String[] getOptions() {
		return options;
	}

	public void setOptions(String[] options) {
		this.options = options;
	}

	public int[] getAnswers() {
		return answers;
	}

	public void setAnswers(int[] answers) {
		this.answers = answers;
	}
	//自動生成set和get方法
	public MultiChoice(String text,String[] options, int[] answers) {
		//調用有參構造器,參數依次爲題幹、選項、答案
		super();
		this.options = options;
		this.answers = answers;
		this.setText(text);//使用set方法,調用父類私有屬性
	}
	
	public MultiChoice() {
		//當定義了有參的構造器之後,需要手動生成無參的構造器
		super();
	}
	
	boolean chech(int[] answer) {
		if((answers[0]==1 && answers[1]==2)||(answers[0]==2&&answer[1]==2)) {
			//多選題的答案是兩個,當答案是1 、2或者2、1時,都會判斷答案正確
			return true;
		}
		else return false;
	}	
}

4、最後使用Test類,創建各子類對象,使用check方法對比輸入答案是否正確


import java.util.Scanner;
public class Test {
	public static void main(String[] args) {
		SingleChoice s=new SingleChoice();
		//創建單選題類的對象s
		MultiChoice m=new MultiChoice();
//		創建多選題的對象m
		s.setText("單選題:爸爸的爸爸叫什麼?");
//		用set方法給父類text屬性賦值
		System.out.println(s.getText());
		//輸出單選題題幹,準備答題
		for(String a:s.getOptions()) {
			//用增強for循環輸出選項
			System.out.println(a);
		}
		Scanner key=new Scanner(System.in);
		//創建一個鍵盤掃描器用於輸入答案
		int num=key.nextInt();
		if(s.check(num)) {//調用check方法判斷輸入的答案是否正確
			System.out.println("回答正確");
		}else {
			System.out.println("回答錯誤");
		}
		m.setText("多選題:媽媽的父母叫什麼");//使用set方法對text屬性進行初始化
		System.out.println(m.getText());//輸入多選題的題幹
		for(String a:m.getOptions()) {
			//使用增強for循環輸出多選題選項
			System.out.println(a);
		}
		
		
		int[] arr=m.getAnswers();//使用get方法獲取私有屬性Answers
		arr[0]=key.nextInt();//對鍵盤掃描器輸入的數值放進數組
		arr[1]=key.nextInt();
		if(m.chech(arr)) {//對多選題答案進行判斷
			System.out.println("回答正確");
		}else {
			System.out.println("回答錯誤");
		}
		key.close();
		
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章