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

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

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 Static Variables Kind of instance variables. One variables used for all objects. All objects read and write the one variable.

6 public class Sample { private static int count; public static final double PI = 3.14159;

7 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

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

9 Wrapper Classes Two Personalities Personality 1: A class to stand in for a primitive type. Personality 2: A class with lots of useful static methods

10 The Wrapper Class Integer (Personality 1) Integer classyInt = new Integer(42); classyInt.intValue() returns 42

11 The Wrapper Class Integer (Personality 2) Integer.toString(42) returns “42” Integer.parseInt(“42”) returns 42

12 Overloading Giving two (or more) definitions to the same method name (in the same class). The two definitions must differ in the number of parameters or the type of some parameter position.

13 public static double average(double first, double second) { return ((first + second)/2.0); } public static double average(double first, double second, double third) { return ((first + second + third)/3.0); } public static char average(char first, char second) { return (char)(((int)first + (int)second)/2); }

14 Constructors Species klingonSpecies = new Species(); Method with same name as the classes. Reserves memory and initilaizes the object of the class. Creates objects.

15 public class Species { private String name; private int population; private double growthRate; public void writeOutput( ) { System.out.println("Name = " + name); System.out.println("Population = " + population); System.out.println("Growth rate = " + growthRate + "%"); }

16 public class Species { private String name; private int population; private double growthRate; public void writeOutput( ) … public Species( ) { name = “No name yet”; population = 0; growthRate = 0.0; }

17 public Species( String theName, int thePopulation, double theGrowthRate) { name = theName; population = thePopulation; growthRate = theGrowthRate; }

18 public Species( String name, int population, double growthRate) { this.name = name; this.population = population; this.growthRate = growthRate; }

19 Constructor Method with same name as the class. Has no returned type, not even void. Is typically overloaded. Should initialize the object. Will do whatever you program it to do.

20 public Species( String name, int population, double growthRate) { System.out.println(“3 arg constructor called”); //Legal. Good for debugging, but //not good for final constructor. this.name = name; this.population = population; this.growthRate = growthRate; }

21 Use of Constructors Species s1 = new Species(); s1.writeOutput(); Species s2 = new Species(“Red Bear”, 10, 5.7); s2.writeOutput();

22 Name = No name yet Population = 0 Growth rate = 0.0% 3 arg constructor called. Name = Red Bear Population = 10 Growth rate = 5.7%

23 Default Constructor Constructor with no parameters If you define No Constructors At All for a class, then Java automatically provides a default constructor. If you provide any constructors at all, then Java provides no other constructors. “Always” define a defualt constructor.

24 Legal but NOT A GOOD DEFINITION. public Species( String name, int population, double growthRate) { this.name = name; this.population = population; this.growthRate = growthRate; }

25 Better version: public Species( String name, int population, double growthRate) { this.name = name; this.growthRate = growthRate; if (population >= 0) this.population = population; else Error messag and exit }

26 Package A named collection of class. A libray of classes. Can be stored in one directory and used in any directory.

27 Adding a class to a package package myLibrary; public class Sample {

28 Using a package import myLibrary; public class MyProgram {

29 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 example myPackages.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 discussed in discussion section.


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