Review: Classes and Object Instances. Review: Creating an Object. Using Multiple Objects. DrawingGizmo pencil; pencil = new DrawingGizmo();

Similar documents
Review: Using Imported Code. What About the DrawingGizmo? Review: Classes and Object Instances. DrawingGizmo pencil; pencil = new DrawingGizmo();

} Each object in a Java program has an identifier (name) } This includes:

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

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

Converting Collections to Arrays. A Bad Approach to Array Conversion. A Better Approach to Array Conversion. public Object[] toarray();

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

C++ for Java Programmers

Name: 1) 2) 3) 4) 5) Learning Objectives (Milestones): 1. Create and use JUnit tests to debug a sample Java program.

Basic Class Diagrams. Class Diagrams, cont d. Class Diagrams, cont d. Car. Car. Car. What does the minus sign here mean?

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

CS/SE 153 Concepts of Compiler Design

Announcements - Grades UBLearns grades just updated Monday, March 28 th (11:00am). Please check grades.

QUIZ. What is wrong with this code that uses default arguments?

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS/SE 153 Concepts of Compiler Design

CMPE 152 Compiler Design

Decisions, Decisions, Decisions. GEEN163 Introduction to Computer Programming

Arrays. myints = new int[15];

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

Unit 10: Data Structures CS 101, Fall 2018

Errors and Exceptions

Lab 1: Introduction to Java

CS61C : Machine Structures

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

COMP 250. Lecture 7. Sorting a List: bubble sort selection sort insertion sort. Sept. 22, 2017

CS/ENGRD 2110 SPRING 2018

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

HW1 due Monday by 9:30am Assignment online, submission details to come

Fundamentals of Computer Science CSCI 136 Syllabus Fall 2018

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

CS 31 Discussion 1A, Week 1. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50

Introduction to C. CS 2060 Week 1. Prof. Jonathan Ventura. Outline Introduction Introduction to C Homework 1 C Coding Style Wrap-up

ECE 15B COMPUTER ORGANIZATION

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

CS 151. Recursion (Review!) Wednesday, September 19, 12

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Week 2. CS 400 Programming III. Read: Module 2 readings before lecture

Chapter 6 Introduction to Defining Classes

COMP 105 Homework: Type Systems

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

D Programming Language

Programming Languages and Techniques (CIS120)

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Introduction to Data Structures

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

CSE 341: Programming Languages

High Performance Computing MPI and C-Language Seminars 2009

CSE 113 A. Announcements - Lab

Announcements. 1. Forms to return today after class:

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

Result: original lp in main is. Goal was.

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

CS558 Programming Languages

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

What we are reinforcing with the exercises this class

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Project 1 Balanced binary

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors

To figure this out we need a more precise understanding of how ML works

Performance Measurement

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

ENCM 335 Fall 2018 Lab 2 for the Week of September 24

Randomized Algorithms: Element Distinctness

Patterns Continued and Concluded. July 26, 2017

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

CS 170 Java Programming 1. Week 10: Loops and Arrays

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Cadence Capture and PSpice Tutorial

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

1 Dynamic Memory continued: Memory Leaks

CS558 Programming Languages

Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7

CS61C : Machine Structures

Due Friday, March 20 at 11:59 p.m. Write and submit one Java program, Sequence.java, as described on the next page.

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

Lab # 2. For today s lab:

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CS61B Lecture #5: Arrays and Objects

CMPE 152 Compiler Design

Austin Community College Google Apps Calendars Step-by-Step Guide

17 February Given an algorithm, compute its running time in terms of O, Ω, and Θ (if any). Usually the big-oh running time is enough.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While

COSC 2P95. Introduction. Week 1. Brock University. Brock University (Week 1) Introduction 1 / 18

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Class 1: Homework. Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CS 241 Data Organization. August 21, 2018

CS61B Lecture #7. Announcements:

CS Introduction to Programming Fall 2016

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

Transcription:

Review: Classes and Object Instances ; = new (); Class #05: Objects, Memory, & Program Traces Software Engineering I (CS 120): M. Allen, 12/13 Sept. 17 We are working with both a class () and an object (actual code and data saved in memory, to which the name refers) We call the object an instance of the class Within a single program we can have more than one instance of a single class (or type of object) Each instance will be declared and instantiated separately, using distinct names Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 2 Review: Creating an Object Often, we declare and instantiate objects separately, using two distinct Java commands: ; = new (); Often, however, we do this together, in a single line: = new (); Using Multiple Objects ; = new (); = new ();.turnclockwise;.moveforward;.turncounterclockwise;.moveforward; One object is declared and instantiated (separately) Second object is declared and instantiated (all at once) Each object has its methods run separately, using its own unique identifier (name) Within a single code block, which approach we choose is often optional, since both do the same thing We will later learn about cases where this is not true Our code doesn t always need to use a single object It may be convenient (or necessary) to use multiple objects of the same (or different) type Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 3 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 4 1

Review: What a Declaration Means A variable declaration tells the JVM/CPU to create a new identifier (name) in its memory-space Note: Eventually, we will want the variable to be the name of an actual object in some memory location. At first, however, the variable points to no actual memory location. (No memory has been set aside for an object yet, just the name.) The variable has a null reference. ; Memory null Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 5 What Instantiation Means A variable instantiation tells the JVM/CPU to: 1. Go to variable (in memory), and find another empty location in memory for it to point to (where we will store the actual object) 2. Now, create a new object and fill up the newly made slot in memory with the required data 3. Point the variable that memory location, so you can find the object later (by storing the address of the object) NOTE: to help make sure our code is free of bugs, the compiler will check that the type of object we are creating agrees with the type of variable that we declared. Here, e.g., has type. = new (); Space for Memory Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 6 The Importance of Initialization Variable Assignment Once we have actually instantiated an object of type, we can do things with it by making method calls What haps when we try to call methods on objects that do not exist yet? The code at right will not compile! ; Error: is never instantiated, but a method is called on it! Giving a variable an actual value for the first time is called initialization Once we declare and instantiate a variable, we can change its value This is called variable assignment, and has the basic syntax: objectname1 = objectname2; = new (); ; In Java this is read a very specific way: the identifier on the left now points to whatever object is given by the expression on the right When we are dealing with objects that are instances of a class (as in this example code), this moves the reference of identifier objectname1 It now points to the same memory location as objectname2 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 7 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 8 2

What Assignment Means A variable assignment tells the JVM/CPU: 1. Take the name on the left 2. Change what it refers to so that it is now the same as thing named on the right = ; Variable Assignment This will all work just fine, as long as objectname1 and objectname2 are both variables of the same type (that is, their classes must agree with one another): = new (); ; Question: what haps to the original object that used to name? Answer: it is orphaned, and no longer exists for use in our program Space for Space for If we try to do this with types that don t agree, the code will not compile at all: = new (); Color col = java.awt.color.blue; = col; // compiler error!! Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 9 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 10 Variables and Their Objects When we properly instantiate an object: ; = new (); The variable name ( ) is bound to its object Each variable can only be bound to one object at a time Before the initialization on the 2 nd line of code above, the variable is bound to no object (it is null) We can also un-bind a variable so it points at no object: The that used to be bound is now an orphan Compilation Errors and Runtime Errors Many of the mistakes a coder makes will be caught when they try to compile their code, before it can ever be run Bad syntax (missing semicolons, etc.) Type errors (the wrong input parameter for a method, etc.) Even more complex things However, some errors cannot be caught at compile time Some code will compile, but then fail when it actually runs These are called runtime errors (or runtime exceptions) The Java environment (JVM) will try to signal what kind of runtime error we have, using an exception message Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 11 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 12 3

Anatomy of a Runtime Error These error messages can be quite complex! They are meant to be helpful, however. The first line tells us what sort of error this is This is a Null Pointer Exception error Lines starting with at tell us where error is made Look for your file names and line numbers In Eclipse, you can often click on the line numbers to go there Tracing Objects,, ; = new (); = new ();.turnclockwise(); It is often useful to keep track of the objects that are bound to variables This helps in figuring out what some code does, and in de-bugging code that is not working properly If you start getting a lot of Null Pointer errors, for example, you may find doing a program trace can be very helpful! Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 13 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 14 Tracing Objects with Object Diagrams (1) Tracing Objects with Object Diagrams (2),, ; = new (); Note: to save typing, Java allows us to declare multiple variables of = new (); the same type on one single line.,, ; = new (); = new (); The syntax is the basically same, but we can have more than one variable name.turnclockwise(); (separated by commas) after the single variable type..turnclockwise(); Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 15 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 16 4

Tracing Objects with Object Diagrams (3) Tracing Objects with Object Diagrams (4),, ; = new ();,, ; = new (); = new (); = new ();.turnclockwise();.turnclockwise(); Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 17 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 18 Tracing Objects with Object Diagrams (5) Tracing Objects with Object Diagrams (6),, ; = new ();,, ; = new (); = new (); = new ();.turnclockwise();.turnclockwise(); Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 19 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 20 5

Reminder: Orphans are Unavailable Forever After the first object is let go, so no variable name is bound to it, we cannot ever use it again This allows the JVM to free up some memory space, which can be useful in large, complex programs Don t do it by accident! = new (); = new (); = ; This Week & Next Friday: in the CS Lab (16 Wing) Meetings this week: Monday Thursday: regular classroom First homework assignment posted to D2L Due by 5:00 PM, Friday 15 Sept., on D2L Reading: sections 3.1 3.10 Due start of day, next Monday (7:45 AM, 18 Sept.) Quiz 01 next Monday, 18 Sept. (end of class) Covers materials from lectures 01 03 (all on class Notes page) Office Hours: Wing 210 Monday/Tuesday, 4:30 PM 6:30 PM Thursday/Friday, 9:00 AM 10:30 AM Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 21 Tue./Wed., 12/13 Sept. 2017 Software Design I (CS 120) 22 6