Oop2 2
Oop2 2
● Java Syntax
the following code to print "Hello World"
Example explained
Every line of code that runs in Java must be inside a class. In our
example, we named the class Main. A class should always start with
an uppercase first letter.
remember that every Java program has a class name which must
match the filename, and that every program must contain the main()
method.
● Double Quotes
When you are working with text, it must be wrapped inside double
quotations marks " ".
The only difference is that it does not insert a new line at the end of
the output:
System.out.println(358);
System.out.println(3 + 3);
● Java Variables
assign a new value to an existing variable, it will overwrite the
previous value:
System.out.println(myNum);
Type casting is when you assign a value of one primitive data type to
another type.
1. Widening Casting
System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
}
2. Narrowing Casting
public class Main {
public static void main(String[] args) {
double myDouble = 9.78d;
int myInt = (int) myDouble; // Manual casting: double
to int
}
● Java Operators
● Arithmetic operators
int x = 10;
x += 5;
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
●
● Comparison operators
int x = 5;
== Equal to x == y
!= Not equal x != y
int x = 5;
● Constants
The Syntax for write constant is like this
● String Length
The indexOf() method returns the index (the position) of the first
occurrence of a specified text in a string (including whitespace):
System.out.println(txt.indexOf("locate")); // Outputs 7
Math.random();
To get more control over the random number, for example, if you only
want a random number between 0 and 100, you can use the following
formula:
E.x:-
if (20 > 18) {
System.out.println("20 is greater than 18");
E.x:-
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
E.x:-
int time = 22;
if (time < 10) {
System.out.println("Good morning.");
} else if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening."); }
● Java Switch
E.x:-
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
// Outputs "Thursday" (day 4)
● The default Keyword
E.x:-
int day = 4;
switch (day) {
case 6:
System.out.println("Today is Saturday");
break;
case 7:
System.out.println("Today is Sunday");
break;
default:
System.out.println("Looking forward to the Weekend");
}
● While Loop
The while loop loops through a block of code as long as a specified
condition is true:
E.x:-
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
● The Do/While Loop
The do/while loop is a variant of the while loop. This loop will
execute the code block once, before checking if the condition is true,
then it will repeat the loop as long as the condition is true.
E.x:-
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
E.x:-
for (int i = 0; i < 5; i++) {
System.out.println(i);}
● Nested Loops
E.x:-
// Outer loop
// Inner loop
E.x:-
if (i == 4) {
break;
System.out.println(i);
● Continue
The continue statement breaks one iteration (in the loop), if a
specified condition occurs, and continues with the next iteration in the
loop
E.x:-
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
System.out.println(i);
}
Assignment 🙂
1. A shop will give discount of 100 if the cost of purchased quantity is
more than 1000 , Imagine that one customer buy product the cost of
it is 100 $ and the quantity are 10 pcs
Judge and print total cost for the customer .