Java Assignment Solutions and Class Design Analysis

First Java Assignment Implementation

Transitioning from C to Java presents unique challenges due to Java's object-oriented nature and enterprise application focus. The initial solution involved multiple interconnected classes with complex relationships.

Class Structure and Relationships

The solution employed three primary classes: Main, AnswerSheet, and Question. The AnswerSheet class contained an array of Question objects, while the Main class coordinated the overal logic. Key technical elements included:

  • Regular expression pattern matching for input parsing
  • Array-based storage of question objects
  • Encapsulation of question handling logic
<code>
Pattern inputPattern = Pattern.compile("#N:(\\d+)\\s+#Q:(.+?)\\s+#A:(.+)");
int questionCount = inputReader.nextInt();
inputReader.nextLine();

for (int i = 0; i < questionCount; i++) {
  String inputLine = inputReader.nextLine();
  Matcher matcher = inputPattern.matcher(inputLine);
  
  if (matcher.find()) {
    int questionId = Integer.parseInt(matcher.group(1));
    String questionText = matcher.group(2);
    String correctAnswer = matcher.group(3);
    // Question processing logic
  }
}
</code>

Implementation Challenges

The initial approach suffered from overcomplicated array relationships and insufficient class separation. Key improvements included:

  • Implementing proper class relationships between Paper, Answer, and Question
  • Moving output methods to the Paper class
  • Simplifying array management

Enhanced Exam System Implementation

The second assignment expanded functionality with a more robust class structure:

Class Hierarchy

<code>
class ExamQuestion {
  int questionId;
  String questionText;
  String correctResponse;
  
  ExamQuestion(int id, String text, String answer) {
    this.questionId = id;
    this.questionText = text;
    this.correctResponse = answer;
  }
}

class ExamPaper {
  int examId;
  Map<Integer, Integer> questionPoints = new HashMap<>();
  
  ExamPaper(int id) {
    this.examId = id;
  }
  
  void addQuestionPoints(int questionId, int points) {
    questionPoints.put(questionId, points);
  }
}

class StudentResponse {
  int examId;
  List<QuestionAnswer> answers = new ArrayList<>();
  
  StudentResponse(int id) {
    this.examId = id;
  }
  
  void addResponse(int questionId, String answer) {
    answers.add(new QuestionAnswer(questionId, answer));
  }
}

class QuestionAnswer {
  int questionId;
  String studentAnswer;
  
  QuestionAnswer(int id, String answer) {
    this.questionId = id;
    this.studentAnswer = answer;
  }
}
</code>

Input Processing Logic

<code>
Map<Integer, ExamQuestion> questions = new HashMap<>();
Map<Integer, ExamPaper> exams = new HashMap<>();

while (scanner.hasNextLine()) {
  String input = scanner.nextLine();
  if (input.equals("end")) break;
  
  if (input.startsWith("#N:")) {
    String[] components = input.split(" ");
    int id = Integer.parseInt(components[0].substring(4));
    String text = components[1].substring(4);
    String answer = components[2].substring(4);
    questions.put(id, new ExamQuestion(id, text, answer));
  } 
  else if (input.startsWith("#T:")) {
    String[] components = input.split(" ");
    int examId = Integer.parseInt(components[0].substring(4));
    ExamPaper exam = new ExamPaper(examId);
    
    for (int i = 1; i < components.length; i++) {
      String[] questionData = components[i].split("-");
      exam.addQuestionPoints(Integer.parseInt(questionData[0]), 
                            Integer.parseInt(questionData[1]));
    }
    exams.put(examId, exam);
  }
}
</code>

Advanced Exam System with Student Management

The third assignment introduced student management and qeustion validation:

Extended Class Structure

<code>
class ExamTaker {
  int studentId;
  String studentName;
  
  ExamTaker(int id, String name) {
    this.studentId = id;
    this.studentName = name;
  }
}

class InvalidQuestion {
  int questionId;
  
  InvalidQuestion(int id) {
    this.questionId = id;
  }
}

class QuestionReferenceError {
  int questionId;
  String providedAnswer;
  
  QuestionReferenceError(int id, String answer) {
    this.questionId = id;
    this.providedAnswer = answer;
  }
}
</code>

Implementatoin Challenges

The increasing complexity revealed several issues:

  • Difficulty managing multiple class relationships
  • Challenges implementing question validation logic
  • Problems handling deleted or invalid questions

Tags: java Object-Oriented Programming Class Design Exam System regular expressions

Posted on Sat, 22 Aug 2026 16:31:09 +0000 by dcmbrown