Tutorial 7 Lecturer: Ken Jackson Tutor: Andria Hunter ========== Friday Tutorial Section: 980227 @1pm (MP102) Tuesday Tutorial Section: 980303 @6pm (RW142) CSC 108, Spring 1998 Tutorial notes, T7 ================================================================== NOTICE THAT THE DAY SECTION (FRIDAY) AND EVENING SECTION (TUESDAY) WILL HAVE DIFFERENT TUTORIALS FOR TUTORIAL 7. Friday Afternoon Tutorial Topics: (February 27) -------------------------------- - midterm test Tuesday Evening Tutorial Topics: (March 3) ------------------------------- - take up midterm test and assignment 2 A1 Average = 81.3% A2 Average = 76.1% Midterm Average = 65.9% Solution for Assignment 2 ========================= import java.io.*; // Program processes a student's record and decides the // student's status in the following session. class Grade { public static void main (String [] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); // These variables should be commented! int maxCourses, mark; String currentSession, name, studentNumber, course, status; String courseSession; double total = 0.0, sessionTotal = 0.0; double countCourses = 0.0, countSession = 0.0; double average, sessionalAverage, weight, weightedMark; // read student info System.out.println ("Enter current session: "); currentSession = stdin.readLine(); System.out.println ("Enter student name: "); name = stdin.readLine(); System.out.println ("Enter student number: "); studentNumber = stdin.readLine(); System.out.println ("Enter number of marks: "); maxCourses = Integer.parseInt(stdin.readLine()); // process each course, reading in the course code, mark, // and session. Keep track of sums to calculate average. for (int count = 1; count <= maxCourses; count++) { // read course info System.out.println ("Enter course code: "); course = stdin.readLine(); System.out.println ("Enter mark: "); mark = Integer.parseInt(stdin.readLine()); System.out.println ("Enter session: "); courseSession = stdin.readLine(); // decide course weight if (MoreHelp.fullcourse(course)) { weight = 1; weightedMark = mark; } else { weight = 0.5; weightedMark = mark/2.0; } // accumulate totals if (currentSession.equals(courseSession)) { countSession+=weight; sessionTotal+=weightedMark; } countCourses+=weight; total+=weightedMark; } // Report error if no courses entered if (countCourses == 0) { System.out.println ("No courses were entered for this student."); } else { // calculate cumulative average average = total/countCourses; // calculate sessional average if (countSession == 0) { System.out.println ("Student has no marks for this session."); } else { sessionalAverage = sessionTotal/countSession; } // read current status System.out.println ("Enter current status: "); status = stdin.readLine(); // output results System.out.println("Student: " + name); System.out.println("Student number: " + studentNumber); System.out.println("Their cumulative average is " + Helper.twoDecPlaceString(average)); System.out.println("Their sessional average is " + Helper.twoDecPlaceString(sessionalAverage)); System.out.println("Their new status is " + MoreHelp.newStatus(status,average,sessionalAverage)); } } } // This class contains two static methods used in the // CSC108S assignment two solution. class MoreHelp { // Decide the status of a student based on three factors: // current status, cumulative average and sessional average. static String newStatus(String status, double average, double sessional) { final int MIN_AVE = 60; // cutoffs for average final int MAX_AVE = 62; final String GOOD = "In good standing"; // status codes final String PROB = "On probation"; final String SUSPEND = "Suspended"; final String UNKNOWN = "Unknown"; if (average > MIN_AVE) return GOOD; else if (status.equals(GOOD) && average < MIN_AVE && sessional < MAX_AVE) return PROB; else if (status.equals(PROB) && average < MIN_AVE && sessional >= MAX_AVE) return PROB; else if (status.equals(PROB) && average < MIN_AVE && sessional < MAX_AVE) return SUSPEND; else return UNKNOWN; } // Decide whether a course is a full or half term course // based on the last character in the course code. static boolean fullcourse(String course) { if (course.charAt(6) == 'Y' || course.charAt(6) == 'A' || course.charAt(6) == 'B') return true; else return false; } } // Assignment 2 Helper class class Helper { // This method takes a double value and returns a string // representing the value rounded to two decimal places. public static String twoDecPlaceString (double number) { // Create the string, without decimal places. // First, round to the nearest 0.01; then convert to a string // by concatenating with the null string. String result = "" + (int)(number*100 + 0.5); // Then decide where the decimal point should go. if (result.length() > 2) return result.substring(0,result.length()-2)+ "." +result.substring(result.length()-2); else if (result.length() == 2) return "0."+result; else return "0.0"+result; } } Marking Scheme for Assignment 2 =============================== Correctness [5 marks] 0.5 or 1 mark off for each error Style [3 marks] - comments - variable names not chosen well - does not use constants, especially for 60 and 62 - improper indentation - improper use of whitespace - inappropriate prompts - inappropriate output labels - repetitive code - clumsy code - bad if statements - bad loops Testing [2 marks] - tests no data case - tests sessional average zero, but cumulative average non-zero - tests all combinations of statuses - tests each course code (ends with Y,A,B,F,S,H, and invalid code) - tests that don't work correctly - annotate test files - tests marks out of range - tests anything related to special checks implemented by student Solution/Marking Scheme for Midterm =================================== Solution to question 1 ---------------------- >i = 4 + 2 * 3 : 10 > >i = 5 % 2 + 1 : 2 > >i = 3 * ( 1 / 2 ) : 0 > >i = 5.0 / 2.0 : > Error: The right side of the assignment statement > is a double but the left side is an int. > You cannot perform a widening type conversion > without an explicit cast. > >f = 2 * 3 : 6.0 > >i = (int) 7.8 : 7 > >s = "Good" + "Luck" : "GoodLuck" > >i = s.indexOf('d') : 3 > >b = 2 + 3 > 10 : false > >b = true || false : true These get 1 mark each. This mark is all or nothing (0 or 1), except for the answers that are "Error" ... in this case they get 0.5 for stating that it is an error, and 0.5 for correctly identifying the error. Solution to question 2(a) ------------------------- >class Part { [1 mark] > String name ; // you could declare these variable to be private > long number ; // it's not a good idea to make them public > double price; [2 marks] > Part ( String name, long number, double price ) { [2 marks] > this.name = name ; > this.number = number ; > this.price = price ; > } [1 marks] > public void print ( ) { > System.out.println ( "\nThe part name is: " + name > + "\nThe part number is: " + number > + "\nThe part price is: " + price ) ; > } [2 marks] >} Generally you'll be marking this by taking one point off for each major error such as wrong variable type, wrong constructor name, wrong parameters for constructor, missing types in parameter list, not having the word "this" in the constructor when parameter names are the same as the variables within the class, not having the print method return type declared as void, not labelling the output in the print method, etc... These deductions should be up to the maximum number of marks shown in square brackets for each section above. Solution to question 2(b) ------------------------- > Part new_part = new Part ( "Widget Wheel", 94523, 9.99 ) ; give 0, 0.5, or 1 mark > new_part.print () ; all or nothing ... give 0 or 1 mark. Solution to question 3 ---------------------- > boolean is_palindrome ( String word ) { [3 marks] > for ( int i = 0; i < word.length() / 2; i++ ) > // Note that if you use a <= above, the program does not work > // for empty strings. [3 marks] > if ( word.charAt(i) != word.charAt(word.length()-1-i) ) [2 marks] > return false ; [1 mark] > return true ; [1 mark] > } The marks shown above should only be used if the answer is close to correct. Otherwise you'll be marking using a deduction system where you deduct marks for each general error, up to approximately the number of marks allocated for each section above. You might want to take a look at a few papers before you actually start assigning the marks to get an overall idea of what kind of answers you might expect. Question 6.23 ============= Design and implement an application that reads an arbitrary number of integers that are in the range 0 to 50 inclusive and counts how many occurences of each is entered. After all input has been processed, print all of the values (with the number of occurences) that were entered one or more times. // Question 6.23, page 246 // Read in integers between 0 and 50 and report how many // of each number there were. // // Assumption: program stops reading numbers when an integer value // that is not in the range of 0 to 50 is entered. import java.io.*; public class q23 { // Main method to read the integers, store counts for each in an // array, and to report how many of each number there was. public static void main(java.lang.String[] args) throws IOException { DataInputStream stdin = new DataInputStream (System.in); final int MAXINT = 50; // Largest integer considered final int MININT = 0; // Smallest integer considered int[] list = new int[MAXINT+1]; // Array has counters for each int // Initialize all locations in the array to zero for (int i=0; i= MININT && value <= MAXINT) { list[value] = list[value] + 1; // Enter next integer value System.out.print ("Enter Integer: "); value = Integer.parseInt (stdin.readLine()); } // Print the summary. System.out.println("\nHere are how many of each value was entered:"); for (int i=0; i 0) System.out.print (i+": "+list[i]); } } } Sample Execution ---------------- Enter a list of integers between 0 and 50. To stop, enter an integer not in this range. Enter Integer: 23 Enter Integer: 18 Enter Integer: 23 Enter Integer: 42 Enter Integer: 18 Enter Integer: 23 Enter Integer: 5 Enter Integer: 50 Enter Integer: 50 Enter Integer: 0 Enter Integer: 0 Enter Integer: 0 Enter Integer: -40 Here are how many of each value was entered: 0: 3 5: 1 18: 2 23: 3 42: 1 50: 2 Question 6.24 ============= Modify the program in Problem 6.23 so that it works for numbers in the range between -25 and 25. // Question 6.24, page 246 // Read in integers between -25 and 25 and report how many // of each number there were. // // Assumption: program stops reading numbers when an integer value // that is not in the range of -25 to 25 is entered. import java.io.*; public class q24 { // Main method to read the integers, store counts for each in an // array, and to report how many of each number there was. public static void main(java.lang.String[] args) throws IOException { DataInputStream stdin = new DataInputStream (System.in); final int MAXINT = 50; // Largest integer considered final int MININT = 0; // Smallest integer considered final int OFFSET = 25; // offset from index zero int[] list = new int[MAXINT+1]; // Array has counters for each int // Initialize all locations in the array to zero for (int i=0; i= (MININT-OFFSET) && value <= (MAXINT-OFFSET)) { list[value+OFFSET] = list[value+OFFSET] + 1; // Enter next integer value System.out.print ("Enter Integer: "); value = Integer.parseInt (stdin.readLine()); } // Print the summary. System.out.println("\nHere are how many of each value was entered:"); for (int i=0; i 0) System.out.println ( i-OFFSET +": "+list[i]); } } } Sample Execution ---------------- Enter a list of integers between -25 and 25. To stop, enter an integer not in this range. Enter Integer: -10 Enter Integer: 3 Enter Integer: -10 Enter Integer: 15 Enter Integer: 3 Enter Integer: -10 Enter Integer: -25 Enter Integer: -25 Enter Integer: 0 Enter Integer: 0 Enter Integer: 0 Enter Integer: 25 Enter Integer: 25 Enter Integer: 26 Here are how many of each value was entered: -25: 2 -10: 3 0: 3 3: 2 15: 1 25: 2 Question 6.26 ============= Design and implement an application that creates a histogram, which allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1-10, 11-20, and so on. Print one asterisk for each value entered. 1 - 10 | ***** 11 - 20 | ** 21 - 30 | ******************* 31 - 40 | 41 - 50 | *** 51 - 60 | ******** 61 - 70 | ** 71 - 80 | ***** 81 - 90 | ******** 91 - 100 | *********** // Question 6.26, pages 246-247 // Read in integers between 1 and 100 and use a histogram to // report how many numbers were in each range of 10. // // Assumption: program stops reading numbers when an integer value // that is not in the range of 1 to 100 is entered. import java.io.*; public class q26 { // Main method to read the integers, store counts for each in an // array, and to report how many of each number there was. public static void main(java.lang.String[] args) throws IOException { DataInputStream stdin = new DataInputStream (System.in); final int MAXRANGE = 10; // Largest range (times 10) final int MINRANGE = 1; // Smallest range final int RANGE = 10; // 10 values in each interval int[] list = new int[MAXRANGE]; // Array has counters for each range // Initialize all locations in the array to zero for (int i=0; i= MINRANGE && value <= (MAXRANGE*RANGE)) { // Divide by range to determine which range to increment list[(value-1)/RANGE] = list[(value-1)/RANGE] + 1; // Enter next integer value System.out.print ("Enter Integer: "); value = Integer.parseInt (stdin.readLine()); } // Print the histogram System.out.println ("\nHere is your histogram:"); for (int i=0; i