COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson...

52
COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014

Transcript of COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson...

Page 1: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110:Introduction to Programming

Tyler JohnsonJanuary 21, 2009

MWF 11:00AM-12:15PMSitterson 014

Page 2: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20092

Announcements

Lab 0 due tomorrow by midnightSubmit via blackboard

Page 3: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20093

Questions?

Page 4: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20094

Today in COMP 110

Objects and Classes

Algorithms and Pseudocode

Variables & Operations

Programming DemoVending Machine Change

Page 5: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20095

Object-Oriented Programming

What are objects?Entities that can represent real-world things or abstractionsE.g. Cars, houses, books, System.out (abstract)

Objects perform or have actions performed on them• car.start()• house.clean()• book.read()

Page 6: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20096

Object-Oriented Programming

What is object-oriented programming?

Programming methodology where programs are composed of a collection of objects that can act alone or interact with one another

ExampleA race car simulation program might have car, driver, racetrack, and onlooker objects• A driver might ‘drive’ a car object• An onlooker object might ‘boo’ a driver object

Page 7: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20097

Object Attributes

Objects have attributesExample

A car object might have the following attributes• Make• Model• Year• Owner• Location

The set of an object’s attributes is called its state

Page 8: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20098

Object Actions

Objects perform actions, or have actions performed on themExample

A car object might allow the following actions• Accelerate• Brake• Sell• Start

Page 9: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20099

Object Actions

Performing actions on a object can change its state

ExampleIf we sell a car, we change its ownerIf we accelerate the car, we will change its location

Page 10: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200910

Classes

A Class describes a general template for creating a certain kind of objectExample

We have two car objects• One is a 2000 Toyota Camry owned by John Doe• A second is a 2001 Ford Focus owned by Samantha

Smart

While the two car objects are different, they are both types of cars and have the same type of attributesBoth cars are thus members of the Class Car

Page 11: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200911

Classes and Objects

Class CarMakeModelYearOwnerLocation

Focus objectMake = FordModel = FocusYear = 2001Owner = Samantha SmartLocation = School

Camry objectMake = ToyotaModel = CamryYear = 2000Owner = John DoeLocation = Garage

Page 12: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200912

Algorithms

What is an algorithm?A set of instructions for solving a problem

Page 13: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200913

Algorithm Example

How to make a peanut butter and jelly sandwich?

Get two slices of breadGet some peanut butterGet some jellyGet a knifeSpread peanut butter on the first slice of breadSpread jelly on the second slice of breadPut the two slices of bread together

Page 14: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200914

Pseudocode

Combination of code and English used to express an algorithm before writing the algorithm into code

Useful in planning out programs before you actually write them

Page 15: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200915

Algorithm Example 2

How to compute the total cost of all the groceries in your shopping cart?

Simple, just start with the number 0 and add the cost of each item one by one.

Let’s look at how we might describe this in pseudocode

Page 16: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200916

Algorithm Example 2

Computing the total cost of groceries in pseudocode

total = 0for each item in the carttotal = total + (cost of that item)

At the end of the algorithm, “total” is the total cost of all items in the cart

Page 17: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200917

Variables

Used to store data in a programThink of it is a container for a value

ExamplemyVariable = 6;

Set the value of “myVariable” to 6

Can change value throughout programmyVariable = 8;

The value of “myVariable” is now 8

Page 18: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200918

Variables

Name of a variable is called its identifierChoose variable names that are meaningful!Example

a = a + b;No one will know what these variables mean

accntBalance = accntBalance + deposit;Is much better

Page 19: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200919

How to use Variables

Declare a variableAssign a value to the variableChange the value of the variable

Page 20: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200920

Variable Declarations

Tells the computer what type of data the variable will holdAllocates space in memory for the data

Examples:int count, score, myInt;char letter;double totalCost, ratio;

Page 21: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200921

Syntax

Set of grammatical rules in a languageSimple syntax for English sentences

Subject Verb Object.E.g “I won the game.”

Syntax for variable declarations in Java:Type Variable_1, Variable_2, …;

Examples:int count, score, myInt;char letter;double totalCost, ratio;

Page 22: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200922

Variables and Memory

A variable corresponds to a location in memory

variable n1

• Use this cell to store the value of n1

• Prevent this cell from being used by other variables later

main memory

Page 23: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200923

Type

What kind of value the variable can holdTwo kinds of types

Primitive type - indecomposable values (single number or letter)• int, double, char, boolean

Class type - objects with both data and methods• Scanner, System

Page 24: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200924

Four Kinds of Primitive Types

Integer typesbyte, short, int, longRepresent whole numbers such as 0, 5, 1883443

Floating-point types float, doubleRepresent numbers with some fractional component such as 1.01, 3932.123532, 0.0

CharactercharRepresents a single character such as ‘A’, ‘;’, ‘8’

BooleanbooleanRepresents a single bit (on or off, true or false, 1 or 0)

Page 25: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200925

Java Primitive Types

Type Name

Kind of Value

Memory Used

Range of Values

byte Integer 1 byte -128 to 127

short Integer 2 bytes -32,768 to 32,768

int Integer 4 bytes -2,147,483,648 to 2,147,483,647

long Integer 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float Floating-point 4 bytes ±3.40282347 x 10+38 to ±1.40239846 x 10-45

double Floating-point 8 bytes ±1.79769313486231570 x 10308 to ±4.94065645841246544 x

10-324

char Character 2 bytes 0 to 65,535

boolean boolean 1 bit True or False (0 to 1)

Page 26: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200926

Variables and Memory

When declaring a variable, a certain amount of memory is assigned based on the declared primitive type

int age;

double length;

char letter;

main memory

Page 27: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200927

Variable Declaration

Why is it important to tell the computer the type of the data?

Different types of data take up different amounts of space in memoryDifferent types of data may allow different operations• We can add two numbers, but does it make sense

to add two letters?

Page 28: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200928

Variable Declarations

For now, declare all your variables inside main

public class ExampleProgram{ public static void main(String[] args) {

int count;double average;…

}}

Page 29: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200929

How to Name Variables

Use a combination ofLetters, digits (0-9), underscore (_)

First character cannot be a digit

Java is case sensitive “myVariable” & “myvariable” are considered different identifiers

Page 30: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200930

Example Variable Names

Legal namesinputStream, my_Grade, my_Grade2

Illegal namesbright*, power-hour, 7eleven

Page 31: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200931

Variable Names Exercise

Name

pinkFloydmichael.boltonthe_cureb3atles kenny-G311

Legal?

YesNoYesYesNoNo

Page 32: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200932

Keywords

Reserved words with predefined meanings

if, else, return, new, …See the inside cover of the textbook for full list

You cannot use a keyword as a variable name

int if; • will not compile

int ifThen;• Is ok

Page 33: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200933

Assignment Statements

Change a variable’s valueSyntax:

variable = expression;

Example:sleepNeeded = 8;sleepDesired = sleepNeeded * 2;

‘=’ sign is called the assignment operator

Page 34: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200934

Behind the Assignment Statement

variable = expression;CPU calculates the value of the expressionWrites the value to the memory location of the variable

sleepDesired = sleepNeeded * 2;Get the current value of sleepNeeded from its memory locationCalculate sleepNeeded * 2Assign the result to the memory location of sleepDesired

Page 35: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200935

Assignment Statement

A variable can occur on both sides of an assignment statement

Examplecount = count + 10;

Page 36: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200936

Variable Initialization

You need to give variables an initial value before you use them

Otherwise you may observe unexpected behavior

int count;count = 1;

Page 37: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200937

Variable Initialization

You can also initialize variables directly in the declaration

For exampleint myVar;myVar = 0;

Is equivalent toint myVar = 0;

Page 38: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200938

Variable Initialization

Multiple variables can be declared and initialized simultaneously

int grade0 = 95, grade1 = 89, grade2 = 71;

Page 39: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200939

Arithmetic Operators

The usual suspectsAddition (+)• a + b

Subtraction (-)• a - b

Multiplication (*)• a * b

Division (/)• a / b

Page 40: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200940

Division Operator

The division operator (/) behaves differently depending on the data types used!Example

9 / 2 = 4 • Truncation when used with integers

9.0 / 2 = 4.5• Maintains fractions when used with real numbers

Page 41: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200941

Division Operator Examples

3 / 2.0 =

3 / 2 =

6 / 3 =

6.0 / 3 =

11. / 2 =

1.5

1

2

2.0

5.5

Page 42: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200942

Remainder Operator

The remainder or modulo operator (%) gives the remainder when one whole number is divided by another

Examplei = 10%4; //the value of i will be 2j = 6%3; //the value of j will be 0k = 17%6; //the value of k will be 5

Page 43: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200943

Remainder Operator

The remainder operator can be used to determine if a number is even or odd

Given any integer n, n%2 will produce either 0 or 1

If n%2 equals 0, n is even

If n%2 equals 1, n is odd

Page 44: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200944

Review

You should nowHave a better understanding of objects and classes

Have a good idea of what an algorithm is

Be able to write a basic Java program that performs simple arithmetic

Page 45: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200945

Vending Machine Change

ProblemGiven an amount of change from 1…99, determine the number of quarters, dimes, nickels, and pennies that equals that amount of change

Example:57 cents would be dispensed as• 2 quarters, 0 dimes, 1 nickel, and 2 pennies

Page 46: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200946

Programming Demo

Vending Machine Change

StepsPseudocode

Programming

Testing/Debugging

Page 47: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200947

Designing the Algorithm

How do we know that 57 cents is 2 quarters, 0 dimes, 1 nickel and 2 pennies?

First we used as many quarters as we could (2)• That leaves 7 cents

Second we used as many dimes as we could (0)• Still left with 7 cents

Third we used as many nickels as we could (1)• That leaves 2 cents

Lastly we used the remaining amount as pennies (2)

Page 48: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200948

Vending Machine Change

PseudocodeAsk user for the amount of changeDetermine the amount in quartersSubtract the value in quarters from amountDetermine the amount in dimesSubtract the value in dimes from amountDetermine the amount in nickelsSubtract the amount in nickels from amountSet the number of pennies to the remaining amountOutput the result

Page 49: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200949

Vending Machine Change

Programming

Page 50: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200950

Vending Machine Change

Testing/Debugging

Page 51: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200951

Program 1

Tip CalculatorPosted on course website

Due Feb 4th

START EARLY!

Page 52: COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200952

Friday

RecitationLab 1

BringLaptops (fully charged)TextbookAny questions you may have about Program 1