Midterms Save the Dates!

Similar documents
Midterms Save the Dates!

Midterms Save the Dates!

Midterms Save the Dates!

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

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

Midterms Save the Dates!

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. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Midterms Save the Dates!

Final Exam. Programming Assignment 3. 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!

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

Survey #2. 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

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

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

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

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

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Chapter 4: Writing Classes

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

CSCI 161 Introduction to Computer Science

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

Defining Classes and Methods

MIDTERM REVIEW. midterminformation.htm

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

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

H212 Introduction to Software Systems Honors

Classes. Classes as Code Libraries. Classes as Data Structures

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

Anatomy of a Class Encapsulation Anatomy of a Method

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

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

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

CS1083 Week 2: Arrays, ArrayList

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

1B1b Classes in Java Part I

5. Defining Classes and Methods

5. Defining Classes and Methods

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

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

Midterms Save the Dates!

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Chapter 4 Defining Classes I

CS111: PROGRAMMING LANGUAGE II

Week 3 Classes and Objects

AP CS Unit 3: Control Structures Notes

JAVA: A Primer. By: Amrita Rajagopal

EECS168 Exam 3 Review

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

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

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

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

COMP 250 Winter 2011 Reading: Java background January 5, 2011

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

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

University of Massachusetts Amherst, Electrical and Computer Engineering

Darrell Bethea May 25, 2011

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Defining Classes and Methods

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

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

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

CS 101 Fall 2006 Midterm 3 Name: ID:

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Object Oriented Programming. Java-Lecture 1

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

CS 2530 INTERMEDIATE COMPUTING

CS1004: Intro to CS in Java, Spring 2005

Defining Classes and Methods

Overview. Lab 5 Methods and Parameters

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS 177 Recitation. Week 1 Intro to Java

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

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

Methods and Data (Savitch, Chapter 5)

AP Computer Science Unit 1. Programs

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

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

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

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

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

Slide 1 CS 170 Java Programming 1

Important Java terminology

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Java Libraries. Lecture 6 CGS 3416 Fall September 21, 2015

CSCE 156 Computer Science II

Initial Coding Guidelines

CS121: Computer Programming I

This Week s Agenda (Part A) CS121: Computer Programming I. Changing Between Loops. Things to do in-between Classes. Answer. Combining Statements

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

public class TicketMachine Inner part omitted. public class ClassName Fields Constructors Methods

Assignment 1 due Monday at 11:59pm

Lab5. Wooseok Kim

Transcription:

University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Instance Variables if Statements Readings This Week s Reading: Review Ch 1-4 (that were previously assigned) (Reminder: Readings are absolutely vital for learning this stuff!) Lecture 8 Some slides borrowed from Kurt Eiselt, Tamara Munzner, and Steve Wolfman. Some learning goals from Beth Simon. Labs and Tutorials This week is Lab #4. 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 Midterm Study Tips: Old midterms on-line. Try programming without notes. Try programming without computer! Programming Assignment 1 Assignment 1 is up on WebCT! Click on the Assignments icon. Due at NOON, February 17 (Tuesday), via electronic hand in. It may take me a couple days to setup the electronic hand in, so if you re really fast, please wait a day or two. Start early! 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. 1

Learning Goals By the end of class today you will be able to Create classes with instance fields. Access public instance fields directly. Protect your instance fields by making them private. Provide accessor and mutator methods for private instance fields. Create constructor methods to initialize the instance fields. Have your program make decisions using if statements. Review: How to Create a Class Client Programmer: File MyClass.java MyClass a; public class MyClass { Review: How to Create a Class Client Programmer: MyClass a; File MyClass.java public class MyClass { a.sayhello(); public void sayhello() { System.out.println ( Hello ); Review: Passing Parameters In the object user (the caller): a.flatter( fabulous ); In the class definition (the callee): public void flatter(string adjective) { System.out.println("Wow, you look " + adjective + "!"); Review: Passing Parameters In the object user (the caller): a.flatter( fabulous ); adjective = fabulous In the class definition (the callee): public void flatter(string adjective) { System.out.println("Wow, you look " + adjective + "!"); Review: 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) 2

Review: return Statement Questions? The return statement in a method tells the method to return to the caller. return; return expression; With an expression, it gets evaluated and that value is what the method call returns. 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. ab Declare the variables in the class. But each (instance of an) object gets its own copy: Called instance fields or instance variables or attributes Example Class: Celebrity public class Celebrity { String name; String description; //...declare methods, too... Each Object Has Its Own Instance Fields You declare the instance fields once, but each object gets its own copies??? 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. 3

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. Public vs. Private public means accessible outside of this class private means accessible only within this class. public class Celebrity { private String name; private String description; 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? 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. 4

What happens if the user forgets to set the instance fields? 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. 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. public Celebrity() { this.name = Joe Schmoe"; this.description = forgettable"; this.salary = new BigInteger( 0 ); 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. 5

Questions? Jumping ahead a bit We re going to jump ahead a little bit. This is useful for Assignment 1. Also makes it easier to write more interesting example classes You can skim Ch51 5.1 and d52f 5.2 for more details. Making Decisions Recall that a sliderule can compute multiplication, but can t make decisions. But even a transistor can make a decision! How do we get our Java programs to make decisions? Java s if statement if ( condition ) statement; Examples: if (tax < 0) tax = 0; if (age < 19) System.out.println( Sorry, you may not buy alcohol. ); Java s if statement Intuitively, a lot like English: If you re hungry, have a sandwich. if (tax < 0) tax = 0; Java s if statement You can control a group of statements with curly braces. if ( condition ) { statement;... Example: if (age < 19) { System.out.print( Sorry, ); System.out.print( you may not ); System.out.println( buy alcohol. ); 6

Java s if statement with else if ( condition ) statement; else statement; Example: if (age < 19) System.out.println( You may not buy alcohol. ); else System.out.println( Buy beer! ); System.out.println( This lecture sponsored by Molson ); Java s if statement with else if ( condition ) statement; else statement; Sort of like English: If it s sunny, we ll go hiking, or else we ll go see a movie. (It d make more sense if the reserved word had been otherwise, but that s too long to type.) Java s if statement with else Questions? if ( condition ) statement; else statement; As before, you can control a group of statements by putting curly braces around them. (In Java, any place where you can put one statement, you can put a bunch of statements enclosed in curly braces.) Designing a Die Class Let s create a class to represent a die (as in rolling dice, not other meanings of die ): What should we call it? What methods should we provide? What sort of things do you do to a die? What instance variables (or attributes, fields) do we need? Implementing Die /** Provides a simple model of a die (as in pair of dice). */ public class Die { 7

Random Numbers Random class in java.util package public Random() Constructor public float nextfloat() Returns random number between 0.0 (inclusive) and 1.0 (exclusive) public int nextint() Returns random integer ranging over all possible int values public int nextint( int num ) Returns random integer in range 0 to (num-1) Trying It Out Die class has no main method. Best is to write another class that instantiates some objects of your new class and tries them out. Sometimes called a tester or testbench Implementing RollDice Questions? public class RollDice { public static void main ( String [] args) { 8