Professional Documents
Culture Documents
Chaptrm
Chaptrm
Chaptrm
Elementary Programming
Concatenation
- Example:
int a = 10;
int b = 12;
int sum = a + b;
System.out.println(“The result is = ”+ sum); // The result is = 22
Correct form:
System.out.println( “ i + j is = ” + (i + j) ) ; // i + j is = 3
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla
Hussein
Concatenation
Q: write a java program to compute the area of a rectangle, suppose width
= 5, and height = 7.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Math Methods
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla
Hussein
Math Methods
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Math Methods
- Math.random() method returns random numbers between only 0 and 1, to make it
generates random numbers in any range, obey the following rule:
a + Math.random( ) * b
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Math Methods
Q: Write a java program to declare two integer numbers, first is 24 while
the second is 15. then find the following:
- absolute value of the subtraction the second number from the first.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Math Methods
Q1: Write a java program to solve the following equations:
a=3 , b=1.5, x is integer.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Character Data type
-Character variable stores only a single character.
-Each character value must be enclosed in single quotation marks.
char varName;
char varName= ‘ value ’ ;
-The value can be a (single alphabet, single digit, or single special character).
Example:
char letter=‘A’;
char num=‘8’;
char x=‘@’;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Character Data type
-Alphabets are represented by numbers in ASCII code, where A : 65,
B : 66, C : 67, …, Z : 90, a : 97, b: 98, c: 99, …, z : 122 .
int g='A';
-Increment & Decrement operators can also be used on char variables
to get next or preceding character.
Example:
char w = ‘b’ ;
System.out.println(++w); // c
char w = ‘b’ ;
System.out.println(w++); // b
char w = ‘B’ ;
System.out.println(--w); // A
char w = ‘B’ ;
System.out.println(w--); // B
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla
Hussein
String Type
- String is a sequence of characters.
- The char data type represents only one character. To represent multiple of
characters use the type called String.
- Syntax for declaring a String variable:
String varName;
String varName = “ Value ” ;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
String Type
- Unlike other data type, the String type has to be started with upper
case letter S (String).
Q1: Write a program to store your name ( first & surname) in two string
variables, then print each of them on a separate line.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla
Hussein
Concatenation for String
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Concatenation
- Example:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
String Methods
- Java provides a set of methods (operations) that can manipulate String
type.
1. stringVar.length( );
- returns the number of characters in this string, regarding spaces.
- return data type ( the output type ) is integer (int) .
Example:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
String Methods
2. stringVar.charAt( i );
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
String Methods
3. stringVar.indexOf( “something” );
- Returns the index of the first occurrence of the string “something” in this
string.
- return data type ( the output type ) is integer (int) .
- String numbering starts from zero ( zero based )
Example:
String message = “Welcome To Java” ;
int x = message.indexOf ( “To” ); // x = 8
System.out.println(x); // 8
int y = message.indexOf ( “J” ); // y = 11
System.out.println(y); // 11
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
String Methods
5. stringVar.substring( i );
- Returns a substring starting from the position of i to the end of this
string.
- return data type ( the output type ) is String.
- String numbering starts from zero ( zero based )
Example:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
String Methods
6. stringVar.substring( i1 , i2);
- Returns a substring starting from the position of i 1 to the position (i2- 1).
- return data type ( the output type ) is String.
- exclude position i2 .
- String numbering starts from zero ( zero based )
Example:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
String Methods
Example:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
`String Methods
Example:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
String Methods
Example:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla
Hussein
Convert String To Numbers
- The input returned from the input dialog box is a string.
- If you enter a numeric value such as 123, it returns "123".
- You have to convert a string into a number to obtain the input as a
number.
1. Convert String into int:
Example:
String x=“35”;
int y=Integer.parseInt( x );// y = 35, but x = “35”
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Convert String To Numbers
2. Convert String into double:
Example:
String x=“19”;
double y=Double.parseDouble( x );// y = 19.0, but x = “19”
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Display Output
- In
order, to use the input dialog and the message dialog boxes in java,
you have to invoke the class that contains the input and message boxes.
-Since JOptionPane is a predefined class in java, and Java’s predefined
classes are grouped into packages.
- JOptionPane is in the javax.swing package. JOptionPane is
imported into the program using the import statement.
- You can write the import statement immediately before the class header
in two ways:
import javax.swing.JOptionPane ; // or
import javax.swing.* ;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Getting Input From Users
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Getting Input From Users
- When the program is executed, a dialog is displayed to enable the user
to enter an input value.
- After entering an input, click OK to accept the input and close the dialog
box.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Getting Input From Users
Example:
String name=JOptionPane.showInputDialog(“Enter your name”);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Display Output
- You can display outputs in a graphical dialog box.
JOptionPane.showMessageDialog(null, Output);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Display Output
Example:
JOptionPane.showMessageDialog(null, “Welcome To Java”);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein
Display Output
Q1: Write a java program that prompts the user to enter his name, and
displays the name name using message box.
Q2: Write a program that prompts a user to enter the width, and the height
for a rectangle, computes the area and prints the output using message
box.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla
Hussein
Programming Errors
1. Syntax Errors.
2. Runtime Errors.
3. Logic Errors
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein