Tutorial 2 Lecturer: Ray Reiter Tutor: Andria Hunter ========== 990113 Wed@1pm (PA105) CSC148, Spring 1999 Tutorial notes, T2 ================================================================== Topics: - Assignment 0 - Static Members - Packages Assignment 0 (10 minutes) -------------------------- - Read the course web page regularly. It may contain hints. - Questions on the assignment or the starter code. At this point, you should understand most of the handout, and should have at least a little understanding of the starter code. - Some clarifications: 1) The order of the hand. The first card is ALWAY North, no matter who leads. Go over the example in the handout and explain it. 2) The input will always be valid. No "6a% Spales" as a Card, no negative number of Tricks. Just valid input. You don't have to check the input for errors. - There are no packages in A0, but you should store your A0 files in the directory H:\jws\csc148\a0\ [From home --- C:\Windows\jws\csc148\a0\*.java] Static instance members (5 minutes) -------------------------- public class Bank_Account { private double balance; private static double bankBalance; private String acctNum; private String name; // Constructor - this constructor is used when // starting balance should be 0. public Bank_Account (String acctNum, String name) { balance = 0.0; this.acctNum = acctNum; this.name = name; } // Constructor - this constructor is used when starting // balance is passed as the parameter. public Bank_Account (double startBalance, String acctNum, String name) { balance = startBalance; bankBalance += startBalance; this.acctNum = acctNum; this.name = name; } // Deposit money into the account public void deposit (double depositAmount) { balance += depositAmount; bankBalance += depositAmount; } // Increase the Bank's Balance by amount passed public static void depositBank (double depositAmount) { bankBalance += depositAmount; } // Increase the Bank's Balance by amount passed public String toString () { return "Account Holder's Name: " + name + "\n" + "Account number: " + acctNum + "\n" + "Account balance: " + balance + "\n" + "Bank balance: " + bankBalance + "\n"; } } public class Bank { public static void main(String args[]) { // Create 2 bank account objects Bank_Account a = new Bank_Account (500.0, "00001", "Sally Green"); Bank_Account b = new Bank_Account ("00002", "Joe Smith"); // Deposit money into the bank accounts a.deposit (500.0); b.deposit (100.0); // Deposit money into the bank Bank_Account.depositBank (200.0); // Report Balances System.out.println ("Reporting Summary for Bank Accounts:\n"); System.out.println (a.toString ()); System.out.println (b.toString ()); } } Output: Reporting Summary for Bank Accounts: Account Holder's Name: Sally Green Account number: 00001 Account balance: 1000.0 Bank balance: 1300.0 Account Holder's Name: Joe Smith Account number: 00002 Account balance: 100.0 Bank balance: 1300.0 - Go over the "static" keyword. Significant Java Example -------------------------- - Make a Student class which records the marks a student has received for the courses they've passed and stores them in a ListOfInts. It also has a method to calculate the student's average mark. There are only two public methods (passCourse & average). - Develop the code, starting with code given below. (1) Designing the Student class -- make it abstract. - think only about what you want to do with a Student, not how it will be done. - write a little main class to test Student, but don't even need to know Student's implementation yet! (2) Designing a ListOfInts class to meet needs of Student class - Can finish Student class's implementation without knowing how ListOfInts will be implemented package studentPackage; public class ListOfInts { // BLACK BOX THAT MAINTAINS A LIST OF INTEGERS, SOMEHOW } package studentPackage; class Student { private int studentNum; private String name; private ListOfInts markList; public Student (int n, String s) { } public void passCourse (int mark) { } public float average() { } } package tutExample; import studentPackage.Student; class Example { public static void main (String[] args) { Student fred; fred = new Student(12345, "Fred Flintstone"); fred.passCourse(75); fred.passCourse(68); System.out.println("Fred's avg: " + fred.average()): } } The code ------------------------ package studentPackage; public class ListOfInts { private int [] theList; private int numElements; private static final int maxListSize = 40; public ListOfInts() { theList = new int[maxListSize]; numElements = 0; } // Precondition: numElements < maxListSize public void addInt (int mark) { numElements++; theList[numElements-1] = mark; // Why -1 ? } public float average () { int sum = 0; for (int i=0; i