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.

2 Home Work 1 Is on the handout. If you do not pass HW 1 by the deadlines, you must drop the course. No exceptions. If you take an interview before the turnin deadline, you do not need to turn in the assignment. Instructions for turning in assignments will be given in a later lecture.

3 Console Output int number = 7; System.out.println(number + “ things”); Screen output: 7 things

4 Console Input System.out.println(“Enter an integer:”); int number = SavitchIn.readLineInt(); Sets the value of number to the integer types in at the keyboard. The integer must be on a line by iteslf.

5 SavitchIn.readLineInt() SavitchIn is a class. readLineInt is a method. () indicates no arguments. Returns a value of type int, namely the integer entered at keyboard. Good error messages if user screws up.

6 SavitchIn A class written in Java. Is not a standard Java library class. The file SavitchIn.java (or SavitchIn.class) must be in the same directory as your program. File SavitchIn.java is with programs on machine in SUNPAL and is on CD in the Savitch text.

7 SavitchIn readLineInt() readLineDouble() readLineNonWhiteChar() readLine()

8 readLineNonWhiteChar() Different from other methods Reads the first nonwhite char on a line and discards the rest of the line. The char need not be the only thing on the line. If line is yes it reads the ‘y’

9 public class FirstProgram { public static void main(String[] args) { System.out.println("Hello out there."); char answerLetter; answerLetter = SavitchIn.readLineNonwhiteChar(); if (answerLetter == 'y') System.out.println("Nice weather."); System.out.println("Good-bye."); }

10 public class NameOfProgram { public static void main(String[] args) { Your Code Goes Here. }

11 Class A type A value of a class type is called an object An object has both data and actions Can have multiple kinds of data Actions called methods (called procedures or functions in some other languages)

12 String class Data is something like “Hi Mom” Sample methods (actions) “Hi Mon”.length() returns 6 So the following is legal int count = “Hi Mom”.length(); String greeting = “Hello; System.out.println( greeting.length());

13 Sample Class Definition

14 public class Sample { public String name; public int count; public void writeOutput() { System.out.println("Name = " + name); System.out.println(“Count = " + count); } //This goes in a file named Sample.java

15 Instance Variables public class Sample { public String name; public int count; --------------------------------------------------- Sample o = new Sample(); o.name = “Sam”;

16 Using a Class Sample anObject = new Sample(); anObject.name = “Sally”; anObject.count = 7; anObject.writeOutput();

17 Two kinds of Methods Methods that return a value Void Methods

18 Methods That Return a Value Must have a return statement of the form return expression ;

19 public class Sample2 { public String name; public int count; public int doubleCount() { return 2*count; }

20 void Method public class Sample { public String name; public int count; public void writeOutput() { System.out.println("Name = " + name); System.out.println(“Count = " + count); }

21 The this parameter public class Sample { public String name; public int count; public void writeOutput() { System.out.println("Name = " + this.name); System.out.println(“Count = " + this.count); }

22 The this parameter Sample anObject = new Sample(); anObject.name = “Sally”; anObject.count = 7; anObject.writeOutput();

23 public void writeOutput() { System.out.println("Name = " + this.name); System.out.println(“Count = " + this.count); } //this means anObject

24 Local Variable A Variable Declared Inside a Method definition

25 Two Kinds of Variables Instance Variables Local Variables No Global Variables

26 Parameters Like blanks in a method definiton. Filled in with arguments when the method is called (invoked).

27 public int projectedPopulation(int years) { double populationAmount = population; int count = years; while ((count > 0) && (populationAmount > 0)) { …. } if (populationAmount > 0) return (int)populationAmount; else return 0; }

28 All Parameters are Call-by-Value Only the value of the argument is plugged in for the parameter. You cannot define a method that takes an int variable as an argument and changes the value of the argument. More to say about class parameters, but they are strictly speaking call-by-value.

29 Information Hiding & Encapsulation Design classes & methods so that a programmer who uses them need only know what the methods do, not how they do it. User need never look at the body of a method definition. Also need not know how the data is implemented.

30 Need not know how the data is implemented A class for amount of money: A single instance variable of type double Or Two instance variables of type int Or One instance variable of type int Or One instance variables of type String

31 All Instance Variables Should Be Private public class Sample3 { private String name; private int count; public void readName() { name = SavitchIn.readLine(); }

32 Cannot Access a Private Instance Variable by Name Outside the Class Definition Public static void main(String[] args) { Sample3 s = new Sample3(); s.name = “Sam”; //Illegal s.count = SavitchIn.readLineInt(); //Illegal

33 Need Accessor and Mutator Methods Accessor methods: read instance variables, usually spelled “get…” Mutator methods: change instance variables, usually spelled “set…”

34 Mutator Methods Should Always Check That Change Is Sensible

35 public void setPopulation(int newPopulation) { if (newPopulation >= 0) population = newPopulation; else { System.out.println( "ERROR: using a negative population."); System.exit(0); }

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

37 javadoc Automatically extracts HTML documentation from classes. Only takes public stuff Only takes comments of the form /** */


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