CS Java. Introduction to Java. Andy Mroczkowski Department of Computer Science Drexel University

Similar documents
Introduction to Programming (Java) 3/12

Lecture 5: Sep. 23 &25

CMSC 132, Object-Oriented Programming II Summer Lecture 6:

Java Programming. Final Examination on December 13, 2015 Fall 2015

REMOTE SENSING OF THE ATMOSPHERE AND OCEANS

Lecture 5: Jun. 10, 2015

Physics Fundamentals of Astronomy

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Physics 141 Course Information

Physics 141 Course Information

SPRING 2014 Department of Physics & Astronomy, UGA PHYS 4202/6202 Electricity and Magnetism II (as of Jan. 07/2014)


Course Announcements. John Jannotti (cs32) Scope, Collections & Generics Feb 8, / 1

Physics Fundamentals of Astronomy

CAS GE 365 Introduction to Geographical Information Systems. The Applications of GIS are endless

Stellar Astronomy 1401 Spring 2009

GEOG 508 GEOGRAPHIC INFORMATION SYSTEMS I KANSAS STATE UNIVERSITY DEPARTMENT OF GEOGRAPHY FALL SEMESTER, 2002

Instructor: Dr. Darryl Kropf 203 S. Biology ; please put cell biology in subject line

Lecture 14: Nov. 11 & 13

PHYSICS 206, Spring 2019

CHEM 4725/8725 Organometallic Chemistry. Spring 2016

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, Name:

Voortgezet Programmeren

AS 203 Principles of Astronomy 2 Introduction to Stellar and Galactic Astronomy Syllabus Spring 2012

MATH 325 LEC Q1 WINTER 2015 OUTLINE

MAE/MSE502 Partial Differential Equations in Engineering. Spring 2019 Mon/Wed 6:00-7:15 PM Classroom: CAVC 101

ITI Introduction to Computing II

ITI Introduction to Computing II

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

SYLLABUS CHEM 202 Lab - Inorganic Chemistry 2 Laboratory Spring, 2014

ICS141: Discrete Mathematics for Computer Science I

Physics 321 Theoretical Mechanics I. University of Arizona Fall 2004 Prof. Erich W. Varnes

Historical Geology, GEOL 1120 (final version) Spring 2009

SYLLABUS SEFS 540 / ESRM 490 B Optimization Techniques for Natural Resources Spring 2017

Today s Outline. Biostatistics Statistical Inference Lecture 01 Introduction to BIOSTAT602 Principles of Data Reduction

Announcements. John Jannotti (cs32) Design Patterns Feb 13, / 1

GIST 4302/5302: Spatial Analysis and Modeling

Intro to Linear & Nonlinear Optimization

MURA Office hours Thurs at 12-1pm in CIT 546 Contact for more info.

CE261 ENGINEERING MECHANICS - DYNAMICS

Intro to Linear & Nonlinear Optimization

Physics 1304 Astronomy of the Solar System

B.Sc. in Electronics and Communication Engineering, Cairo University, Cairo, Egypt with Distinction (honors), 1992

CSEP 521 Applied Algorithms. Richard Anderson Winter 2013 Lecture 1

GE 226 Introduction to Experimental Labs. Stan Shadick, Lab Co-ordinator

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, 2018

Natural Science I: Quarks to Cosmos V Spring 2010 Meyer 121

CSE 311: Foundations of Computing I. Lecture 1: Propositional Logic

CS483 Design and Analysis of Algorithms

Announcements. Distances in the Solar System. The Main Point. Lecture #10: Solar System Survey II

CMSC 132, Object-Oriented Programming II Summer Lecture 1:

General Chemistry I Office: Chem

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 07/2014 SESSION 8:00-10:00

CS 340: Discrete Structures for Engineers

Homework. Staff. Happy and fruitful New Year. Welcome to CSE105. (Happy New Year) שנה טובה. and. Meeting Times

Course Description Course Objectives Online resources

Special Topic: Organic Chemistry I (SCI )

public void run ( ) { i f ( this. id == 0) System. out. p r i n t ( 3 ) ; bro. j o i n ( ) ; else System. out. p r i n t ( 2 ) ;

GIST 4302/5302: Spatial Analysis and Modeling

Don t Trust Atoms, they Make Up Everything High School Chemistry

Chemistry 14C: Structure of Organic Molecules - Winter 2017 Version 56

ESS 102 Space and Space Travel

Physics 112 for class and recitation WF 10:10 a.m. - 10:40 a.m. or by appointment

Los Angeles Mission College Chemistry 65 Syllabus Spring Semester 2012

General Chemistry I Office: Chem

EOS-310 Severe & Unusual Weather Spring, 2009 Associate Prof. Zafer Boybeyi 1/20/09

AS102 -The Astronomical Universe. The boring details. AS102 - Major Topics. Day Labs - Rooms B4 & 606. Where are we (earth, sun) in the universe?

ENSC Weather & Climate Labs

Lecture 5: Linear Regression

Michelle Liu, Neelay Phadke, Dogan Gidon W 5-6 in Hildebrand 100-D

Page 1 of 5 Printed: 2/4/09

Astronomy 102/104: Our Solar System MWF 11:15-12:05P, Uris Auditorium Spring 2008 Course Syllabus

Introduction to Computing II (ITI 1121) MIDTERM EXAMINATION

Android Services. Lecture 4. Operating Systems Practical. 26 October 2016

Elementary Sorts 1 / 18

ENVIRONMENT AND NATURAL RESOURCES 3700 Introduction to Spatial Information for Environment and Natural Resources. (2 Credit Hours) Semester Syllabus

Texts 1. Required Worboys, M. and Duckham, M. (2004) GIS: A Computing Perspective. Other readings see course schedule.

PHYS 3446 Lecture #1. 6. Lab 7. Evaluation Policy. Syllabus Semester projects. Wednesday, Jan. 19, 2005 PHYS 3446, Spring 2005 Jae Yu

Machine Learning (CS 567) Lecture 2

Course Syllabus Phy320L - Modern Physics Laboratory Spring 1999

GEOG People and their Environment Section 01 Spring 2015 Monday and Thursday 1:10 pm to 2:25 pm Hunter West 511

DAILY QUESTIONS 28 TH JUNE 18 REASONING - CALENDAR

13 Concurrent Programming using Tasks and Services

General Chemistry I (CHE 1401)

CS483 Design and Analysis of Algorithms

Big Bang, Black Holes, No Math

MT Dynamic Meteorology II MWF Spring :00 a.m. - 8:50 a.m. Dr. Jim Koermer Boyd 306 DYNAMIC METEOROLOGY II SYLLABUS

Physics Fundamentals of Astronomy

PHY 114 A General Physics II 11 AM-12:15 PM TR Olin 101. Plan for Lecture 1:

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

Adam Blank Spring 2017 CSE 311. Foundations of Computing I. * All slides are a combined effort between previous instructors of the course

Urban Planning Internet Geographic Information Systems Fall 2010

CS 6110 Lecture 28 Subtype Polymorphism 3 April 2013 Lecturer: Andrew Myers

GEO 433/533: Winter 2009 Coastal Geomorphology

CHEMISTRY 100 : CHEMISTRY and MAN

ASTR 4 Solar System Astronom y

Lab Assistant: Kathy Tang Office: SSC 2208 Phone: ext

MAE502/MSE502 Partial Differential Equations in Engineering. Spring 2012 Mon/Wed 5:00-6:15 PM

Information System Design IT60105

Department of Civil and Environmental Engineering. CE Surveying

Transcription:

CS 190 - Java Introduction to Java Andy Mroczkowski uamroczk@cs.drexel.edu Department of Computer Science Drexel University February 18, 2008 / Lecture 5

Outline Course Status Course Information & Schedule Assignments Basics.this and.new Anonymous Nested Classes Other Topics

Course Information Instructor Andy Mroczkowski uamroczk@cs.drexel.edu Office Location: UC 147 Office Hours: Monday 6-7 & evenings by appointment Course CS 190 - Java Web: www.cs.drexel.edu/ uamroczk/cs190 Book: Thinking in Java, 4th Edition Previous edition available for free at wwww.mindview.net/books/tij/ Course materials adapted from Nadya Belov

Schedule 1 Jan 7 Jan 14 Jan 21 Jan 28 Feb 4 Feb 11 Feb 18 Feb 25 Mar 3 Mar 10 Mar 17 Course Overview, Introduction to Java No Class No Class (University Holiday) Object Oriented Concepts, Control Statements Access Control, Intialization & Cleanup, Reuse, Interfaces, Containers, Exceptions, Polymorphism, Midterm Review Midterm, Type Information, Generics, Arrays File I/O, Threading GUI, Special Topics (Networking, Reflection) Final 1 Also subject to change

Assignment 4 Questions? Due 22 Feb 11:59 PM EST

Assignment 4 Questions? Due 22 Feb 11:59 PM EST

Basics A class that is defined within a certain scope. Example class O u t t i e { class I n n i e {

Basics Example class O u t t i e { class I n n i e { class MoreInnie {

Parcel1 Example public class Parcel1 { class Contents { class D e s t i n a t i o n { private S t r i n g label ; D e s t i n a t i o n ( S t r i n g whereto ) { label = whereto ; S t r i n g readlabel ( ) { return label ; public void ship ( S t r i n g dest ) { Contents c = new Contents ( ) ; D e s t i n a t i o n d = new D e s t i n a t i o n ( dest ) ; System. out. p r i n t l n ( d. readlabel ( ) ) ; public s t a t i c void main ( String [ ] args ) { Parcel1 p = new Parcel1 ( ) ; p. ship ( " Tasmania " ) ;

References to public class O u t t i e { class I n n i e { Innie myinnie = new Innie ( ) ; Innie getinnie ( ) { return myinnie ; public s t a t i c void main ( String [ ] args ) { Outtie out = new Outtie ( ) ; O u t t i e. I n n i e i n = out. g e t I n n i e ( ) ;

Is that it?

Accessing the outer-class Example class O u t t i e { S t r i n g s = " Hi " ; class I n n i e { S t r i n g t = s ;

Accessing the outer-class Example class O u t t i e { private S t r i n g s = " Hi " ; class I n n i e { S t r i n g t = s ; Even private members

Example Accessing the outer-class class O u t t i e { private S t r i n g s = " Hi " ; void boom ( ) { class I n n i e { S t r i n g t = s ; void bang ( ) { boom ( ) ; And methods too!

Selector Example interface S e l e c t o r { boolean end ( ) ; Object c u r r e n t ( ) ; void next ( ) ;

Selector Example (Cont.) public class Sequence { private Object [ ] items ; private i n t next = 0; public Sequence ( i n t size ) { items = new Object [ size ] public void add ( Object x ) { i f ( next < items. length ) { items [ next ++] = x ; private class SequenceSelector implements S e lector { private i n t i = 0; public boolean end ( ) { return i == items. length ; public Object c u r r e n t ( ) { return items [ i ] ; public void next ( ) { i f ( i < items. length ) i ++; public Selector selector ( ) { return new SequenceSelector ( ) ;

Selector Example (Cont.) public s t a t i c void main ( String [ ] args ) { Sequence sequence = new Sequence ( 10); for ( i n t i = 0; i < 10; i ++ ) sequence. add ( I n t e g e r. t o S t r i n g ( i ) ) ; Selector selector = sequence. selector ( ) ; while (! s e l e c t o r. end ( ) ) { System. out. p r i n t ( s e l e c t o r. c u r r e n t ( ) + " " ) ; s e l e c t o r. next ( ) ;

Accessing the outer-class Accessing outer-class s instance variables? Direct access Accessing outer-class instance itself?.this

Accessing the outer-class Accessing outer-class s instance variables? Direct access Accessing outer-class instance itself?.this

Accessing the outer-class Accessing outer-class s instance variables? Direct access Accessing outer-class instance itself?.this

Accessing the outer-class Accessing outer-class s instance variables? Direct access Accessing outer-class instance itself?.this

Accessing the outer-class Example class O u t t i e { class I n n i e { O u t t i e g e t O u t t i e ( ) { return O u t t i e. this ;

Example Creating an Inner-Class directly class O u t t i e { class I n n i e { private i n t y = 10; public gety ( ) { return y ; public s t a t i c void main ( S t r i n g [ ] args ) { O u t t i e out = new O u t t i e ( ) ; O u t t i e. I n n i e i n = out.new I n n i e ( ) ; System. out. p r i n t l n ( i n. gety ( ) ) ;

Anonymous public class O u t t i e { public I n n i e g e t I n n i e ( ) { return new I n n i e ( ) { private i n t y = 10; public i n t gety ( ) { return y ; ; Madness! public s t a t i c void main ( S t r i n g [ ] args ) { O u t t i e out = new O u t t i e ( ) ; System. out. p r i n t l n ( out. g e t I n n i e ( ). gety (

Anonymous - Why? Shorthand Unclutter the code Common with Threads

Anonymous - Why? Shorthand Unclutter the code Common with Threads

Anonymous - Why? Shorthand Unclutter the code Common with Threads

Anonymous - Why? Shorthand Unclutter the code Common with Threads

Anonymous - Limitations Unlike regular classes, can only implement or extend but not both Objects used in the anonymous class but defined outside must be final

Anonymous - Limitations Unlike regular classes, can only implement or extend but not both Objects used in the anonymous class but defined outside must be final

Anonymous - Limitations Unlike regular classes, can only implement or extend but not both Objects used in the anonymous class but defined outside must be final

Nested Classes Don t need a connection to the outer-class? Make the the inner-class static

Nested Classes Don t need a connection to the outer-class? Make the the inner-class static

Nested Classes Don t need a connection to the outer-class? Make the the inner-class static

Nested Classes Example class O u t t i e { s t a t i c class I n n i e { s t a t i c i n t x ; public s t a t i c void main ( S t r i n g [ ] args ) { System. out. p r i n t l n ( O u t t i e. I n n i e. x ) ;

Nested Classes What about? class O u t t i e { s t a t i c class I n n i e { i n t x ; public s t a t i c void main ( S t r i n g [ ] args ) { System. out. p r i n t l n ( O u t t i e. I n n i e. x ) ;

Nested Classes What about? class O u t t i e { s t a t i c class I n n i e { i n t x ; public s t a t i c void main ( S t r i n g [ ] args ) { System. out. p r i n t l n ( O u t t i e. I n n i e. x ) ; FAIL

Nested Classes Fixed class O u t t i e { s t a t i c class I n n i e { i n t x = 5; public s t a t i c void main ( S t r i n g [ ] args ) { System. out. p r i n t l n ( (new I n n i e ( ) ). x ) ;

Nested Classes - Other Points Nested-classes can have static data and methods, regular inner-classes cannot

Other Topics Classes inside interfaces Inheriting inner-classes Overriding inner-classes Local inner-classes

Classes inside interfaces Example interface Car { void d r i v e ( ) ; class Engine { i n t c y l i n d e r s ;

Inheriting inner-classes Example class O u t t i e { class I n n i e { public class I n h e r i t I n n i e extends O u t t i e. I n n i e { Whats the problem?

Inheriting inner-classes Example - Fixed class O u t t i e { class I n n i e { public class I n h e r i t I n n i e extends O u t t i e. I n n i e { I n h e r i t I n n i e ( O u t t i e o ) { o. super ( ) ;

Overriding inner-classes Example class Egg { private Yolk y ; protected class Yolk { public Yolk ( ) { p r i n t ( " Egg. Yolk ( ) ) p u b l i c Egg ( ) { p r i n t ( "New Egg ( ) " ) ; y = new Yolk ( ) ; p u b l i c class BigEgg extends Egg { p u b l i c class Yolk ( ) { p r i n t ( " BigEgg. Yolk ( ) " ) ; p u b l i c s t a t i c void main ( S t r i n g [ ] args ) { new BigEgg ( ) ;

Overriding inner-classes Output New Egg() Egg.Yolk()

Local inner-classes Example class O u t t i e { void go ( ) { class I n n i e { i n t x = 5; System. out. p r i n t l n ( (new I n n i e ( ) ). x ) ; public s t a t i c void main ( S t r i n g [ ] args ) { (new O u t t i e ( ) ). go ( ) ;

Example Local inner-classes class O u t t i e { void go ( ) { class I n n i e { i n t x = 5; System. out. p r i n t l n ( (new I n n i e ( ) ). x ) ; public s t a t i c void main ( S t r i n g [ ] args ) { (new O u t t i e ( ) ). go ( ) ; Inner-classes can be declared in any scope.