JAVA Programming Concepts

Similar documents
Chapter 2 Primitive Data Types and Operations. Objectives

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming

Motivations 9/14/2010. Introducing Programming with an Example. Chapter 2 Elementary Programming. Objectives

Chapter 2 ELEMENTARY PROGRAMMING

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

Chapter 2 Primitive Data Types and Operations

Chapter 2 Primitive Data Types and Operations

CEN 414 Java Programming

Chapter 2 Elementary Programming. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution

Chapter 2. Elementary Programming

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Chapter 2 Elementary Programming

Datatypes, Variables, and Operations

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Full file at

Elementary Programming. CSE 114, Computer Science 1 Stony Brook University

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

Elementary Programming

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

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

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

4 WORKING WITH DATA TYPES AND OPERATIONS

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

Chapter 2: Data and Expressions

Basics of Java Programming

Chapter 2: Data and Expressions

Important Java terminology

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Chapter 2: Data and Expressions

3. Java - Language Constructs I

Chapter 02: Using Data

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

Getting started with Java

4 Programming Fundamentals. Introduction to Programming 1 1

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lecture Set 2: Starting Java

Chapter 2: Using Data

Lecture Set 2: Starting Java

Full file at

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Zheng-Liang Lu Java Programming 45 / 79

Computer Programming, I. Laboratory Manual. Experiment #4. Mathematical Functions & Characters

Program Fundamentals

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

Introduction to Computers, Programs, and Java. CSE 114, Computer Science 1 Stony Brook University

Full file at

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

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Computational Expression

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Programming with Java

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Visual C# Instructor s Manual Table of Contents

A variable is a name for a location in memory A variable must be declared

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Chapter 2: Using Data

JAVA Programming Fundamentals

Date: Dr. Essam Halim

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

Information Science 1

Computer System and programming in C

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Chapter 3 Selection Statements

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Basic Computation. Chapter 2

Introduction to Java Applications

Course Outline. Introduction to java

CS-201 Introduction to Programming with Java

Java Primer 1: Types, Classes and Operators

These are reserved words of the C language. For example int, float, if, else, for, while etc.

CMPT 125: Lecture 3 Data and Expressions

COMP Primitive and Class Types. Yi Hong May 14, 2015

Section 2: Introduction to Java. Historical note

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Welcome to the Primitives and Expressions Lab!

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Java Notes. 10th ICSE. Saravanan Ganesh

Primitive Data Types: Intro

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

Chapter. Let's explore some other fundamental programming concepts

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CS110: PROGRAMMING LANGUAGE I

COMP 202 Java in one week

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Java Programming Fundamentals. Visit for more.

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Chapter 4 Mathematical Functions, Characters, and Strings

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

Introduction to Java Applications; Input/Output and Operators

II. Compiling and launching from Command-Line, IDE A simple JAVA program

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Transcription:

JAVA Programming Concepts M. G. Abbas Malik Assistant Professor Faculty of Computing and Information Technology University of Jeddah, Jeddah, KSA mgmalik@uj.edu.sa

Programming is the art of Problem Solving M. G. Abbas Malik, University of Jeddah 2

Starting from this chapter, you will learn how to solve practical problems programmatically. Through these problems, you will learn Java primitive data types and related subjects, such as variables, constants, data types, operators, expressions, and input and output. M. G. Abbas Malik, University of Jeddah 3

Writing a program involves two steps: 1. designing the Algorithm Problem Solving Step 2. Translating Coding Step the algorithm into programming instructions. Algorithm describes how a problem is solved by listing the actions needed to be taken to solve the problem. M. G. Abbas Malik, University of Jeddah 4

Read in the circle s radius. Compute the area using the following formula: area = radius * radius * pi (ᴫ) Display the result. Pseudo Code an informal high-level description of the operating principle of a computer program or other algorithm M. G. Abbas Malik, University of Jeddah 5

Start Flow Chart Graphical representation of an Algorithm Rhombus shows the input and output of the system Rectangle shows a process or computation Diamond shows a decision Arrows show the flow of the control. Input Computations - Operations Output Decisi on End M. G. Abbas Malik, University of Jeddah 6

Read in the circle s radius. Compute the area using the following formula: area = radius * radius * pi (ᴫ) Display the result. Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 7

M. G. Abbas Malik, University of Jeddah 8

M. G. Abbas Malik, University of Jeddah 9

M. G. Abbas Malik, University of Jeddah 10

ComputeCircleArea M. G. Abbas Malik, University of Jeddah 11

M. G. Abbas Malik, University of Jeddah 12

Every java program starts with a class public class ComputeCircleArea{ public static void main (String [] args){ // main function the starting point of the software } } M. G. Abbas Malik, University of Jeddah 13

public class ComputeCircleArea{ public static void main (String [] args){ // Input: Read the radius of the circle // Processing/Computation: Compute the area of the circle // Display the computed Area of the circle } } Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 14

A program/algorithm consists of: 1. Data / Information 2. Processing 1. Action 2. Computation Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 15

A program/algorithm consists of: 1. Data / Information 2. Processing 1. Action 2. Computation Data Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 16

A program/algorithm consists of: 1. Data / Information 2. Processing 1. Action 2. Computation Processing Actions Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 17

A program/algorithm consists of: 1. Data / Information 2. Processing 1. Action 2. Computation Processing Computations Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 18

A program/algorithm consists of: 1. Data / Information 2. Processing 1. Action 2. Computation What are different types of DATA? How we store the DATA in our program? Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 19

What are different types of DATA? Numeric Data: Real Numbers (Rational Numbers Integers, Natural Numbers, Fractions, etc. and Irrational Numbers) Text Data: Characters and Strings Multimedia: Images, Sound and Video Logical Data M. G. Abbas Malik, University of Jeddah 20

How we store the DATA in our program? Data Types: kind of a container in which we can store only one kind of data. Numeric Integer (byte, int, short, long) Floating points (float, double) Text (char, String) Logical Data Boolean (True False) Built in Data Types of Java are called Primitive Data Types M. G. Abbas Malik, University of Jeddah 21

A program/algorithm consists of: 1. Data / Information 2. Processing 1. Action 2. Computation How to perform different actions? How to do computations? Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 22

A program/algorithm consists of: 1. Data / Information 2. Processing 1. Action 2. Computation Different actions are control by the Control Unit in the CPU Computations are performed by the ALU (Arithematic Logic Unit) in the CPU Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 23

Variables are required to store Data/Information in a program Every variable has a name, size, data type and value. Variable is a memory area reserved in the RAM. public class ComputeCircleArea{ public static void main (String [] args){ Data Type of Variable Size is 8 Bytes Double precision } } Name of Variable // Input: Read the radius of the circle double radius; // Processing/Computation: Compute the area of the circle // Display the computed Area of the circle Default value for Double is 0.0 M. G. Abbas Malik, University of Jeddah 24

public class ComputeCircleArea{ public static void main (String [] args){ double radius; double area; // Declaring another variable for Area radius = 20; // Assigning a value to the radius // computing Area of the circle area = radius * radius * 3.14159; // Displaying the results System.out.println( The area of the circle with radius + radius + is + area); } } The plus sign (+) has two meanings: one for addition Operator and the other for concatenating (combining) stings. String concatenation operator, combines two strings into one. M. G. Abbas Malik, University of Jeddah 25

+ In Java, + sign is also used as String Concatenation Operator Consider the string Java Programming String Concatenation In Java: Java + Programming + String + Concatenation String 1 + String 2 + String 3 + String 4 Java Programming String Concatenation JavaProgrammingStringConcatenation M. G. Abbas Malik, University of Jeddah 26

+ In Java, + sign is also used as String Concatenation Operator Consider the string Java Programming String Concatenation In Java: Java Programming + String + Concatenation String 1 String 2 String 3 + + Java Programming String Concatenation M. G. Abbas Malik, University of Jeddah 27

// Three strings are concatenated String message = "Welcome " + "to " + "Java"; // String Chapter is concatenated with number 2 String s = "Chapter" + 2; // s becomes Chapter2 // String Supplement is concatenated with character B String s1 = "Supplement" + 'B'; // s1 becomes SupplementB M. G. Abbas Malik, University of Jeddah 28

Compiler is a software that translates the high-level programming code into the machine code so that we can execute the code on our computers/machines. In Java, Java Compiler translates the java code into the Byte- Code first and then Byte-Code is executed on Java Virtual Machine (JVM). Compilation to byte code brings several genuine advantages. One advantage is that compiled programs are portable in the sense that they can be run on a number of different computer systems (such as Linux, Windows, Solaris, and many others) for which a JVM is available. T M. G. Abbas Malik, University of Jeddah 29

M. G. Abbas Malik, University of Jeddah 30

M. G. Abbas Malik, University of Jeddah 31

Register Register Register Control Unit Operating System Files Netbeans Files ComputeCircleArea.java ComputeCircleArea.class ComputeCircleArea.exe ALU M. G. Abbas Malik, University of Jeddah 32

public class ComputeCircleArea{ public static void main (String [] args){ 1 double radius; 2 double area; 3 radius = 20; 4 area = radius * radius * 3.14159; 5 System.out.println( The area of the circle with radius + radius + is + area); } } Operating System Files Netbeans Files ComputeCircleArea.java ComputeCircleArea.class ComputeCircleArea.exe A program is executed sequentially statement by statement.. M. G. Abbas Malik, University of Jeddah 33

public class ComputeCircleArea{ public static void main (String [] args){ 1 double radius; 2 double area; 3 radius = 20; 4 area = radius * radius * 3.14159; 5 System.out.println( The area of the circle with radius + radius + is + area); } } A program is executed sequentially statement by statement.. Variable: radius ComputeCircleArea.exe 0.0 M. G. Abbas Malik, University of Jeddah 34

public class ComputeCircleArea{ public static void main (String [] args){ 1 double radius; 2 double area; 3 radius = 20; 4 area = radius * radius * 3.14159; 5 System.out.println( The area of the circle with radius + radius + is + area); } } A program is executed sequentially statement by statement.. Variable: radius Variable: area ComputeCircleArea.exe 0.0 0.0 M. G. Abbas Malik, University of Jeddah 35

public class ComputeCircleArea{ public static void main (String [] args){ 1 double radius; 2 double area; 3 radius = 20; 4 area = radius * radius * 3.14159; 5 System.out.println( The area of the circle with radius + radius + is + area); } } A program is executed sequentially statement by statement.. Variable: radius Variable: area ComputeCircleArea.exe 20.0 0.0 M. G. Abbas Malik, University of Jeddah 36

public class ComputeCircleArea{ public static void main (String [] args){ 1 double radius; 2 double area; 3 radius = 20; 4 area = radius * radius * 3.14159; 5 System.out.println( The area of the circle with radius + radius + is + area); } } A program is executed sequentially statement by statement.. Variable: radius Variable: area ComputeCircleArea.exe 20.0 1256.636 M. G. Abbas Malik, University of Jeddah 37

public class ComputeCircleArea{ public static void main (String [] args){ 1 double radius; 2 double area; 3 radius = 20; 4 area = radius * radius * 3.14159; 5 System.out.println( The area of the circle with radius + radius + is + area); } } A program is executed sequentially statement by statement.. Variable: radius Variable: area ComputeCircleArea.exe 20.0 1256.636 The area will be displayed on the screen M. G. Abbas Malik, University of Jeddah 38

Comments (//, /* */) Reserved words (public, void, static, class etc. ) Modifiers (public, static) Statements (System.out.println( Welcome to Java! ) Blocks (Class Block, Method Block) Classes (Welcome) Methods (At least one method is required) The main method M. G. Abbas Malik, University of Jeddah 39

Three types of comments in Java. Line comment: A line comment is preceded by two slashes (//) in a line. Paragraph comment: A paragraph comment is enclosed between /* and */ in one or multiple lines. javadoc comment: javadoc comments begin with /** and end with */. They are used for documenting classes, data, and methods. They can be extracted into an HTML file using JDK's javadoc command. M. G. Abbas Malik, University of Jeddah 40

Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class. Some other reserved words are public, static, and void. M. G. Abbas Malik, University of Jeddah 41

Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and static. Other modifiers are private, final, abstract, and protected. A public data, method, or class can be accessed by other programs. A private data or method cannot be accessed by other programs. Modifiers are discussed later in more details M. G. Abbas Malik, University of Jeddah 42

A statement represents an action or a sequence of actions. The statement System.out.println( ") in the program is a statement to display the area of the circle Every statement in Java ends with a semicolon (;). M. G. Abbas Malik, University of Jeddah 43

A pair of braces in a program forms a block that groups components of a program. public class Test { public static void main(string[] args) { System.out.println("Welcome to Java!"); } } Method block Class block M. G. Abbas Malik, University of Jeddah 44

The class is the essential Java construct. A class is a template or blueprint for objects. For now, though, understand that a program is defined by using one or more classes. M. G. Abbas Malik, University of Jeddah 45

What is System.out.println? It is a method: a collection of statements that performs a sequence of operations to display a message on the console. It can be used even without fully understanding the details of how it works. It is used by invoking a statement with a string argument. The string argument is enclosed within parentheses. In our example, the argument is ( The area of the circle with radius + radius + is + area ) You can call the same println method with a different argument to print a different message. M. G. Abbas Malik, University of Jeddah 46

The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method. The main method looks like this: } public static void main(string[] args) { // Statements; M. G. Abbas Malik, University of Jeddah 47

Variables are required to store Data/Information in a program Every variable has a name, size, data type and value. Variable is a memory area reserved in the RAM. Name of Variable public class ComputeCircleArea{ public static void main (String [] args){ Data Type of Variable Size is 8 Bytes Double precision } } // Input: Read the radius of the circle double radius; // Processing/Computation: Compute the area of the circle // Display the computed Area of the circle Default value for Double is 0.0 Identifier M. G. Abbas Malik, University of Jeddah 48

An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($). An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. An identifier cannot be a reserved word. An identifier cannot be true, false, or null. An identifier can be of any length. Examples: number1, Demo, i_j, number$4, area, volume, M. G. Abbas Malik, University of Jeddah 49

x = 1; // Assign 1 to variable x; radius = 1.0; // Assign 1.0 to variable radius; a = 'A'; // Assign 'A' to variable a; M. G. Abbas Malik, University of Jeddah 50

In General, an assignment statement is like: Single_Variable(identifier) = expression An expression can be: 1. A value x = 1; // Assign 1 to variable x; radius = 1.0; // Assign 1.0 to variable radius; a = 'A'; // Assign 'A' to variable a; M. G. Abbas Malik, University of Jeddah 51

In General, an assignment statement is like: Single_Variable(identifier) = expression An expression can be: 1. A value 2. Another variable x = 1; // Assign 1 to variable x; radius = x; // Assign 1.0 to variable radius; M. G. Abbas Malik, University of Jeddah 52

In General, an assignment statement is like: Single_Variable(identifier) = expression An expression can be: 1. A value 2. Another variable 3. An expression that can be evaluated into a single value x = 1; // Assign 1 to variable x; y = (23 + 45)*25 // Assign the value of expression to variable y; radius = x + y; // Assign addition of variables x and y to variable radius; M. G. Abbas Malik, University of Jeddah 53

A named constant is an identifier that represents a permanent value. final datatype CONSTANTNAME = VALUE; final double PI = 3.14159; final int SIZE = 3; M. G. Abbas Malik, University of Jeddah 54

Name Range Storage Size byte 2 7 (-128) to 2 7 1 (127) 8-bits signed short 2 15 (-32768) to 2 15 1 (32767) 16-bit signed int 2 31 (-2147483648) to 2 31 1 (2147483647) 32-bit signed long 2 63 to 2 63 1 (i.e., -9223372036854775808 to 9223372036854775807) float double Negative range: -3.4028235E+38 to -1.4E-45 Positive range: 1.4E-45 to 3.4028235E+38 Negative range: -1.7976931348623157E+308 to -4.9E-324 Positive range: 4.9E-324 to 1.7976931348623157E+308 64-bit signed 32-bit IEEE 754 64-bit IEEE 754 M. G. Abbas Malik, University of Jeddah 55

Name Meaning Example Result + Addition 34 + 1 35 - Subtraction 34.0 0.1 33.9 * Multiplicatio n 30 * 3 90 / Division 8.4 / 2.0 4.2 % Remainder 20 % 3 2 M. G. Abbas Malik, University of Jeddah 56

5 / 2 yields an integer 2. 5.0 / 2 yields a double value 2.5 5 % 2 yields 1 (the remainder of the division) M. G. Abbas Malik, University of Jeddah 57

Remainder is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd. Suppose today is Saturday and you and your friends are going to meet in 10 days. What day is in 10 days? Saturday is the 6 th day in a week A week has 7 days 6 + 10 % 7 = 2 The 2 nd day in a week is Tuesday After 10 Days M. G. Abbas Malik, University of Jeddah 58

A literal is a constant value that appears directly in the program. For example, 34, 1,000,000, and 5.0 are literals in the following statements: int i = 34; long x = 1000000; double d = 5.0; M. G. Abbas Malik, University of Jeddah 59

An integer literal can be assigned to an integer variable as long as it can fit into the variable. A compilation error would occur if the literal were too large for the variable to hold. For example, the statement byte b = 1000 would cause a compilation error, because 1000 cannot be stored in a variable of the byte type. An integer literal is assumed to be of the int type, whose value is between -2 31 (-2147483648) to 2 31 1 (2147483647). To denote an integer literal of the long type, append it with the letter L or l. M. G. Abbas Malik, University of Jeddah 60 L is preferred because l (lowercase L) can easily be confused with 1 (the digit one).

Floating-point literals are written with a decimal point. By default, a floating-point literal is treated as a double type value. For example, 5.0 is considered a double value, not a float value. We can make a number a float by appending the letter f or F, and make a number a double by appending the letter d or D. For example, you can use 100.2f or 100.2F for a float number, and 100.2d or 100.2D for a double number. M. G. Abbas Malik, University of Jeddah 61

Floating-point literals can also be specified in scientific notation, for example, 1.23456e+2, same as 1.23456e-2, is equivalent to 123.456, and 1.23456e-2 is equivalent to 0.0123456. E (or e) represents an exponent and it can be either in lowercase or uppercase. M. G. Abbas Malik, University of Jeddah 62

3 + 4x 5 10 y 5 x a + b + c + 9 4 x + 9 + x y How can write this in Java? (3+4*x)/5 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y) M. G. Abbas Malik, University of Jeddah 63

The result of a Java expression and its corresponding arithmetic expression are the same. Therefore, you can safely apply the arithmetic rule for evaluating a Java expression. M. G. Abbas Malik, University of Jeddah 64

M. G. Abbas Malik, University of Jeddah 65

Write a program that obtains hours and minutes from seconds. How can we achieve this? What is the Algorithm? M. G. Abbas Malik, University of Jeddah 66

Write a program that obtains hours and minutes from seconds. Input to our program Outputs to our program How to calculate the results/outputs from the input? 1. Compute minutes from the seconds 2. Compute remaining seconds 3. Compute hours from the minutes 4. Compute remaining minutes M. G. Abbas Malik, University of Jeddah 67

Write a program that obtains hours and minutes from seconds. Read the input seconds Compute the minutes from the seconds using the formula minutes = seconds / 60 Compute the remaining seconds using the formula seconds = seconds % 60 Compute the hours from the minutes using the formula hours = minutes / 60 Compute the remaining minutes using the formula minutes = minutes % 60 Display the results as Hours : Minutes : Seconds M. G. Abbas Malik, University of Jeddah 68

Current Time of Computer Gives current time in Milliseconds M. G. Abbas Malik, University of Jeddah 69

Current Time of Computer Converting time from Milliseconds into seconds M. G. Abbas Malik, University of Jeddah 70

Current Time of Computer Getting current seconds from the Total seconds M. G. Abbas Malik, University of Jeddah 71

Current Time of Computer Getting total minutes from the Total seconds M. G. Abbas Malik, University of Jeddah 72

Current Time of Computer Getting current minutes from the Total Minutes M. G. Abbas Malik, University of Jeddah 73

Current Time of Computer Getting total hours from the Total Minutes M. G. Abbas Malik, University of Jeddah 74

Current Time of Computer Getting current hours from the Total hours M. G. Abbas Malik, University of Jeddah 75

Current Time of Computer Displaying current time on screen M. G. Abbas Malik, University of Jeddah 76

Write a program that converts a Fahrenheit degree to Celsius using the formula: celsius = 5 9 For Problem Analysis: What is the input of our program? What is the output of our program? fahrenheit 32 What are the calculations/action that we will perform in our program? M. G. Abbas Malik, University of Jeddah 77

Write a program that converts a Fahrenheit degree to Celsius using the formula: celsius = 5 9 fahrenheit 32 M. G. Abbas Malik, University of Jeddah 78

Write a program that converts a Fahrenheit degree to Celsius using the formula: celsius = 5 9 fahrenheit 32 Input to our program M. G. Abbas Malik, University of Jeddah 79

Write a program that converts a Fahrenheit degree to Celsius using the formula: celsius = 5 9 fahrenheit 32 Outputs to our program Input to our program M. G. Abbas Malik, University of Jeddah 80

Write a program that converts a Fahrenheit degree to Celsius using the formula: celsius = 5 9 computations fahrenheit 32 Outputs to our program Input to our program M. G. Abbas Malik, University of Jeddah 81

Write a program that converts a Fahrenheit degree to Celsius using the formula: celsius = 5 9 Read in the Fahrenheit degree fahrenheit 32 Calculate the Celsius degree using the above given formula Display the results on the screen M. G. Abbas Malik, University of Jeddah 82

Equal sign (=) is the Assignment Operator It is used to assign a value to a variable. Examples: radius = 20; area = radius * radius * 3.14159; M. G. Abbas Malik, University of Jeddah 83

Operator Example Equivalent += i += 8 i = i + 8 -= f -= 8.0 f = f - 8.0 *= i *= 8 i = i * 8 /= i /= 8 i = i / 8 %= i %= 8 i = i % 8 M. G. Abbas Malik, University of Jeddah 84

Operator Name Description ++var Preincrement The expression (++var) increments var by 1 and evaluates to the new value in var after the increment. var++ Postincrement The expression (var++) evaluates to the original value in var and increments var by 1. --var Predecrement The expression (--var) decrements var by 1 and evaluates to the new value in var after the decrement. var-- Postdecrement The expression (var--) evaluates to the original value in var and decrements var by 1. M. G. Abbas Malik, University of Jeddah 85

int i = 10; int newnum = 10 * i++; Post Increment Same effect as int newnum = 10 * i; i = i + 1; // i += 1; int i = 10; int newnum = 10 * ++i; newnum = 100 Pre Increment i = 11 Same effect as i = i + 1; // i += 1; int newnum = 10 * i; newnum = 110 i = 11 M. G. Abbas Malik, University of Jeddah 86

Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoid using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this: int k = ++i + i. M. G. Abbas Malik, University of Jeddah 87

The following types of expressions can be Assignment statements: variable op= expression; // Where op is +, -, *, /, or % ++variable; variable++; --variable; variable--; M. G. Abbas Malik, University of Jeddah 88

Consider the following statements: byte i = 100; long k = i * 3 + 4; double d = i * 3.1 + k / 2; M. G. Abbas Malik, University of Jeddah 89

When performing a binary operation involving two operands of different types, Java automatically converts the operand based on the following rules: 1. If one of the operands is double, the other is converted into double. 2. Otherwise, if one of the operands is float, the other is converted into float. 3. Otherwise, if one of the operands is long, the other is converted into long. 4. Otherwise, both operands are converted into int. M. G. Abbas Malik, University of Jeddah 90

Implicit casting (automatically done by Java) double d = 3; (type widening) Explicit casting int i = (int)3.0; (type narrowing) int i = (int)3.9; (Fraction part is truncated) What is wrong with: int x = 5 / 2.0; M. G. Abbas Malik, University of Jeddah 91

Use Scanner class for console input. Need to create an object of Scanner class to read input. Scanner input = new Scanner(System.in); Scanner input declare input variable whose type is Scanner. the instruction creates a Scanner object and assigns its reference to the variable input. To invoke the method of the object input type scanner we will use nextdouble() M. G. Abbas Malik, University of Jeddah 92

import java.util.scanner; // Scanner class is in the java.util package public class ComputeCircleArea{ public static void main (String [] args){ double radius; double area; Scanner input = new Scanner(System.in); system.out.print( Enter a value for the radius of the circle: ); radius = input.nextdouble(); area = radius * radius * 3.14159; System.out.println( The area of the circle with radius + radius + is + area); } } M. G. Abbas Malik, University of Jeddah 93

Write a program that displays the sales tax with two digits after the decimal point. This program lets the user enter the interest rate, number of years, and loan amount and computes monthly payment and total payment. monthly payment = 1 loanamount monthlyinterestrate 1 1 + monthlyinterestrate numberofyear 12 ComputeLoan M. G. Abbas Malik, University of Jeddah 94

char letter = 'A'; (ASCII) char numchar = '4'; (ASCII) Four hexadecimal digits. char letter = '\u0041'; (Unicode) char numchar = '\u0034'; (Unicode) NOTE: The increment and decrement operators can also be used on char variables to get the next or preceding Unicode character. For example, the following statements display character b. char ch = 'a'; System.out.println(++ch); M. G. Abbas Malik, University of Jeddah 95

Java characters use Unicode, a 16-bit encoding scheme established by the Unicode Consortium to support the interchange, processing, and display of written texts in the world s diverse languages. Unicode takes two bytes, preceded by \u, expressed in four hexadecimal numbers that run from '\u0000' to '\uffff'. So, Unicode can represent 65535 + 1 characters. Unicode \u03b1 \u03b2 \u03b3 for three Greek letters M. G. Abbas Malik, University of Jeddah 96

Write a program that displays two Chinese characters and three Greek letters. DisplayUnicode Run M. G. Abbas Malik, University of Jeddah 97

Description Escape Sequence Backspace \b \u0008 Tab \t \u0009 Linefeed \n \u000a Carriage return \r \u000d Backslash \\ \u005c Single quote \ \u0027 Double quote \ \u0022 Unicode M. G. Abbas Malik, University of Jeddah 98

ASCII Character Set is a subset of the Unicode from \u0000 to \u007f M. G. Abbas Malik, University of Jeddah 99

ASCII Character Set is a subset of the Unicode from \u0000 to \u007f M. G. Abbas Malik, University of Jeddah 100

int i = 'a'; // Same as int i = (int)'a'; char c = 97; // Same as char c = (char)97; M. G. Abbas Malik, University of Jeddah 101

This program lets the user enter the amount in decimal representing dollars and cents and output a report listing the monetary equivalent in single dollars, quarters, dimes, nickels, and pennies. Your program should report maximum number of dollars, then the maximum number of quarters, and so on, in this order. ComputeChange Run M. G. Abbas Malik, University of Jeddah 102

The char type only represents one character. To represent a string of characters, use the data type called String. For example, String message = "Welcome to Java"; String is actually a predefined class in the Java library The String type is not a primitive type. It is known as a reference type. Any Java class can be used as a reference type for a variable. M. G. Abbas Malik, University of Jeddah 103

Appropriate Comments Naming Conventions Proper Indentation and Spacing Lines Block Styles M. G. Abbas Malik, University of Jeddah 104

Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Include your name, class section, instructor, date, and a brief description at the beginning of the program. M. G. Abbas Malik, University of Jeddah 105

Choose meaningful and descriptive names. Variables and method names: Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area, and the method computearea. M. G. Abbas Malik, University of Jeddah 106

Class names: Capitalize the first letter of each word in the name. For example, the class name ComputeArea. Constants: Capitalize all letters in constants, and use underscores to connect words. For example, the constant PI and MAX_VALUE M. G. Abbas Malik, University of Jeddah 107

Indentation Indent two spaces. Spacing Use blank line to separate segments of the code. M. G. Abbas Malik, University of Jeddah 108

Use end-of-line style for braces. Next-line style public class Test { public static void main(string[] args) { System.out.println("Block Styles"); } } public class Test { public static void main(string[] args) { System.out.println("Block Styles"); } } End-of-line style M. G. Abbas Malik, University of Jeddah 109

Syntax Errors Detected by the compiler Runtime Errors Causes the program to abort Logic Errors Produces incorrect result M. G. Abbas Malik, University of Jeddah 110

public class ShowSyntaxErrors { } public static void main(string[] args) { } i = 30; System.out.println(i + 4); i is not declared as variable thus compiler does not recognize it M. G. Abbas Malik, University of Jeddah 111

public class ShowRuntimeErrors { } public static void main(string[] args) { } int i = 1 / 0; Division by ZERO M. G. Abbas Malik, University of Jeddah 112

Logic errors occur when a program does not perform the way it was intended to. This kind of error is done by the programmer. Wrong use of formula An error in algorithm Hard to identify such errors public class ShowLogicErrors { public static void main(string[] args) { // Add number1 to number2 int number1 = 3; int number2 = 3; number2 += number1 + number2; System.out.println("number2 is " + number2); } } M. G. Abbas Malik, University of Jeddah 113

Logic errors are called bugs. The process of finding and correcting errors is called debugging. A common approach to debugging is to use a combination of methods to narrow down to the part of the program where the bug is located. You can hand-trace the program (i.e., catch errors by reading the program), or you can insert print statements in order to show the values of the variables or the execution flow of the program. This approach might work for a short, simple program. But for a large, complex program, the most effective approach for debugging is to use a debugger utility. M. G. Abbas Malik, University of Jeddah 114

Debugger is a program that facilitates debugging. You can use a debugger to Execute a single statement at a time. Trace into or stepping over a method. Set breakpoints. Display variables. Display call stack. Modify variables. M. G. Abbas Malik, University of Jeddah 115

We will use two ways of obtaining input. 1. Using the Scanner class (console input) 2. Using JOptionPane input dialogs (GUI input) M. G. Abbas Malik, University of Jeddah 116

String input = JOptionPane.showInputDialog( "Enter an input"); M. G. Abbas Malik, University of Jeddah 117

String string = JOptionPane.showInputDialog( null, Prompting Message, Dialog Title, JOptionPane.QUESTION_MESSAGE); M. G. Abbas Malik, University of Jeddah 118

There are several ways to use the showinputdialog method. For the time being, you only need to know two ways to invoke it. One is to use a statement as shown in the example: String string = JOptionPane.showInputDialog(null, x, y, JOptionPane.QUESTION_MESSAGE); where x is a string for the prompting message, and y is a string for the title of the input dialog box. The other is to use a statement like this: JOptionPane.showInputDialog(x); where x is a string for the prompting message. M. G. Abbas Malik, University of Jeddah 119

The input returned from the input dialog box is a string. If you enter a numeric value such as 123, it returns 123. To obtain the input as a number, you have to convert a string into a number. To convert a string into an int value, you can use the static parseint method in the Integer class as follows: int intvalue = Integer.parseInt(intString); where intstring is a numeric string such as 123. M. G. Abbas Malik, University of Jeddah 120

To convert a string into a double value, you can use the static parsedouble method in the Double class as follows: double doublevalue =Double.parseDouble(doubleString); where doublestring is a numeric string such as 123.45. M. G. Abbas Malik, University of Jeddah 121

Same as the preceding program for computing loan payments, except that the input is entered from the input dialogs and the output is displayed in an output dialog. loanamount monthlyinterestrate 1 1 (1 monthlyinterestrate ) numberofyears 12 ComputeLoanUsingInputDialog Run M. G. Abbas Malik, University of Jeddah 122