Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

Chapter 2 - Introduction to Java Applications
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 3 Variables Primitive Data Types calculations & arithmetic operators Readings: Chapter 2.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2008.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Introduction to Computers and Programming Lecture 5 New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. Modified for use with this class. All rights reserved. 1 Introduction to Computers and Programming in JAVA: V Primitive.
Control structures: if-else statements and switch statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Introduction to Computers and Programming Lecture 4: Mathematical and Relational Operators.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Chapter 2 - Introduction to Java Applications
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
1 Java Programming 1 Introduction to Basic GUI Lesson 4.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
Introduction to Computers and Programming Lecture 7:
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Object Oriented Programming Object and Classes Lecture 3 MBY.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing a Line of Text 2.2.1Compiling and Executing.
Chapter 4: Control Structures I
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
Chapter 4: Control Structures I
2.5 Another Java Application: Adding Integers
Chapter 2 - Introduction to Java Applications
Chapter 4: Control Structures I
Chapter 2 - Introduction to Java Applications
MSIS 655 Advanced Business Applications Programming
Chapter 2 - Introduction to Java Applications
Chapter 4 - Control Structures: Part 1
Chapter 2 - Introduction to Java Applications
F II 2. Simple Java Programs Objectives
Presentation transcript:

Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in JAVA: V

 2000 Prentice Hall, Inc. All rights reserved. 2 Displaying Text in a Dialog Box Display –Most Java applications use windows or a dialog box We have used command window –Class JOptionPane allows us to use dialog boxes Packages –Set of predefined classes for us to use –Groups of related classes called packages Group of all packages known as Java class library or Java applications programming interface (Java API) –JOptionPane is in the javax.swing package Package has classes for using Graphical User Interfaces (GUIs)

 2000 Prentice Hall, Inc. All rights reserved. 3 Review Packages in Java –Two groups of packages in Java API –Core packages Begin with java Included with Java 2 Software Development Kit –Extension packages Begin with javax –import declarations Used by compiler to identify and locate classes used in Java programs Tells compiler to load class JOptionPane from javax.swing package 4 // Java packages 5 import javax.swing.JOptionPane; // program uses OptionPane

 2000 Prentice Hall, Inc. All rights reserved. 4// InputDataDemo.java: Entering input from input dialog boxes. Finding the square of a number import javax.swing.JOptionPane; public class Class5_Input_DataDemo { public static void main(String args[]) { int result; // declare the result // Prompt the user to enter a number: String numString = JOptionPane.showInputDialog(null, "Enter a number from 1 to 10:", "Input Window Demo", JOptionPane.QUESTION_MESSAGE); // Convert the string into an int value int num = Integer.parseInt(numString); result = num * num ; // Display the result in a message dialog box JOptionPane.showMessageDialog(null, num + " squared is " + result, "Input Window Demo", JOptionPane.INFORMATION_MESSAGE); System.exit(0); }

 2000 Prentice Hall, Inc. All rights reserved. 5 Three Basic Control Structures All programs can be written with just these types of structures –Sequence structure Statements run one after the other –Selection structure Depending on a condition, do one thing (single selection using if); otherwise, do something else (Double selection using if else) if, if-else, and switch (multiple selections). –Repetition structure Repeat some actions over and over for loops, while loops, and do/while loops.

 2003 Prentice Hall, Inc. All rights reserved (Modified). 6 Flow Chart Basics 1 Rectangles represent statements of work. For example: print() Diamonds (decision symbol) contain conditions flow line

 2003 Prentice Hall, Inc. All rights reserved (Modified). 7 Flow Chart Basics Sequence structure Connector symbol Triangle boxes represent statements such as x=30; int a; System.out.print(“x”); x=20;

 2003 Prentice Hall, Inc. All rights reserved (Modified). 8 Decision Making: Equality and Relational Operators if control statement –If a condition is true, then the body of the if statement gets executed –Control always resumes after the if structure if ( condition ) statement executed if condition true No semicolon needed after condition –Else: otherwise the conditional task is not performed

 2003 Prentice Hall, Inc. All rights reserved (Modified). 9 The if structure If some condition is true – do this Example: if ( x == y ) { System.out.println(“ x is equal to y!\n” ) ; } Every programming language has some form of an if statement. Note the operator: = vs ==

 2003 Prentice Hall, Inc. All rights reserved (Modified). 10 Flow Chart Basics Selection structure Single selection (if) print “passed” grade >=60 true Connector symbol false

Equality and Relational Operators

 2003 Prentice Hall, Inc. All rights reserved (Modified). 12 The if structure If some condition is true – do this –else, do something else Example: if ( x == y ) { System.out.println(“ x is equal to y!\n“ ) ; } else { System.out.println(“ x is NOT equal to y!\n“); }

 2000 Prentice Hall, Inc. All rights reserved. 13 if/else Flow Chart grade >=60 Print “You passed” Print “You failed” True False

 2000 Prentice Hall, Inc. All rights reserved. 14 Even Odd program Lets write a program in class that will: – input a number between 1-10 – test whether a number is even or odd

 2000 Prentice Hall, Inc. All rights reserved. 15 Even or Odd numbers // even & odd numbers: using "mod" import javax.swing.JOptionPane; public class even_odd { public static void main(String args[]) { // Prompt the user to enter a number: String numString = JOptionPane.showInputDialog(null, "Enter a number from 1 to 10:", "Input Window Demo", JOptionPane.QUESTION_MESSAGE); // Convert the string into an int value int num = Integer.parseInt(numString); // Display the result in a message dialog box if ( ( num % 2 ) == 0 ) System.out.println(" The number " + num + " is even."); else System.out.println(" The number " + num + " is odd."); System.exit(0); }

 2003 Prentice Hall, Inc. All rights reserved. 16 Comparison.java 1. import 2. Class Comparison 2.1 main 2.2 Declarations 2.3 Input data ( showInputDialo g ) 2.4 parseInt 2.5 Initialize result 1 // Comparison.java 2 // Compare integers using if statements, relational operators 3 // and equality operators. 4 5 // Java packages 6 import javax.swing.JOptionPane; 7 8 public class Comparison { 9 10 // main method begins execution of Java application 11 public static void main( String args[] ) 12 { 13 String firstNumber; // first string entered by user 14 String secondNumber; // second string entered by user 15 String result; // a string containing the output int number1; // first number to compare 18 int number2; // second number to compare // read first number from user as a string 21 firstNumber = JOptionPane.showInputDialog( "Enter first integer:" ); // read second number from user as a string 24 secondNumber = 25 JOptionPane.showInputDialog( "Enter second integer:" ); // convert numbers from type String to type int 28 number1 = Integer.parseInt( firstNumber ); 29 number2 = Integer.parseInt( secondNumber ); // initialize result to empty String 32 result = ""; 33

 2003 Prentice Hall, Inc. All rights reserved. 17 Comparison.java 3. if statements 4. showMessageDialo g 34 if ( number1 == number2 ) 35 result = result + number1 + " == " + number2; if ( number1 != number2 ) 38 result = result + number1 + " != " + number2; if ( number1 < number2 ) 41 result = result + "\n" + number1 + " < " + number2; if ( number1 > number2 ) 44 result = result + "\n" + number1 + " > " + number2; if ( number1 <= number2 ) 47 result = result + "\n" + number1 + " <= " + number2; if ( number1 >= number2 ) 50 result = result + "\n" + number1 + " >= " + number2; // Display results 53 JOptionPane.showMessageDialog( null, result, "Comparison Results", 54 JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate application } // end method main } // end class Comparison Test for equality, create new string, assign to result. Notice use of JOptionPane.INFORMATION_MESSAGE

 2003 Prentice Hall, Inc. All rights reserved. 18 Program Output

 2003 Prentice Hall, Inc. All rights reserved (Modified) Decision Making: Equality and Relational Operators –Lines 1-12: Comments, import JOptionPane, begin class Comparison and main –Lines 13-18: declare variables Can use comma-separated lists instead: –Lines 21-30: obtain user-input numbers and parses input string into integer variables 13 String firstNumber, 14 secondNumber, 15 result;

 2003 Prentice Hall, Inc. All rights reserved (Modified) Decision Making: Equality and Relational Operators –Initialize result with empty string –if statement to test for equality using ( == ) If variables equal (condition true) –result concatenated using + operator –result = result + other strings –Right side evaluated first, new string assigned to result If variables not equal, statement skipped 32 result = ""; 34 if ( number1 == number2 ) 35 result = result + number1 + " == " + number2;

 2003 Prentice Hall, Inc. All rights reserved (Modified) Decision Making: Equality and Relational Operators –Lines 37-50: other if statements testing for less than, more than, etc. If number1 = 123 and number2 = 123 –Line 34 evaluates true (if number1 = = number 2) Because number1 equals number2 –Line 40 evaluates false (if number1 < number 2) Because number1 is not less than number2 –Line 49 evaluates true (if number1 >= number2) Because number1 is greater than or equal to number2 –Lines 53-54: result displayed in a dialog box using showMessageDialog

 2003 Prentice Hall, Inc. All rights reserved (Modified) Decision Making: Equality and Relational Operators Precedence of operators –All operators except for = (assignment) associates from left to right For example: x = y = z is evaluated x = (y = z)

 2000 Prentice Hall, Inc. All rights reserved. 23 Lets write a program in class: –Write a program that will input a number from 1-10 (using input dialog box) –Determine if the number entered is equal to 5 –OR less than 5 –Or larger than 5

 2000 Prentice Hall, Inc. All rights reserved. 24 // less than five!! import javax.swing.JOptionPane; public class less_than_5{ public static void main(String args[]) { // Prompt the user to enter a number: String numString = JOptionPane.showInputDialog(null, "Enter a number from 1 to 10:", "Input Window Demo", JOptionPane.QUESTION_MESSAGE); // Convert the string into an int value int num = Integer.parseInt(numString); // Display the result in a message dialog box if ( num < 5 ) System.out.println(" The number " + num + " is less than five."); else if (num == 5) System.out.println(" The number " + num + " is equal to five."); else System.out.println(" The number " + num + " is greater than five."); System.exit(0); }