Chaptrm

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Chapter 2

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

Q: what is the output of the following statements:


int i=1 , j = 2 ;
System.out.println( “ i + j is = ” + i + j ) ; // i + j is = 12
i is first concatenated with “ i + j ” & then concatenated with j.

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.

The output should be as following:

The width is = 5 , The height = 7, and The area is = 35

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

// returns random number between a and a + b , excluding a + b


-For example:
50 + Math.random( )*50
// returns a random number between 50 and 100, exclude (50 and 100)
0 + Math.random( )*10
// returns a random number between 0 and 10 exclude (0 and 10)
50 + (int) (Math.random( )*50)
// returns a random integer number starting from 50 to 99 (50 and 99 inclusive) .

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:

- raise the first number to the power of 4.

- absolute value of the subtraction the second number from the first.

- the maximum of the two numbers.

- the minimum of the two numbers.

- the square root of the subtraction.

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.

Q2: Find the maximum number of 35, 10 , 62 .

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.

The syntax for declaring a character variable is:

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

- The value of the string must be enclosed in double quotation marks.


- Example:
String message = “Welcome to Java” ;
String x = “H” ;
String y = “ ” ;

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.

Q2:Write a java program that declares a String variable


message=“Welcome to Java”, and generates an integer random number
between 0 and length of that String. Then prints the character at that
random number.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla 
Hussein
Concatenation for String

- Two strings can be combined together into one.


- The (+) operator is called concatenation operator if at least one of the
operands is a string.
- If the string is combined with a variable (or value), the value will be
converted into a string and concatenated with other string.
- In general, if one of the operands is nonstring, the nonstring value is
converted into a string.

- Example: String message= “Welcome ” + “To ” + “Java” ;


// message = “Welcome To Java”;

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein 
Concatenation
- Example:

String x = “Chapter ” + 2 ; // 2 is converted into a string, since + is a


//concatenation operator

String y = 9.4 + “ Marks” ; // y = “9.4 Marks”

String z = “see ”+ ‘u’ ; // z= “see u”

- note: if neither of the operands is a string, the + operator is the addition


operator that adds two numbers.

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:

String message = “Welcome To Java” ;


int num = message.length(); // num = 15
System.out.println(num); // 15

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

- returns the character at the specified position i in this string, where i is


integer.
- return data type ( the output type ) is character (char) .
- String numbering starts from zero ( zero based )
Example:

String message = “Welcome To Java” ;


char x = message.charAt ( 5 ); // x = m
System.out.println(x); // m

- The index i is between 0 and stringVar.length( ) – 1 , otherwise error.

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

- If the string not found, the output will be -1.


Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein 
String Methods
stringVar.indexOf( “something” , position);
4.
- Returns the index of the first occurrence of the string “something” , starting from
the position 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 ( “e” , 2 ); // x = 6
System.out.println(x); // 6
int y = message.indexOf ( “e” , 1); // y = 1
System.out.println(y); // 1
If the string not found, the output will be -1.

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:

String message = “Welcome To Java” ;


String x = message.substring ( 3 ); // x = “come To Java”
System.out.println(x); // come To Java
String y = message.substring ( 11 ); // y = “Java”
System.out.println(y); // Java

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:

String message = “Welcome To Java” ;


String x = message.substring ( 8 , 15 ); // x = “To Java”
System.out.println(x); // To Java
String y = message.substring ( 8, 10 ); // y = “To”
System.out.println(y); // To

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein 
String Methods
Example:

String name = “Mohammed Ali Mahmoud” ;


System.out.println( name.length() );// 20
System.out.println( name.charAt(2) );// h
System.out.println( name.charAt(9) );// A
System.out.println( name.charAt(8) );// space
System.out.println( name.charAt(20) );// error
System.out.println( name.indexOf(“a”) );// 3

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein 
`String Methods
Example:

String name = “Mohammed Ali Mahmoud” ;


System.out.println( name.indexOf(“ ”, 9 ) );// 12
System.out.println( name.indexOf(“M”) );// 0
System.out.println( name.indexOf(“M”, 3) );// 13
System.out.println( name.substring(9) );// Ali Mahmoud
System.out.println( name.substring(9, 12) );// Ali
System.out.println( name.substring(name.indexOf(“ ”)+1) );
// Ali Mahmoud

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein 
String Methods
Example:

String name = “Mohammed Ali Mahmoud” ;

- Find the second occurrence of letter “h”.

- Find the middle name.

- Find the third name.

- Find the last character.

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:

int varName=Integer.parseInt(String varName);

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:

double varName=Double.parseDouble(String varName);

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

- You can obtain input from an input dialog box.


- An input dialog box prompts the user to enter an input graphically.

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.

- The input is returned as a String.

- The Syntax for invoking the input dialog box:

String varName=JOptionPane.showInputDialog(“Enter an input”);

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.

- The Syntax for invoking the Message dialog box:

JOptionPane.showMessageDialog(null, Output);

- The Output can be any of data types.


- Think about it as the content of print statement.

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 

You might also like