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

Similar documents
Review: Classes and Object Instances. Review: Creating an Object. Using Multiple Objects. 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.

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

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

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

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

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

C++ for Java Programmers

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

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

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

Decisions, Decisions, Decisions. GEEN163 Introduction to Computer Programming

Cadence Capture and PSpice Tutorial

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

Lab 1: Introduction to Java

COMP 105 Homework: Type Systems

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

Project 1 Balanced binary

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

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

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

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

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CMPE 152 Compiler Design

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

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

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

Using System.out.println()

CS/SE 153 Concepts of Compiler Design

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

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

Practice Midterm 1 Answer Key

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

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

CS/SE 153 Concepts of Compiler Design

: Distributed Systems Principles and Paradigms Assignment 1 Multithreaded Dictionary Server

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

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

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

CMPE 152 Compiler Design

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

Chapter 6 Introduction to Defining Classes

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

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

Lab 1 Introduction to UNIX and C

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

Unit 10: Data Structures CS 101, Fall 2018

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

Errors and Exceptions

Announcements. 1. Forms to return today after class:

Performance Measurement

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

CS162 Week 1. Kyle Dewey. Friday, January 10, 14

CS61C : Machine Structures

Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

CS/ENGRD 2110 SPRING 2018

Result: original lp in main is. Goal was.

EE 422C HW 6 Multithreaded Programming

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

Lab 7 Unit testing and debugging

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

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

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

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

CMPSCI 187 / Spring 2015 Sorting Kata

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

CS354 gdb Tutorial Written by Chris Feilbach

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

Remotely Test Any Networked Equipment

Lecture 7: Implementing Lists, Version 2

CMPE 152 Compiler Design

Lab: Supplying Inputs to Programs

Exception Handling Generics. Amit Gupta

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

Programming Languages and Techniques (CIS120)

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

ECE 15B COMPUTER ORGANIZATION

CS 103 Lab The Files are *In* the Computer

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

CSC 101 Spring 2010 Lab #8 Report Gradesheet

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

CS61B Lecture #5: Arrays and Objects

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

CMSC 201 Spring 2018 Lab 01 Hello World

CSC209. Software Tools and Systems Programming.

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

CS Introduction to Programming Fall 2016

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

CMSC201 Computer Science I for Majors

CS61B Lecture #7. Announcements:

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

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

CP Lab 5: Functions, pointers, some arrays

Transcription:

Review: Using Imported Code Class #06: Objects, Memory, & Program Traces Software Engineering I (CS 120): M. Allen, 30 Jan. 2018 ; = new ();.setbackground( java.awt.color.blue );.setforeground( java.awt.color.yellow );.turnby( 90 );.turnby( 90 );.turnby( 90 ); import java.awt.color; ; = new (); This adds the import command to the class, and now we don t have to use the full address for our colors These programs produce exactly the same result on-screen when they run.setbackground( Color.blue );.setforeground( Color.yellow );.turnby( 90 );.turnby( 90 );.turnby( 90 ); Tuesday, 30 Jan. 2018 Software Design I (CS 120) 2 What About the? If we want to use built-in Java classes like Color, we need to either refer to their full class address or import For classes that aren t built in to the language, there is another simple option: put all the class files into the same source directory (folder) When we run our program, the JVM will allow us to use any class of object that we address, import, or that it can find along-side the source code for the main() method Review: Classes and Object Instances cil; cil = new (); We are working with both a class () and an object (actual code and data saved in memory, to which the name cil 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 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 3 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 4 1

Review: Creating an Object Often, we declare and instantiate objects separately, using two distinct Java commands: cil; cil = new (); Often, however, we do this together, in a single line: cil = new (); Using Multiple Objects ; = new (); cil = new ();.turnclockwise;.moveforward; cil.turncounterclockwise; cil.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 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 5 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 6 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 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 7 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 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 8 2

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! ;.moveforward(); 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 (); cil; 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 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 9 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 10 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 = cil; cil 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 (); cil; 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!! Tuesday, 30 Jan. 2018 Software Design I (CS 120) 11 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 12 3

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 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 13 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 14 Anatomy of a Runtime Error Lines starting with at tell us where the error was made Look for your file names and line numbers In Eclipse, you can click on line number to go there in the code These error messages can be quite complex! They are meant to be helpful, however. The first line tells us what sort of error we have made This is a common type: Null Pointer Exception Tracing Objects, cil, ; = new ();.moveforward();.turnclockwise(); cil = new (); = cil; 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! Tuesday, 30 Jan. 2018 Software Design I (CS 120) 15 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 16 4

Tracing Objects with Object Diagrams (1) Tracing Objects with Object Diagrams (2), cil, ; = new (); Note:.moveForward(); to save typing, Java allows us.turnclockwise(); to declare multiple variables of cil = new (); the same type on one single line. The syntax is the basically same, but we can have more than one variable = cil; name (separated by commas) after the single variable type. cil, cil, ; = new ();.moveforward();.turnclockwise(); cil = new (); = cil; cil Tuesday, 30 Jan. 2018 Software Design I (CS 120) 17 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 18 Tracing Objects with Object Diagrams (3), cil, ; = new ();.moveforward();.turnclockwise(); cil = new (); = cil; cil Tracing Objects with Object Diagrams (4), cil, ; = new ();.moveforward();.turnclockwise(); cil = new (); = cil; cil Tuesday, 30 Jan. 2018 Software Design I (CS 120) 19 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 20 5

Tracing Objects with Object Diagrams (5) Tracing Objects with Object Diagrams (6), cil, ; = new ();, cil, ; = new ();.moveforward();.moveforward();.turnclockwise(); cil = new (); cil.turnclockwise(); cil = new (); cil = cil; = cil; Tuesday, 30 Jan. 2018 Software Design I (CS 120) 21 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 22 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! cil = new (); cil = new (); = cil; This Week & Next Meetings this week: Monday Thursday: regular classroom Friday: in the CS Lab (16 Wing) Homework 01: Groundhog image Friday, 02 Feb., 5:00 PM, D2L Reading assignment: Chapter 3, sections 1 10 Next Monday, 05 Feb., by start of class Quiz 01: Covers lectures 01 04 (all on Notes page) Monday, 05 Feb. (end of class) Office Hours: Wing 210 Monday/Wednesday/Friday, 10:00 AM 11:00 AM Tuesday/Thursday, 1:30 PM 3:00 PM Tuesday, 30 Jan. 2018 Software Design I (CS 120) 23 Tuesday, 30 Jan. 2018 Software Design I (CS 120) 24 6