Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "COMP 110: Introduction to Programming Tyler Johnson January 21, 2009 MWF 11:00AM-12:15PM Sitterson 014."— Presentation transcript:

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

2 COMP 110: Spring Announcements Lab 0 due tomorrow by midnight Submit via blackboard

3 COMP 110: Spring Questions?

4 COMP 110: Spring Today in COMP 110 Objects and Classes Algorithms and Pseudocode Variables & Operations Programming Demo Vending Machine Change

5 COMP 110: Spring Object-Oriented Programming What are objects? Entities that can represent real-world things or abstractions E.g. Cars, houses, books, System.out (abstract) Objects perform or have actions performed on them car.start() house.clean() book.read()

6 COMP 110: Spring 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 Example A 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

7 COMP 110: Spring Object Attributes Objects have attributes Example A car object might have the following attributes Make Model Year Owner Location The set of an objects attributes is called its state

8 COMP 110: Spring Object Actions Objects perform actions, or have actions performed on them Example A car object might allow the following actions Accelerate Brake Sell Start

9 COMP 110: Spring Object Actions Performing actions on a object can change its state Example If we sell a car, we change its owner If we accelerate the car, we will change its location

10 COMP 110: Spring Classes A Class describes a general template for creating a certain kind of object Example 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 attributes Both cars are thus members of the Class Car

11 COMP 110: Spring Classes and Objects Class Car Make Model Year Owner Location Focus object Make = Ford Model = Focus Year = 2001 Owner = Samantha Smart Location = School Camry object Make = Toyota Model = Camry Year = 2000 Owner = John Doe Location = Garage

12 COMP 110: Spring Algorithms What is an algorithm? A set of instructions for solving a problem

13 COMP 110: Spring Algorithm Example How to make a peanut butter and jelly sandwich? Get two slices of bread Get some peanut butter Get some jelly Get a knife Spread peanut butter on the first slice of bread Spread jelly on the second slice of bread Put the two slices of bread together

14 COMP 110: Spring 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

15 COMP 110: Spring 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. Lets look at how we might describe this in pseudocode

16 COMP 110: Spring Algorithm Example 2 Computing the total cost of groceries in pseudocode total = 0 for each item in the cart total = total + (cost of that item) At the end of the algorithm, total is the total cost of all items in the cart

17 COMP 110: Spring Variables Used to store data in a program Think of it is a container for a value Example myVariable = 6; Set the value of myVariable to 6 Can change value throughout program myVariable = 8; The value of myVariable is now 8

18 COMP 110: Spring Variables Name of a variable is called its identifier Choose variable names that are meaningful! Example a = a + b; No one will know what these variables mean accntBalance = accntBalance + deposit; Is much better

19 COMP 110: Spring How to use Variables Declare a variable Assign a value to the variable Change the value of the variable

20 COMP 110: Spring Variable Declarations Tells the computer what type of data the variable will hold Allocates space in memory for the data Examples: int count, score, myInt; char letter; double totalCost, ratio;

21 COMP 110: Spring Syntax Set of grammatical rules in a language Simple 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;

22 COMP 110: Spring 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

23 COMP 110: Spring Type What kind of value the variable can hold Two 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

24 COMP 110: Spring Four Kinds of Primitive Types Integer types byte, short, int, long Represent whole numbers such as 0, 5, Floating-point types float, double Represent numbers with some fractional component such as 1.01, , 0.0 Character char Represents a single character such as A, ;, 8 Boolean boolean Represents a single bit (on or off, true or false, 1 or 0)

25 COMP 110: Spring Java Primitive Types Type Name Kind of Value Memory Used Range of Values byteInteger1 byte-128 to 127 shortInteger2 bytes-32,768 to 32,768 intInteger4 bytes-2,147,483,648 to 2,147,483,647 longInteger8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 floatFloating-point4 bytes± x to ± x doubleFloating-point8 bytes± x to ± x charCharacter2 bytes0 to 65,535 boolean 1 bitTrue or False (0 to 1)

26 COMP 110: Spring 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

27 COMP 110: Spring 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 memory Different types of data may allow different operations We can add two numbers, but does it make sense to add two letters?

28 COMP 110: Spring Variable Declarations For now, declare all your variables inside main public class ExampleProgram { public static void main(String[] args) { int count; double average; … }

29 COMP 110: Spring How to Name Variables Use a combination of Letters, digits (0-9), underscore (_) First character cannot be a digit Java is case sensitive myVariable & myvariable are considered different identifiers

30 COMP 110: Spring Example Variable Names Legal names inputStream, my_Grade, my_Grade2 Illegal names bright*, power-hour, 7eleven

31 COMP 110: Spring Variable Names Exercise Name pinkFloyd michael.bolton the_cure b3atles kenny-G 311 Legal? Yes No Yes No

32 COMP 110: Spring 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

33 COMP 110: Spring Assignment Statements Change a variables value Syntax: variable = expression; Example: sleepNeeded = 8; sleepDesired = sleepNeeded * 2; = sign is called the assignment operator

34 COMP 110: Spring Behind the Assignment Statement variable = expression; CPU calculates the value of the expression Writes the value to the memory location of the variable sleepDesired = sleepNeeded * 2; Get the current value of sleepNeeded from its memory location Calculate sleepNeeded * 2 Assign the result to the memory location of sleepDesired

35 COMP 110: Spring Assignment Statement A variable can occur on both sides of an assignment statement Example count = count + 10;

36 COMP 110: Spring Variable Initialization You need to give variables an initial value before you use them Otherwise you may observe unexpected behavior int count; count = 1;

37 COMP 110: Spring Variable Initialization You can also initialize variables directly in the declaration For example int myVar; myVar = 0; Is equivalent to int myVar = 0;

38 COMP 110: Spring Variable Initialization Multiple variables can be declared and initialized simultaneously int grade0 = 95, grade1 = 89, grade2 = 71;

39 COMP 110: Spring Arithmetic Operators The usual suspects Addition (+) a + b Subtraction (-) a - b Multiplication (*) a * b Division (/) a / b

40 COMP 110: Spring 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

41 COMP 110: Spring Division Operator Examples 3 / 2.0 = 3 / 2 = 6 / 3 = 6.0 / 3 = 11. / 2 =

42 COMP 110: Spring Remainder Operator The remainder or modulo operator (%) gives the remainder when one whole number is divided by another Example i = 10%4; //the value of i will be 2 j = 6%3; //the value of j will be 0 k = 17%6; //the value of k will be 5

43 COMP 110: Spring 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

44 COMP 110: Spring Review You should now Have 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

45 COMP 110: Spring Vending Machine Change Problem Given 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

46 COMP 110: Spring Programming Demo Vending Machine Change Steps Pseudocode Programming Testing/Debugging

47 COMP 110: Spring 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)

48 COMP 110: Spring Vending Machine Change Pseudocode Ask user for the amount of change Determine the amount in quarters Subtract the value in quarters from amount Determine the amount in dimes Subtract the value in dimes from amount Determine the amount in nickels Subtract the amount in nickels from amount Set the number of pennies to the remaining amount Output the result

49 COMP 110: Spring Vending Machine Change Programming

50 COMP 110: Spring Vending Machine Change Testing/Debugging

51 COMP 110: Spring Program 1 Tip Calculator Posted on course website Due Feb 4 th START EARLY!

52 COMP 110: Spring Friday Recitation Lab 1 Bring Laptops (fully charged) Textbook Any questions you may have about Program 1


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

Similar presentations


Ads by Google