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

Similar documents
Course PJL. Arithmetic Operations

Chapter 8 JavaScript/JScript: Introduction to Scripting 201

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

א א א E2-1F Welcome to Java Programming!

TTh 9.25 AM AM Strain 322

More about JOptionPane Dialog Boxes

Part 1: Introduction. Course Contents. Goals. Books. Difference between conventional and objectoriented

C: How to Program. Week /Mar/05

Introduction to Java. Nihar Ranjan Roy.

Introduction to Programming

Programming for Engineers Introduction to C

Chapter 2 - Introduction to C Programming

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

4 WORKING WITH DATA TYPES AND OPERATIONS

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Introduction to C# Applications

Introduction to Java Applications; Input/Output and Operators

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

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

Chapter 1 Introduction to Computers and C++ Programming

Object-Oriented Programming in Java

Course Outline. Introduction to java

Control Structures (Deitel chapter 4,5)

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Full file at C How to Program, 6/e Multiple Choice Test Bank

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

2.5 Another Application: Adding Integers

CS 106 Introduction to Computer Science I

Chapter 1 & 2 Introduction to C Language

CS 106 Introduction to Computer Science I

2.8. Decision Making: Equality and Relational Operators

Chapter 2 Primitive Data Types and Operations

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Chapter 2 ELEMENTARY PROGRAMMING

JavaScript: Introduction to Scripting

19. GUI Basics. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Full file at

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

JavaScript: Introductionto Scripting

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

Introduction to Scientific Programming Using Java

Handout 3 cs180 - Programming Fundamentals Fall 17 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

ECE 161 WEEK 4 Introduction to Programing in Java

CS 106 Introduction to Computer Science I

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

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved.

download instant at

Introduction to Java Applications

Chapter 2: Using Data

Chapter 2: Using Data

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CMPT 125: Lecture 3 Data and Expressions

Full file at

Fundamentals of Programming Session 4

Introduction to Java Applications

Introduction to Python Programming

Chapter 2, Part I Introduction to C Programming

Chapter 02: Using Data

Chapter 3 - Introduction to Java Applets

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

IT 374 C# and Applications/ IT695 C# Data Structures

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

by Pearson Education, Inc. All Rights Reserved. 2

Chapter 3 Selection Statements

Program Elements -- Introduction

Formatted Output Pearson Education, Inc. All rights reserved.

JAVA OPERATORS GENERAL

Chapter 2: Java Fundamentals

8/23/2014. Chapter Topics. Chapter Topics (2) Parts of a Java Program. Parts of a Java Program. Analyzing The Example. Chapter 2: Java Fundamentals

download instant at Introduction to C++

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Chapter 8: GUI Dialog & Table. Informatics Practices Class XII. By- Rajesh Kumar Mishra. KV No.1, AFS, Suratgarh

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Introduction to C# Applications Pearson Education, Inc. All rights reserved.

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

COMP 110 Project 1 Programming Project Warm-Up Exercise

Chapter 2 Primitive Data Types and Operations. Objectives

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

Chapter. Let's explore some other fundamental programming concepts

Full file at

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

JAVA Programming Concepts

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Guessing Game with Objects

Basics of Java Programming

Chapter 2: Data and Expressions

Prof. Navrati Saxena TA: Rochak Sachan

Object Oriented Programming. Java-Lecture 1

JOptionPane Dialogs. javax.swing.joptionpane is a class for creating dialog boxes. Has both static methods and instance methods for dialogs.

Introduction to Classes and Objects

Chapter 2 Elementary Programming

Lecture 3 Tao Wang 1

Lecture 3: Variables and assignment

IS 0020 Program Design and Software Tools

3chapter C ONTROL S TATEMENTS. Objectives

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Transcription:

CHAPTER 2 1 2 1 // Fig. 2.1: Welcome1.java 2 // A first program in Java 3 4 public class Welcome1 { 5 public static void main( String args[] ) 6 { 7 System.out.println( "Welcome to Java Programming!" ); 8 } 9 } Welcome to Java Programming! Fig. 2.1 A first program in Java.

2 CHAPTER 2 Fig. 2.2 Executing the Welcome1 application in a Microsoft Windows MS-DOS Prompt.

CHAPTER 2 3 1 // Fig. 2.3: Welcome2.java 2 // Printing a line with multiple statements 3 4 public class Welcome2 { 5 public static void main( String args[] ) 6 { 7 System.out.print( "Welcome to " ); 8 System.out.println( "Java Programming!" ); 9 } 10 } Welcome to Java Programming! Fig. 2.3 Printing on one line with separate statements.

4 CHAPTER 2 1 // Fig. 2.4: Welcome3.java 2 // Printing multiple lines with a single statement 3 4 public class Welcome3 { 5 public static void main( String args[] ) 6 { 7 System.out.println( "Welcome\nto\nJava\nProgramming!" ); 8 } 9 } Welcome to Java Programming! Fig. 2.4 Printing on multiple lines with a single statement.

CHAPTER 2 5 Escape sequence Description \n Newline. Position the screen cursor to the beginning of the next line. \t Horizontal tab. Move the screen cursor to the next tab stop. \r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. Any characters output after the carriage return overwrite the previous characters output on that line. \\ Backslash. Used to print a backslash character. \" Double quote. Used to print a double quote character. For example, System.out.println( "\"in quotes\"" ); displays "in quotes" Fig. 2.5 Some common escape sequences.

6 CHAPTER 2 1 // Fig. 2.6: Welcome4.java 2 // Printing multiple lines in a dialog box 3 import javax.swing.joptionpane; // import class JOptionPane 4 5 public class Welcome4 { 6 public static void main( String args[] ) 7 { 8 JOptionPane.showMessageDialog( 9 null, "Welcome\nto\nJava\nProgramming!" ); 10 11 System.exit( 0 ); // terminate the program 12 } 13 } Fig. 2.6 Displaying multiple lines in a dialog box.

CHAPTER 2 7 button label menu menu bar text field Fig. 2.7 A sample Netscape Navigator window with GUI components.

8 CHAPTER 2 1 // Fig. 2.8: Addition.java 2 // An addition program 3 4 import javax.swing.joptionpane; // import class JOptionPane 5 6 public class Addition { 7 public static void main( String args[] ) 8 { 9 String firstnumber, // first string entered by user 10 secondnumber; // second string entered by user 11 int number1, // first number to add 12 number2, // second number to add 13 sum; // sum of number1 and number2 14 15 // read in first number from user as a string 16 firstnumber = 17 JOptionPane.showInputDialog( "Enter first integer" ); 18 19 // read in second number from user as a string 20 secondnumber = 21 JOptionPane.showInputDialog( "Enter second integer" ); 22 23 // convert numbers from type String to type int 24 number1 = Integer.parseInt( firstnumber ); 25 number2 = Integer.parseInt( secondnumber ); 26 27 // add the numbers 28 sum = number1 + number2; 29 30 // display the results 31 JOptionPane.showMessageDialog( 32 null, "The sum is " + sum, "Results", 33 JOptionPane.PLAIN_MESSAGE ); 34 35 System.exit( 0 ); // terminate the program 36 } 37 } Fig. 2.8 An addition program in action.

CHAPTER 2 9 Message dialog type Icon Description JOptionPane.ERROR_MESSAGE JOptionPane.INFORMATION_MESSAGE JOptionPane.WARNING_MESSAGE JOptionPane.QUESTION_MESSAGE JOptionPane.PLAIN_MESSAGE no icon Displays a dialog that indicates an error to the application user. Displays a dialog with an informational message to the application user the user can simply dismiss the dialog. Displays a dialog that warns the application user of a potential problem. Displays a dialog that poses a question to the application user. This normally requires a response such as clicking a Yes or No button. Displays a dialog that simply contains a message with no icon. Fig. 2.9 JOptionPane constants for message dialogs.

10 CHAPTER 2 number1 45 Fig. 2.10 Memory location showing the name and value of variable number1.

CHAPTER 2 11 number1 45 number2 72 Fig. 2.11 Memory locations after values for variables number1 and number2 have been input.

12 CHAPTER 2 number1 45 number2 72 sum 117 Fig. 2.12 Memory locations after a calculation.

CHAPTER 2 13 Java operation Arithmetic operator Algebraic expression Java expression Addition + f + 7 f + 7 Subtraction p c p - c Multiplication * bm b * m Division / x x / y x / y or - or x y y Modulus % r mod s r % s Fig. 2.13 Arithmetic operators.

14 CHAPTER 2 Operator(s) Operation(s) Order of evaluation (precedence) ( ) Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses on the same level (i.e., not nested), they are evaluated left to right. *, / or % Multiplication Division Modulus + or - Addition Subtraction Evaluated second. If there are several, they are evaluated left to right. Evaluated last. If there are several, they are evaluated left to right. Fig. 2.14 Precedence of arithmetic operators.

CHAPTER 2 15 Step 1. y = 2 * 5 * 5 + 3 * 5 + 7; 2 * 5 is 10 (Leftmost multiplication) Step 2. y = 10 * 5 + 3 * 5 + 7; 10 * 5 is 50 (Leftmost multiplication) Step 3. y = 50 + 3 * 5 + 7; 3 * 5 is 15 (Multiplication before addition) Step 4. y = 50 + 15 + 7; 50 + 15 is 65 (Leftmost addition) Step 5. y = 65 + 7; 65 + 7 is 72 (Last addition) Step 6. y = 72; (Last operation assignment) Fig. 2.15 Order in which a second-degree polynomial is evaluated.

16 CHAPTER 2 Standard algebraic equality operator or relational operator Java equality or relational operator Example of Java condition Meaning of Java condition Equality operators = == x == y x is equal to y!= x!= y x is not equal to y Relational operators > > x > y x is greater than y < < x < y x is less than y >= x >= y x is greater than or equal to y <= x <= y x is less than or equal to y Fig. 2.16 Equality and relational operators.

CHAPTER 2 17 1 // Fig. 2.17: Comparison.java 2 // Using if statements, relational operators 3 // and equality operators 4 5 import javax.swing.joptionpane; 6 7 public class Comparison { 8 public static void main( String args[] ) 9 { 10 String firstnumber, // first string entered by user 11 secondnumber, // second string entered by user 12 result; // a string containing the output 13 int number1, // first number to compare 14 number2; // second number to compare 15 16 // read first number from user as a string 17 firstnumber = 18 JOptionPane.showInputDialog( "Enter first integer:" ); 19 20 // read second number from user as a string 21 secondnumber = 22 JOptionPane.showInputDialog( "Enter second integer:" ); 23 24 // convert numbers from type String to type int 25 number1 = Integer.parseInt( firstnumber ); 26 number2 = Integer.parseInt( secondnumber ); 27 28 // initialize result to the empty string 29 result = ""; 30 31 if ( number1 == number2 ) 32 result = result + number1 + " == " + number2; 33 34 if ( number1!= number2 ) 35 result = result + number1 + "!= " + number2; 36 37 if ( number1 < number2 ) 38 result = result + "\n" + number1 + " < " + number2; 39 40 if ( number1 > number2 ) 41 result = result + "\n" + number1 + " > " + number2; 42 43 if ( number1 <= number2 ) 44 result = result + "\n" + number1 + " <= " + number2; 45 46 if ( number1 >= number2 ) 47 result = result + "\n" + number1 + " >= " + number2; 48 Fig. 2.17 Using equality and relational operators (part 1 of 2).

18 CHAPTER 2 49 // Display results 50 JOptionPane.showMessageDialog( 51 null, result, "Comparison Results", 52 JOptionPane.INFORMATION_MESSAGE ); 53 54 System.exit( 0 ); 55 } 56 } Fig. 2.17 Using equality and relational operators (part 2 of 2).

CHAPTER 2 19 Operators Associativity Type () left to right parentheses * / % left to right multiplicative + - left to right additive < <= > >= left to right relational ==!= left to right equality = right to left assignment Fig. 2.18 Precedence and associativity of the operators discussed so far.