COMP 110: Introduction to Programming

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 11, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

COMP 110: Introduction to Programming Tyler Johnson Feb 18, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 13, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 26, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 12, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 28, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 27, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming.
© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.
AŽD Praha s.r.o. CRV&AVV ATO system used on vehicles of Czech Railways Dr.Ing. Ivo Myslivec AŽD Praha, Plant Technics, R&D WG1.
CSci 1130 Intro to Programming in Java
30 min Scratch July min intro to Scratch A Quick-and-Dirty approach Leaving lots of exploration for the future. (5 hour lesson plan available)
COMP 14 Introduction to Programming
Introduction to Programming G51PRG University of Nottingham Revision 1
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
COMP 110 Primitive Types, Strings, and Console I/O Tabitha Peck M.S. January 23, 2008 MWF 3-3:50 pm Philips
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.
COMP 110: Spring Announcements Lab 0 was due today (noon) Lab 1 is on Friday Bring laptops New students see me after class.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 2 Variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 2 Variables.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Building Java Programs
Data types and variables
Building Java Programs Chapter 2
Chapter 2 Edited by JJ Shepherd
Introduction to Java, and DrJava part 1
Building Java Programs
Chapter 2 Variables.
Building Java Programs Chapter 2
Introduction to Java, and DrJava
Expressions and Assignment
Building Java Programs
Primitive Types and Expressions
Unit 3: Variables in Java
Building Java Programs Chapter 2
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Variables and Constants
Presentation transcript:

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

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

Questions?

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

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()

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

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

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

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

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

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

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

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

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

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

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

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

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

How to use Variables Declare a variable Assign a value to the variable Change the value of the variable

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;

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;

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

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

Four Kinds of Primitive Types Integer types byte, short, int, long Represent whole numbers such as 0, 5, 1883443 Floating-point types float, double Represent numbers with some fractional component such as 1.01, 3932.123532, 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)

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 ±3.40282347 x 10+38 to ±1.40239846 x 10-45 double ±1.79769313486231570 x 10308 to ±4.94065645841246544 x 10-324 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 = 5.9742 × 10^24 kilograms, mass of the sun = 1.98892 × 10^30 kilograms. Character ‘!’. Whether this class is fun?

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

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?

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

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

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

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

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

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

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

Assignment Statement A variable can occur on both sides of an assignment statement Example count = count + 10;

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

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

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.. ;]

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

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

Division Operator Examples 3 / 2.0 = 3 / 2 = 6 / 3 = 6.0 / 3 = 11. / 2 = 1.5 1 2 2.0 5.5

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

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

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

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

Programming Demo Vending Machine Change Steps Pseudocode Programming Testing/Debugging

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)

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

Vending Machine Change Programming

Vending Machine Change Testing/Debugging

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

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