310 likes | 327 Views
This post provides the deadlines for turning in CSE 11 homework 2, 3, and 4, as well as the reading materials for the following chapters. It also covers topics such as inner classes, packages, arrays, and recursion.
E N D
CSE 11 • HW 2 , 3 and 4 Posted in public and on web • Deadlines HW 2turnin: Wed Jan 22 interview: Saturday January 25 • .class files in public directory for HW 2 & 3 • Do not need to turnin the HW if you take an interview before the turnin deadline
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any other purpose without the written permission of Walter Savitch. wsavitch@ucsd.edu
Reading • Cover approx. Chapters 1-5 so far. • Next doChapter 6, today, read all of itChapter 10, sec. 10.1 VectorsChapter 11 RecursionAll needed for hw3-4
Inner Class • Class defined within another class. • Can be private and then local to the outer class. • For now, we use it only to make a hw assignment into a single class for the turnin program. (Can make inner classes either public or private for this.)
You may need to make an inner class static public class Match { public static class Person { …. }//end Person …. }//end Match
Package • A named collection of class. • A library of classes. • Can be stored in one directory and used in any directory.
Adding a class to a package package myLibrary; public class Sample {
Using a package import myLibrary; public class MyProgram {
Package Names and Directories • Package name is a relative path name from a class path directory to the package directory. • Uses dots rather than /, for examplemyPackages.CSE11.speciesStuff • Class path is an environment variables of the operating system. How you set it depends on the operating system. Typically looks like: path; path; path; • Be sure to include a dot for the current directory in your class path.
Class Path on sunpal setenv CLASSPATH .:/home/solaris/ieng9/cs11w/cs11wab/path1:/home/solaris/ieng9/cs11w/cs11wab/path2 Note dot at start Note the use of colons not semicolons
Class Path on sunpal • setenv CLASSPATH ~/lib • import iolib.savitchstuff.*; • If SavitchIn.class is in ~/lib/iolib/savitchstuff Then it does not have to be in the same directory as your program or class.
Class Path on sunpal • Must add the following to SavitchIn.java package iolib.savitchstuff; • ~/lib should be on CLASSPATH • SavitchIn.class must be in the directory~/lib/iolib/savitchstuff • Must add the following to your programimport iolib.savitchstuff.*;
Arrays • Give uniform names to a collection of variables all of the same type. • Array type names: int[], double[], Species[] • int[] a = new int[3];declares 3 int variables nameda[0], a[1], and a[2]
double[] temperature = new double[7]; int index; double sum, average; System.out.println("Enter 7 temps:"); sum = 0; for (index = 0; index < 7; index++) { temperature[index] = SavitchIn.readLineDouble( ); sum = sum + temperature[index]; } average = sum/7;
Java Arrays • Indexes always start with 0 • An array (as a whole) is an object • Arrays check for index out of bounds • Arrays know their size
Length Instance Variable • String[] a = new String[10]; • a.length has the value 10 • a.length is read-only; You cannot change it.
Use of length for (index = 0; index < temperature.length; index++) { temperature[index] = SavitchIn.readLineDouble( ); sum = sum + temperature[index]; }
Array Instance Variables as Arguments • Nothing new. • If a is an array of doubles, the a[2] can be used just like any other variables of type double.
Entire Array as an Argument • Array variables are reference variables, just like class objects. • Array parameters are just like class parameters (Call-by-value for a reference.) • Arrays know their size. One array type for all arrays with the same base type, e.g. double[] is the type for any array of doubles of any size.
public static double[] averageArray (int firstScore, int[] nextScore) { double[] temp = new double[nextScore.length]; int i; for (i = 0; i < temp.length; i++) temp[i] = average(firstScore, nextScore[i]); return temp; }
What is wrong? • Species[] a = new Species[3];a[0].readInput(); • Error message “Null Pointer Exception” • a[0] is a varaible of type Species, but isjust a variable. It has no refernce to any object. • Need a[0] = new Species();
Species[] a = new Species[3]; for (int i = 0; i < a.length; i++) a[i] = new Species(); a[0].readInput(); //OK a[1].readInput(); //OK a[2].readInput(); //OK
Multidimentional Arrays • More than one index. • Implemented as array of arrays • Read on your own.
Recursion (Chapter 11) • Recursive method == one that invokes itself • Anything will compile. • Need some care to get method to run OK. • Read all of Chapter 11
writeVertical Example Static method ClassName.writeVertical(1234); Outputs 1 2 3 4
public static void writeVertical(int n) { if (n < 10) System.out.println(n); else //n is two or more digits long: { writeVertical(n/10); System.out.println(n%10); } }
ClassName.writeVertcal(123);Equivalent towriteVertical(123/10);System.out.println(123%10); Equivalent towriteVertical(12);System.out.println(3); Equivalent towriteVertical(12/10);System.out.println(12%10);System.out.println(3);
writeVertical(12/10);System.out.println(12%10);System.out.println(3); Equivalent towriteVertical(1);System.out.println(2);System.out.println(3); Equivalent to System.out.println(1);System.out.println(2);System.out.println(3);
Stack • Like a stack of paper • Last-in/First-out • Used to implement recursion
Successful Recursive Method • Two cases • Case with recursive calls • Stopping case (Base case) with no recursive calls