This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CSE 201 – Elementary Computer Programming 1 CSE 201 Primitive types and expressions Basic I/O CSE 201 – Elementary Computer Programming 2 In This Section… ! Primitive Types ! Simple numbers ! Elementary math ! Letters ! True / False Statements ! Basic I/O CSE 201 – Elementary Computer Programming 3 Second Java Program import java.util.Scanner; public class EggBasket { public static void main(String args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner(System.in); System.out.print( "Enter the number of eggs in each basket: "); eggsPerBasket = keyboard.nextInt(); System.out.print("Enter the number of baskets: "); numberOfBaskets = keyboard.nextInt(); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); } } CSE 201 – Elementary Computer Programming 4 What Does EggBasket Do? ! Take a look at the program and see if you can figure out what it does. CSE 201 – Elementary Computer Programming 5 What Is a Program Type? ! A variable’s type determines the kind of values that a variable can hold and what operations can be applied to it. ! Some Java primitive types: ! int (integer, whole values, e.g., 0, 1, 13, 231) ! double (real values, e.g., 0.0, 3.1415, 2.72) ! char (single character values, e.g., ‘a’, ‘3’, ‘$’) ! boolean (only one of two values: true , false ) See Also Primitive Data Types Pages 44  51 CSE 201 – Elementary Computer Programming 6 How Do We Assign/Change the Value of a Variable? eggsPerBasket = keyborad.nextInt(); totalEggs = numberOfBaskets * eggsPerBasket; ! Assignment statement: variable = expression; ! Assigns the value of the expression on the right side of = to the variable on the left side ! It does not mean “equal” like in math! See Also Variable Assignment Pages 52  54 CSE 201 – Elementary Computer Programming 7 What Is an Expression? numberOfBaskets * eggsPerBasket ! Program expressions are very much like arithmetic expressions you are familiar with (usual operators, parenthesis, precedence rules, etc.) CSE 201 – Elementary Computer Programming 8 What Is an Expression? numberOfBaskets * eggsPerBasket ! Program expressions are very much like arithmetic expressions you are familiar with (usual operators, parenthesis, precedence rules, etc.) ! Expressions can be evaluated to produce a value and they have a type (the type of the value of the expression) CSE 201 – Elementary Computer Programming 9 Numeric Operators ! Some common integer operators: ! + (obvious) ! (obvious) ! * (obvious) CSE 201 – Elementary Computer Programming 10 Numeric Operators ! Some common integer operators: ! + (obvious) ! (obvious) ! * (obvious) ! / (integer division, e.g., 6/2=3, 5/2=2, 19/5=?) CSE 201 – Elementary Computer Programming 11 Numeric Operators ! Some common integer operators: ! + (obvious) ! (obvious) ! * (obvious)...
View
Full
Document
This note was uploaded on 10/07/2009 for the course CSE 201 taught by Professor All during the Spring '09 term at Ohio State.
 Spring '09
 ALL
 Computer Science

Click to edit the document details