Midterms Save the Dates!

Similar documents
Midterms Save the Dates!

Midterms Save the Dates!

Midterms Save the Dates!

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

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

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

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

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

Midterms Save the Dates!

Midterms Save the Dates!

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

Midterms Save the Dates!

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

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

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

Midterms Save the Dates!

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

Classes. Classes as Code Libraries. Classes as Data Structures

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

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

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

JAVA: A Primer. By: Amrita Rajagopal

1B1b Classes in Java Part I

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

Administrivia. IBM Info Session Date: Wed,, Jan 13 Time: 5:30 7 pm Location: Wesbrook 100

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

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

CSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes

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

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

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

Express Yourself. Writing Your Own Classes

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

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

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

! 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.

Introduction to Objects. James Brucker

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

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

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

Software and Programming 1

CS121/IS223. Object Reference Variables. Dr Olly Gotel

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

Exploring the Java API, Packages & Collections

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

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

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

Midterms Save the Dates!

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

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

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

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

Software and Programming 1

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

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

Tips from the experts: How to waste a lot of time on this assignment

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Readings for This Lecture

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

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

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 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

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

CS18000: Programming I

CS112 Lecture: Defining Instantiable Classes

Introduction to Object-Oriented Programming

Fundamentos de programação

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Final Exam Practice. Partial credit will be awarded.

Chapter 6 Lab Classes and Objects

Lab 1: Introduction to Java

Chapter Two Bonus Lesson: JavaDoc

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

CS 101 Fall 2006 Midterm 3 Name: ID:

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

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

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

BM214E Object Oriented Programming Lecture 8

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

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

Object-Oriented Programming in Processing

Week 7 - More Java! this stands for the calling object:

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Tips from the experts: How to waste a lot of time on this assignment

EECS168 Exam 3 Review

Lecture 02, Fall 2018 Friday September 7

Object-Oriented Programming

Agenda: Notes on Chapter 3. Create a class with constructors and methods.

Exam Percentage: / 55 = %

Transcription:

University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Creating Your Own Class Lecture 7 Readings This Week s Reading: Ch 3.1-3.8 (Major conceptual jump) Next Week: Review Ch 1-4 (that were previously assigned) (Reminder: Readings are absolutely vital for learning this stuff!) Some slides borrowed from Kurt Eiselt, Tamara Munzner, and Steve Wolfman. Some learning goals from Beth Simon. Labs and Tutorials This week is Lab #3. Lab #4 is up. 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 If you have a conflict with the first midterm, send me an email with your name, student ID number, and a brief explanation of your conflict, by noon, Tuesday, February 3! 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. PeerWise Instructions For people participating in PeerWise, instructions are now available on WebCT. Your first questions/answers must be done by February 9. If you have questions/problems, please contact Paul Denny. However, to encourage participation, I will give 1 pt of extra credit, applied to your labs, if you complete all three surveys in time. 1

WebCT Bulletin Boards I encourage you to read and ask questions on the WebCT bulletin boards: Faster response time: many TAs, both profs, and fellow students read and respond. Everyone benefits from answer. Learning Center The TAs mentioned that they are in the CS Learning Center, ready to help, but often nobody shows up. Just a reminder that TAs are there, and they want to help you! Learning Goals By the end of the next several lectures you will be able to Create your own classes, with: Public and private fields and methods Helpful documentation that works with javadoc Basic principles i of abstraction and encapsulation (information hiding) Explain why abstraction and information hiding are important. Learning Goals By the end of class today you will be able to Create your own new class. Create simple methods in your class. Call your methods from another class. Trace the execution flow between the two classes. Pass parameters to your methods and return values. Declare instance variables in your class, make them private, and create getters/setters From Using to Creating Like the difference between buying clothes versus designing and sewing clothes: Buyer: Browses stores, looks for the clothes they want, gets the clothes, and wears them. Designer/Tailor: Tries to design clothing that people want, creates patterns for making the clothing, makes lots of clothes. (Designer/Tailor is also a buyer: buying fabric, thread, buttons, beading, etc. Using simpler objects to create new objects.) From Using to Creating Like the difference between buying clothes versus designing and sewing clothes: Using Classes: Browse Java library, look for the classes we want, import them, and use them. Creating Classes: Try to design classes that people want, create patterns for making objects, make objects. (Creator is also a user: we ll often use objects when creating new objects.) 2

Wait! We ve been doing this already! A Simple Java Program // Our first Java program. /* Traditionally, one s first program in a new language prints out Hello, World! */ public class HelloWorld { public static void main(string[ ] args) { System.out.println( Hello, World! ); A Simple Java Program // Our first Java program. /* Traditionally, one s first program in a new language prints out Hello, World! */ public class HelloWorld { public static void main(string[ ] args) { System.out.println( Hello, World! ); Last Few Lectures: Using classes import java.math.biginteger; BigInteger salary; salary = new BigInteger( 111222333 ); totalcompensation = salary.add(bonus); Using HelloWorld Using HelloWorld Not needed if.class file in same directory 3

Using HelloWorld Not needed if.class file in same directory HelloWorld foo; Using HelloWorld Not needed if.class file in same directory HelloWorld foo; foo = new HelloWorld(); Using HelloWorld Not needed if.class file in same directory HelloWorld foo; foo = new HelloWorld(); foo.main(null); // Don t worry about null Using HelloWorld Not needed if.class file in same directory HelloWorld foo; foo = new HelloWorld(); HelloWorld.main(null); // main is static Whoa what s going on? Sequential Execution Suppose you have a list of tasks: Get money from bank machine. Pick up clothes from dry cleaner. Get lunch from takeout place. 4

Suppose you have a list of tasks: Get money from bank machine. Pick up clothes from dry cleaner. Get lunch from takeout place. Bank Machine: Check PIN Check balance Dispense cash Dry Cleaner: Find clothes Deliver clothes Restaurant: Take order Prepare food Suppose you have a list of tasks: Get money from bank machine. Pick up clothes from dry cleaner. Get lunch from takeout place. Bank Machine: Check PIN Check balance Dispense cash Dry Cleaner: Find clothes Deliver clothes Restaurant: Take order Prepare food Suppose you have a list of tasks: Get money from bank machine. Pick up clothes from dry cleaner. Get lunch from takeout place. Bank Machine: Check PIN Check balance Dispense cash Dry Cleaner: Find clothes Deliver clothes Restaurant: Take order Prepare food Suppose you have a list of tasks: Get money from bank machine. Pick up clothes from dry cleaner. Get lunch from takeout place. Bank Machine: Check PIN Check balance Dispense cash Dry Cleaner: Find clothes Deliver clothes Restaurant: Take order Prepare food Sequential Execution Suppose you have a list of tasks: Get money from bank machine. Pick up clothes from dry cleaner. Get lunch from takeout place. Bank Machine: Check PIN Check balance Dispense cash Dry Cleaner: Find clothes Deliver clothes Restaurant: Take order Prepare food Suppose you some Java statements: bonus = options.multiply(gain); p py(g ); total = salary.add(bonus); System.out.println(total); 5

Suppose you some Java statements: BigInteger Class: Suppose you some Java statements: BigInteger Class: bonus = options.multiply(gain); py(g total = salary.add(bonus); System.out.println(total); BigInteger multiply () { BigInteger add () { System Class: bonus = options.multiply(gain); py(g total = salary.add(bonus); System.out.println(total); BigInteger multiply () { BigInteger add () { System Class: Suppose you some Java statements: BigInteger Class: Suppose you some Java statements: BigInteger Class: bonus = options.multiply(gain); py(g total = salary.add(bonus); System.out.println(total); BigInteger multiply () { BigInteger add () { System Class: bonus = options.multiply(gain); py(g total = salary.add(bonus); System.out.println(total); BigInteger multiply () { BigInteger add () { System Class: How to Create a Class Suppose you some Java statements: BigInteger Class: Client Programmer: File MyClass.java bonus = options.multiply(gain); py(g total = salary.add(bonus); System.out.println(total); BigInteger multiply () { BigInteger add () { System Class: MyClass a; public class MyClass { 6

Client Programmer: How to Create a Class File MyClass.java Let s try it MyClass a; public class MyClass { a.sayhello(); public void sayhello() { System.out.println ( Hello ); Parameter Passing How do we get information into a method? We pass it parameters. ( pass as in passing the puck.) Creating New Methods Let s add a method for flattery: parameters Passing Parameters In the object user (the caller): a.flatter( fabulous ); In the class definition (the callee): Passing Parameters In the object user (the caller): a.flatter( fabulous ); In the class definition (the callee): 7

Review: Passing Parameters In the object user (the caller): a.flatter( fabulous ); adjective = fabulous In the class definition (the callee): Passing Parameters In the object user (the caller): a.flatter( fabulous ); adjective = fabulous In the class definition (the callee): Formal Parameters In the object user (the caller): a.flatter( fabulous ); In the class definition (the callee): The formal parameters define new variables. These are the types the method expects to get. Actual Parameters In the object user (the caller): a.flatter( fabulous ); In the class definition (the callee): The actual parameters are what gets passed when the method is called. Their values are copied into the formal parameters. Multiple Parameters? Let s add a method for flattery: formal parameters public void flatter(string interj, String adj) { System.out.println(interj + "You look " + adj + "!"); return Statement How do we get information back from the method? We use a return statement. ( return as in election returns results that come back to you) 8

return Statement The return statement in a method tells the method to return to the caller. Syntax: return; return expression; With an expression, it gets evaluated and that value is what the method call returns. Example Let s write a method that prints Hello on the screen and returns the number 42. (You ll use the return statement for much more interesting things later) Void Return Types Common confusion of output on screen versus returning a value. Can you say: int b = 3 + a.sayhello(); Void Return Types Common confusion of output on screen versus returning a value. Can you say: int b = 3 + a.sayhello(); No. sayhello returns type void. But we can use a method that has an int return type: int b = 3 + a.sayhelloandreturn42(); Questions? Objects Can Contain Variables It s convenient for objects to remember things about themselves, e.g.: java.awt.rectangle class in the book if we created a UBCStudent class To allow this, objects can have their own variables. Declare the variables in the class. But each (instance of an) object gets its own copy: Called instance fields or instance variables 9

Example Class: Celebrity public class Celebrity { String name; String description; Each Object Has Its Own Instance Fields You declare the instance fields once, but each object gets its own copies??? //...declare methods, too... Each Object Has Its Own Instance Fields You declare the instance fields once, but each object gets its own copies??? Remember: the class is like a blueprint or pattern. It says how to make objects: A car blueprint shows a steering wheel, but each car gets its own steering wheel. A dress pattern shows one pocket, but each dress has its own pocket. Celebrity class shows String variables for name and description, but each object gets its own. Accessing Instance Fields If the instance fields are public, users of the class can treat them like normal variables: prof.name = Alan ; prof.description = dignified ; (If you say neither public nor private, the default is called package access, which is basically public for our purposes.) Keep Instance Fields Private! If the instance fields are public, users of the class can treat them like normal variables: prof.name = Alan ; prof.description = dignified ; If the instance fields are public, users of the class can misuse or mess up your instance fields! Good style is to keep them private. Keep Instance Fields Private! Good style is to keep them private. If the instance fields are private, users of the class can t access them. How do they use your class? 10

Accessor and Mutator Methods Questions? Good style is to keep them private. If the instance fields are private, users of the class can t access them. How do they use your class? The standard Java convention is to provide public methods to access (read) or mutate (modify) the instance fields. E.g., getname() or setname(string name) You can control access to fields this way. Constructors What happens if the user forgets to set the instance fields? Constructors What happens if the user forgets to set the instance fields? It s best to supply a default for instance fields at the moment the object is created. This way, all objects are always properly created. We do this by writing our own code for the constructor methods. Constructors To create a constructor method, declare a method with: the same name as the class no return type (at all, not even void) parameters, if you want Put code in the method to initialize the instance fields. Constructors public Celebrity() { this.name = Joe Schmoe"; this.description = forgettable"; this.salary = new BigInteger( 0 ); 11

Constructors public Celebrity() { name = Joe Schmoe"; description = forgettable"; salary = new BigInteger( 0 ); Using vs. Designing Classes Using a Class Read API. Get and use objects. Instantiate objects with new ClassName() Call methods to get things done. Access public things only. Designing a Class Write API. Provide blueprint/pattern for objects. Define constructor ClassName() { Implement methods. Say how to actually do things. Decide what is public and what is private. Questions? 12