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!

Midterms Save the Dates!

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

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

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. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

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

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!

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

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

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

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

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

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

Chapter 4: Writing Classes

CS 177 Recitation. Week 1 Intro to Java

IT150/IT152 Concepts Summary Sheet

Static Methods. Why use methods?

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

Anatomy of a Class Encapsulation Anatomy of a Method

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

Initial Coding Guidelines

CS 251 Intermediate Programming Methods and More

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

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

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

Midterms Save the Dates!

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

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

AP Computer Science Unit 1. Programs

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Lab # 2. For today s lab:

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

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

Lecture Set 2: Starting Java

Example Program. public class ComputeArea {

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

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

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

CS112 Lecture: Defining Instantiable Classes

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

CS 251 Intermediate Programming Methods and Classes

CS1004: Intro to CS in Java, Spring 2005

Express Yourself. Writing Your Own Classes

4. Java Project Design, Input Methods

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

Lecture Set 2: Starting Java

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CSCI 1103: Introduction

Slide 1 CS 170 Java Programming 1

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

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

JAVA: A Primer. By: Amrita Rajagopal

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

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

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

1st Semester Examinations CITS1001 3

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

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

Review for Midterm. Instructor: Scott Kristjanson CMPT 135 SFU Surrey, Spring 2016

How to approach a computational problem

H212 Introduction to Software Systems Honors

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

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

MIDTERM REVIEW. midterminformation.htm

Darrell Bethea May 25, 2011

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

A short guide to learning more technology This week s topic: Windows 10 Tips

Utilities (Part 3) Implementing static features

Documentation Requirements Computer Science 2334 Spring 2016

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

CSCI 161 Introduction to Computer Science

COMP 102: Computers and Computing

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

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

Lab5. Wooseok Kim

Introduction to Computer Science Unit 2. Notes

Chapter Two Bonus Lesson: JavaDoc

Software and Programming 1

ing execution. That way, new results can be computed each time the Class The Scanner

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

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

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

AP Computer Science A

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

Advanced Object Oriented Programming EECS2030Z

Name: Checked: Objectives: Practice creating classes and methods, and using them in your programs.

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

Transcription:

University of British Columbia CPSC 111, Intro to Computation Alan J. Hu if Statements Designing Classes Abstraction and Encapsulation Readings This Week s Reading: Review Ch 1-4 (that were previously assigned) Next Week: Ch 5.1-5.4 (Ch 6.1-6.4 in 2 nd ed). (Reminder: Readings are absolutely vital for learning this stuff!) Lecture 09 Some slides borrowed from Kurt Eiselt and Beth Simon. Labs and Tutorials This week is Lab #4. Next week is Lab #5! (Some on-line material had out-of-date schedule fixed now.) 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 Have your program make decisions using if statements. Methodically approach the task of designing a class. Have more confidence creating your own classes. Jumping ahead a bit We re going to jump ahead a little bit. This is useful for Assignment 1 and Lab 5. Also makes it easier to write more interesting example classes You can skim Ch51 5.1 and d52f 5.2 for more details. Explain what abstraction is, and why it s important. Explain what information hiding (also known as encapsulation or data protection ) is, and why it s important. 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. ); 2

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? How do we test it? What should we make sure we can do? Class Design Two types of things in every class: things we KNOW about that object thing we can DO to/with that object This is how you start designing a class 3

Designing a Die Class: Let s do it Let s create a class to represent a die (as in rolling dice, not other meanings of die ): Design before you implement. You might have to adjust your design a bit as you implement, but that s t OK. Use UML if you want, or just variable declarations and method headers. What do we KNOW about a die? (attributes) What do we DO with a die? (methods) What should our tester try? Implementing Die /** Provides a simple model of a die (as in pair of dice). */ public class Die { 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) { 4

Mileage Computer Design a class for a trip computer in a car that computes the gas mileage Every 1m, a distance sensor sends a signal to the computer. Every 1 second, a fuel sensor sends a signal to the computer indicating how many ml of gas were used during the last second. The measurements can be reset We can query the computer for the gas mileage (reported as l/100km) averaged since the last reset. Let s do it Design before you implement. You might have to adjust your design a bit as you implement, but that s OK. Use UML if you want, or just variable declarations and method headers. What do we KNOW about the objects? (instance variables/attributes) What do we DO with with/to the object? (methods) What should our tester try? Mileage Computer Design a class for a trip computer in a car that computes the gas mileage Every 1m, a distance sensor sends a signal to the computer. Every 1 second, a fuel sensor sends a signal to the computer indicating how many ml of gas were used during the last second. The measurements can be reset We can query the computer for the gas mileage (reported as l/100km) averaged since the last reset. Updated Specs: Mileage Computer Design a class for a trip computer in a car that computes the gas mileage Every 1m, a distance sensor sends a signal to the computer. Every 1 second, a fuel sensor sends a signal to the computer indicating how many ml of gas were used during the last second. The measurements can be reset We can query the computer for the gas mileage averaged since the last reset. The computer can be configured in two modes: English or Metric. In Metric, it reports mileage as l/100km. In English, it reports mileage as mpg. How do we change our design? Implementation? Tests? Updated Specs 2: Mileage Computer Questions? Design a class for a trip computer in a car that computes the gas mileage Every 1m, a distance sensor sends a signal to the computer. Every 1 second, a fuel sensor sends a signal to the computer indicating how many ml of gas were used during the last second. The measurements can be reset We can query the computer for the gas mileage averaged since the last reset. We can query the computer for the current gas mileage that was achieved during the previous 1 second period. The computer can be configured in two modes: English or Metric. In Metric, it reports mileage as l/100km. In English, it reports mileage as mpg. The user can switch modes. How do we change our design? Implementation? Tests? 5

Managing Complexity Computer science creates the most complex artifacts ever created by humans: Windows Vista has 50 million lines of code. Current mainstream Intel processors (Core 2 Duo) have 410 million transistors; t quad-core Core i7 chips have 731 million transistors. How do we manage to do this? Abstraction Abstraction means creating higher-level ways to think about things, so you can ignore lots of lower-level details. Answer: Abstraction Abstraction in Real Life Abstraction means creating higher-level ways to think about things, so you can ignore lots of lower-level details. You already do this all the time to manage complexity: How do you get home after school? How do you manage your time in a day? How do you learn biology or chemistry or music or literature or? Abstraction in Software Abstraction means creating higher-level ways to think about things, so you can ignore lots of lower-level details. Creating new classes is a common and powerful way to create new abstractions: How do you print information to the console window? How do you make a Java program take control of the mouse? How do you compute a logarithm? Encapsulation Why is the gas cap on the outside of a car, while the places to add oil, coolant, brake fluid, etc., are under the hood? Why are the on/off, channel, and volume switches of a TV on the front, while other controls are hidden away? Why is the keyboard on a laptop obvious, while the jumpers to configure the disk drive are hidden inside? Encapsulation A car, a TV, a laptop, these are all abstractions! They were intended by their creators to be used in certain ways, and not in other ways: My mom adding water to the engine oil filler. Phone phreaking The designer encapsulates the product to hide the details, to make it harder to misuse. 6

Encapsulation, Information Hiding, Data Protection In computer software, the same idea is also called data protection or information hiding. Parnas s Law: Only what is hidden can be changed without risk: If I change problem 2 of the midterm, does that upset you? If I move the midterm to be RIGHT NOW instead, does that upset you? Encapsulation, Information Hiding, Data Protection In computer software, the same idea is also called data protection or information hiding. Parnas s Law: Only what is hidden can be changed without risk: If Java 7 implements System.out.println slightly differently, does that upset people? If Java 7 changes the name of System.out.println to be System.out.writeln, does that upset people? Encapsulation, Information Hiding, Data Protection By making the instance fields private, we protect them from misuse. They are accessible to others only through the public methods that we provide. If we write these methods carefully, we can help prevent mistakes. Designing a Class You want to provide a useful abstraction. It should let the user think about higher-level things, without worrying about the details. You use encapsulation to prevent other programmers (or yourself!) from misusing your class. Questions? Commenting Code Conventions explain what classes and methods do plus anywhere that you've done something non-obvious usually better to say why than what not useful int wishes = 3; // set wishes to 3 useful int wishes = 3; // follow fairy tale convention 7

javadoc Comments Specific format for method and class header comments running javadoc program will automatically generate HTML documentation Rules /** to start, first sentence used for method summary @param tag for parameter name and explanation @return tag for return value explanation other tags: @author, @version, etc. */ to end Running % javadoc Die.java % javadoc *.java javadoc Method Comment Example /** Sets the die shape, thus the range of values it can roll. @param numsides the number of sides of the die */ public void setsides(int numsides) { sides = numsides; /** Gets the number of sides of the die. @return the number of sides of the die */ public int getsides() { return sides; javadoc Class Comment Example /** Die: simulate rolling a die * @author: CPSC 111, Section 206, Spring 05-06 * @version: Jan 31, 2006 * * This is the final Die code. We started on Jan 24, * tested and improved in on Jan 26, and did a final * cleanup pass on Jan 31. */ 8