Fall 2006CSC311: Data Structures1 Java Review. Fall 2006CSC311: Data Structures2 The Java Virtual Machine (JVM) Virtual machine -- the logical machine.

Slides:



Advertisements
Similar presentations
Java Review Interface, Casting, Generics, Iterator.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 7 Designing Classes Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling  To.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Class Design CSC 171 FALL 2004 LECTURE 11. READING Read Chapter 7 It’s abstract But it should help with project #1.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
Chapter 10 Classes Continued
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Chapter 7 Designing Classes. Chapter Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Data Structures and Algorithms in JAVA Chapter 2.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Internet and Java Foundations, Programming and Practice
Programming Language Concepts (CIS 635)
Computer Science II Exam 1 Review.
Chapter 9 Inheritance and Polymorphism
Extending Classes.
Java Programming Language
Presentation transcript:

Fall 2006CSC311: Data Structures1 Java Review

Fall 2006CSC311: Data Structures2 The Java Virtual Machine (JVM) Virtual machine -- the logical machine (non- physical machine) that performs machine functions, typically implemented in software on top of a "real" hardware platform and operating system JVM -- the software implementation of a "CPU" designed to run compiled Java code Includes stand-alone Java applications, as well as "applets" that are downloaded and run in Web browsers such as the NetScape Navigator Thanks to the JVM, programs written in Java don't have to be rewritten to run on different computers Need an interpreter to translate JVM code to machine code

Fall 2006CSC311: Data Structures3 Class and Object What is a class? –A class is corresponding to a concept, e.g. People, Student, Faculty, Staff, Rectangle, Circle, etc –In Java, a class is a set of objects with the same behavior What is an object? –An object is corresponding an entity, e.g. Jack, Peter –In Java, an object is an entity that you can manipulate in your programs (by invoking methods) –Each object belongs to a class –Object constructions

Fall 2006CSC311: Data Structures4 Class Components Components –Fields – name, type and specifier –Methods – signature (prototype) –Constructors – object initialization Declaration –Variable declaration –Object variables w/o initialization –Method declaration // method calls

Fall 2006CSC311: Data Structures5 Data Types Data types –Primitive data types – int, long, short, double, float, boolean, char –Classes – String, Object Type conversion –Explicit type conversion – casting may cause information lost –Implicit type conversion – default no information lost

Fall 2006CSC311: Data Structures6 Casting and Autoboxing/Unboxing Casting: (type) expr Autoboxing –Implicit casting –Number – Integer, Double, Float, etc –Anytime a Number object is expected as a parameter to a method, the corresponding base type can be passed: Number  base type Unboxing –Anytime a base type is expected in an expression involving a Number reference, that Number object is changed to the corresponding base type base type  Number

Fall 2006CSC311: Data Structures7 Enum Types Declaration modifier enum name {vale_name 0, value_name 1, …, value_name n-1 Example Public enum Day{Mon, Tue, Wed, Thu, Fri, Sat, Sun}; Constants -- final

Fall 2006CSC311: Data Structures8 Statements Assignments – location Decisions –If-then –If-then-else –Switch-case Iterations –while –do-while –for Restricted gotos –break –continue –return Boolean expressions and short-circuit evaluation

Fall 2006CSC311: Data Structures9 Simple Input and Output Simple output methods: –Built-in static object: System.out An instance of java.io.PrintStream class –print(Object) –Print(String) –print(base_type) –Println(String) Simple input methods –Built-in special object: System.in –Related class: java.util.Scanner –Scanner sc = new Scanner(System.in); hasNext()next()hasNextType()nextType()hasNextLine()nextLine()findInLine(String)

Fall 2006CSC311: Data Structures10 Designing classes Choosing classes –A class represents a single concept Concepts from mathematics: Point, Rectangle, Ellipse Concepts from real life: BankAccount, Person, Student –Utility classes--no objects, only static methods Math Principles: –Responsibilities –Independence –Behaviors

Fall 2006CSC311: Data Structures11 Designing classes Cohesion and Coupling –Cohesive: all methods are closely related to the single concept that the class represents –Coupling: A class depends on another if it calls one of its methods High Coupling  many class dependencies Minimize coupling to minimize the impact of interface changes

Fall 2006CSC311: Data Structures12 Accessor and Mutator Classes Accessor: does not change the state of the implicit parameter (e.g. getBalance()) Mutator: changes the state of the implicit parameter (e.g. deposit(double amount ) Rule of thumb: Mutator should return void Immutable class: all methods are accessors (e.g. String)

Fall 2006CSC311: Data Structures13 Preconditions/postconditions Precondition –The condition that must be met before the method executes –Publish preconditions so the caller won't call methods with bad parameters Postcondition –The condition that's true after a method has completed –If not, the method functioned incorrectly

Fall 2006CSC311: Data Structures14 Scope Scope of variable: region of program where the variable can be referred by its name –Local variable scope: from definition to end of block –Class scope: all methods of the class –Overlapping scope: local scope wins over class scope Static scope –Static fields Define a field that belongs to a class instead of any object Non-static fields are instance fields, while static fields are class fields Minimize the use of static fields –Static methods No implicit parameter Too many static methods are a sign of too little Object-oriented

Fall 2006CSC311: Data Structures15 Coding and Pseudo-code Coding –programming in Java –IDE Pseudo-code –Mixes natural language with standard programming language constructs –Constructs: Expressions Method declarations Decision structures Loop structures Array indexing Method calls Method returns comments

Fall 2006CSC311: Data Structures16 Object-oriented Design Goals –Robustness –Adaptability –Reusability Principles –Abstraction –Encapsulation –Modularity Design Patterns –Describe a solution to a typical software design problem –Some typical design patterns: Position, Adaptor, Iterator, Template method, Composition, Comparator, Amortization, Divide-and-conquer, etc.

Fall 2006CSC311: Data Structures17 Inheritance Specialization and generalization (extension) –Specialization – subclass –Generalization -- superclass Method overriding –Refinement –Replacement The keyword this

Fall 2006CSC311: Data Structures18 Inheritance Hierarchies Hierarchies of classes, subclasses, and sub- subclasses are common Similar to concept hierarchies: –Person has subclasses like Faculty, Student, Staff; –Faculty may have subclasses FullTimeFaculty and PartTimeFaculty; –FullTimeFaculty may have TenuredFaculty and TenureTrackFaculty; –Student may have subclasses FullTimeStudent and PartTimeStudent; –etc. A superclass can have multiple subclasses, but a subclass should have at most ONE superclass

Fall 2006CSC311: Data Structures19 Inheritance of Methods Override method: Supply a different implementation of a method that exists in the superclass Inherit method: Don't supply a new implementation of a method that exists in the superclass Add method: Supply a new method that doesn't exist in the superclass Subclass can not access the private methods of superclass Calling a Superclass Method/constructor

Fall 2006CSC311: Data Structures20 Inheritance of Fields Inherit field: All fields from the superclass are automatically inherited Add field: Supply a new field that doesn't exist in the superclass Can't override fields Subclass can not access the private fields of superclass

Fall 2006CSC311: Data Structures21 Polymorphism Polymorphism (greek: many shapes): The type of the object determines the method to call Called late binding. Resolved at runtime Different from overloading. Overloading is resolved by the compiler Converting from subclasses to superclass The instanceof method –Test whether an object is of a specified class –Example: x instanceof String to test if x is a string

Fall 2006CSC311: Data Structures22 Access control public – accessible anywhere private – accessible inside the class protected -- accessible by class, subclasses and package default – no specifier, accessible by package Recommended Access Levels –Fields: Always private Exception: public static final constants –Methods: public or private –Classes: public or package –Don't use protected –Beware of accidental package access (forgetting public or private)

Fall 2006CSC311: Data Structures23 The Object Object The Cosmic Superclass All classes extend Object by default Most useful methods: – –String toString(): to convert the object to a string message – –boolean equals(Object otherObject): to test if the object is equal to the given object otherObject – –Object clone(): to create a new copy of the object

Fall 2006CSC311: Data Structures24 Exceptions Throwing exceptions Catching exceptions –Try-catch-finally block try { … } catch (exception_type1 e1) { … } catch (exception_type2 e2) { … } … finally { … } finally { … }

Fall 2006CSC311: Data Structures25 Interfaces Special classes All methods in an interface are abstract –no implementation All methods in an interface are automatically public An interface doesn't have instance fields An interface doesn’t have constructors Interface implementation –Should be implemented by classes Multiple inheritance in Interfaces

Fall 2006CSC311: Data Structures26 Abstract Class and Method Abstract class –Cannot have direct instances (objects) –Must be extended by subclass(es) –Can implement some methods (main difference from interfaces) Abstract method –Must be overridden by subclass(es) –Must be inside abstract classes –Non-abstract class cannot have abstract method

Fall 2006CSC311: Data Structures27 Strong Typing Strong typing –All variables must be typed Java –A strong-typing language –An object can be viewed as being of various types –But declared as being of only one type

Fall 2006CSC311: Data Structures28 Generics Casting –Widening conversion: to superclass –Narrowing conversion: to subclass –Casting exception –Casting with interfaces Generics –Generic type: a type that is not defined at compilation time, but becomes fully specified at run time –Formal type parameters –Actual type parameters

Fall 2006CSC311: Data Structures29 Generics: Example public class Pair { K key; V value; public void set(K k, V v) { key = k; value = v; } public K getKey() { return key; } public V getValue() { return value; } } Public static void main(String[] args) { Pair pair1 = new Pair (); pair1.set(new String(“height”), new Integer(36)); Pair pair2 = new Pair (); pair2.set(new Student(“A5678”, “Sue”, 19), new Double(9.5)); }}

Fall 2006CSC311: Data Structures30 Arrays A set of data cells with fixed size, each cell holding a data item and all data items being the same type Purpose: to construct a linear structure to hold a given number of elements. Once an array is created, its size is fixed – length Indices starting from 0 Common algorithms on arrays –Find max/min values –Find average value –Linear search Copying array and clone array High-dimensional arrays

Fall 2006CSC311: Data Structures31 Array List ArrayList is a class – data type Purpose: to construct a linear structure to hold a flexible number of elements Methods: –size() – returns the number of elements in the ArrayList: a.size() –get( ) – returns the -th element that is the type of Object should convert the return value to your specific type The starts at 0 –set(, ) – sets the -th element to, overwrites the old element –add(, ) – adds before the -th element –remove( -th element

Fall 2006CSC311: Data Structures32 Recursion A recursive computation solves a problem by using the solution of the same problem with simpler input For recursion to terminate, there must be special cases for the simplest inputs The simplest case can be directly solved The recursive cases should call the method itself