Unformatted text preview:

Intro to Programming II Review Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science University of San Francisco p 1 25 2 Slides So what have we learned about this semester Department of Computer Science University of San Francisco p 2 25 3 Scope Scope refers to the area of a program where a variable can be accessed Java has three types of scope Local scope the variable exists only within a method Object scope the variable can be accessed from any method belonging to an object Class scope the variable can be accessed by all instances of a class Department of Computer Science University of San Francisco p 3 25 4 Parameters 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 25 5 Example This is a method definition public double depositFunds double amt balance balance amt return balance 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 25 6 Method signature Specification of all data types coming in and out of a method Type and order of input parameters Type of return variable A method signature allows the compiler to uniquely identify a method Department of Computer Science University of San Francisco p 6 25 7 Example Consider the following method declaration double calculate double a1 double a2 double a3 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 calculate Hello 4 4 2 calculate calculate 3 3 Department of Computer Science University of San Francisco p 7 25 8 Objects An object is a type of abstraction It provides a way of grouping together related data and 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 work internally just how to use them Department of Computer Science University of San Francisco p 8 25 9 Classes and objects A class is a template or category An object is a particular instance of that class CartoonAnimals might be a class BugsBunny Tweety are instances of that class Classes let us specify behavior common to a set of objects Department of Computer Science University of San Francisco p 9 25 10 Methods As we know classes also contain methods Methods are pieces of code that can be invoked on an object The allow us to encapsulate both state and behavior Department of Computer Science University of San Francisco p 10 25 11 Data hiding It s also important to protect instance data from outside users One way to do this is by providing accessors and mutators setters and getters Rather than the user modifying your object s data directly they use a method to do it Reduces error Hides implementation from the user Department of Computer Science University of San Francisco p 11 25 12 Constructors A constructor is a method that is called when an object is first created Its responsibility is to initialize an object s instance variables It must have the same name as the class in constructs It has no return type It is called when new is invoked Department of Computer Science University of San Francisco p 12 25 13 Example public class Point public int xval public int yval Point int x int y xval x yval y Point p new Point 3 4 Department of Computer Science University of San Francisco p 13 25 14 Multiple Constructors It s often helpful to be able to specify some instance variables but not all For example let s say our circle class has a default radius of 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 p1 new Point 3 3 Circle c new Circle p 5 creates a circle with radius 5 at 3 3 Department of Computer Science University of San Francisco p 14 25 15 Multiple Constructors public class Circle private Point center private int radius public Circle Point c center c radius 1 public Circle Point c int r center c radius r Department of Computer Science University of San Francisco p 15 25 16 Strings in Java Strings in Java are objects This mean that they have a set of methods they respond to compareTo equals indexOf length replace startsWith endsWith etc Department of Computer Science University of San Francisco p 16 25 17 Overloaded operators Unlike most other objects Strings also have special behavior for creating string literals and for concatenation String literals are strings where the value is known at compile time String s1 hello world String s2 USF String s3 I love Java We can create a string without calling new Department of Computer Science University of San Francisco p 17 25 18 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 Department of Computer Science University of San Francisco p 18 25 19 Using Scanner to read from files 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 Department of Computer Science University of San Francisco p 19 25 20 Working with Files We can use Scanner to also read from a file Relevant methods hasNext next nextLine nextInt Department of Computer Science University of San Francisco p 20 25 21 Using Scanner to read from files 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 Department of Computer Science University of San Francisco p 21 25 22 File output Output is a little more complicated No equivalent of the Scanner class PrintWriter is the thing to use Department of Computer Science University of San Francisco p 22 25 23 File Output 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 Department of Computer Science University of


View Full Document

USF CS 112 - Intro to Programming II

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Strings

Strings

27 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

Arrays

Arrays

5 pages

ArrayList

ArrayList

24 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Loading Unlocking...
Login

Join to view Intro to Programming II and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Intro to Programming II and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?