1 / 31

CSE 11

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.

eberhardt
Télécharger la présentation

CSE 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  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.)

  5. You may need to make an inner class static public class Match { public static class Person { …. }//end Person …. }//end Match

  6. Package • A named collection of class. • A library of classes. • Can be stored in one directory and used in any directory.

  7. Adding a class to a package package myLibrary; public class Sample {

  8. Using a package import myLibrary; public class MyProgram {

  9. 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.

  10. 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

  11. 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.

  12. 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.*;

  13. 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]

  14. 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;

  15. 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

  16. Length Instance Variable • String[] a = new String[10]; • a.length has the value 10 • a.length is read-only; You cannot change it.

  17. Use of length for (index = 0; index < temperature.length; index++) { temperature[index] = SavitchIn.readLineDouble( ); sum = sum + temperature[index]; }

  18. 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.

  19. 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.

  20. Arrays Can Be Returned by a Method

  21. 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; }

  22. 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();

  23. 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

  24. Multidimentional Arrays • More than one index. • Implemented as array of arrays • Read on your own.

  25. 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

  26. writeVertical Example Static method ClassName.writeVertical(1234); Outputs 1 2 3 4

  27. 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); } }

  28. 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);

  29. 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);

  30. Stack • Like a stack of paper • Last-in/First-out • Used to implement recursion

  31. Successful Recursive Method • Two cases • Case with recursive calls • Stopping case (Base case) with no recursive calls

More Related