Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 CSE 11 Section B ROOM CHANGE Starting Next Time: Solis 111

3 CSE Majors Freshman Reception 4-5 pm Faculty Club Today

4 CSE 11 Warning JOptionPane is covered in this class. But you read about it on your own.

5 Home Work 2 Posted in public and on web Deadlines turnin: Wed Jan 22 (Do not turn it in before January 19th) interview: Saturday January 25.class files in public directory Do not need to turnin the HW if you take an interview before the turnin deadline

6 turnin –c cs11w FileName.java Instructions in public. You must be logged onto sunpal as yourself. The file you are turning in must be in the same directory as you. Only has the last file you turned in. If you do an interview before the turnin deadline, then you do not turn in the assignment at all.

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

8 Encapsulation ADT (Abstract Data Type) API (Application Programmer Interface) You should be able to change the implementation of a class and not have to change any application program that uses the class.

9 javadoc Automatically extracts HTML documentation from classes. Only takes public stuff Only takes comments of the form /** This comment must be just before something public. */

10 Some Style Guidelines Identifiers: Class, aVariable, aMethod, A_CONSTANT Comments: No obvious comments Comment at head of file with name, assignment number, etc. Comment before each class definition describing comment. Comment before each non-obvious method telling what the parameters are and what the method does.

11 Class Type Variables Contain References A reference is a memory location. A reference is a pointer, but you never say “pointer” in Java. Java has no pointer types, but All class objects are handed by reference. (I.e., its all pointers, but automatic pointers.)

12 The Species Class public class Species { private String name; private int population; private double growthRate;

13 public class Species { private String name; private int population; private double growthRate; public void setPopulation(int newPopulation) {//This method not in book, but … if (newPopulation >= 0) population = newPopulation; else { System.out.println("ERROR."); Sytem.exit(0); }

14 Species s1 = new Species(); s1.setPopulation(13); //s1 now has a population of 13 Species s2; s2 = s1; s2.setPopulation(100); //s1 now has a population of 100

15 Class Parameter Are call by value, but The value is a reference. Cannot change the reference (pointer) value, but you can change what it references (points to).

16 Class Parameters A method can change the values of instance variables in a class argument.

17 == checks for the actual content of the variables (or whatever) and returns true if the contents are equal. Primitive types: No surprises. Class types: returns true if the the two references are the same. Returns false if two classes stored in different locations in memory, even if data all matches

18 Species klingonSpecies = new Species(); Species earthSpecies = new Species (); klingonSpecies.set("Klingon ox", 10, 15); earthSpecies.set("Klingon ox", 10, 15); if (klingonSpecies == earthSpecies) System.out.println("They are EQUAL."); else System.out.println("They are NOT equal.");

19 Define an equals method for classes public class Species { private String name; private int population; private double growthRate;

20 Define an equals method for classes public boolean equals(Species otherObject) { return ((this.name.equalsIgnoreCase(otherObject.name)) && (this.population == otherObject.population) && (this.growthRate == otherObject.growthRate)); }

21 Quiz Time

22 public class Species { private String name; private int population; private double growthRate;

23 public int femalePopulation() { return (population/2 + population%2); }

24 Static Methods and Variables. Applies to both methods and instance variables. Indicated with modifier static. Means there is only one for all objects. Static methods can be, normally are, invoked with class name, e.g SavitchIn.readLine()

25 Staic Variables Kind of instance variables. One variables used for all objects. All objects read and write the one variable.

26 public class Sample { private static int count; public static final double PI = 3.14259;

27 Static Methods Can be invoked using class name instead of a calling object. They have no this anything of the form this.variable or this.method is not allowed Even if the “this.” is only implicit

28 Static Methods All the methods in SavithcIn Methods in the class Math Methods in Wrapper Classes


Download ppt "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."

Similar presentations


Ads by Google