Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building Java Programs

Similar presentations


Presentation on theme: "Building Java Programs"— Presentation transcript:

1 Building Java Programs
Final Exam Review/Husky Tournament

2 Order of Exercises Announcements Exam Review Husky Tournament!
Course Evaluations

3 File Input Uses Scanner (same as keyboard/console input)
import java.io.*; // for File import java.util.*; // for Scanner ... public static void <method> throws FileNotFoundException { Scanner <name> = new Scanner(new File(“<file>”)); } Token-based: single loop, uses hasNext()/next() Line-based: single loop, uses hasNextLine()/nextLine() Hybrid: nested loop, uses hasNextLine()/nextLine() in outer loop and hasNext()/next() in inner loop Usually creates a Scanner on the string for each line

4 File Output Uses PrintStream (same as System.out)
import java.io.*; // for File import java.util.*; // for PrintStream ... PrintStream <name> = new PrintStream(new File(“<file>”)); If file doesn’t exist, creates it If it does exist, overwrites it Can use print(), println(), printf() just like with System.out

5 Arrays Creation: Indexes start at 0, end at length – 1
<type>[] <name> = new <type>[<length>]; Indexes start at 0, end at length – 1 Careful of overrunning bounds when using expressions as index: e.g. arr[i – 1], arr[i + 1] Quick initialization: <type>[] <name> = {<value>, <value>, ..., <value>};

6 Reference/Value Semantics
Parameter of primitive types pass by value A copy of the value is made Modifications in the method do not persist at the call site Arrays and objects pass by reference Assignments to the variable itself do not persist Changes to state (e.g. fields, array elements) do persist

7 Classes and Objects Abstract details and encapsulate state and behavior Can contain: fields (data) instance methods (no static) accessors modifiers constructors (special methods to create object) Shadowing: local variable/parameter of same name as field is allowed Use this to get at field instead of local toString() is implicitly called when an object is passed to print(), println(), or printf()

8 Inheritance Uses extends keyword
Each class can only have one direct superclass But the hierarchy can be arbitrarily wide or deep Subclasses inherit all methods and fields from superclass private fields are inherited, but cannot be directly accessed (need to go through a method) Subclasses can override some methods to provide new behavior Use super to call the superclass’s version of a method

9 Polymorphism Can use any subclass in a superclass variable/parameter
Can only call superclass methods, but get subclass behavior Can type-cast to subclass to call subclass methods Always calls subclass’s version of a method, even if the call appears in code inherited from the base class

10 Critters Use fields to remember state Override some or all of:
number of moves last direction moved ate last time? etc. Override some or all of: toString(), getColor(), getMove(), eat(), fight(String opponent) Do not use a loop in getMove()


Download ppt "Building Java Programs"

Similar presentations


Ads by Google