منتدى مصر التقني
هل تريد التفاعل مع هذه المساهمة؟ كل ما عليك هو إنشاء حساب جديد ببضع خطوات أو تسجيل الدخول للمتابعة.

امثلة محلولة :Test Questions In This Exercise You Will Use Inheritance To Read, Store, And Print Questions For A Test

اذهب الى الأسفل

امثلة محلولة :Test Questions In This Exercise You Will Use Inheritance To Read, Store, And Print Questions For A Test Empty امثلة محلولة :Test Questions In This Exercise You Will Use Inheritance To Read, Store, And Print Questions For A Test

مُساهمة من طرف أحمد مناع الأربعاء مارس 02, 2022 10:49 am

اهلا وسهلا بكم

راسلني أحد الاصدقاء لحل التمرين التالى فأحببت أن اشاركه معكم حتى تعم الفائدة على الجميع :

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

الحل :

أولا : تحديد المطلوب من التمرين :

يتطلب التمرين اربعة فئات بالاسماء و الصفات التالىة :

  1. فئة مجردة Abstract class بأسم TestQuestion  تحتوي على ما يلي:

    • متغير نصي محمي لتخزين سؤال الاختبار به.
    • طريقة مجردة محمية باسم readQuestion لقراءة اسئلة الاختبار


  • فئة فرعية من الفئة TestQuestion باسم Essay تحتوي على متغير رقمى int لتخزين عدد الاسطر المطلوبة لاجابة السؤال 
  • فئة فرعية من الفئة TestQuestion باسم MultChoice تحتوي على مصفوفة نصية لتخزين خيارات الاجابة على السؤال
  • فئة باسم WriteTest و التى يجب أن تقوم بعمل التالى :

    • إنشاء قائمة مصفوفة ArrayList من كائنات الفئة TestQuestion
    • قراءة اسئلة الاختبار من المستخدم 
    • حفظ كل عنصر في كائن ArrayList
    • استخدم حلقة لطباعة الأسئلة من ArrayList




    ثانيا : كتابة شفرة البرنامج

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

        }
    }



  • ـــــــــــــــــــ التوقيع ــــــــــــــــــــ
    سبحان الله وبحمدة .....سبحان الله العظيم
    أحمد مناع
    أحمد مناع
    .
    .

    تاريخ التسجيل : 15/02/2011
    المساهمات : 1108
    النقاط : 202034
    التقيم : 144
    الدولة : مصر
    الجنس : ذكر

    https://egy-tech.forumegypt.net

    الرجوع الى أعلى الصفحة اذهب الى الأسفل

    الرجوع الى أعلى الصفحة

    ََ

    مواضيع ذات صلة


     
    صلاحيات هذا المنتدى:
    لاتستطيع الرد على المواضيع في هذا المنتدى