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!

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

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

Last Time. 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

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

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

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. Programming Assignment 3. Final Exam. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

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

CS 177 Recitation. Week 1 Intro to Java

Lab # 2. For today s lab:

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

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

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

Do this by creating on the m: drive (Accessed via start menu link Computer [The m: drive has your login id as name]) the subdirectory CI101.

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

CS1150 Principles of Computer Science Boolean, Selection Statements

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

Midterms Save the Dates!

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

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

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

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

IT150/IT152 Concepts Summary Sheet

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

Building Java Programs

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

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

CS 177 Week 5 Recitation Slides. Loops

COMP163. Introduction to Computer Programming. Introduction and Overview of the Hardware

Computer Science is...

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

Initial Coding Guidelines

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

Recap: Assignment as an Operator CS 112 Introduction to Programming

CONDITIONAL EXECUTION

Example Program. public class ComputeArea {

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

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

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

Java Style Guide. 1.0 General. 2.0 Visual Layout. Dr Caffeine

discrete logic do not

Warmup : Name that tune!

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

CS 251 Intermediate Programming Methods and More

Building Java Programs

CS 112 Introduction to Programming

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

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

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

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

1 Getting started with Processing

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Getting started with Java

CIS 110: Introduction to Computer Programming

CS 251 Intermediate Programming Coding Standards

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

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

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

4. Java Project Design, Input Methods

1.00 Lecture 6. Java Methods

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

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

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

CS112 Lecture: Defining Instantiable Classes

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

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

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

Part 2: The Material PART 2

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

ICOM 4015: Advanced Programming

Building Java Programs

CS 351 Design of Large Programs Coding Standards

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS 251 Intermediate Programming Methods and Classes

Documentation Requirements Computer Science 2334 Spring 2016

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Lecture 1: Overview

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

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

Encapsulation in Java

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Lecture Set 2: Starting Java

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 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Transcription:

University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Abstraction and Encapsulation javadoc More About if Statements Readings This 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 16 Some slides borrowed from Kurt Eiselt and Beth Simon. Labs and Tutorials This week is Lab #5. Midterms Save the Dates! Midterm #1 is 5:30-6:30pm on February 10 (Tuesday) in Woodward IRC 2 Done! 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. 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 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. Use javadoc to generate nice-looking documentation webpages. Write complicated, nested if statements. Review: 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 litres/100km) averaged since the last reset. If you want, try doing updated specifications in my slides. 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 that can compute with arbitrarily big numbers? How do you compute a logarithm? 2

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. 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 on the final, does that upset you? If I move the final 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. 3

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 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 Questions? /** 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. */ 4

Java s if statement Examples: if (tax < 0) tax = 0; Java s if statement Intuitively, a lot like English: If you re hungry, have a sandwich. if (tax < 0) tax = 0; if (age < 19) System.out.println( Sorry, you may not buy alcohol. ); 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. ); Java s if statement with statement; Example: if (age < 19) System.out.println( You may not buy alcohol. ); System.out.println( Buy beer! ); System.out.println( This lecture sponsored by Molson ); Java s if statement with statement; Sort of like English: If it s sunny, we ll go hiking, or 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 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.) 5

Nested If Syntax Statements within if- statements can themselves be if- statements. Use curly braces to be clear. public class NestTest public static void main (String[] args) int x = 1; int y = 3; int z = 2; if (x == y) if (y == z) System.out.println("all three values the same"); System.out.println("y is not equal to z"); System.out.println("x is not equal to y"); Body Mass Index (BMI) Calculator The Body Mass Index (BMI) is a standard medical test to give a quick and approximate indicator of healthy body weight. Your BMI is defined as your weight in kilograms divided by the square of your height in meters. BMI < 19: Underweight, higher risk of disease 19 <= BMI <= 25: Normal 25 < BMI <= 30: Overweight, higher risk 30 < BMI: Obese, much higher risk of disease Nested if Syntax A common solution (others are possible): if ( bmi < 19 ) // underweight statements if ( bmi <= 25 ) // normal statements if ( bmi <= 30 ) // overweight statements // obese statements if Syntax Easier to read indented like this: if ( bmi < 19 ) // underweight statements if ( bmi <= 25 ) // normal statements if ( bmi <= 30 ) // overweight statements // obese statements Dangling What does this code do? // print warnings only if BMI not normal if (bmi >= 19) if (bmi > 25) System.out.println( Over ); System.out.println( Under ); Beware the Dangling What does this code do? // print warnings only if BMI not normal if (bmi >= 19) if (bmi > 25) System.out.println( Over ); System.out.println( Under ); goes with nearest if (but doesn t look inside curly braces) common mistake whitespace ignored by compiler 6

Use Curly Braces to Control Nesting Questions? What does this code do? // print warnings only if BMI not normal if (bmi >= 19) if (bmi > 25) System.out.println( Over ); System.out.println( Under ); goes with nearest if but doesn t look inside curly braces 7