Assignment 1: Task 2 Part 1 - Elements Of The GUI ✓ Solved

ASSIGNMENT #1: Task 2, Part 1 - Elements of the GUI Task 2

In this part of your assignment, you are required to produce a fully functional GUI program to determine and display the final mark of students enrolled in the “Programming in Java 2” subject. It will also calculate and display the average mark for all students entered into the system.

Part 1: Elements of the GUI

The GUI must include the following elements:

  • An appropriate title appearing on top of the main frame of the GUI (e.g., “Programming in Java 2”)
  • Five text fields with appropriate labels namely “Student ID”, “Quiz”, “Assignment 1”, “Assignment 2”, and “Final Exam” to take user inputs
  • One read-only text area with an appropriate label (“Results”) used to display outputs
  • Two buttons (“Student Mark” and “Average Mark”) used to trigger calculations
  • Tooltip text display for the buttons, text fields, and the text area
  • Three panels to be added to the main frame containing the other components

1.1 Algorithm: Pseudocode

1. Start the program.

2. Create main frame.

3. Add title to the main frame.

4. Create panels for text fields, results area, and buttons.

5. Define action listeners for buttons.

6. Initialize input fields and results area.

7. Validate inputs.

8. Calculate final mark based on weights.

9. Display results in results area.

10. Calculate average mark and display results.

11. End the program.

1.2 Source Code

// Sample Source Code - Java

import javax.swing.*;

import java.awt.event.*;

public class GradeCalculator {

public static void main(String[] args) {

JFrame frame = new JFrame("Programming in Java 2");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(400, 300);

// Create panels

JPanel inputPanel = new JPanel();

JPanel buttonPanel = new JPanel();

JPanel resultPanel = new JPanel();

// Create text fields

JTextField studentIdField = new JTextField(10);

JTextField quizField = new JTextField(5);

JTextField assignment1Field = new JTextField(5);

JTextField assignment2Field = new JTextField(5);

JTextField finalExamField = new JTextField(5);

JTextArea resultArea = new JTextArea(5, 20);

resultArea.setEditable(false);

// Add labels and fields to the input panel

inputPanel.add(new JLabel("Student ID:"));

inputPanel.add(studentIdField);

inputPanel.add(new JLabel("Quiz:"));

inputPanel.add(quizField);

inputPanel.add(new JLabel("Assignment 1:"));

inputPanel.add(assignment1Field);

inputPanel.add(new JLabel("Assignment 2:"));

inputPanel.add(assignment2Field);

inputPanel.add(new JLabel("Final Exam:"));

inputPanel.add(finalExamField);

// Add buttons to the button panel

JButton studentMarkButton = new JButton("Student Mark");

JButton averageMarkButton = new JButton("Average Mark");

buttonPanel.add(studentMarkButton);

buttonPanel.add(averageMarkButton);

// Add result area to the result panel

resultPanel.add(new JLabel("Results:"));

resultPanel.add(resultArea);

// Add panels to the frame

frame.getContentPane().add(inputPanel, "North");

frame.getContentPane().add(buttonPanel, "South");

frame.getContentPane().add(resultPanel, "Center");

// Button action listeners

studentMarkButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// Calculate final mark logic

// Update results area

}

});

averageMarkButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// Calculate average logic

// Update results area

}

});

frame.setVisible(true);

}

}

1.3 Analysis

During the creation of this program, challenges may arise, such as ensuring that the input validation accurately reflects the requirements (0-100 for each assessment item) and managing event listeners effectively to update the results area. Furthermore, debugging may be necessary to ensure that the final mark and average calculations function correctly.

1.4 Testing and Output

Four sets of testing data could include the following:

  • Set 1: Student ID: 12345678, Quiz: 80, Assignment 1: 75, Assignment 2: 85, Final Exam: 90
  • Set 2: Student ID: 87654321, Quiz: 60, Assignment 1: 70, Assignment 2: 65, Final Exam: 80
  • Set 3: Student ID: 23456789, Quiz: 90, Assignment 1: 95, Assignment 2: 100, Final Exam: 80
  • Set 4: Student ID: 98765432, Quiz: 80, Assignment 1: 50, Assignment 2: 55, Final Exam: 70

Part 2: Functionality

In this part, the GUI program must also ensure that user input for marks is validated between 0 and 100. Upon pressing the “Student Mark” button, the program should calculate and display the final marks and the average across all students.

2.1 Algorithm: Pseudocode

1. Start the program.

2. Use a loop to allow multiple student entries.

3. Validate input values.

4. Calculate and store marks for each submission.

5. Upon button click, compute final and average marks.

6. Display results in the results area.

7. Repeat or exit based on user choice.

2.2 Source Code

// Cont'd Sample Source Code - Java

// Add validation in button action listener

studentMarkButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try {

int quiz = Integer.parseInt(quizField.getText());

int assignment1 = Integer.parseInt(assignment1Field.getText());

int assignment2 = Integer.parseInt(assignment2Field.getText());

int finalExam = Integer.parseInt(finalExamField.getText());

if(quiz >= 0 && quiz = 0 && assignment1

&& assignment2 >= 0 && assignment2 = 0 && finalExam

// Calculate final mark

double finalMark = (quiz 0.05) + (assignment1 0.20) + (assignment2 0.25) + (finalExam 0.50);

resultArea.append("Final mark for " + studentIdField.getText() + ": " + finalMark + "\n");

} else {

JOptionPane.showMessageDialog(frame, "Marks must be between 0 and 100.");

}

} catch (NumberFormatException ex) {

JOptionPane.showMessageDialog(frame, "Invalid input. Please enter integers only.");

}

}

});

// Similar for average button

2.3 Analysis

Common issues may pertain to the input validation, where incorrect data types could lead to exceptions that must be handled adequately. Furthermore, logical errors in mark calculations may need to be debugged.

2.4 Testing and Output

For testing average calculations, we can use the same sets of data and verify that the average displayed reflects the expected results accurately, ensuring that inputs lead to the correct calculations across multiple entries.

References

  • Deitel, P. J. & Deitel, H. M. (2019). Java: How to Program (11th ed.). Pearson.
  • Horstmann, C. S. (2020). Core Java Volume I - Fundamentals (11th ed.). Prentice Hall.
  • Bloch, J. (2018). Effective Java (3rd ed.). Addison-Wesley.
  • GeeksforGeeks. (n.d.). Java GUI Programming. Retrieved from https://www.geeksforgeeks.org/java-gui-programming/
  • Oracle. (n.d.). Swing Tutorial. Retrieved from https://docs.oracle.com/javase/tutorial/ui/index.html
  • Martin, R. C. (2011). Clean Code: A Handbook of Agile Software Craftsmanship. Prentice Hall.
  • Effective Java Programming Language Guide from Oracle. (n.d.). Retrieved from https://docs.oracle.com/javase/tutorial/java/index.html
  • Fowler, M. (2018). Refactoring: Improving the Design of Existing Code. Addison-Wesley.
  • Schildt, H. (2018). Java: A Beginner's Guide (8th ed.). McGraw-Hill.
  • Java Documentation (n.d.). Retrieved from https://docs.oracle.com/en/java/javase/11/docs/api/index.html