Using Classes and Objects. Lecture 7. Midterms Save the Dates! Extra Credit Survey

Similar documents
PeerWise Study. Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Constants Using Classes and Objects

Midterms Save the Dates!

Midterms Save the Dates!

Midterms Save the Dates!

Midterms Save the Dates!

Midterms Save the Dates!

Midterms Save the Dates!

Midterms Save the Dates!

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Survey #2. Assignment #3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Static Interface Types.

Survey #2. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Survey #2. Programming Assignment 3. Final Exam. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

Midterms Save the Dates!

Midterms Save the Dates!

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

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

Welcome to CS 115 (Winter 2018)

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Midterms Save the Dates!

Welcome to CS 115 (Winter 2019)

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

CSCI Wednesdays: 1:25-2:15 Keller Thursdays: 4:00-4:50 Akerman 211

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

! Rest of Chap 2 ! 2.3-4, ! Rest of Chap 4 ! ! Things that do not vary. ! unlike variables. ! will never change. !

Java Basics Lecture: January 26, 2012 (On-line Lecture 1)

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

San Jose State University College of Science Department of Computer Science CS151, Object-Oriented Design, Sections 1,2 and 3, Spring 2017

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

CS 240 Fall 2015 Section 004. Alvin Chao, Professor

CS 241 Data Organization. August 21, 2018

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Survey #3. Final Exam. Today is totally optional! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings.

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

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

CS 415 Midterm Exam Spring 2002

CS 231 Data Structures and Algorithms, Fall 2016

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

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

Programming with Java

Topic 2: Introduction to Programming

TA hours and labs start today. First lab is out and due next Wednesday, 1/31. Getting started lab is also out

Midterms Save the Dates!

Syntax and Grammars 1 / 21

Welcome to... CS113: Introduction to C

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

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Introduction to the course and basic programming concepts

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

! Midterm 2: Thu Mar 16, 6:30pm (TODAY!) ! Woodward 2. ! hour-long exam, reserve 6:30-8 time slot. ! no labs/tutorials this week. ! Bunny.

Semantic Analysis. Lecture 9. February 7, 2018

Lab5. Wooseok Kim

! This week: Chapter 6 all ( ) ! Formal parameter: in declaration of class. ! Actual parameter: passed in when method is called

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

San Jose State University College of Science Department of Computer Science CS151, Object-Oriented Design, Sections 1, 2, and 3, Spring 2018

ENGR/CS 101 CS Session Lecture 3

B. Subject-specific skills B1. Problem solving skills: Supply the student with the ability to solve different problems related to the topics

Java Fall 2018 Margaret Reid-Miller

CS1 Lecture 3 Jan. 18, 2019

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

CMPE 152 Compiler Design

CPSC 213. Introduction to Computer Systems. About the Course. Course Policies. Reading. Introduction. Unit 0

Computer Science is...

KOMAR UNIVERSITY OF SCIENCE AND TECHNOLOGY (KUST)

Lecture C1 Data Representation. Computing and Art : Nature, Power, and Limits CC 3.12: Fall 2007

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

61A LECTURE 1 FUNCTIONS, VALUES. Steven Tang and Eric Tzeng June 24, 2013

Lesson 3: Accepting User Input and Using Different Methods for Output

VARIABLES AND TYPES CITS1001

Lecture 1. Course Overview, Python Basics

Using System.out.println()

CS 6371: Advanced Programming Languages

Initial Coding Guidelines

CS/SE 153 Concepts of Compiler Design

Introduction to Computers and Java

CS/SE 153 Concepts of Compiler Design

CS16 Exam #1 7/17/ Minutes 100 Points total

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

1/11/2010 Topic 2: Introduction to Programming 1 1

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

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

Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Advance mode: Auto

! labs this week. ! midterms returned. ! work through what you got wrong on midterm. ! can earn back up to 5 out of 70 points

CS 116. Lab Assignment # 1 1

Last Week: Organizing Data. Last Week: Parallel Arrays. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

printf( Please enter another number: ); scanf( %d, &num2);

Lecture 1. Course Overview, Python Basics

! labs last week. ! still time to work through lab 7 (midterm correction) ! can earn back up to 5 out of 70 points

COMP 105 Homework: Type Systems

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Topic 4 Expressions and variables

Lecture 1. Course Overview Types & Expressions

CS 121 Intro to Programming:Java - Lecture 2. Professor Robert Moll (+ TAs) CS BLDG

Transcription:

University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Readings Your textbook is Big Java (3rd Ed). This Week s Reading: Ch 2.6-2.10, Finish Ch 4 Using Classes and Objects Lecture 7 Some slides borrowed from Kurt Eiselt, Tamara Munzner, and Steve Wolfman. Some learning goals from Beth Simon. Labs and Tutorials This week is Lab #2. It has a pre-lab! Midterms Save the Dates! Midterm #1 is 5:30-6:30pm on February 10 (Tuesday) in Woodward IRC 2 Midterm #2 is 6-7pm on March 11 (Wednesday) in Woodward IRC 2 You must notify me well in advance (I will specify deadlines as the dates approach) if you have an unavoidable conflict with a midterm. Extra Credit Survey Dr. Ben Yu is studying attitudes towards learning in introductory CS classes. Three surveys to complete during the term. Survey 1 is on WebCT now. Due January 28 These are completely optional. However, to encourage participation, I will give 1 pt of extra credit, applied to your labs, if you complete all three surveys in time. Learning Goals By the end of the next several lectures you will be able to Explain the concepts of class, object, and method as used in computer programming. Find pre-defined classes in the Java library, understand the documentation, and use the methods for basic classes. Write programs that use common Java classes like BigInteger, String, and Scanner. 1

Learning Goals By the end of class today you will be able to Use constructors to create new objects, and explain why this is necessary. Identify the basic components of a method call. (Explain what syntax, runtime, and logical errors are.) Review: Programming with Classes What if data we want to work with is more complex than these few primitive data types? We can make our own data type: specifies nature of data we want to work with operations that can be performed on that kind of data (Other people can make data types, too, and we can use their work.) Review: Primitive Types vs. Classes Primitive Types Classes Pre-defined in Java Written by other programmers or by you Simplest things, e.g., int Can be arbitrarily complex Operators: +, -, Values belong to types. E.g., 3 is an int, 3.14159 is a double Methods Objects belong to classes E.g., a Toyota Prius is a car. Review: API Documentation Online Java library documentation at http://java.sun.com/javase/6/docs/api let s take a look! Everything we need to know: critical details and often many things far beyond current need Classes in libraries are often referred to as Application Programming Interfaces or just API Example: BigInteger Let s write a simple program to print out how much money you d make if your compensation package is a combination of salary and stock options. GetRichQuick int salary = 123456; int stockoptions = 10000; int profitpershare = 30; int optioncompensation = stockoptions * profitpershare; int totalcompensation = salary + optioncompensation; 2

GetRichQuick 2 int salary = 111222333444555666777888999; int stockoptions = 10000000000000000000000; int profitpershare = 3000000000000000000000; int optioncompensation = stockoptions * profitpershare; int totalcompensation = salary + optioncompensation; GetRichQuick w/ BigInteger import java.math.biginteger; //Tell Java to use BigInteger pkg int salary = 111222333444555666777888999; int stockoptions = 10000000000000000000000; int profitpershare = 3000000000000000000000; int optioncompensation = stockoptions * profitpershare; int totalcompensation = salary + optioncompensation; GetRichQuick w/ BigInteger import java.math.biginteger; //Tell Java to use BigInteger pkg BigInteger salary = 111222333444555666777888999; BigInteger stockoptions = 10000000000000000000000; BigInteger profitpershare = 3000000000000000000000; BigInteger optioncompensation = stockoptions * profitpershare; BigInteger totalcompensation = salary + optioncompensation; GetRichQuick: Constructors import java.math.biginteger; //Tell Java to use BigInteger pkg BigInteger salary = new BigInteger( 111222333444555666777888999 ); BigInteger stockoptions = new BigInteger( 10000000000000000000000 ); BigInteger profitpershare = new BigInteger( 3000000000000000000000 ); BigInteger optioncompensation = stockoptions * profitpershare; BigInteger totalcompensation = salary + optioncompensation; GetRichQuick: Methods import java.math.biginteger; //Tell Java to use BigInteger pkg BigInteger salary = new BigInteger( 111222333444555666777888999 ); BigInteger stockoptions = new BigInteger( 10000000000000000000000 ); BigInteger profitpershare = new BigInteger( 3000000000000000000000 ); BigInteger optioncompensation = stockoptions.multiply(profitpershare); BigInteger totalcompensation = salary.add(optioncompensation); Recap: Using classes Import the name of the class you want: import java.math.biginteger; Declare variables using the class name: BigInteger salary; Create values (objects) of the new type/class by using constructors: salary = new BigInteger( 111222333 ); Operate on the objects using methods: 3

Recap: Using classes Import the name of the class you want: import java.math.biginteger; Declare variables using the class name: BigInteger salary; Create values (objects) of the new type/class by using constructors: salary = new BigInteger( 111222333 ); Operate on the objects using methods: Literals With the primitive types, how do you create values with that type? E.g., how do we create integer values? 1. You type some digits, like 3, or 42 2. You combine integer-valued things with operators that work on integers, e.g., 3+42*(a-b) Literals With the primitive types, how do you create values with that type? E.g., how do we create integer values? 1. You type some digits, like 3, or 42 2. You combine integer-valued things with operators that work on integers, e.g., 3+42*(a-b) A bunch of digits are an integer literal. It s the basic way to create an integer value More Literals How about a value of type double? 1. You type a bunch of digits with a decimal point, and optionally the letter e or E followed by an exponent 2. You can combine doubles with operators that work on doubles. More Literals How about a value of type double? 1. You type a bunch of digits with a decimal point, and optionally the letter e or E followed by an exponent 2. You can combine doubles with operators that work on doubles. Long Literals How about values of type long? 1. You type a bunch of digits followed by the letter l or L 2. You combine previously created longs Those are literals! 4

Literals General Pattern To create values of a primitive type: 1. There s some way to type a literal 2. There are operators that create values of the given type. Literals for Classes? Classes are like primitive types, except they can be defined any way you like, and they can be much more complex. How to create a value (an object) of a given class? 1. Invent some way to type a literal??? 2. Operators that create objects of that class (methods). Constructors! A constructor is the equivalent of a literal for a class. It s how you create a new object that belongs to that class. Examples: new BigInteger( 999999 ) new Rectangle(10, 20, 30, 40) new UBCStudent( Alan Hu,12345678, ) Constructor Syntax The reserved word new Followed by the name of the class Followed by an open parenthesis ( Followed by any parameters (information needed to construct the object) Followed by a closing parenthesis ) Using Constructors Use a constructor just as you d use a literal. Example: For the int type: int a = 3; For the BigInteger class: BigInteger a = new BigInteger( 3 ); Primitive Types vs. Classes Primitive Types Classes Pre-defined in Java Written by other programmers or by you Simplest things, e.g., int Can be arbitrarily complex Operators: +, -, Values belong to types. E.g., 3 is an int, 3.14159 is a double Literals Methods Objects belong to classes E.g., you are a UBC Student Constructors 5

Focus on Constructors Import the name of the class you want: import java.math.biginteger; Declare variables using the class name: BigInteger salary; Create values (objects) of the new type/class by using constructors: salary = new BigInteger( 111222333 ); Operate on the objects using methods: Focus on Methods Import the name of the class you want: import java.math.biginteger; Declare variables using the class name: BigInteger salary; Create values (objects) of the new type/class by using constructors: salary = new BigInteger( 111222333 ); Operate on the objects using methods: People say call or invoke a method: As in call a plumber, call a doctor, invoke the Charter of Rights, or invoke demons from Hell Shared meaning: getting someone or something else to get something done. ( Something else is the Java by the person who created the class. More next week.) object object method The object determines what methods are available to act on it. E.g., BigInteger objects have an add method. 6

Add to salary. Hit the ball. Beat the drum. object (noun) method (verb) object method parameters Stuff in parentheses after the method name. Provide additional information to method. Can be many parameters, depending on method. Notation like math functions f(x,y,z) = xy+z Change of Gears object (noun) method (verb) parameter Add bonus to salary. Hit the ball deep to the backhand corner. Beat the drum lightly in triple time. Program Development Use an editor to create your Java program often called source used interchangeably with program or instructions in the computer world Another program, a compiler or an interpreter, translates source into target language or object, which is often machine language Finally, your computer can execute object insight source object results editing translating executing Syntax Rules to dictate how statements are constructed. Example: open bracket needs matching close bracket If program is not syntactically correct, cannot be translated by compiler Different than humans dealing with natural languages like English. Consider statement t t with incorrect syntax (grammar) for weeks. rained in Vancouver it hasn t we still have pretty good shot at figuring out meaning 7

Semantics What will happen when statement is executed Programming languages have well-defined semantics, no ambiguity Different than natural languages like English. Consider statement: Mary counted on her computer. How could we interpret this? Programming languages cannot allow for such ambiguities or computer would not know which interpretation to execute Errors Computers follows our instructions exactly If program produces the wrong result, it s the programmer s fault unless the user inputs incorrect data then cannot expect program to output correct results: Garbage in, garbage out (GIGO) Debugging: process of finding and correcting errors Unfortunately can be very time consuming! compile-time error Errors Errors run-time error compile-time error insight source object results editing translating executing insight source object results editing translating executing Error at compile time (during translation) you did not follow syntax rules that say how Java elements must be combined to form valid Java statements Error at run time (during execution) Source compiles Syntactically (structurally) correct But program tried something computers cannot do like divide a number by zero. Typically program will crash: halt prematurely compile-time error Errors run-time error logical error Questions? insight source object results editing translating executing Logical error Source compiles Object runs But program may still produce incorrect results because logic of your program is incorrect Typically hardest problems to find 8