26
L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Welcome to Object Oriented With JAVA Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

Embed Size (px)

Citation preview

Page 1: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Welcome toObject Oriented

With JAVA

Lecture #2By

Dr. Basheer M. Nasef

Page 2: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 2

Chapter2: Elementary Programming

Page 3: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 3

Objectives

To use Java operators to write numeric expressions.

To represent characters using the char type.

To represent a string using the String type.

To obtain input from the console using the Scanner class.

To distinguish syntax errors, runtime errors, and logic errors.

To debug logic errors.

(GUI) To obtain input using the JOptionPane input dialog boxes.

Page 4: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 4

Character Data Type

char letter = 'A'; (ASCII) char numChar = '4'; (ASCII)

char letter = '\u0041'; (Unicode)char numChar = '\u0034'; (Unicode)

Four hexadecimal digits.

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);

Page 5: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 5

Unicode FormatJava 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

Page 6: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 6

Problem: Displaying Unicodes

Write a program that displays two Chinese characters and three Greek letters.

DisplayUnicode Run

Page 7: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 7

Escape Sequences for Special Characters

Description Escape Sequence Unicode

Backspace \b \u0008

Tab \t \u0009

Linefeed \n \u000A

Carriage return \r \u000D

Backslash \\ \u005C

Single Quote \' \u0027

Double Quote \" \u0022

Page 8: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 8

Appendix B: ASCII Character SetASCII Character Set is a subset of the Unicode from \u0000 to \u007f

Page 9: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 9

ASCII Character Set, cont.ASCII Character Set is a subset of the Unicode from \u0000 to \u007f

Page 10: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 10

The String Type

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"; 

Page 11: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 11

String Concatenation // Three strings are concatenatedString message = "Welcome " + "to " + "Java"; // String Chapter is concatenated with number 2String s = "Chapter" + 2; // s becomes Chapter2 // String Supplement is concatenated with character BString s1 = "Supplement" + 'B'; // s1 becomes SupplementB

Page 12: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 12

Problem 2-1

Write a program that asks the user to enter two integers, obtains the numbers from the user, and then prints the larger number followed by the words "is larger." If the numbers are equal, print the message "These numbers are equal.“

MaxInt

Page 13: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 13

Write a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers.

The screen dialog should appear as follows:

Problem 2-2

Prob2_2

Page 14: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 14

Quiz: (Summing the digits in an integer)

Write a program that reads an integer between0 and 1000 and adds all the digits in the integer. For example, if an integer is 932, the sum of all its digits is 14.

Hint: Use the % operator to extract digits, and use the / operator to remove the extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93. Here is a sample run:

Page 15: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 15

Example: (Distance of two points)

Page 16: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 16

Quiz: (area of a triangle)

Page 17: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 20

Programming ErrorsSyntax Errors

Detected by the compilerRuntime Errors

Causes the program to abortLogic Errors

Produces incorrect result

Page 18: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 21

Syntax Errorspublic class ShowSyntaxErrors { public static void main(String[] args) { i = 30; System.out.println(i + 4); }}

Page 19: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 22

Runtime Errors

public class ShowRuntimeErrors { public static void main(String[] args) { int i = 1 / 0; }}

Page 20: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 23

Logic Errors

suppose you wrote the following program to add number1 to number2.

Page 21: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 26

Getting Input from Input Dialog Boxes String input = JOptionPane.showInputDialog( "Enter an input");

Page 22: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 27

Getting Input from Input Dialog Boxes String string = JOptionPane.showInputDialog( null, “Prompting Message”, “Dialog Title”, JOptionPane.QUESTION_MESSAGE);

Page 23: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 28

Two Ways to Invoke the Method 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.

Page 24: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 29

Converting Strings to IntegersThe 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”.

Page 25: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 30

Converting Strings to Doubles

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”.

Page 26: L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef

L2:CSC210 2014-2015 © Dr. Basheer M. Nasef 31

Decision Making: Equality and Relational Operators