25 2 Slides 25 3 So what have we learned about this semester Scope Scope refers to the area of a program where a variable can be accessed Intro to Programming II Java has three types of scope Local scope the variable exists only within a method Object scope the variable can be accessed from any Review method belonging to an object Chris Brooks Class scope the variable can be accessed by all instances of a class Department of Computer Science University of San Francisco Department of Computer Science University of San Francisco p 1 25 4 Parameters Department of Computer Science University of San Francisco p 2 25 5 Parameters are the variables passed into a method We can talk about Formal parameters these are the variables named in the method definition Actual parameters these are the variables in the method invocation Department of Computer Science University of San Francisco p 4 Example Department of Computer Science University of San Fra 25 6 Method signature Specification of all data types coming in and out of a method Type and order of input parameters This is a method definition public double depositFunds double amt balance balance amt return balance Type of return variable A method signature allows the compiler to uniquely identify a method bankacct b new bankacct paycheck 100 0 this is a method invocation b depositFunds paycheck Department of Computer Science University of San Francisco p 5 Department of Computer Science University of San Fra 25 7 Example 25 8 Objects 25 9 Classes and objects Consider the following method declaration An object is a type of abstraction A class is a template or category double calculate double a1 double a2 double a3 It provides a way of grouping together related data and An object is a particular instance of that class CartoonAnimals might be a class Which of the following are valid calls to this method calculate 3 52 0 5 1 double y calculate 0 1 1 2 2 calculate 1 1 2 3 functionality Makes it easier to organize and extend your program Also gives a black box effect Users of your objects don t need to worry about how they BugsBunny Tweety are instances of that class Classes let us specify behavior common to a set of objects work internally just how to use them calculate Hello 4 4 2 calculate calculate 3 3 Department of Computer Science University of San Francisco p 7 25 10 Methods Department of Computer Science University of San Francisco p 8 25 11 Data hiding Department of Computer Science University of San Fra 25 12 Constructors As we know classes also contain methods It s also important to protect instance data from outside users A constructor is a method that is called when an object is first Methods are pieces of code that can be invoked on an object One way to do this is by providing accessors and mutators setters and getters Its responsibility is to initialize an object s instance variables Rather than the user modifying your object s data directly they It must have the same name as the class in constructs The allow us to encapsulate both state and behavior use a method to do it Reduces error Hides implementation from the user Department of Computer Science University of San Francisco p 10 created It has no return type It is called when new is invoked Department of Computer Science University of San Francisco p 11 Department of Computer Science University of San Fran 25 13 Example 25 14 public class Point public int xval public int yval Multiple Constructors 25 15 It s often helpful to be able to specify some instance variables but not all Multiple Constructors public class Circle private Point center private int radius For example let s say our circle class has a default radius of 1 Point int x int y xval x yval y public Circle Point c center c radius 1 If the radius is something else users can specify it Point p1 new Point 3 3 Circle c new Circle p creates a circle with radius 1 at 3 3 Point p new Point 3 4 Point p1 new Point 3 3 Circle c new public Circle Point c int r center c radius r Circle p 5 creates a circle with radius 5 at 3 3 Department of Computer Science University of San Francisco p 13 25 16 Strings in Java Department of Computer Science University of San Francisco p 14 25 17 Overloaded operators 25 18 Strings in Java are objects Unlike most other objects Strings also have special behavior This mean that they have a set of methods they respond to compareTo equals String literals are strings where the value is known at compile for creating string literals and for concatenation time String s1 hello world String s2 USF String s3 I love Java indexOf length replace startsWith endsWith Department of Computer Science University of San Fran Overloaded operators We can also use the symbol to concatenate strings String s1 hello String s2 world String s3 s1 s2 s3 hello world This is a phenomenon called overloading an operator is redifined to provide different functionality We can create a string without calling new etc Department of Computer Science University of San Francisco p 16 Department of Computer Science University of San Francisco p 17 Department of Computer Science University of San Fran 25 19 Using Scanner to read from files 25 20 We can use the Scanner class to read from a file instead of System in try Scanner sc new Scanner new File studentlist while sc hasNext System out println sc next catch FileNotFoundException e System out println File not found Working with Files File output try Scanner sc new Scanner new File studentlist while sc hasNext System out println sc next catch FileNotFoundException e System out println File not found next nextLine nextInt Department of Computer Science University of San Francisco p 20 25 23 Output is a little more complicated No equivalent of the Scanner class File Output Department of Computer Science University of San Francisco p 22 Department of Computer Science University of San Fran 25 24 More detailed tracing Box and arrow tracing is nice but too high level sometimes import java io public class printtester public static void main String args try PrintWriter p new PrintWriter foo p println hello p println p close catch FileNotFoundException e System out println file not found PrintWriter is the thing to use Using Scanner to read from files We can use the Scanner class to read from a file instead of System in Relevant methods hasNext Department of Computer Science University of San Francisco p 19 25 22 25 21 We can use Scanner to also read from a file Doesn t let us keep track of
View Full Document
Unlocking...