امثلة محلولة :Test Questions In This Exercise You Will Use Inheritance To Read, Store, And Print Questions For A Test
منتدى مصر التقني :: قسم لغات البرمجة Programming languages :: لغة الجافا Java :: نقاش و أستفسارات الاعضاء
صفحة 1 من اصل 1 • شاطر
امثلة محلولة :Test Questions In This Exercise You Will Use Inheritance To Read, Store, And Print Questions For A Test
اهلا وسهلا بكم
راسلني أحد الاصدقاء لحل التمرين التالى فأحببت أن اشاركه معكم حتى تعم الفائدة على الجميع :
In this Assignment you will use an abstract class to define, read, store, and print questions for a test.
you will start by writing an abstract TestQuestion class that contains the following:
A protected String variable that holds the test question.
An abstract method protected abstract void readQuestion() to read the question.
Now define two subclasses of TestQuestion, Essay and MultChoice which implement the abstract methods. Essay will need an instance variable to store the number of blank lines needed for the answering space. MultChoice will not need this variable, but it will
need an array of Strings to hold the choices along with the main question.
You will also need to write toString methods for the MultChoice and Essay classes that return nicely formatted versions of the questions.
Now define a class WriteTest that creates an ArrayList of TestQuestion objects.
Assume that the input as the following:
type of question (character, m=multiple choice, e=essay).
number of blank lines for essay, number of blank lines for multiple choice (integer).
the question text.
choice 1 (multiple choice only)
choice 2 (multiple choice only) ...
Save each object in the ArrayList object to hold the various TestQuestion objects (Essay or MultChoice objects). Use a loop to print the questions from the ArrayList as completed quiz.
Note that a test may have questions such as an essay, a multiple choice question, fill in the blanks etc etc. Use factory pattern that makes your code open for extension
الحل :
أولا : تحديد المطلوب من التمرين :
يتطلب التمرين اربعة فئات بالاسماء و الصفات التالىة :
فئة فرعية من الفئة TestQuestion باسم Essay تحتوي على متغير رقمى int لتخزين عدد الاسطر المطلوبة لاجابة السؤال
فئة فرعية من الفئة TestQuestion باسم MultChoice تحتوي على مصفوفة نصية لتخزين خيارات الاجابة على السؤال
فئة باسم WriteTest و التى يجب أن تقوم بعمل التالى :
إنشاء قائمة مصفوفة ArrayList من كائنات الفئة TestQuestion
قراءة اسئلة الاختبار من المستخدم
حفظ كل عنصر في كائن ArrayList
استخدم حلقة لطباعة الأسئلة من ArrayList
ثانيا : كتابة شفرة البرنامج
TestQuestion.java
MultChoice.java
WriteTest .java
راسلني أحد الاصدقاء لحل التمرين التالى فأحببت أن اشاركه معكم حتى تعم الفائدة على الجميع :
In this Assignment you will use an abstract class to define, read, store, and print questions for a test.
you will start by writing an abstract TestQuestion class that contains the following:
A protected String variable that holds the test question.
An abstract method protected abstract void readQuestion() to read the question.
Now define two subclasses of TestQuestion, Essay and MultChoice which implement the abstract methods. Essay will need an instance variable to store the number of blank lines needed for the answering space. MultChoice will not need this variable, but it will
need an array of Strings to hold the choices along with the main question.
You will also need to write toString methods for the MultChoice and Essay classes that return nicely formatted versions of the questions.
Now define a class WriteTest that creates an ArrayList of TestQuestion objects.
Assume that the input as the following:
type of question (character, m=multiple choice, e=essay).
number of blank lines for essay, number of blank lines for multiple choice (integer).
the question text.
choice 1 (multiple choice only)
choice 2 (multiple choice only) ...
Save each object in the ArrayList object to hold the various TestQuestion objects (Essay or MultChoice objects). Use a loop to print the questions from the ArrayList as completed quiz.
Note that a test may have questions such as an essay, a multiple choice question, fill in the blanks etc etc. Use factory pattern that makes your code open for extension
الحل :
أولا : تحديد المطلوب من التمرين :
يتطلب التمرين اربعة فئات بالاسماء و الصفات التالىة :
- فئة مجردة Abstract class بأسم TestQuestion تحتوي على ما يلي:
- متغير نصي محمي لتخزين سؤال الاختبار به.
- طريقة مجردة محمية باسم readQuestion لقراءة اسئلة الاختبار
ثانيا : كتابة شفرة البرنامج
TestQuestion.java
- الكود:
// كتابة الفئة المجردة
public abstract class TestQuestion {
// متغير نصي محمي لتخزين سؤال الاختبار
protected String testQuestion;
// طريقة مجردة محمية لقراءة السؤال
protected abstract void readQuestion();
}
Essay.java
- الكود:
import java.util.*;
public class Essay extends TestQuestion {
int numLines; // متغير لتخزين عدد الاسطر الفارغة المطلوبة للاجاب
public Essay() {
super();
}
@Override // تنفيذ الطريقة المجردة فى الفئة الام
protected void readQuestion() {
// إنشاء كائن الادخال
Scanner in = new Scanner(System.in);
// اظهار رسالة للمستخدم لتحديد عدد الاسطر الفارعة المخصصة للاجابة
System.out.println("Enter number of blank lines for essay:");
// تخزين ما أدخلة المستخدم فى المتغير
numLines = in.nextInt();
// اظهار رسالة للمستخدم لانشاء شؤال الاختبار
System.out.println("Enter the question, ending with a Period:");
// تخزين السؤال فى المتغير المخصص له
testQuestion = in.next();
}
@Override // طريقة لتنسيق المخرجات
public String toString() {
// نص السؤال
String output = testQuestion;
// حلقة دوران لانشاء الاسطر الفارغة لمحددة للاجابة
for (int i = 0; i < numLines; i++) {
output += "\n";
}
return output;
}
}
MultChoice.java
- الكود:
import java.util.*;
public class MultChoice extends TestQuestion {
public String[] choices;
public int numChoices;
public MultChoice() {
super();
}
@Override
public void readQuestion() {
Scanner in = new Scanner(System.in);
System.out.println("How many choices will this question have?");
numChoices = in.nextInt();
choices = new String[numChoices];
System.out.println("Enter the question text:");
testQuestion =in.next();
for (int i = 0; i < numChoices; i++) {
System.out.println("Enter the choice " + i + ":");
choices[i] = in.next();
}
}
@Override
public String toString() {
String output;
output = testQuestion;
char charo = 'A';
for (int i = 0; i < numChoices; i++) {
output += ("\n" + charo + ": " + choices[i]);
charo = (char) ((int) charo + 1);
}
return output;
}
}
WriteTest .java
- الكود:
import java.io.IOException;
import java.util.*;
public class WriteTest {
public static void main(String[] args) throws IOException {
int numOfQuestions; // متغير لتخزين عدد اسئلة الاختبار
char type; // متغير لتخزين نوع السؤال
// إنشاء مصفوفة كائنات الفئة TestQuestion
TestQuestion[] midterm = new TestQuestion[100];
// إنشاء قائمة مصفوفة لتخزين كائنات الفئة TestQuestion
ArrayList<TestQuestion> term = new ArrayList();
// إنشاء كائن الادخال
Scanner scan = new Scanner(System.in);
// رسالة للمستخدم بتحديد عدد اسئلة الاختبار
System.out.println("How many questions?");
// تخزين ما تم ادخالة من المستخدم
numOfQuestions = scan.nextInt();
// حلقة دوران تبدء من واحد وتنتهى بعدد اسئلة الاختبار
for (int i = 1; i <= numOfQuestions; i++) {
// فى كل دوران يتم سؤال المستخدم على نوع السؤال
System.out.println("Enter type of question number "+i+" e for essay, m for multiple choice:");
// تخزين ما تم ادخالة المستخدم
type = scan.next().charAt(0);
// اذا كان نوع السؤال كمقال يتم تنفيذ الطريق الخاصة به
if (type == 'e') {
Essay essay = new Essay();
essay.readQuestion();
midterm[i] = essay;
// أما اذا كان نوع السؤال كاختيار متعدد يتم تنفيذة الكائن الخاص به
} else if (type == 'm') {
MultChoice multchoix = new MultChoice();
multchoix.readQuestion();
midterm[i] = multchoix;
}
// حفظ كل الكائنات فى قائمة مصفوفة
term.add(midterm[i]);
}
System.out.println();
System.out.println();
System.out.println("Here's the test:");
// طباعة محتوي الاختبار
for (TestQuestion term1 : term) {
System.out.println(term1.toString());
}
}
}
ـــــــــــــــــــ التوقيع ــــــــــــــــــــ
سبحان الله وبحمدة .....سبحان الله العظيم
| |
منتدى مصر التقني :: قسم لغات البرمجة Programming languages :: لغة الجافا Java :: نقاش و أستفسارات الاعضاء
صفحة 1 من اصل 1
صلاحيات هذا المنتدى:
لاتستطيع الرد على المواضيع في هذا المنتدى
أمس في 7:09 pm من طرف moslema_r
» شركة تنظيف فرشات بالرياض
الثلاثاء مارس 26, 2024 6:26 pm من طرف moslema_r
» شركة تنظيف استراحات بالرياض
الأحد مارس 24, 2024 10:49 pm من طرف moslema_r
» كشف تسربات المياه في جدة
السبت مارس 23, 2024 7:23 pm من طرف gmalnagy
» خدمات كشف تسربات المياه بخصم 25% - اتصل الان
السبت مارس 23, 2024 7:22 pm من طرف gmalnagy
» شركة تنظيف ثريات بالرياض
الخميس مارس 21, 2024 7:23 pm من طرف moslema_r
» كهربائي منازل بالرياض
الأحد مارس 17, 2024 1:52 pm من طرف رانيا حماد
» افضل شركة نقل عفش بالرياض نقل باكستاني 40% خصم | موقع الانوار
الأحد مارس 17, 2024 1:25 pm من طرف رانيا حماد
» افضل معلم جبس بالرياض تركيب جبس بورد بالرياض 20% خصم اتصل الآن
الأحد مارس 17, 2024 1:09 pm من طرف رانيا حماد
» شراء اثاث مستعمل بالكويت بخصم 30%
الأحد مارس 17, 2024 12:54 pm من طرف رانيا حماد