0% found this document useful (0 votes)
8 views

02+-+Development+Software+for+Mobile

Uploaded by

loosolooso2000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

02+-+Development+Software+for+Mobile

Uploaded by

loosolooso2000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Software Development for

Mobile Devices

Lecture3

Dr.Marwa Elseddik
Lecturer, Department of Robotics and Intelligent Machines
2024-2025
Revision
Eclipse IDE
• In this course, we use Eclipse as an IDE to write Java
programs.

• You can download the Eclipse IDE from


https://www.eclipse.org/downloads/

3
Eclipse IDE

4
Simple Java Program
• Let’s begin with a simple Java program that displays the
message Welcome to Java! on the console

Welcome.java
/* My First Program!!*/
1 public class Welcome {
2 public static void main(String[] args) {
3 // Display message Welcome to Java! on the console
4 System.out.println("Welcome to Java!");
5 }
6 }

5
Simple Java Program
Java Program Process
The 3 Basic Types of Programming Errors

▪ Syntax Errors
▪ Syntax errors mean that they don’t follow a correct sequence in the computer language.
▪ Runtime Errors
▪ Runtime errors in a program are the errors that occur while the program is running after
being successfully compiled.
▪ Logical Errors
▪ Logical errors are the hardest of all error types to detect. They do not cause the program to
crash or simply not work at all, they cause it to “misbehave” in some way.
More Java Programs
• you can perform mathematical computations and display
the result on the console.

10.5 + 2 × 3
• Example: Evaluating
45 − 3.5

ComputeExpression.java
public class ComputeExpression {
public static void main(String[] args) {
System.out.println((10.5 + 2 * 3)/(45-3.5));
}
}
Output:

0.39759036144578314
10
Comments
• Comments help programmers to understand the program.

• They are not programming statements and thus are ignored by


the compiler.

Single line comment:


//This application program displays Welcome to Java!

Block comment
/* This application program displays Welcome to Java! */
/* This application program
displays Welcome to Java! */

11
Basic Syntax
In Java programs, it is very important to keep in mind the following
points.

• Case Sensitivity

• Class Names: For all class names the first letter should be in Upper
Case. If several words are used to form a name of the class, each
inner word's first letter should be in Upper Case.

Example: class MyFirstJavaClass

• Method Names: All method names should start with a Lower Case
letter. If several words are used to form the name of the method, then
each inner word's first letter should be in Upper Case.

Example: public void myMethodName()


12
Basic Syntax
In Java programs, it is very important to keep in mind the following
points.

• Program File Name: Name of the program file should exactly


match the class name.

Example: Assume 'MyFirstJavaProgram' is the class name. Then the


file should be saved as 'MyFirstJavaProgram.java'

• public static void main(String args[]): Java program processing starts


from the main() method which is a mandatory part of every Java
program.

13
Java Identifiers
Names used for classes, variables, and methods are called identifiers.

• All identifiers should begin with a letter (A to Z or a to z), currency character


($) or an underscore (_).

• After the first character, identifiers can have any combination of characters.

• A key word cannot be used as an identifier.

• Most importantly, identifiers are case sensitive.

• Examples of legal identifiers: age, $salary, _value, __1_value.

• Examples of illegal identifiers: 123abc, -salary.

14
Java Keywords
abstract assert boolean break
byte case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
private protected public return
short static strictfp super
switch synchronized this throw
throws transient try void
volatile while
15
Variables
What is a Variable
▪ A container that stores a meaningful value that can be used throughout a program.
▪ A variable must be declared before it is used.
▪ The declaration allocates a space in memory to hold values of this type.
▪ Variable have 3 characteristics:
1. Variable Name (Identifier).
2. Data Type.
3. Value.

▪ The syntax of a variable declaration is

data-type variable-name; int id;

▪ The syntax of a variable initialization is

data-type variable-name = value; int id = 5;

1 - 19
Variable Name (Identifier)
▪ An identifier is a sequence of characters that consist of
▪ Letters, digits ,underscore (_) ,dollar sign ($).
▪ An identifier must start with
▪ A letter, an underscore (_), or a dollar sign ($).
▪ Cannot start with a digit.
▪ An identifier can be made of any length.
▪ Java is case sensitive.
▪ Cannot match any of Java's reserved words
▪ public, class, static, void.

1 - 20
Naming Conventions
▪ 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
❖ radius , area and computeArea.
▪ 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

1 - 21
Variable Data Types

▪ Variable data types can be:

1. Primitive (simple) data types.

2. Non-Primitive/Reference data types.

1 - 22
Primitive Data Types
Integers
▪ There are four integer data types
▪ They differ by the amount of memory used to store them.
▪ Default value is 0

Type Bits Value Range


byte 8 -128 … 127

short 16 -32768 … 32767

int 32 -231 …231-1

long 64 -263 …. 263-1

1 - 24
Floating Point
▪ There are two floating point types.
▪ Default value of float is 0.0f
▪ Default value of double is 0.0d

Type Bits

float 32

double 64

1 - 25
Characters
▪ A char value stores a single character from the Unicode character set.
▪ A character set is an ordered list of characters and symbols.
▪ ‘A’, ‘B’, ‘C’, … , ‘a’, ‘b’, … ,‘0’, ‘1’, … , ‘$’, …

▪ The Unicode character is physically stored in UTF-16BE encoding which uses 16


bits (2 bytes) per character.

1 - 26
Boolean
▪ A boolean value represents a true/false condition.
▪ The reserved words true and false are the only valid values for a
boolean type.
▪ The boolean type uses one bit.

1 - 27
Constants
▪ We may declare that a variable is a constant and its value may never
change.
▪ The word final is a java keyword which means that the constant cannot be
changed.

final double PI = 3.14159;


final int CHINA_OLYMPICS_YEAR = 2008;

1 - 28
Non-Primitive/Reference Data Types
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 = "Hello";

▪ String is a predefined class in the Java library just like the System class.

▪ The String type is not a primitive type.

▪ It is known as a reference type.

1 - 30
String Concatenation

// Three strings are concatenated


String message = "Welcome " + "to " + "Java"; // message becomes “Welcome
to Java”

// String Chapter is concatenated with number 2


String chapterNum = "Chapter " + 2; // chapterNum becomes "Chapter 2"

// String Supplement is concatenated with character B


String s = "Supplement " + 'B'; // s becomes "Supplement B"

1 - 31
Example
public class DeclarationExample {
public static void main (String[] args) {
int weeks = 14;
long numberOfStudents = 120;
double averageFinalGrade = 78.6;
char ch = ‘a’;
String courseName = “Introduction to IS”;
System.out.println(weeks);
System.out.println(numberOfStudents);
System.out.println(averageFinalGrade);
System.out.println(ch);
System.out.println(courseName);
}
}

1 - 32
Arithmetic Operators
Arithmetic Operators

Operator Meaning Example

+ Addition total = cost + tax;

- Subtraction cost = total – tax;

* Multiplication tax = cost * rate;

/ Division salePrice = original / 2;

% Modulus remainder = value % 5;

1 - 34
Assignment Operators
Assignment Operators

Operator Example Equivalent Value of variable after operation

+= x += 5; x = x + 5; The old value of x plus 5.

-= y -= 2; y = y – 2; The old value of y minus 2

*= z *= 10; z = z * 10; The old value of z times 10

/= a /= b; a = a / b; The old value of a divided by b.

The remainder of the division of the old value of c


%= c %= 3; c = c % 3;
divided by 3.

1 - 36
Increment and Decrement
Operators
Increment and Decrement Operators

Operator Name Description

Increments var by 1 and evaluates to the new value in var after the
++var Pre-increment
increment.

var++ Post-increment Evaluates to the original value in var and increments var by 1.

Decrements var by 1 and evaluates to the new value in var after


--var Pre-decrement
the decrement

var-- Post-decrement Evaluates to the original value in var and decrements var by 1.

1 - 38
Increment and Decrement Operators, Cont.

int i = 10; Same effect as


int newNum = 10 * i++; int newNum = 10 * i;
i = i + 1;

int i = 10; Same effect as


int newNum = 10 * (++i); i = i + 1;
int newNum = 10 * i;

1 - 39
Operator Precedence
Operator Precedence

Operators Precedence

Parentheses ()

Unary post-increment/decrement expr++, expr--

Unary pre-increment/decrement ++expr, --expr

Multiplicative/Division/Modulus */%

Addition/Subtraction +-

Assignment = += -= *= /= %=

1 - 41
Assignment
Type Conversion/Casting
Type Conversion/Casting
▪ Implicit Casting (Type Widening) - converting a smaller type to a larger type size.
▪ byte -> short -> char -> int -> long -> float -> double

byte b = 5;
int i = b;

▪ Explicit Casting (Type Narrowing) - converting a larger type to a smaller type size.
▪ double -> float -> long -> int -> char -> short -> byte

int i = 5;
byte b = (byte)i;

1 - 44
Integer Conversion
▪ Convert from Floating-Point to Integer
▪ To convert floating-point into integer value, you can use the explicit casting method as follows:
▪ float f = 50.8;
▪ byte b = (byte) 3.0; (explicit casting)
▪ int i = (int) f; (Fraction part is truncated)
▪ long l = (long) f; (explicit casting)
▪ Convert from Char to Integer
▪ To convert a char into an integer value, you can use the implicit/explicit casting method as follows:
▪ Char c = 'a’;
▪ int i = c; // i = 97
▪ int i = (int) 'a’; // the same: i = 97
▪ Convert from String to Integer
▪ To convert a string into an int value, you can use the parseInt/valueOf method in the Integer class as
follows:
▪ String intString = “123”;
▪ int intValue = Integer.parseInt(intString); //where intString is a numeric string such as “123”.
▪ int intValue2 = Integer.valueOf(intString); //where intString is a numeric string such as “123”.

1 - 45
Floating-Point Conversion
▪ Convert from Integer to Floating-Point
▪ To convert integer into floating-point value, you can use the implicit casting method as follows:
▪ byte b = 100;
▪ long l = b * 3 + 4; (implicit casting)
▪ float f = l; (implicit casting)
▪ double d = b * f + l / 2; (implicit casting)
▪ Convert from Char to Floating-Point
▪ To convert a char into a Floating-Point value, you can use the implicit/explicit casting method as follows:
▪ float f = 'a’; // f = 97.0
▪ double d = (double) 'a’; // d = 97.0
▪ Convert from String to Floating-Point
▪ To convert a string into a floating-point value, you can use the parseDouble/valueOf method in the Double
class as follows:
▪ String dString = “123.45”;
▪ double doubleValue = Double.parseDouble(dString); //where dString is a numeric string such as
“123.45”.
▪ double doubleValue = Double.valueOf(dString); //where dString is a numeric string such as “123.45”.

1 - 46
Char Conversion
▪ Convert from Integer to char
▪ To convert integer into char value, you can use the implicit/explicit casting method as follows:
▪ char c = 97; // c = ‘a’
▪ char c = (char) 97; // c = ‘a’
▪ Convert from Floating-Point to char
▪ To convert a floating-point into a char value, you can use the explicit casting method as follows:
▪ float f = 97.0;
▪ char c = (char) f; // c = ‘a’
▪ Convert from String to char
▪ To convert a string into a char value, you can use the charAt method in the String class as follows:
▪ String cString = “Hello”;
▪ char c = cString.charAt(0); // c = ‘H’.
▪ char c = cString.charAt(1); // c = ‘e’.

1 - 47
String Conversion
▪ Convert Integer to String
▪ To convert an integer into String value, you can use the valueOf method in the String class as
follows:
▪ int num = 140;
▪ String s = String.valueOf(num); // s = “140”

▪ Convert Floating-Point to String


▪ To convert a floating-point into String value, you can use the valueOf method in the String
class as follows:
▪ double num = 140;
▪ String s = String.valueOf(num); // s = “140”

▪ Convert char to String


▪ To convert a char into String value, you can use the valueOf method in the String class as
follows:
▪ char c = ‘A’;
▪ String s = String.valueOf(c); // s = “A”

1 - 48

You might also like