Introduction to Computer Science II CSE1030Z 1. Academic Support Programs: Bethune  having trouble with your FSC and LSE courses?  consider using the.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Written by: Dr. JJ Shepherd
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
CS 206 Introduction to Computer Science II 09 / 09 / 2009 Instructor: Michael Eckmann.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Access to Names Namespaces, Scopes, Access privileges.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Enhancing classes Visibility modifiers and encapsulation revisited
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Classes, Objects, and Methods
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
COSC 1P03 Data Structures and Abstraction 4.1 Abstract Data Types The advantage of a bad memory is that one enjoys several times the same good things for.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Classes CS 21a: Introduction to Computing I First Semester,
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Introduction to Computer Science II CSE1030Z 1. Your Instructor 2  Dr. Burton Ma  office  CSEB 1012J (near Gillian Moore's office)  hours : 2:30–4:00.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Peyman Dodangeh Sharif University of Technology Spring 2014.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Written by: Dr. JJ Shepherd
Methods. Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Chapter 7 User-Defined Methods.
Introduction to Classes and Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Namespaces, Scopes, Access privileges
CS 302 Week 15 Jim Williams, PhD.
Advanced Object Oriented Programming
Java Programming Language
CSE 1020:Programming by Delegation
CSE 1020:Inheritance Mark Shtern.
Advanced Object Oriented Programming
Namespaces, Scopes, Access privileges
Java Programming Review 1
Advanced Object Oriented Programming
Object Oriented Programming in java
Zorah Fung University of Washington, Spring 2015
Using java libraries CGS3416 spring 2019.
More on iterations using
Zorah Fung University of Washington, Winter 2016
Presentation transcript:

Introduction to Computer Science II CSE1030Z 1

Academic Support Programs: Bethune  having trouble with your FSC and LSE courses?  consider using the Academic Support Programs at Bethune College  PASS  free, informal, structured, facilitated study groups:  peer tutoring  free, one-on-one, drop-in tutoring: 2

Academic Support Programs: Bethune  Bethune College is looking for Class Representatives  3

ACM Programming Contest   EECS team website   first contest  Fri Sep 13 at 13:13 in Lassonde

Who Am I? 5  Dr. Burton Ma  office  Lassonde 2046  hours : see syllabus on course web page  

Course Format 6  everything you need to know is on the course website   labs start next Tuesday (Sep 17)

CSE1030 Overview 7  in CSE1020, you learned how to use objects to write Java programs  a Java program is made up of one or more interacting objects  each object is an instance of a class  where do the classes come from?  in CSE1030, you will learn how to design and implement classes  introduction to concepts in software engineering and computer science

What You Should Know from CSE1020  how to read an API  determine what package a class is located in  determine what the class/interface/field/method is supposed to do  determine the name of a method  determine what types a method requires for its parameters  determine what type a method returns  determine what exceptions might be thrown 8

What You Should Know from CSE1020  create and use primitive type variables and their associated operators  int, double, boolean, char 9

What You Should Know from CSE1020  create (using a constructor) and use reference variables  e.g., type.lib.Fraction, java.util.Date  Random, String, List, Set, Map 10

What You Should Know from CSE1020  understand the difference between primitive and reference types  memory diagrams 11

What You Should Know from CSE1020  understand the difference between == and equals 12

What You Should Know from CSE1020  use class methods (and fields)  e.g., double value = Math.sqrt(2.0);  use instance methods (and fields)  e.g., String s = "hello"; String t = s.toUpperCase(); 13

What You Should Know from CSE1020  if statements  e.g. if (grade >= 65) { System.out.println("Go to second year"); } else { System.out.println("Try again"); } 14

What You Should Know from CSE1020  for loops  e.g., for some String reference s for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == 'a') { System.out.println(s + " contains an \'a\'"); break; } } 15

What You Should Know from CSE1020  for each loops  e.g., for some List reference t for (String s : t) { for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == 'a') { System.out.println(s + " contains an \'a\'"); break; } } } 16

What You Should Know from CSE1020  the difference between aggregation and composition  the differences between aliasing, shallow copying, and deep copying 17

What You Should Know from CSE1020  inheritance and substitutability 18

What You Should Know from CSE1020  what an exception is  the difference between a checked and unchecked exception  how to handle exceptions ( try and catch ) 19

What You Should Know from CSE  style public class hairsOnHead { public static void main(String[] args) { int Diameter = 17; double f = 0.5; double areaCovered=f*Math.PI*Diameter*Diameter; int d = 200; double numberofhairs = areaCovered * d; System.out.print("The number of hairs on a human head is "); System.out.println(numberofhairs); } class names should start with a capital letter inconsistent brace alignment variable names should start with a lowercase letter; magic number variable names should be informative; magic number variable names should use camelcase inconsistent indenting 1 space around operators

Organization of a Java Program Packages, classes, fields, and methods 21

In This Lecture 1. demonstrate the use of eclipse by solving a CSE1020 eCheck problem 2. review the organization of a typical CSE1020 Java program 3. improve the organization of the program by writing a method 4. explain the organization of a typical Java program that uses packages and multiple classes 22

eCheck04A  in a nutshell:  write a program that computes the fraction where x, y, z, and t are proper fractions entered by a user from the command line 23

eCheck04A Sample Output For each fraction enter its numerator/denominator, pressing ENTER after each Enter x Enter y 5 9 Enter z Enter t -2 3 A = 12470/3 = /3 =

eclipse Demo Here  if you missed this class then you missed this demo 25

Organization of a CSE1020 Program  one file  Check04A.java 26

Organization of a CSE1020 Program  one file  Check04A.java  zero or more import statements 27

Organization of a CSE1020 Program  file  Check04A.java  zero or more import statements  one class  Check04A 28

Organization of a CSE1020 Program  file  Check04A.java  zero or more import statements  one class  Check04A  one static method  main 29

Organization of a Typical Java Program  one or more files 30

Organization of a Typical Java Program  one or more files  one package name 31

Organization of a Typical Java Program  one or more files  zero or one package name  zero or more import statements 32

Organization of a Typical Java Program  one or more files  zero or one package name  zero or more import statements  one class 33

Organization of a Typical Java Program  one or more files  zero or one package name  zero or more import statements  one class  one or more fields (class variables) 34

Organization of a Typical Java Program  one or more files  zero or one package name  zero or more import statements  one class  zero or more fields (class variables)  zero or more more constructors 35

Organization of a Typical Java Program  one or more files  zero or one package name  zero or more import statements  one class  zero or more fields (class variables)  zero or more more constructors  zero or more methods 36

Organization of a Typical Java Program  it's actually more complicated than this  static initialization blocks  non-static initialization blocks  classes inside of classes (inside of classes...)  classes inside of methods  see

Packages  packages are used to organize Java classes into namespaces  a namespace is a container for names  the namespace also has a name 38

Packages  packages are use to organize related classes and interfaces  e.g., all of the Java API classes are in the package named java 39

Packages  packages can contain subpackages  e.g., the package java contains packages named lang, util, io, etc.  the fully qualified name of the subpackage is the fully qualified name of the parent package followed by a period followed by the subpackage name  e.g., java.lang, java.util, java.io 40

Packages  packages can contain classes and interfaces  e.g., the package java. lang contains the classes Object, String, Math, etc.  the fully qualified name of the class is the fully qualified name of the containing package followed by a period followed by the class name  e.g., java.lang.Object, java.lang.String, java.lang.Math 41

Packages  packages are supposed to ensure that fully qualified names are unique  this allows the compiler to disambiguate classes with the same unqualified name, e.g., your.Fraction f = new your.Fraction(1, 3); type.lib.Fraction g = new type.lib.Fraction(1, 3); 42

Packages  how do we ensure that fully qualified names are unique?  package naming convention  packages should be organized using your domain name in reverse, e.g.,  EECS domain name eecs.yorku.ca  package name ca.yorku.eecs  we might consider putting everything for this course under the following package  ca.yorku.eecs.cse

Packages  most Java implementations assume that your directory structure matches the package structure, e.g.,  there is a sequence of folders ca\yorku\eecs\cse1030 inside the project src folder 44 eclipse workspace folder project folder project sources folder Java source files

Things For You to do this Week  get a CSE account if you do not already have one  do Lab 00 to get (re)acquainted with eclipse and the CSE labs  available tomorrow  review CSE

CSE1020 Review Questions 46

CSE1020 Review  what does the following program print? public class Puzzle01 { public static void main(String[] args) { System.out.print("C" + "S" + "E"); System.out.println('1' + '0' + '3' + '0' + 'z'); } 47

CSE1020 Review  which of the following methods are associated with a class? static boolean disjoint(Collection c1, Collection c2) void setIcon(Icon newIcon) String toString() static int round(double a) static void showMessageDialog(Component parent, Object message) 48

CSE1020 Review  what is the return type for each of the following methods? static boolean disjoint(Collection c1, Collection c2) void setIcon(Icon newIcon) String toString() static int round(double a) static void showMessageDialog(Component parent, Object message) 49

CSE1020 Review  how many parameters do each of the following methods have, and what are their types? static boolean disjoint(Collection c1, Collection c2) void setIcon(Icon newIcon) String toString() static int round(double a) PrintStream printf(String format, Object... args) 50

CSE1020 Review  what is a method precondition  what is a method postcondition?  what happens if a precondition is violated?  who is responsible if a postcondition is false? 51

CSE1020 Review  a type.lib.Fraction object has two attributes: a numerator and a denominator  draw the memory diagram for the following program  after line 1 completes  after line 2 completes import type.lib.Fraction; public class Fraction1 { public static void main(String[] args) { Fraction f = new Fraction(1, 2); // 1 f.add(new Fraction(3, 4)); // 2 } 52

CSE1020 Review  class X is an aggregation of one Y ; it has a method getY that returns a reference to its Y object  what are the values of sameState and sameObject ? Y y = new Y(); X x = new X(y); // x has a reference to y boolean sameState = y.equals(x.getY()); boolean sameObject = y == x.getY(); 53

CSE1020 Review  class X is an composition of one Y ; it has a method getY that returns a reference to its Y object  what are the likely values of sameState and sameObject ? Y y = new Y(); X x = new X(y); // x uses composition with y boolean sameState = y.equals(x.getY()); boolean sameObject = y == x.getY(); 54

CSE1020 Review  class X is an composition of one Y ; it has a method getY that returns a reference to its Y object  furthermore, Y is immutable  what are the likely values of sameState and sameObject ? Y y = new Y(); X x = new X(y); // x uses composition with y boolean sameState = y.equals(x.getY()); boolean sameObject = y == x.getY(); 55

CSE1020 Review  consider the following UML diagram  which statements are true? 1. Object is a CreditCard 2. CreditCard is an Object 3. RewardCard is an Object 4. RewardCard is a CreditCard 5. a CreditCard is usable anywhere a RewardCard is required 6. a RewardCard is usable anywhere a CreditCard is required 56

CSE1020 Review  t is a reference to a List object  write some code that prints out each element of t 57

CSE1020 Review  p is a reference to a Map object  write some code that prints out each key-value pair of p 58

CSE1020 Review  consider the UML diagram for Java exceptions:  checked exceptions are subclasses of … ?  unchecked exceptions are subclasses of … ? 59

CSE1020 Review  consider the UML diagram for some common exceptions:  will the following code fragment compile? try { // some legal code not shown here } catch (IndexOutOfBoundsException e) { // not shown } catch (StringIndexOutOfBoundsException e) { // not shown } 60

CSE1020 Review  more questions can be found here: 