Slide 3 - COP3502 Programming Fundamentals for CIS Majors 1...

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

View Full Document Right Arrow Icon
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi
Background image of page 1

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

View Full DocumentRight Arrow Icon
GUI input Programming style Programming errors Eclipse Tutorial Last Class
Background image of page 2
Chapter 3 boolean data type Control statements If - else statements switch statements Formatting output Objectives
Background image of page 3

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

View Full DocumentRight Arrow Icon
Comparisons
Background image of page 4
Another primitive data type in Java Only has two values true false Examples boolean test = false ; boolean checked = true ; Usually used for checking conditions boolean Data Type
Background image of page 5

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

View Full DocumentRight Arrow Icon
Six comparison operators in Java Comparison Operators Operator Name < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to != Not equal to
Background image of page 6
…. …. Primitive data type Predefined range A primitive variable stores the value Reference data type Class, Array [will be discussed later] Size is not predefined! stores a “reference” to the actual object Class vs. Primitive Type 10 int x = 10; x Integer y = new Integer(10); y Integer
Background image of page 7

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

View Full DocumentRight Arrow Icon
Be careful about == Primitive data type: Compares the value Reference data type: Compares the references (memory address)! So only use with primitive data types E.g. if x and y are String x==y will not always return the correct result! Instead use String.compare(x,y) In general, use equals() to compare two objects E.g. a.equals(b) Comparison Operators
Background image of page 8
Result of comparison is boolean boolean x = (3 > 2); x is true boolean y = (1 > 2); y is false Comparison Operators
Background image of page 9

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

View Full DocumentRight Arrow Icon
Create a program to let a first grader practice additions. The program randomly generates two single-digit integers number1 and number2 and displays a question such as: “What is 7 + 9?” to the student. After the student types the answer, the program displays a message to indicate whether the answer is true or false. Program AdditionQuiz Run
Background image of page 10
“if” statement
Background image of page 11

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

View Full DocumentRight Arrow Icon
Checks for a simple condition It is written as E.g. One way if if (boolean-expression) { statement(s); } if (radius >= 0) { area = radius * radius * PI; }
Background image of page 12
Be careful! One way if if i > 0 { System.out.println( "i is positive" ); } (a) Wrong (b) Correct if ( i > 0 ) { System.out.println( "i is positive" ); } if (i > 0 ) { System.out.println( "i is positive" ); } (a) Equivalent (b) if (i > 0 ) System.out.println( "i is positive" );
Background image of page 13

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

View Full DocumentRight Arrow Icon
Write a program that prompts the user to enter an integer. If the number is a multiple of 5 , print HiFive . If the number is divisible by 2 , print HiEven . If Program SimpleIfDemo Run
Background image of page 14
If you get here by 10:00 am, then we can have brunch, else we can go for lunch. Two-way if if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; }
Background image of page 15

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

View Full DocumentRight Arrow Icon
Example Two-way if if (radius >= 0) { area = radius * radius * 3.14159; System.out.println("The area is: “ + area); } else { System.out.println("Negative input"); }
Background image of page 16
else if is used for checking multiple conditions Multiple if-else if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F';
Background image of page 17

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

View Full DocumentRight Arrow Icon
Let’s trace a program!
Background image of page 18
Image of page 19
This is the end o