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

Oop2 2

oop fundementals

Uploaded by

kokiiahmed2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Oop2 2

oop fundementals

Uploaded by

kokiiahmed2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Java (Fundamentals Review)

● 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 " ".

System.out.println("This sentence will work!");

● The Print() Method

There is also a print() method, which is similar to println().

The only difference is that it does not insert a new line at the end of
the output:

System.out.println("Hello World! ");


System.out.print("I will print ");
● Print Numbers

we don't put numbers inside double quotes

System.out.println(358);

You can also perform mathematical calculations inside the println()


method:

System.out.println(3 + 3);

● Java Variables
assign a new value to an existing variable, it will overwrite the
previous value:

int myNum = 15;


myNum = 20; // myNum is now 20

System.out.println(myNum);

● Java Data Types

int myNum = 5; // Integer (whole number)


float myFloatNum = 5.99f; // Floating point number
char myLetter = 'D'; // Character
boolean myBool = true; // Boolean

String myText = "Hello"; // String


● Java Type Casting

Type casting is when you assign a value of one primitive data type to
another type.

In Java, there are two types of casting:

● Widening Casting (automatically) - converting a smaller type to a


larger type size
byte -> short -> char -> int -> long -> float -> double

● Narrowing Casting (manually) - converting a larger type to a


smaller size type
double -> float -> long -> int -> char -> short -> byte

1. Widening Casting

public class Main {


public static void main(String[] args) {
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to
double

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

System.out.println(myDouble); // Outputs 9.78


System.out.println(myInt); // Outputs 9
}

}
● Java Operators

Operators are used to perform operations on variables and values.

Java divides the operators into the following groups:

● Arithmetic operators

int sum1 = 100 + 50; // 150 (100 + 50)

int sum2 = sum1 + 250; // 400 (150 + 250)

int sum3 = sum2 + sum2; // 800 (400 + 400)

Operator Name Description Example

+ Addition Adds together two values x+y

- Subtraction Subtracts one value from x-y


another

* Multiplication Multiplies two values x*y

/ Division Divides one value by another x/y

% Modulus Returns the division remainder x%y

++ Increment Increases the value of a ++x


variable by 1

-- Decrement Decreases the value of a --x


variable by 1
● Assignment operators

int x = 10;

x += 5;

Operator Example Same As

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

int y = 3;System.out.println(x > y); // returns true,


because 5 is higher than 3

Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


● Logical operators

int x = 5;

System.out.println(x > 3 && x < 10); // returns true


because 5 is greater than 3 AND 5 is less than 10

Operator Name Description Example

&& Logical Returns true if both x < 5 && x <


and statements are true 10

|| Logical Returns true if one of x < 5 || x < 4


or the statements is true

! Logical Reverse the result, !(x < 5 && x <


not returns false if the result 10)
is true

● Constants
The Syntax for write constant is like this

final int y=5;

Constant mean you can not change the value of varible

● String Length

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

System.out.println("The length of the txt string is: " +


txt.length());
● More String Methods
There are many string methods available, for example toUpperCase()
and toLowerCase():

String txt = "Hello World";


System.out.println(txt.toUpperCase()); // Outputs "HELLO
WORLD"

System.out.println(txt.toLowerCase()); // Outputs "hello


world"

● Finding a Character in a String

The indexOf() method returns the index (the position) of the first
occurrence of a specified text in a string (including whitespace):

String txt = "Please locate where 'locate' occurs!";

System.out.println(txt.indexOf("locate")); // Outputs 7

● Java Math(Random Numbers)


Math.random() returns a random number between 0.0 (inclusive),
and 1.0 (exclusive):

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:

int randomNum = (int)(Math.random() * 101); // 0 to 100


● If Statement
Use the if statement to specify a block of Java code to be executed if
a condition is true.

E.x:-
if (20 > 18) {
System.out.println("20 is greater than 18");

● The else Statement


Use the else statement to specify a block of code to be executed if the
condition is false.

E.x:-
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");

● The else if Statement

Use the else if statement to specify a new condition if the first


condition is false.

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

Instead of writing many if..else statements, you can use the


switch statement.

The switch statement selects one of many code blocks to be


executed:

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

The default keyword specifies some code to run if there is no case


match:

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

// Outputs "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);

● Java For Loop


When you know exactly how many times you want to loop through a
block of code, use the for loop instead of a while loop:

E.x:-
for (int i = 0; i < 5; i++) {
System.out.println(i);}

● Nested Loops

It is also possible to place a loop inside another loop. This is called a


nested loop.

E.x:-

// Outer loop

for (int i = 1; i <= 2; i++) {

System.out.println("Outer: " + i); // Executes 2 times

// Inner loop

for (int j = 1; j <= 3; j++) {

System.out.println(" Inner: " + j); // Executes 6 times


(2 * 3) } }
● Break
The break statement can be used to jump out of a loop.

E.x:-

for (int i = 0; i < 10; i++) {

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 .

2. Print multiplication table of 24 using loop

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy