Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110: Introduction to Programming

Similar presentations


Presentation on theme: "COMP 110: Introduction to Programming"— Presentation transcript:

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

2 Announcements Lab 0 due tomorrow by midnight Submit via blackboard
Michele Weigle - COMP 14 - Spr 04 Announcements Lab 0 due tomorrow by midnight Submit via blackboard 2

3 Questions?

4 Today in COMP 110 Objects and Classes Algorithms and Pseudocode
Michele Weigle - COMP 14 - Spr 04 Today in COMP 110 Objects and Classes Algorithms and Pseudocode Variables & Operations Programming Demo Vending Machine Change 4

5 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 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 Object Attributes Objects have attributes Example
A car object might have the following attributes Make Model Year Owner Location The set of an object’s attributes is called its state

8 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 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 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 Classes and Objects Class Car Camry object Focus object Make Model
Year Owner Location Camry object Make = Toyota Model = Camry Year = 2000 Owner = John Doe Location = Garage Focus object Make = Ford Model = Focus Year = 2001 Owner = Samantha Smart Location = School

12 Algorithms What is an algorithm?
Michele Weigle - COMP 14 - Spr 04 Algorithms What is an algorithm? A set of instructions for solving a problem 12

13 Algorithm Example How to make a peanut butter and jelly sandwich?
Michele Weigle - COMP 14 - Spr 04 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 13

14 Michele Weigle - COMP 14 - Spr 04
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 14

15 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

16 Michele Weigle - COMP 14 - Spr 04
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 16

17 Variables Used to store data in a program Example
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 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 How to use Variables Declare a variable Assign a value to the variable
Change the value of the variable

20 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 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 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 Type What kind of value the variable can hold Two kinds of types
Michele Weigle - COMP 14 - Spr 04 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 We have seen these class types in the FirstProgram code 23

24 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 Java Primitive Types Type Name Kind of Value Memory Used
Range of Values byte Integer 1 byte -128 to 127 short 2 bytes -32,768 to 32,768 int 4 bytes -2,147,483,648 to 2,147,483,647 long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 float Floating-point ± x to ± x 10-45 double ± x to ± x char Character 0 to 65,535 boolean 1 bit True or False (0 to 1) Size of this class? Population of the world? mass of Earth = × 10^24 kilograms, mass of the sun = × 10^30 kilograms. Character ‘!’. Whether this class is fun?

26 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 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 Variable Declarations
For now, declare all your variables inside main public class ExampleProgram { public static void main(String[] args) { int count; double average; }

29 How to Name Variables Use a combination of
Michele Weigle - COMP 14 - Spr 04 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 29

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

31 Variable Names Exercise
pinkFloyd michael.bolton the_cure b3atles kenny-G 311 Legal? Yes No

32 Keywords Reserved words with predefined meanings
Michele Weigle - COMP 14 - Spr 04 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 32

33 Assignment Statements
Change a variable’s value Syntax: variable = expression; Example: sleepNeeded = 8; sleepDesired = sleepNeeded * 2; ‘=’ sign is called the assignment operator

34 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 Assignment Statement A variable can occur on both sides of an assignment statement Example count = count + 10;

36 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 Variable Initialization
You can also initialize variables directly in the declaration For example int myVar; myVar = 0; Is equivalent to int myVar = 0;

38 Variable Initialization
Multiple variables can be declared and initialized simultaneously int grade0 = 95, grade1 = 89, grade2 = 71; For example, this is a student who stopped paying attention during lectures in COMP110, let that be a lesson to you.. ;]

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

40 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 Division Operator Examples
3 / 2.0 = 3 / 2 = 6 / 3 = 6.0 / 3 = 11. / 2 = 1.5 1 2 2.0 5.5

42 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 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 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 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 Programming Demo Vending Machine Change Steps Pseudocode Programming
Testing/Debugging

47 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 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 Vending Machine Change
Programming

50 Vending Machine Change
Testing/Debugging

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

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


Download ppt "COMP 110: Introduction to Programming"

Similar presentations


Ads by Google