AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Similar documents
2.8. Decision Making: Equality and Relational Operators

download instant at

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Introduction to Java Applications

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

2.5 Another Application: Adding Integers

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Introduction to Java Applications

Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program.

Introduction to Java Applications; Input/Output and Operators

Programming with Java

Section 2.2 Your First Program in Java: Printing a Line of Text

CS141 Programming Assignment #6

Section 2.2 Your First Program in Java: Printing a Line of Text

Object Oriented Programming. Java-Lecture 1

Supplementary Test 1

Full file at

ECE 161 WEEK 4 Introduction to Programing in Java

CS111: PROGRAMMING LANGUAGE II

Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved.

Control Statements: Part 1

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

b. Suppose you enter input from the console, when you run the program. What is the output?

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Chapter 2, Part III Arithmetic Operators and Decision Making

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Midterm Examination (MTA)

Introduction to Computer Science Unit 2. Exercises

CSC 1051 Data Structures and Algorithms I

Tutorial 03. Exercise 1: CSC111 Computer Programming I

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Arrays OBJECTIVES. In this chapter you will learn:

Java Programming Language. 0 A history

Introduction to Java & Fundamental Data Types

Fundamentals of Programming Data Types & Methods

Computational Expression

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

I. Variables and Data Type week 3

Algorithms and Conditionals

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Software Practice 1 Basic Grammar

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

COMP 110 Project 1 Programming Project Warm-Up Exercise

Introduction to Classes and Objects

Welcome to the Primitives and Expressions Lab!

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

1. Find the output of following java program. class MainClass { public static void main (String arg[])

Course Outline. Introduction to java

CS141 Programming Assignment #8

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Course PJL. Arithmetic Operations

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

THE JAVA FOR STATEMENT

Example: Monte Carlo Simulation 1

Repetition CSC 121 Fall 2014 Howard Rosenthal

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

Chapter 2: Basic Elements of Java

Formatted Output / User IO

Anatomy of a Java Program: Comments

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Tutorial 06. Conditional statement: if then, if else, switch

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

5) (4 points) What is the value of the boolean variable equals after the following statement?

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

ITERATION WEEK 4: EXMAPLES IN CLASS

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Arrays. Eng. Mohammed Abdualal

Elementary Programming

4 WORKING WITH DATA TYPES AND OPERATIONS

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

CS110: PROGRAMMING LANGUAGE I

CS110 Programming Language I. Lab 6: Multiple branching Mechanisms

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

CSCI 355 Lab #2 Spring 2007

Lab Exercise 1. Objectives: Part 1. Introduction

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Programs

Building Java Programs

CSCI Problem Solving, Programming and Computers Spring, 2016 Assignment 2 {Inheritance, Interfaces, Arrays}

Chapter 3. Selections

CSIS 10A Assignment 4 SOLUTIONS

Reading Input from Text File

Introduction to Computer Programming

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

STUDENT LESSON A7 Simple I/O

Building Java Programs

Manipulating One-dimensional Arrays

Coding Standards for Java

ing execution. That way, new results can be computed each time the Class The Scanner

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Transcription:

AL GHURAIR UNIVERSITY College of Computing CSC 209 JAVA I week 2- Arithmetic and Decision Making: Equality and Relational Operators Objectives: To use arithmetic operators. The precedence of arithmetic operators. To write decision-making statements. To use relational and equality operators Examples: Example 1 Write and execute complete java program which print the result of the following algebra equations: y = a + b + c X y = a + b + c X y = a x x + b x + c Text-printing program. public class Welcome1 { // main method begins execution of Java application public static void main( String args[] ) { int a, b, c, X; double y; System.out.print( "\nplease Enter value of the variable (a) " ); // write your code to enter the value to the variable a. System.out.print( "\nplease Enter value of the variable (b) " ); // write your code to enter the value to the variable b. System.out.print( "\nplease Enter value of the variable (c) " );

// write your code to enter the value to the variable c. System.out.print( "Please Enter value of the variable (X) " ); // write your code to enter the value to the variable X. y= (a + b + c ) / X ; System.out.println( The output of the first equation is: + y); y= a + b + c / X; System.out.println( The output of the second equation is: + y); y= a * X * X + b * X + c; System.out.println( The output of the third equation is: + y); y= a + x * x + b * x / c; System.out.println( The output of the third equation is: + y); } // end method main } // end class Welcome1 Example 2 The application of Fig. 2.15 uses six if statements to compare two integers input by the user. If the condition in any of these if statements is true, the assignment statement associated with that if statement executes. The program uses a Scanner to input the two integers from the user and store them in variables number1 and number2. Then the program compares the numbers and displays the results of the comparisons that are true. Figure 2.15. Equality and relational operators. 1 // Comparison.java 2 // Compare integers using if statements, relational operators 3 // and equality operators. 4 import java.util.scanner; // program uses class Scanner 5 6 public class Comparison 7 { 8 // main method begins execution of Java application 9 public static void main( String args[] ) 10 { 11 // create Scanner to obtain input from command window 12 Scanner input = new Scanner( System.in ); 13 14 int number1; // first number to compare 15 int number2; // second number to compare 16 17 System.out.print( "Enter first integer: " ); // prompt 18 number1 = input.nextint(); // read first number from user 2 P a g e F i r a s A l S h a i k h l y h t t p : / / a g u. y o l a s i t e. c o m

19 20 System.out.print( "Enter second integer: " ); // prompt 21 number2 = input.nextint(); // read second number from user 22 23 if ( number1 == number2 ) 24 System.out.printf( "%d == %d\n", number1, number2 ); 25 26 if ( number1!= number2 ) 27 System.out.printf( "%d!= %d\n", number1, number2 ); 28 29 if ( number1 < number2 ) 30 System.out.printf( "%d < %d\n", number1, number2 ); 31 32 if ( number1 > number2 ) 33 System.out.printf( "%d > %d\n", number1, number2 ); 34 35 if ( number1 <= number2 ) 36 System.out.printf( "%d <= %d\n", number1, number2 ); 37 38 if ( number1 >= number2 ) 39 System.out.printf( "%d >= %d\n", number1, number2 ); 40 41 } // end method main 42 43 } // end class Comparison Enter first integer: 777 Enter second integer: 777 777 == 777 777 <= 777 777 >= 777 Enter first integer: 1000 Enter second integer: 2000 1000!= 2000 1000 < 2000 1000 <= 2000 Enter first integer: 2000 Enter second integer: 1000 2000!= 1000 2000 > 1000 2000 >= 1000 The declaration of class Comparison begins at line 6 public class Comparison The class's main method (lines 9 to 41) begins the execution of the program. 3 P a g e F i r a s A l S h a i k h l y h t t p : / / a g u. y o l a s i t e. c o m

Line 12 Scanner input = new Scanner( System.in ); declares Scanner variable input and assigns it a Scanner that inputs data from the standard input (i.e., the keyboard). Lines 14,15 int number1; // first number to compare int number2; // second number to compare declare the int variables used to store the values input from the user. Lines 17,18 System.out.print( "Enter first integer: " ); // prompt number1 = input.nextint(); // read first number from user prompt the user to enter the first integer and input the value, respectively. The input value is stored in variable number1. Lines 20,21 System.out.print( "Enter second integer: " ); // prompt number2 = input.nextint(); // read second number from user prompt the user to enter the second integer and input the value, respectively. The input value is stored in variable number2. Lines 23,24 if ( number1 == number2 ) System.out.printf( "%d == %d\n", number1, number2 ); declare an if statement that compares the values of the variables number1 and number2 to determine whether they are equal. An if statement always begins with keyword if, followed by a condition in parentheses. An if statement expects one statement in its body. The indentation of the body statement shown here is not required, but it improves the program's readability by emphasizing that the statement in line 24 is part of the if statement that begins on line 23. Line 24 executes only if the numbers stored in variables number1 and number2 are equal (i.e., the condition is true). The if statements at lines 26 27, 29 30, 32 33, 3536 and 38 39 compare number1 and number2 with the operators!=, <, >, <= and >=, respectively. If the condition in any of the if statements is true, the corresponding body statement executes. 4 P a g e F i r a s A l S h a i k h l y h t t p : / / a g u. y o l a s i t e. c o m

H/W Exercise: Explain the order in which the operators are applied for the following equations: y = ax 2 + bx + c y = pr%q + w x y y = a + b + c x y = a x + b + c x Ex 5 P a g e F i r a s A l S h a i k h l y h t t p : / / a g u. y o l a s i t e. c o m