Computer Science II (20082) Week 1: Review and Inheritance

Similar documents
Computer Science II (20073) Week 1: Review and Inheritance

Introduction to Programming Using Java (98-388)

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Lecture 5: Methods CS2301

Binghamton University. CS-140 Fall Dynamic Types

CS111: PROGRAMMING LANGUAGE II

CS260 Intro to Java & Android 03.Java Language Basics

CS111: PROGRAMMING LANGUAGE II

Fall 2017 CISC124 9/16/2017

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

TeenCoder : Java Programming (ISBN )

Zheng-Liang Lu Java Programming 45 / 79

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Lecture Set 4: More About Methods and More About Operators

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Brief Summary of Java

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CompuScholar, Inc. 9th - 12th grades

Operators and Expressions

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Chapter 5: Methods. by Tony Gaddis. Starting Out with Java: From Control Structures through Objects. Fourth Edition

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Lecture Set 4: More About Methods and More About Operators

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Selected Questions from by Nageshwara Rao

Chapter 4 Defining Classes I

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

PROGRAMMING FUNDAMENTALS

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

BM214E Object Oriented Programming Lecture 7

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Chapter 6 Introduction to Defining Classes

Simple Java Reference

COP 3330 Final Exam Review

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Getting started with Java

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

13 th Windsor Regional Secondary School Computer Programming Competition

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.


Inheritance and Interfaces

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Java Primer 1: Types, Classes and Operators

1 Shyam sir JAVA Notes

Objects and Iterators

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Programming Languages and Techniques (CIS120)

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Lecture #6-7 Methods

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Anatomy of a Class Encapsulation Anatomy of a Method

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Chapter 2: Data and Expressions

A Short Summary of Javali

CSCI 355 Lab #2 Spring 2007

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Java Fundamentals (II)

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

CHAPTER 7 OBJECTS AND CLASSES

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CHAPTER 7 OBJECTS AND CLASSES

Chapter 2. Elementary Programming

Chapter 2: Data and Expressions

CS 231 Data Structures and Algorithms, Fall 2016

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Building Java Programs

Topic 4 Expressions and variables

Computer Science II Lecture 1 Introduction and Background

Declarations and Access Control SCJP tips

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

COP 3014: Fall Final Study Guide. December 5, You will have an opportunity to earn 15 extra credit points.

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CS 106 Introduction to Computer Science I

BM214E Object Oriented Programming Lecture 4

Lecture 3. Lecture

CS1150 Principles of Computer Science Methods

Admin. CS 112 Introduction to Programming. Recap: Exceptions. Summary: for loop. Recap: CaesarFile using Loop. Summary: Flow Control Statements

Building Java Programs

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

CS 112 Introduction to Programming

Transcription:

Computer Science II 4003-232-08 (20082) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology

Review of CS-I

Syntax and Semantics of Formal (e.g. Programming) Languages Syntax The rules defining legal statements and their combination e.g. Rules for valid Rochester area phone numbers e.g. Rules for constructing legal Java programs Semantics The meaning of statements in the language (what statements represent). Often depends on context of use e.g. What does the following represent: 349-5313 e.g. What does the following Java statement represent: System.out.println(x); - 3 -

A Waterfall Model of Software Development Requirements Specification System Analysis (Data Flow) System Design (Components) CS2 focus Implementation Testing Deployment Maintenance - 4 -

Miscellaneous CS1 Java Topics Primitive Data and Operations Variable declaration, initialization, and assignment Constants ( final ) Arguments (pass-by-value) and local variables Floating point vs. integer arithmetic Type conversions (widening and narrowing), and casting Operator precedence and associativity (Liang Appendix C) Object-Oriented Features Class and method syntax: constructors, return types (e.g. void, int, double) The new operator (instantiates objects from classes) Visibility modifiers (public and private) - 5 -

Variable Properties 1. Location (in memory) 2. Identifier (name) A symbol representing the location 3. Type (of encoding used for stored data) Primitive (e.g. int, boolean), or Reference (address in memory of a class instance (object)) 4. Value The primitive value or reference (for objects) stored at the variable location - 6 -

Memory Diagrams: Illustrating Variable Properties Variable Storage (Memory Locations) Represented using a box Variable Identifiers and Types Indicated using labels outside the box (e.g. x : int) For static variables, indicate static and class name e.g. x : int (static Widget) Variable Values Primitive types: show value in the box (e.g. for integers, show decimal value) Reference variables: draw arrow from box to object data Objects Drawn as circles, with internal boxes to represent data members Strings are a special case (see next slide) - 7 -

Program: i : int 1050 int i = 1050; boolean j = false; String mystring = new String( Hello World ); j : boolean false mystring : String Hello World - 8 -

name : String John Smith birthdate : BirthDate active : boolean true current : Student numberstudents : int (static Student) year : int 1982 month : int 2 day : int 22 18952-9 -

Numeric Type Casting Type Casting Changes the type (representation or encoding) of a variable or constant Narrowing Conversion Convert from a larger range of values to a smaller range of values e.g. int x = (int) 5.32; Widening Conversion Convert from a smaller to a larger range of values e.g. double x = (double) 5; - 10 -

Testing Reference Variable Values (==) vs. Object States (.equals() ) Equivalent References ( == ) Tests whether the memory location referred to by reference variables is identical ( A == B ): Does String variable A refer to the same memory location as String variable B? Equivalent Object States (.equals() ) A method defined for the Object class, and overwritten by other Java classes (e.g. String) that normally tests for identical object states ( A.equals(B)): Does String variable A have the same state (characters) as String variable B? WARNING: for Object class, equals() and == are the same - 11 -

Variable Scope Definition The program statements from which a variable may be referenced Local variable A variable declared within a method May be referenced only in block in which they are declared Formal method parameters define local variables that may be referenced within the body of a method Actual parameters (arguments) provide initial value for formal parameters (Java has a pass-by-value semantics for parameters) Variable Masking or Shadowing Local variable definition assumes the identity (identifier) of variable in the parent scope References in the local scope are to the locally declared variable Local variables may mask instance or static variables in a method - 12 -

Space for main() k: result: 1 j: 2 i: 5 Space for max() result: num2: 2 num1: 5 Space for main() k: result: 1 j: 2 i: 5 Space for main() k: 5 result: 1 j: 2 i: 5 main() invoked max() invoked max() returns main() returns public class TestMax { public static int max(int num1, int num2) { int result; ; return result;} public static void main(string[] args) { int i = 5, j = 2; int result = 1; int k = max(i, j); System.out.println( Max is " + k +, result = + result); }} - 13 -

Organizing Variables: Arrays Purpose Allow us to organize variables in a structure One-dimensional (1D) Arrays A rectangular box containing slots for variables Really a group of adjacent memory locations for storing variables Treated as a list with elements indexed starting from 0, up to array size 1 Reason: array name represents location of first element; index is offset Eliminates the need to provide a different name for every variable in the array Arrays may contain primitive or reference data types - 14 -

1D Array: Primitive Data intarray : int[] 0 intarray[0] int intarray[] = new int[5]; for ( int i=0; i < intarray.length; i++ ) intarray[i] = i; vs. int intarray[] = { 0, 1, 2, 3, 4 }; 1 intarray[1] 2 intarray[2] 3 intarray[3] 4 intarray[4] - 15 -

1D Array: Reference Data strarray : String[] String[] strarray = new String[5]; null null null strarray[0] strarray[1] strarray[2] null strarray[3] null strarray[4] - 16 -

String[] strarray = new String[5]; for ( int i=0; i < strarray.length; i++) strarray[i] = new Integer(i).toString(); strarray : String[] strarray[0] 0 1 2 3 4 strarray[1] strarray[2] strarray[3] strarray[4] - 17 -

Mutli-Dimensional Arrays Example We might represent 150 marks as A one-dimensional array of 150 (double) floating point numbers double[] marks = new double[150]; marks[0] = 95.1; A 2-D array of 15 (students) x 10 ((double) marks per student) double[][] marks = new double[15][10]; marks[0][0]= 95.1; A 3-D array of 15 (students) x 5 (quizzes) x 2 ((double) mark for each section of each quiz, e.g. programming and short answer) double[][][] marks = new double[15][5][2]; marks[0][0][0] = 95.1; marks[0][0][1]=85.0; - 18 -

(Side Note) Ragged Arrays Ragged Array Example Array of 15 (students) x *different* sized arrays for each student, to represent the case where some students miss quizzes double[][] marks = new double[15][]; marks[0] = new double[2]; marks[1] = new double[3]; Possible because java implements 2D and higher dimensional arrays as *arrays of arrays* - 19 -

Control Flow Definition The order in which statements in a program are executed Simple control flow: sequential execution of statements do a, then b, then c Conditional Statements (if, switch) Change control flow by defining different branches of execution followed depending on Boolean conditions (expressions) if C is true then do a, else do b if C is true then do {a, then b, then c}, else do {d, then e, then f} Iteration Statements (while, do...while, for) Change control flow by repeating a statement or block (compound statement) while a Boolean condition holds while C is true, do {a, then b, then c } Method Invocation Produces a jump to the instructions in a method invocation **also changes context (e.g. local variables): see earlier TestMax example on slide 14-20 -

What is this? Definition A reference to myself for an object Used within instance methods for object invoking the method All instance variable references and method invocations implicitly refer to this Within an instance method: x = 2 same as this.x = 2; tostring() same as this.tostring() ) Some Uses 1. Prevent masking of variables, e.g. formal params. and instance variables in a constructor: public MyClass(int x){ this.x = x; } 2. Invoke other constructors within a class Note: this(arg-list) must be first statement in constructor definition public MyClass(int x){ this(); this.x = x; } 3. Have object pass itself as a method argument someclass.printfancy(this); - 21 -

Exercise: Variables A. What are the four variable properties we discussed? Name and define each in point form. B. Draw memory diagrams for the following. 1. int x = 5; double y = 2.0; y = x; 2. String s1 = first ; String s2 = second ; s1 = s2; 3. String strarray[ ] = { a, b, c, d }; 4. boolean f[ ][ ] = new boolean[3][2]; 5. int y[ ][ ] = { { 1, 2, 3 }, { 4, 5, 6, 7} }; C. Are the following legal? Why? 1. double y = (double) 5; 2. int x = 5.0; 3. int x = (int) 5.0; - 22 -

Exercise: Variables (Cont d) What is output by the following program? How do the definitions for x and y differ? What kind of parameter and variable is args? class SimpleExample { static int x = 5; int y = 2; } public static void main(string args[ ]) { int x = 4; y = 9; System.out.println(x + y); } - 23 -

Exercise: Methods A. What is produced as output by the following? int a = 2; switch (a) { case 2: System.out.println( Case 2 ); case 1: System.out.println( Case 1 ); break; default: System.out.println( Default Case ); } C. Answer the following in 1-2 sentences each. 1. In what way are an if statement and a while statement the same? 2. How do an if statement and a while statement differ? 3. What extra elements are added to the conditional test in a while loop to produce a for loop? - 24 -

Exercise: Methods, cont d C. What is wrong with the following? class MethodExample { private int x = 5; static private int y = 3; public int methodone() {return methodtwo();} public int methodone(int x) {this.x = x; return x;} } static public int methodtwo() {return y + methodone(2); } static public int methodtwo(int x) { this.x = x; return x;} - 25 -