Tutorial 10 Lecturer: Ken Jackson Tutor: Andria Hunter =========== Friday Tutorial Section: 980320 @1pm (MP102) Tuesday Tutorial Section: 980324 @6pm (RW142) CSC 108, Spring 1998 Tutorial notes, T10 ================================================================== - give back assignment 3 and go over the marking scheme and solution. - answer questions about the project. - do questions 7.15, 7.16 & 7.23 on pages 282 & 283 of the textbook (if time permits). Assignment 3 Overview ===================== 3 Main Classes: (1) CourseManager - contains the main method that: creates a CourseList object reference. reads in and processes each command by calling methods from the CourseList class (and possibly some methods from the Student class to check that the grades are in the appropriate range etc.). (2) CourseList - contains an array of objects of type Student, one element for each student in the course. - contains instance variables & methods associated with a course (3) Student - contains all the instance variables and methods associated with an individual student and all the associated methods. Marking Breakdown: Class Structure (2 marks) Style (2 marks) Correctness (5 marks) Testing (1 marks) Bonus (2 marks) Solution Shown in Tutorial: The overheads for this tutorial contain part of a basic solution for the assignment. They do not implement the extra checking that is required for bonus credit. Marking scheme for Assignment 3 =============================== Class Structure (2 marks) - Each program is required to have at least 3 classes: (Interaction class with main method, CourseList, Student) - The main method should process the input data by calling methods from the CourseList class (and possibly some methods from the Student class to check that the grades are in the appropriate range etc.). - The CourseList class should contain an array of objects of type Student, one element for each student in the course. The CourseList class should contain all instance variable and methods associated with a course -- including the course list array. - The main point here is that the Student class should contain all instance variables and methods associated with an individual student and all the associated methods. Style (2 marks) - comments (each class & method must be commented) - variable names not chosen well - does not use constants, especially for - improper indentation - improper use of whitespace - inappropriate prompts or output labels - repetitive or clumbsy code - bad if statements or loops Correctness (5 marks) - MaxCourseSize: - don't make an array if negative course sized entered - must be the first command processed, and can set the maximum course size once only - AddStudent: - make sure array is not already full - make sure this student's ID isn't already on file. - DeleteStudent: - do something reasonable if you try to delete a student that is not in the course list - AddMark: - do something reasonable if you try to add a mark for a student that is not in the course list - should overwrite previous mark instead of adding to it. - if you use an array in Student class for marks, make sure there is no chance you'd pass an invalid assign mark number. - ReportGrades: - should show NA for total if ComputeFinalGrades not used yet - all marks that haven't been entered should be shown as NA - ReportStats: - don't divide by zero if no students, - average should be displayed to 1 decimal place - average should not be calculated using integers (-1 mark) Testing (1 mark) - show non-standard student numbers - show marks out of the required range Bonus (2 marks) - handling white space in the input data appropriately (for example, if their input is "ReportStats " and they compare it to "ReportStats", the two strings won't be equal even though they really should be) - catching invalid integers and handling them appropriately rather than just crashing if parseInt throws a NumberFormatException - check that the student number is valid for each student number input - avoid adding a student with the same course number as another student already in the course list - check that the marks are in the right range before adding them to the student records -- do something reasonalbe if they are not - anything else that you think shows that they are thinking about exceptions Basically correct assignments get at least 5 marks. import java.io.*; class CourseManager { public static void main (String[] args) throws IOException { DataInputStream stdin = new DataInputStream (System.in); System.out.println ("\nType your command:"); String command = stdin.readLine(); // Loop until MaxCourseSize has been read in as first command. while (!command.equals("MaxCourseSize")) { System.out.println ("Invalid command."); System.out.println ("\nType your next command:"); command = stdin.readLine(); } // Loop until a positive course size is entered. System.out.println ("Enter course size:"); int size = Integer.parseInt(stdin.readLine()); while (size <= 0) { System.out.println ("Invalid size (must be >0)."); System.out.println ("Enter course size:"); size = Integer.parseInt(stdin.readLine()); } // Initialize the course_list to size entered. CourseList course_list = new CourseList ( // Main loop to process each command entered by the user while (!command.equals("Stop")) { if (command.equals("MaxCourseSize")) { System.out.println ("MaxCourseSize can only be entered once."); } else if (command.equals("AddStudent")) { System.out.println ( "Type the student's name:"); String student_name = stdin.readLine(); System.out.println ( "Type the student's number:"); String student_number = stdin.readLine()); course_list.add_student (student_name, student_number); } else if ... ... else { System.out.println ("Invalid command."); } // Read in next command System.out.println ("\nType your next command:"); command = stdin.readLine(); } } } class CourseList { private Student[] course_list; private int current_course_size; private final static int VOID_STUDENT_NUMBER = -999; // CourseList constructor CourseList (int size) { course_list = new Student[size]; current_course_size = 0; } // This method returns the index of the first student with the // student number student_number in the course list. It returns // VOID_STUDENT_NUMBER if there is no student with the student number // student_number in the course list. private int student_number_index (String student_number) { for (int i=0; i < current_course_size; i++) if (student_number.equals(course_list[i].get_student_number())) return i; return VOID_STUDENT_NUMBER; } // Add a new student to the end of the course_list. public void add_student (String student_name, String student_number) { int student_index = student_number_index(student_number); if (current_couse_size == course_list.length) { System.out.println ("Not enough room for this student."); } else if (student_index != VOID_STUDENT_NUMBER) { System.out.println ("Student ID entered is already on file."); } else { course_list[current_course_size] = new Student (student_name, student_number); current_course_size++; } } // Find a student with student number equal to the argument // student_number, delete that student from the course and moves the // last student down to fill the gap created by deleting a student. public void delete_student (String student_number) { int student_index = student_number_index(student_number); if (student_index == VOID_STUDENT_NUMBER) { System.out.println ("Student ID entered is not on file."); } else { course_list[student_index] = course_list[current_course_size-1]; current_course_size--; } } // Find a student with student number student_number and change that // student's mark for assignment which_assignment to assignment_mark. // If there is more than one student with this student number in the // course, this method changes the mark for the first one found only. // If there is no student with this student number in the course, this // method does nothing. public void add_assignment_mark (String student_number, int which_assignment, int assignment_mark) { int student_index = student_number_index(student_number); if (student_index == VOID_STUDENT_NUMBER) { System.out.println ("Student ID entered is not on file."); } else { course_list[student_index].add_assignment_mark (which_assignment, assignment_mark); } } } class Student { private String student_name, student_number; private int a1_mark, a2_mark, a3_mark, project_mark, term_test_mark, exam_mark, final_grade; private final static int MARK_UNINITIALIZED = -999 ; private final static int A1=1, A2=2, A3=3; // Student constructor Student (String student_name, String student_number) { this.student_name = student_name ; this.student_number = student_number ; a1_mark = MARK_UNINITIALIZED; a2_mark = MARK_UNINITIALIZED; a3_mark = MARK_UNINITIALIZED; project_mark = MARK_UNINITIALIZED; term_test_mark = MARK_UNINITIALIZED; exam_mark = MARK_UNINITIALIZED; final_grade = MARK_UNINITIALIZED; } // Get the student's number public String get_student_number () { return student_number; } // This routine changes this student's mark for this assignment. // If the assignment number is invalid, no mark is changed. public void add_assignment_mark (int which_assignment, int assignment_mark) { if (which_assignment == A1) { this.a1_mark = assignment_mark; } else if (which_assignment == A2) { this.a2_mark = assignment_mark; } else if (which_assignment == A3) { this.a3_mark = assignment_mark; } } } Questions from Chapter 7 ======================== // Programming Projects: Question 7.15 // Design and implement an applet that draws a red stop // sign with black letters. import java.applet.Applet; import java.awt.*; public class Q7_15 extends Applet { public void paint (Graphics page) { // Define the polygon points for stop sign octagon int[] xset = {80,100,120,140,140,120,100,80}; int[] yset = {80,60,60,80,100,120,120,100}; // Set applet background colour to white setBackground (Color.white); // Draw the red octagon for stop sign page.setColor (Color.red); page.fillPolygon (xset, yset, 8); // Draw black border around stop sign page.setColor (Color.black); page.drawPolygon (xset, yset, 8); // Print the word STOP on stop sign (in black) page.drawString("STOP",93,95); } } // Programming Projects: Question 7.15 // Design and implement an applet that draws a bullseye // by drawing five concentric circles centred around two // lines forming a crosshair. Fill every other circle // with a different colour. import java.applet.Applet; import java.awt.*; public class Q7_16 extends Applet { public void paint (Graphics page) { // Set applet background colour to white setBackground (Color.white); // Draw green outer circle page.setColor (Color.blue); page.fillOval (100,100,200,200); // Draw orange circle page.setColor (Color.green); page.fillOval (120,120,160,160); // Draw yellow circle page.setColor (Color.yellow); page.fillOval (140,140,120,120); // Draw blue circle page.setColor (Color.orange); page.fillOval (160,160,80,80); // Draw red circle page.setColor (Color.red); page.fillOval (180,180,40,40); // Draw two lines for the arrows page.setColor (Color.black); page.drawLine (60,200,100,200); page.drawLine (200,200,340,200); // Draw three lines for the arrow head page.drawLine (340,190,340,210); page.drawLine (340,210,360,200); page.drawLine (360,200,340,190); } } // Programming Projects: Question 7.15 // Design and implement an applet that simulates // the swinging movement of a pendulum. import java.applet.Applet; import java.awt.*; public class Q7_23 extends Applet { public void paint (Graphics page) { // Number of pauses between each redraw final int PAUSE_TIME = 100000; // Number of swings to display final int NUM_SWINGS = 300; // Set applet background colour to white setBackground (Color.white); page.setXORMode (getBackground()); // Pendulum arm will be drawn in black page.setColor (Color.black); // For each swing of the pendulum, draw a line at // several different positions on the applet. int x=100, y=200; // starting bottom position of // pendulum arm int direction = 1; // swing to right is 1, and // swing to left is -1 to reverse // Loop continues 'NUM_SWINGS' times for (int i=0; i= 300) direction = -1; else if (x <= 100) direction = 1; // Draw pendulum arm, pause, then redraw to // erase pendulum arm (due to XOR mode). // Note: Top of arm is always at (200,100). page.drawLine (200,100,x,200); for (int j=0; j