Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA II using the driver which creates the frame and the panel completely separa

ID: 3880453 • Letter: J

Question

JAVA II

using the driver which creates the frame and the panel completely separate.

for some students, this will be a review. For other students, this is your first introduction to GUIs in Java. Either way, keep your solution simple this week. You are not permitted to use a Layout Manager for this assignment. This is to be a simple GUI solution. You can control layout a little by changing the size of the panel. You are primarily using text fields, labels, and buttons for this assignment.
Dr. Java teaches a class which uses the following weighted percentages for student grades:
60% from homework
15% from weekly quizzes
25% from the final exam
You will create a Java application which Dr. Java can use to calculate a student’s final grade. To do this:
1. Create a driver similar to the PushCounter class on page 248. This driver instantiates a panel object from a new class that was created for this specific GUI example. Explicitly, in PushCounter, an object named panel was instantiated from a new class called PushCounterPanel. PushCounterPanel is a new class that is defined on pages 249-250. You are to create a driver for this homework assignment which is similar in format to PushCounter. Name your driver, ClassAverage
2. Create a class similar to PushCounterPanel that defines all the GUI components you need for the assignment described below. You should not create multiple panels. Use only one panel. I want simple solutions this week. Overcomplicating your homework assignment will cause a deduction in points. Remember you are NOT to use a Layout Manager for this assignment. Name your panel ClassAveragePanel.
Grading Guidelines – REMEMBER USE ONLY ONE PANEL CLASS
(3 points) Input areas
Create the appropriate GUI components to input the following 3 items (all on a scale of 0-100):
o Homework average
o Quiz average
o Final exam score
(3 points) Output area
Create an appropriate GUI component to display a student’s course average
(3 points) Headings and Clearly Labeled Components
Create appropriate GUI components to format the panel with headings and clearly identified input and output areas of the panel
Each GUI you create should be easy to use and clearly defines input and output areas for the user (so that know how to use the panel)
(3 points) Calculate, Clear, and Exit Buttons
Add three buttons to the panel:
o Calculate course average
o Clear data
o Exit
(8 points) Button listener
You are to create one Button listener to properly handle the three buttons
For the calculate button:
o Use the values entered by the user and the weighting factors for the 3 categories to calculate a student’s course average.
o Example:
Homework average of 92%
Quiz average of 84.5%
Final exam of 74%
The course average will be: 86.4% (rounded to one decimal place)
For the clear button:
o Clear all of the data in the input fields and output area.
o Place the curser in the first text field (set the focus to the text field) so that the form is ready for the user to enter data again.
o There is a method named requestFocusInWindow() which will place the focus on a specific component (a button, a text field, etc.). For example, if there is a JTextField object named firstName, the following will set the focus to the firstName text field:
firstName.requestFocusInWindow();
For the exit button:
This button will end and close the application when clicked.
Do a quick search on System.exit(0);

Explanation / Answer

//following are the classes required in the question

//class ClassAveragePanel

public class ClassAveragePanel extends JPanel {
public ClassAveragePanel() {
  JTextField tf1 = new JTextField();
  JTextField tf2 = new JTextField();
  JTextField tf3 = new JTextField();
  JLabel l1 = new JLabel("Homework Average");
  JLabel l2 = new JLabel("Quiz Average");
  JLabel l3 = new JLabel("Final Exam Score");
  JLabel l4 = new JLabel("Output");
  JLabel l5 = new JLabel("Answer");
  JButton Calculate = new JButton("Calculate");
  JButton Clear = new JButton("Clear");
  JButton Exit = new JButton("Exit");
  setLayout(null);
  tf1.setBounds(180, 48, 187, 20);
  tf2.setBounds(180, 96, 187, 20);
  tf3.setBounds(180, 144, 187, 20);
  l1.setBounds(61, 51, 130, 14);
  l2.setBounds(90, 99, 110, 14);
  l3.setBounds(72, 147, 110, 14);
  l4.setBounds(121, 275, 110, 14);
  l5.setBounds(235, 272, 110, 20);
  Calculate.setBounds(61, 197, 100, 50);
  Clear.setBounds(171, 197, 100, 50);
  Exit.setBounds(281, 197, 100, 50);
  add(tf1);
  add(tf2);
  add(tf3);
  add(l1);
  add(l2);
  add(l3);
  add(l4);
  add(l5);
  add(Calculate);
  add(Clear);
  add(Exit);
  
  ActionListener calc = new ActionListener() {
   
   public void actionPerformed(ActionEvent e) {
    float homework = Float.parseFloat(tf1.getText());
    float quiz = Float.parseFloat(tf2.getText());
    float finalExam = Float.parseFloat(tf3.getText());
    
    double courseAverage = (homework*0.60)+(quiz*0.15)+(finalExam*0.25);
    courseAverage = Math.round(courseAverage*10);
    courseAverage = courseAverage/10;
    l5.setText(Double.toString(courseAverage));
   }
  };
  
  ActionListener clear = new ActionListener() {
   
   public void actionPerformed(ActionEvent e) {
    tf1.setText("");
    tf2.setText("");
    tf3.setText("");
    l5.setText("");
    tf1.requestFocusInWindow();
   }
  };
  
  ActionListener exit = new ActionListener() {
   
   public void actionPerformed(ActionEvent e) {
    System.exit(0);
   }
  };
  
  Calculate.addActionListener(calc);
  Clear.addActionListener(clear);
  Exit.addActionListener(exit);
}

  
}

//class ClassAverage - it is a jframe that i have created according to me as exact references were not mentioned

public class ClassAverage extends JFrame {
public ClassAverage() {
  setBounds(100, 100, 700, 470);
}
static ClassAverage avg = new ClassAverage();

public static void main(String[] args) {
  ClassAveragePanel p1 = new ClassAveragePanel();
  avg.setContentPane(p1);
  p1.setVisible(true);
  avg.setVisible(true);
}

}