1.0.primitivetypes

1.0.primitivetypes - CSE 201 Elementary Computer...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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 variables 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

Page1 / 15

1.0.primitivetypes - CSE 201 Elementary Computer...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online