1 / 28

Copywrite 2003 Walter Savitch

Stay informed about the latest changes in CSE 11 Section B, including room relocation and important deadlines for assignments and interviews. Make sure to adhere to the specified guidelines to ensure smooth submission. Learn about topics like JOptionPane, inner classes, encapsulation, and static methods and variables.

higginsj
Download Presentation

Copywrite 2003 Walter Savitch

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. 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 CHANGEStarting Next Time:Solis 111

  3. CSE MajorsFreshman Reception4-5 pm Faculty ClubToday

  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 • Deadlinesturnin: 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 publicclass Species { private String name; privateint population; privatedouble growthRate;

  13. publicclass Species { private String name; privateint population; privatedouble 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 publicclass Species { private String name; privateint population; privatedouble growthRate;

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

  21. Quiz Time

  22. publicclass Species { private String name; privateint population; privatedouble growthRate;

  23. publicint 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.gSavitchIn.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 thisanything of the form this.variable or this.method is not allowedEven if the “this.” is only implicit

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

More Related