Write and test a Java project that satisfies the following r

Write and test a Java project that satisfies the following requirements: Create a class definition file and a test file as follows: Create a class definition file for a Question item for a test.The class will have these fields: questionText (String) points for the question (from 1 to 5) (integer) test ID for the test version the question appears on (String) Write a default constructor and a second constructor that accepts values for each of thefields. Write a toString( ) method using the values for all the fields. Write a compareTo( ) method using the points field. Write an equals( ) method using the questionText. Once the Question class compiles write a Driver class with the main( ) method. The main( ) method will create an array of five Question object references. It will thencreate the Question objects themselves. Use file input and the StringTokenizer classmethod to read the data for each object. The name of the data file is “questions.txt”. Thedata file is comma delimited. After the array has been created use a loop to print the data of each object referred to bythe array. Test the equals( ) method using the first Question object and the last Question object. Test the compareTo( ) method by using the second Question object and the last Questionobject.

Solution

public class Question implements Comparable<Question> {

   private String questionText;
   private int points;
   private String testID;

   public Question() {
       // TODO Auto-generated constructor stub
   }

   /**
   * @param questionText
   * @param points
   * @param testID
   */
   public Question(String questionText, int points, String testID) {
       this.questionText = questionText;
       this.points = points;
       this.testID = testID;
   }

   /**
   * @return the questionText
   */
   public String getQuestionText() {
       return questionText;
   }

   /**
   * @param questionText
   * the questionText to set
   */
   public void setQuestionText(String questionText) {
       this.questionText = questionText;
   }

   /**
   * @return the points
   */
   public int getPoints() {
       return points;
   }

   /**
   * @param points
   * the points to set
   */
   public void setPoints(int points) {
       this.points = points;
   }

   /**
   * @return the testID
   */
   public String getTestID() {
       return testID;
   }

   /**
   * @param testID
   * the testID to set
   */
   public void setTestID(String testID) {
       this.testID = testID;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return \"Question [questionText=\" + questionText + \", points=\" + points
               + \", testID=\" + testID + \"]\";
   }

   @Override
   public boolean equals(Object arg) {
       // TODO Auto-generated method stub
       if (arg instanceof Question) {
           Question question = (Question) arg;
           if (this.getQuestionText().equals(question.getQuestionText()))
               return true;
           else
               return false;

       } else
           return false;
   }

   @Override
   public int compareTo(Question o) {
       // TODO Auto-generated method stub
       return this.getPoints() - o.getPoints();
   }

}

import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;

public class QuestionTest {

   public static void main(String[] args) {

       Question[] questions = new Question[5];

       Scanner scanner = null;
       try {
           scanner = new Scanner(new File(\"questions.txt\"));
           int count = 0;
           while (scanner.hasNext()) {
               String line = scanner.nextLine();

               StringTokenizer tokenizer = new StringTokenizer(line, \",\");

               questions[count] = new Question(tokenizer.nextToken(),
                       Integer.parseInt(tokenizer.nextToken()),
                       tokenizer.nextToken());
               count++;
           }

       } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
       }

       System.out.println(\"Question using loop : \");
       for (int i = 0; i < questions.length; i++) {
           System.out.println(questions[i]);
       }

       Arrays.sort(questions);

       System.out.println(\"Question using loop After sorting: \");
       for (int i = 0; i < questions.length; i++) {
           System.out.println(questions[i]);
       }

       System.out.println(\"Comparing first and last object :\"
               + questions[0].equals(questions[4]));
   }
}

questions.txt

question test 1,4,1
question test 2,5,2
question test 3,3,3
question test 4,2,4
question test 5,1,5

OUTPUT:

Question using loop :
Question [questionText=question test 1, points=4, testID=1]
Question [questionText=question test 2, points=5, testID=2]
Question [questionText=question test 3, points=3, testID=3]
Question [questionText=question test 4, points=2, testID=4]
Question [questionText=question test 5, points=1, testID=5]
Question using loop After sorting:
Question [questionText=question test 5, points=1, testID=5]
Question [questionText=question test 4, points=2, testID=4]
Question [questionText=question test 3, points=3, testID=3]
Question [questionText=question test 1, points=4, testID=1]
Question [questionText=question test 2, points=5, testID=2]
Comparing first and last object :false

Write and test a Java project that satisfies the following requirements: Create a class definition file and a test file as follows: Create a class definition fi
Write and test a Java project that satisfies the following requirements: Create a class definition file and a test file as follows: Create a class definition fi
Write and test a Java project that satisfies the following requirements: Create a class definition file and a test file as follows: Create a class definition fi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site