Building Java Programs

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
ITEC200 – Week03 Inheritance and Class Hierarchies.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: File Output; Reference Semantics reading: , 7.1, 4.3, 3.3 self-checks:
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Building java programs chapter 6
File and Stream I/O, Redirection Privacy and Super
Advanced File Processing NOVEMBER 21 ST, Objectives Write text output to a file. Ensure that a file can be read before reading it in your program.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Copyright 2010 by Pearson Education Homework 8: Critters reading: HW8 spec.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Programming in Java: lecture 7
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
OOP: Encapsulation &Abstraction
Inheritance and Polymorphism
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Nested class.
Object Oriented Programming
Inheritance, Polymorphism, and Interfaces. Oh My
Computer Science II Exam 1 Review.
Chapter 9 Inheritance and Polymorphism
Lecture 8-3: Encapsulation, this
CSE 143 Lecture 22 The Object class; Polymorphism read
Chapter 9 Object-Oriented Programming: Inheritance
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
CSE 142 Critters (IPL) behavior: Ant Bird Hippo Vulture
Interfaces.
CSE 143 Lecture 22 The Object class; Polymorphism read
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Java – Inheritance.
Java Programming Course
Java Inheritance.
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Lecture 25: Inheritance and Polymorphism
Building Java Programs
Building Java Programs
Chapter 11 Inheritance and Polymorphism Part 1
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Review for Midterm 3.
Building Java Programs
Presentation transcript:

Building Java Programs Final Exam Review/Husky Tournament

Order of Exercises Announcements Exam Review Husky Tournament! Course Evaluations

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

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

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>};

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

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()

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

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

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()