Tutorial 4 Lecturer: Ken Jackson Tutor: Andria Hunter ========== Friday Tutorial Section: 980130 @1pm (MP102) Tuesday Tutorial Section: 980203 @6pm (RW142) CSC 108, Spring 1998 Tutorial notes, T4 ================================================================== Tutorial Topics: - Take up Assignment 1 - point out common errors - explain marking scheme - questions 4.18 and 4.19 on page 169 of the Lewis and Loftus textbook. - answer questions about chapter 4 and about assignment 2 Assignment 1 Solution: ===================== // Asst 1: a solution for Assignment 1, CSC108S, spring 1998 import java.io.*; // Class Asst1 contains a main method that is used to read in // marks for a student and to summarize mark information. public class Asst1 { // Main method to read in course marks and to print a summary. public static void main(java.lang.String[] args) throws IOException { DataInputStream stdin = new DataInputStream (System.in); final String LISTENDER = "XXX"; // flag to signal end of input final int PASSMARK = 50; // Marks lower than this fail // Read the student's name and number. System.out.println ("Student name? "); String name = stdin.readLine(); System.out.println ("Student number? "); String number = stdin.readLine(); // (could be int or String) int numMarks = 0; // Count Number of Marks int numFailures = 0; // Count Number of Failing Marks int sum = 0; // Sum of Marks // Loop to read grades until user enters sentinel number while (true) { // Read in course name and check for sentinel number System.out.println ("Next course name?"); String course = stdin.readLine(); if (course.equals(LISTENDER)) // Exit if sentinel number read break; // Read in next mark and update counters System.out.println ("Mark for this course?"); int mark = Integer.parseInt(stdin.readLine()); numMarks = numMarks + 1; // or numMarks += 1; or numMarks++; sum = sum + mark; if (mark < PASSMARK) numFailures += 1; } // Print the summary. System.out.println ("\nSummary of record for student " + name + " (student number " + number + ")"); if (numMarks == 0) System.out.println ("\n No marks read!"); else { double average = (double) sum/numMarks; int intAverage = (int) (average + 0.5); System.out.println ("\n Average = " + intAverage); System.out.println ("\n Number of failures: " + numFailures); } } } Assignment 1 Marking Notes ========================== Correctness: - did not check for division by zero when calculating average (-1) - average is calculted as double, but did not round (-.5) (no deduction for integer division for average) - Inappropriate prompt messages, or missing prompts (-.5) - Not having a loop that allows marks to be entered continuously, and other problems with the program's correctness (up to -5) - Comparison to exit while loop should be something like: course_code.equals(SENTINEL) Style: - Variable names not chosen well (-.5) - Did not use a constant for "XXX" or failing mark of 50 (-.5) NOTE: Use upper case for constant names, and lower case for variable names. Here are 2 example constants: final String SENTINEL = "XXX"; final int FAIL_MARK = 50; - Improper indentation (-.5) - Not using enough whitespace, or too much whitespace (-.5) Comments: - each method, class, block should be commented (-.5 or -1) NOTE: Comments should be meaningful. Comments should be placed BEFORE the section of code that they describe. Use "//" if comment is only one line long - variable names should be commented (-.5) - top of program should have initial comments that include: (-.5) Your name, ID, Prof name, TA name, Tutorial Section (Friday or Tuesday), A brief program description. Testing: - improper input echoing (no deduction this time) - did not annotate your test files (-.5) - did not test both passing and failing marks (-.5) - did not test marks out of range (<0 and >100) (-.5) - did not test no marks entered (giving an average of 0) (-.5) (i.e. XXX is entered before any grades are entered) - did not test non-standard student ID numbers (-.5) Question 4.18 ============= Write a class called Bank_Account that stores the current balance of the account and contains two methods to debit and credit the account. Define a third method that returns the current balance. Pass a value into a constructor to set an initial balance. Write a main method that instantiates two bank accounts and exercises the methods of the class. Sample execution: (For Method 2: Bank_Account2.java) ---------------- Enter starting balance of account 1: 10 Account 1 balance: 10.0 Enter deposit amount for account 1: 5 Account 1 balance: 15.0 Enter withdrawl amount for account 1: 3 Account 1 balance: 12.0 Enter starting balance of account 2: 150 Account 2 balance: 150.0 Enter deposit amount for account 2: 40 Account 2 balance: 190.0 Enter withdrawl amount for account 2: 100 Account 2 balance: 90.0 Account 1 final balance: 12.0 Account 2 final balance: 90.0 Question 4.18 Solution - Method 1 ================================= //package Chap4; import java.io.*; // CSC108 Chapter 4, Question 18 // Name: Iam Me Student ID: 555555555 // Tutor: Andria Hunter Prof: Ken Jackson // // Program Description: This program uses the Bank_Account class // to create two bank account objects. It then uses the // debit, credit, and get_balance methods on these objects. // This program uses two classes: one for the bank account // and its operations (Bank_Account), and one that contains // the main method (CIBC_Bank). // The Bank_Account class contains a variable that stores the // current balance, and methods that can be used to deposit, // withdraw, and display the current balance. class Bank_Account { // Stores the current balance of the Bank_Account object. private double balance; // Constructor to initialize the account balance. public Bank_Account (double start_balance) { balance = start_balance; } // Method to decrease the account balance by the amount passed. public void debit (double amount) { balance = balance - amount; } // Method to increase the account balance by the amount passed. public void credit (double amount) { balance = balance + amount; } // Method that returns the current balance of the account. public double get_balance () { return balance; } } // The CIBC_Bank class contains one main method where program execution // starts. The two bank accounts are created in this method. class CIBC_Bank { // Main method to create two bank account (Bank_Account) objects, // and to perform various methods on these objects. public static void main (String[] args) throws IOException { //--------- // Create account 1 and use methods on this account. Bank_Account account_one = new Bank_Account (10.0); account_one.credit(5.0); account_one.debit(3.0); System.out.println ("Account 1 final balance: "+ account_one.get_balance()); //--------- // Create account 2 and use methods on this account. Bank_Account account_two = new Bank_Account (150.0); account_two.credit(40.0); account_two.debit(100.0); System.out.println ("Account 2 final balance: "+ account_two.get_balance()); } } Question 4.18 Solution - Method 2 ================================= //package Chap4; import java.io.*; // CSC108 Chapter 4, Question 18 // Name: Iam Me Student ID: 555555555 // Tutor: Andria Hunter Prof: Ken Jackson // // Program Description: This program uses the Bank_Account2 class // to create two bank account objects. It then uses the // debit, credit, and get_balance methods on these objects. // This program is different than the program that uses the // Bank_Account class, because the values are read from input. // This program uses two classes: one for the bank account // and its operations (Bank_Account2), and one that contains // the main method (CIBC_Bank2). // The Bank_Account2 class contains a variable that stores the // current balance, and methods that can be used to deposit, // withdraw, and display the current balance. class Bank_Account2 { // Stores the current balance of the Bank_Account2 object. private double balance; // Constructor to initialize the account balance. public Bank_Account2 (double start_balance) { balance = start_balance; } // Method to decrease the account balance by the amount passed. public void debit (double amount) { balance = balance - amount; } // Method to increase the account balance by the amount passed. public void credit (double amount) { balance = balance + amount; } // Method that returns the current balance of the account. public double get_balance () { return balance; } } // The CIBC_Bank2 class contains one main method where program execution // starts. The two bank accounts are created in this method. class CIBC_Bank2 { // Main method to create two bank account (Bank_Account2) objects, // and to perform various methods on these objects. Unlike the // program that uses the Bank_Account class, the values are read // from input. public static void main (String[] args) throws IOException { // Declare stdin so data can be read from input. DataInputStream stdin = new DataInputStream (System.in); //--------- // Create account 1 and use debit and credit on this account. // Enter the starting balance of account 1 from stdin System.out.println ("Enter starting balance of account 1: "); double start1 = new Double(stdin.readLine()).doubleValue(); Bank_Account2 account1 = new Bank_Account2 (start1); System.out.println ("Account 1 balance: "+account1.get_balance()); // Deposit into Account 1 System.out.println ("\nEnter deposit amount for account 1: "); double deposit1 = new Double(stdin.readLine()).doubleValue(); account1.credit(deposit1); System.out.println ("Account 1 balance: "+account1.get_balance()); // Withdraw from Account 1 System.out.println ("\nEnter withdrawl amount for account 1: "); double withdraw1 = new Double(stdin.readLine()).doubleValue(); account1.debit(withdraw1); System.out.println ("Account 1 balance: "+account1.get_balance()); //--------- // Create account 2 and use debit and credit on this account. // Enter the starting balance of account 2 from stdin System.out.println ("\nEnter starting balance of account 2: "); double start2 = new Double(stdin.readLine()).doubleValue(); Bank_Account2 account2 = new Bank_Account2 (start2); System.out.println ("Account 2 balance: "+account2.get_balance()); // Deposit into Account 2 System.out.println ("\nEnter deposit amount for account 2: "); double deposit2 = new Double(stdin.readLine()).doubleValue(); account2.credit(deposit2); System.out.println ("Account 2 balance: "+account2.get_balance()); // Withdraw from Account 2 System.out.println ("\nEnter withdrawl amount for account 2: "); double withdraw2 = new Double(stdin.readLine()).doubleValue(); account2.debit(withdraw2); System.out.println ("Account 2 balance: "+account2.get_balance()); //--------- // Report balances of the two accounts System.out.println ("\nAccount 1 final balance: "+ account1.get_balance()); System.out.println ("Account 2 final balance: "+ account2.get_balance()); } } Question 4.19 =============== Write a class called Triangle that can be used to represent a triangle. It should include the following methods that return boolean values indicating if the particular property holds: (a) is_right (a right triangle) (b) is_scalene (no two sides are the same length) (c) is_isosceles (exactly two sides are the same length) (d) is_equilateral (all three sides are the same length) Sample execution: ---------------- Enter side1 length: 5 Enter side2 length: 5 Enter side3 length: 5 Is triangle right-angle? false Is triangle scalene? false Is triangle isosceles? false Is triangle equilateral? true Do you want to examine more triangles? (type 'y' for yes or 'n' for no) y Enter side1 length: 4 Enter side2 length: 5 Enter side3 length: 4 Is triangle right-angle? false Is triangle scalene? false Is triangle isosceles? true Is triangle equilateral? false Do you want to examine more triangles? (type 'y' for yes or 'n' for no) y Enter side1 length: 4 Enter side2 length: 5 Enter side3 length: 6 Is triangle right-angle? false Is triangle scalene? true Is triangle isosceles? false Is triangle equilateral? false Do you want to examine more triangles? (type 'y' for yes or 'n' for no) y Enter side1 length: 3 Enter side2 length: 4 Enter side3 length: 5 Is triangle right-angle? true Is triangle scalene? true Is triangle isosceles? false Is triangle equilateral? false Do you want to examine more triangles? (type 'y' for yes or 'n' for no) n Question 4.19 Solution ====================== import java.io.*; // CSC108 Chapter 4, Question 19 // Name: Iam Me Student ID: 555555555 // Tutor: Andria Hunter Prof: Ken Jackson // // Program Description: This program uses the Triangle class // to create a triangle object. It then uses the is_right, // is_scalene, is_isosceles, and is_equilateral methods // to test what type of triangle it is. // The Triangle class contains three variables to store the length // of each side of the triange, and methods that can be used to determine // determine if a triange is right, scalene, isoscelese, and equilateral. class Triangle { // Stores the length of each side of the Triangle object. private int side1, side2, side3; // Constructor to initialize the sides of the triangle. public Triangle (int s1, int s2, int s3) { side1 = s1; side2 = s2; side3 = s3; } // Method to test for a right-angled triangle. public boolean is_right () { if (((side1*side1) == ((side2*side2) + (side3*side3))) || ((side2*side2) == ((side1*side1) + (side3*side3))) || ((side3*side3) == ((side1*side1) + (side2*side2)))) return true; else return false; } // Method to test for a scalene triangle. public boolean is_scalene () { if ((side1 != side2) && (side1 != side3) && (side2 != side3)) return true; else return false; } // Method to test for an isosceles triangle. public boolean is_isosceles () { if (((side1 == side2) && (side1 != side3)) || ((side1 == side3) && (side1 != side2)) || ((side2 == side3) && (side2 != side1))) return true; else return false; } // Method to test for an equilateral triangle. public boolean is_equilateral () { if ((side1 == side2) && (side1 == side3)) return true; else return false; } } // The Test_Triangle class contains one main method where program // execution starts. It allows the user to create triangle objects // by entering the dimensions for the three sides of the triangle, // and it determines the type of each triangle. The user types 'n' // when they want to quit the program (must enter at least 1 triangle). class Test_Triangle { // Main method that allows the user to enter the dimentions for // three sides of a triangle. These dimensions are then used to // create a Triangle object, and this object is tested to determine // what type of triangle it is. public static void main (String[] args) throws IOException { // Declare stdin so data can be read from input. DataInputStream stdin = new DataInputStream (System.in); // loop exits when the user response is "n" String user_response = "y"; while (!user_response.equals("n")) { // Ask user for 3 dimensions of triangle. System.out.println ("\nEnter side1 length: "); int side1 = Integer.parseInt (stdin.readLine()); System.out.println ("Enter side2 length: "); int side2 = Integer.parseInt (stdin.readLine()); System.out.println ("Enter side3 length: "); int side3 = Integer.parseInt (stdin.readLine()); // Now use these values to create a Triangle object. Triangle tri = new Triangle (side1,side2,side3); // Determine what kind of triangle it is. System.out.println ("\nIs triangle right-angle? "+tri.is_right()); System.out.println ("Is triangle scalene? "+tri.is_scalene()); System.out.println ("Is triangle isosceles? "+ tri.is_isosceles()); System.out.println ("Is triangle equilateral? "+ tri.is_equilateral()); // Ask user if they want to continue. System.out.println ("\nDo you want to examine more triangles?"); System.out.println ("(type 'y' for yes or 'n' for no)"); user_response = stdin.readLine(); } } }