Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any other purpose without the written permission of Walter Savitch.
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.
Console Output int number = 7; System.out.println(number + “ things”); Screen output: 7 things
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.
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.
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.
SavitchIn readLineInt() readLineDouble() readLineNonWhiteChar() readLine()
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’
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."); }
public class NameOfProgram { public static void main(String[] args) { Your Code Goes Here. }
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)
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());
Sample Class Definition
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
Instance Variables public class Sample { public String name; public int count; Sample o = new Sample(); o.name = “Sam”;
Using a Class Sample anObject = new Sample(); anObject.name = “Sally”; anObject.count = 7; anObject.writeOutput();
Two kinds of Methods Methods that return a value Void Methods
Methods That Return a Value Must have a return statement of the form return expression ;
public class Sample2 { public String name; public int count; public int doubleCount() { return 2*count; }
void Method public class Sample { public String name; public int count; public void writeOutput() { System.out.println("Name = " + name); System.out.println(“Count = " + count); }
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); }
The this parameter Sample anObject = new Sample(); anObject.name = “Sally”; anObject.count = 7; anObject.writeOutput();
public void writeOutput() { System.out.println("Name = " + this.name); System.out.println(“Count = " + this.count); } //this means anObject
Local Variable A Variable Declared Inside a Method definition
Two Kinds of Variables Instance Variables Local Variables No Global Variables
Parameters Like blanks in a method definiton. Filled in with arguments when the method is called (invoked).
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; }
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.
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.
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
All Instance Variables Should Be Private public class Sample3 { private String name; private int count; public void readName() { name = SavitchIn.readLine(); }
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
Need Accessor and Mutator Methods Accessor methods: read instance variables, usually spelled “get…” Mutator methods: change instance variables, usually spelled “set…”
Mutator Methods Should Always Check That Change Is Sensible
public void setPopulation(int newPopulation) { if (newPopulation >= 0) population = newPopulation; else { System.out.println( "ERROR: using a negative population."); System.exit(0); }
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.
javadoc Automatically extracts HTML documentation from classes. Only takes public stuff Only takes comments of the form /** */