This if you still don't know the values
double[] the_salaries = …… double[5];
Ex
the_salaries[0] = 50000.0; // Assigning a value to the first element
the_salaries[1] = 60000.0; // Assigning a value to the second element
this if you know the values
double[] numbers = {10, 20, 30, 40, 50};
new
what will be the output of this code
public class main {
static int day;
static int
day
;
public static void main(String[] args) {
public static void main(String[] args)
{
int a=10 ,b= 20;
int
a=10 ,b= 20;
char x='A', y='a';
char
x='A', y='a';
System.out.println(larger(a, b));
System.
out
.println(
larger
(a, b));
System.out.println(larger(x,y));
(x,y));
}
public static int larger (int n1 ,int n2)
if ( n1 > n2 ) return n1 ;
else return n2;
public static char larger (char c1, char c2)
if (c1>c2) return c1;
else return c2;
20
a
Explnation
In this code, you have two larger() methods:
larger()
One that takes two integers and returns the larger integer
Another that takes two characters and returns the lexicographically larger character
When you call larger(), Java determines which method to invoke based on the argument types:
For larger(10, 20), it uses the integer method (Function)
larger(10, 20)
For larger('A', 'a'), it uses the character method (Function)
larger('A', 'a')
The compiler determines which method to call based on the arguments passed
NOTES
https://youtu.be/n5pcp_WOMWY?si=wFL3cvDCF2HnrVhg
what is the logical error here and corect it?
Scanner scan = new Scanner (System.in);
Scanner scan = new Scanner (System.
in
);
double [] sal = new double [5];
int i = 0;
for ( i=0;i<sal.length;i++)
sal[i]= scan.nextDouble();
System.out.println(sal[i]);
.println(sal[i]);
the error making this System.out.println(sal[i]);
corrected one
System.out.println(sal[i-1]);
.println(sal[i-1]);
what will be the output of in this code
Scanner scan = new Scanner(System.in);
int i;
double Y=0;
for (i=0; i<5;i++)
System.out.println("Enter Salary For Employee No" + (i+1));
Y= scan.nextDouble();
System.out.println(Y);
if user enter in first case 2 and second case 3 and third case 4 and fourth case7 and fifth case 9
will be 9 because in this specific code, there's a logical issue: the Y variable will only store the last employee's salary, not all salaries. If you want to store multiple salaries, you should use an array instead of a single variable.
The corrected version:
double[] salaries = new double[5];
double[
] salaries = new double[5];
for (int i=0; i<5; i++) {
salaries[i] = scan.nextDouble();
byte
short
……
EX
short x = 10;
x = x * 5;
System.out.print(x); }
x * 5
x
Note
When converting from a larger numeric type to a smaller one you must cast manual:
Long x = 5;
int y = x;
System.out.println(y);// Must cast to prevent data loss
When converting from a smaller numeric type to a larger one casting done automaticly:
int x = 5;
Long y = x;
System.out.println(y); // Automatic widening conversion
System.out.println(y);
// Automatic widening conversion
int y = (int) x;
integer
instead of make this : char grade = scan.nextchar();
char grade = scan.nextchar();
make this
……….
char grade;
grade = scan.next().charAt(0);
make a grade classification program ,It takes a single character input (representing a grade, e.g., A, B, C, etc.), processes the input using a switch statement, and outputs a corresponding message describing the grade's quality.
import java.util.Scanner;
System.out.println("Enter Your Grade");
switch (grade)
case 'A' :
System.out.println("Excellent");
break;
case 'B':
System.out.println("Very Good");
case 'C':
System.out.println("Good");
case 'D':
case 'E':
System.out.println("Fair");
default : System.out.println("Invalid one, pleasechooseanother one");
//case E is false we add it only for learn the concept
}}
Make a program say if the number is even or odd
Note : why we can not just type = instead of == in this code
Ans
Using = in a conditional statement like if (n % 2 = 0) is not valid, as it will always evaluate to true because the expression n % 2 = 0 is an assignment that assigns the value 0 to n % 2, which is then interpreted as a boolean value of true.
Instead, you should use the comparison operator == to check if the remainder of n divided by 2 is equal to 0 (for even numbers) or 1 (for odd numbers).
Remember (very important)
we can use {} in if to define the code block that should be executed for each condition and make it more readable
if (x > 0) {
if (x > 0)
System.out.println("Positive");
x = x - 1; }
x = x - 1;
System.out.print(x);
the output is
Positive
4
Attempting to use a ….. statement with a double or float will result in a compile-time error.
switch
// This will cause a COMPILE-TIME ERROR
double price = 10.5;
switch(price){
switch(price)
// Cannot use double in switch
case 10.0:
System.out.println("Exact match");
int x [] = {10,20,30};
for(int i=0;i<x.length;i++){
System.out.println(x[i]);i
.println(x[i]);i
//it will do it 3 times automatic
for(int s : x) {
System.out.println(s);
.println(s);
https://youtu.be/_2n7Q4uKAEM?si=TZOtsLP75pktxEzh
the diffrence between ‘‘ and ““
Single quotes =….. , Double quotes = ……
character
string
case “eyad”:
System.out.println("Monday");
.println("Monday");
………………..
case ‘A‘:
double Y; // Y is …. but not …..
double Y = 0; // Y is … and ……
declared
initialized
correct the following java code
char grade = scan.next().charAt(0);
If the user enters multiple characters like "ABC", next().charAt(0) will only take the first character 'A'
If the user enters multiple characters like "ABC", next().charAt(1) will only take the first character 'B'
Dividing a double by a double gives a ….
System.out.println(1.0 / 2.0); // 0.5
2. Dividing a float by a float gives a
System.out.println(1.0f / 2.0f); // 0.5
double
float
type the image
System.out.println("Work days Are \n Sunday \t Monday \t Tuesday \t Wendsday \t");
@\t is tape
Another example
int x = 1;
System.out.println("Hello" + "\t" + x);
//Hello 1
for (int i=0; i<10;i++)
System.out.print("E");
.print("E");
System.out.println("E"+ i);
.print
ln
("E"+ i);
for(int x=1;x<=10;++x);
for(int x=1
x<=10
++x);
in prevous sentence its ; not ,
EEEEEEEEEE
E0
E1
E2
E3
E4
E5
E6
E7
E8
E9
the output of the next two images
Hello1
Hello2
Hello3
Hello4
Hello5
what is the 17&5?
2 (the reminder is 2)
First find how many times 5 goes into 17 evenly:
17 ÷ 5 = 3.4
We take only the whole number part: 3
Then multiply that whole number (3) by the divisor (5):
3 × 5 = 15
Finally, subtract that result from the original number to get the remainder:
17 - 15 = 2
This 2 is your remainder/modulus
Remember
%(Modulus) Returns the division remainder
THE DECREMENT OPERATOR is like INCREMENT but Used to decrease the value of a variable by ….
decrement operator: --
THE DECREMENT OPERATOR is like INCREMENT but Used to decrease the value of a variable by 1
tell me the output of this code
System.out.println("The sum is " + (1 + 3) );
System.out.println("The sum is + 1 + 3 );
The sum is 4
The sum is 13
++i increments the value of i by 1 and uses the new value in the statement
i++ increments the value of i by 1 and uses the original value in the statement
Test Your self
In the two images
The answer 👆👆
int i1 = 2;
i1 *= 5 + 1;
i1
*=
5 + 1;
→ Equivalent to:
….
→ Not to:
i1 = il* (5 + 1) // 12
i1 = il * 5 + 1 // 11
Logical AND operator: &&
Logical OR operator: ||
Logical NOT operator: !
Test you self
the x in the image is true or false?
True
int a=200, b=33, c=500;
if (a>b && c>a)
System.out.println("C is the greatest number");
corect the following sentence int x = 12383402047527075;
long x = 12383402047527075L; we choose it because it from -9223372036854775808 to 9223372036854775807 but int from -2147483648 to 2147483647
Intl
make a program calculate the Area of a Circle
System.out.println("What is the redius");
//this is extension that we added that scan and we make it scan the keyboard which what will user type
double redius = scan.nextDouble();
if (redius>0) {
System.out.println("Area of square is"+ 3.14*redius*redius); }
OR
System.out.println("Area of square is"+ 3.14* Math.pow(radius, 2));
type 3.14 with to ways with initialization
float X = 3.14f;
System .out.println(X);
double y = 3.14;
// it's without f
🟥🟥
fractional number up to 6-7 digits
ex. 3.141592f
fractional number up to 15 digits
ex. 3.141592653589793
boolean is either ....or....
true or false EX double y = 3.14; boolean z = true; System.out.println(z); the console will be : true
what is the difference in
int x = 123;
System.out.println(x);
AND
System.out.println("x");
first output will be x
the second outpot will be 123
tell me each one in the image
source code
compiler
byte code
jvm (which it subset from jdk which is Java Development Kit
machine code
{}
()
/ (its a division)
curly braces
parentheses
forward slash
what will be the output
int count = 1;
while (count<=5) { //[
System.out.println("Hi");
++count;
Hi
Make the output in 3 ways
eyad
yasser
System.out.println("eyad"); System.out.println("yasser");
System.out.print("eyad\n"); System.out.print("yasser\n");
System.out.println("eyad\nyasser");
string is alot of....
char (@is line of words)
tell me each one
int x = 123; is called ........
make this my number is : 123 with initialization
initialization
System.out.println("my number is " + x);
A …..: is a self-contained block of statements that perform a specific task.
Functions - Methods
Correct the follwing code
System.out.println("eyad");
.println("eyad");
System.out.print( 5+sum(2,10)) ;
.print( 5+
sum
(2,10)) ;
////////////////
public static void sum(int x1, int x2)
public static void
(int x1, int x2)
int result=0;
result= x1+x2;
drawline('*' ,10);
drawline
(
'*' ,10
System.out.println("yasser");
.println("yasser");
drawline('E',30);
'E',30
public static void drawline(char line, int length)
public static void drawline(
char line, int length
)
for (int i=0; i<length;i++)
for (int i=0; i<
length
;i++)
System.out.print(line);
.print(
line
System.out.println();
.println();
public static int sum (int x1, int x2)
public static
sum (int x1, int x2)
return result;
the output of the prevous code is
12
**********
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
drawline();
();
public static void drawline()
"E"
where is the error in this code
int sum =10;
System.out.println(sum);
.println(sum);
what will be the output?
public class main
static int sum=10;
static
=10;
// called Global variable
int sum =12;
public class
main
System.out.println(main.sum);
main.sum
// this is called block
The error in your code arises because the variable sum is declared inside the block of the curly braces {} and is therefore out of scope when you attempt to access it in the System.out.println(sum) statement. Variables declared inside a block are only accessible within that block
System.out.println(sum)
If the line int sum = 12; were removed, the output would be 10, because the System.out.println(sum); would then refer to the class-level static sum.
int sum = 12;
10
static sum
put the name of class before the name of variable to print global variable not the local variable
// Creating an array
int[] numbers = {10, 20, 30, 40, 50};
int[]
numbers
= {10, 20, 30, 40, 50};
// Passing array to function
int totalSum = calculateSum(numbers);
int totalSum =
calculateSum
// Printing result
System.out.println("Total Sum: " + totalSum);
.println("Total Sum: " + totalSum);
// Method to calculate sum of array elements
public static int calculateSum(int[] arr) {
public static int calculateSum(
int[] arr
) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
return sum;
What will be the output of this code
System.out.print("\b\b = ");
• Classes can have special methods(functions) called constructors.
• A constructor is a method that is ….. called when an object is created.
Constructors are used to perform operations at the time an object is …..
• Constructors typically initialize instance fields and perform other object initialization tasks.
automatically
created
invoke=….
Call or mention
Remember this
class
object
attrbute
Scanner is class (the data type of the object ) and we take from it object to can adjust it and we name it scan
we type new to make place in ram for it
➤ …… is a style of programming, a way of thinking about software construction.
A programming paradigm
ex for procedural (a type in Paradigms)
package eyad;
Book myBook = new Book();
myBook.displayBookInfo(" OOP java", "eyad", "9780446310789");
System.out.println("-------------------------");
Book myBook2 = new Book();
myBook2.title="OOP java";
myBook2.author="eyad";
myBook2.isbn="9780446310789";
myBook2.text();
public class Book {
String title;
String author;
String isbn;
public void displayBookInfo(String title, String author, String isbn) {
System.out.println("Book Details:");
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("ISBN: " + isbn);
public void text () {
System.out.println("logo: " + title);
System.out.println("name: " + author);
System.out.println("id: " + isbn);
Book Details:
Title: OOP java
Author: eyad
ISBN: 9780446310789
-------------------------
logo: OOP java
name: eyad
id: 9780446310789
——————————————————————————————————————
Book myBook = new Book(" OOP java", "eyad", "9780446310789");
myBook.author="eyad";
myBook.displayBookInfo();
// Instance variables (attributes) of the Book
String
title
author
isbn
// Constructor to initialize the Book object
public Book(String title, String author, String isbn) {
public Book(String
, String
this.title = title;
this.title
=
this.author = author;
this.author
this.isbn = isbn;
this.isbn
// Method to display book information
public void displayBookInfo() {
.println("Book Details:");
.println("Title: " + title);
.println("Author: " + author);
.println("ISBN: " + isbn);
why there is an error in title?
myBook2.
="OOP java";
private String title = "oop c++";
private
String title = "oop c++";
public String author;
.println("logo: " + title);
.println("name: " + author);
make set function to adjust (set) the title to become oop java
make get function to get the name of the title
Bec. title attribute is private
Visibility: The private modifier restricts the access to the member (e.g., a variable or method) to within the same class only.
Visibility: The public modifier allows the member to be accessible from any other class in the program, provided the class itself is accessible.
public
public void set_title(String title ) {
this.title=title;
public void get_title() {
System.out.println(title) ;
.println(title) ;
myBook2.set_title("oop java");
myBook2.get_title() ;
oop java
logo: oop java
1. Belongs to the Class, Not to Objects
- The method belongs to the class itself, not to any specific instance (object) of the class
- You can call the method directly using the class name, without creating an object
2. Can be Called Without Object Creation
public class MathOperations {
// Static method
public static int sum(int x1, int x2) {
return x1 + x2;
// Can call directly using class name
int result = MathOperations.sum(5, 3);
System.out.println(result); // Prints 8
students student1 = new students("eyad", 15, "10th"); // All info at once!
student1.setname(“salem“);
student1.
setname
(“salem“);
student1.printname(); // will print salem yasser instead of eyad yasser
student1.printname(); //
will print salem yasser instead of eyad yasser
System.out.println(student1.getname());
student1.getname()
students student2 = new students("ziad", 18, "12th"); // All info at once!
student2.printname();
System.out.println(student2.getname());
student2.getname()
premium_students zeko=new premium_students("ali", 13, "8th",011);
zeko.printname();
zeko.getname();
zeko.printage(); // override age application!
public class students {
// Using constructor:
private String name;
private String
name
private int age;
private int
age
private String grade;
grade
public students(String name, int age, String grade) {
public students(String
, int
this.name = name;
this.
this.age = age;
this.grade = grade;
public void setname(String name) {
public String getname() {
return name;
//function
public void printname() {
System.out.println(name +""+" yasser");
+""+" yasser");
public void printage() {
System.out.println(age +""+" years");
public class premium_students extends students {
public class premium_students
extends
students {
int phone;
public premium_students(String name, int age, String grade,int phone) {
super(name, age, grade);
//OR
// this.name=name;
// this.age=age;
// this.grade=grade;
this.phone=phone;
@Override // we can delete it
@Override
// we can delete it
System.out.println(name +""+"salem");
.println(name +""+"salem");
mohsen yasser
mohsen
ziad yasser
ziad
alisalem
ali
13 years
Identify the prototype of the default constructor.
Public class Solution {}
// Parent class
class Animal {
void eat() {
void eat
() {
System.out.println("This animal eats food.");
// Child class
class Dog extends Animal {
void bark() {
void bark()
System.out.println("The dog barks.");
// Using inheritance
public class Main {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal
dog.bark(); // Defined in Dog
class Person {
void speak() {
void
speak
System.out.println("A person speaks in general.");
class Boy extends Person {
class Boy extends Person
super.speak(); // Call the parent class method
System.out.println("The boy adds: 'Hey, I'm a boy!'");
class Girl extends Person {
class Girl extends Person
System.out.println("The girl adds: 'Hi, I'm a girl!'");
Girl girl1 = new Girl();
girl1.speak();
import java.util.ArrayList;
ArrayList<String>list= new ArrayList<String>();
list.add("Ahmed");
list.add("Mohamed");
list.add("Ali");
System.out.println(list.size());
.println(list.size());
System.out.println(list);
.println(list);
System.out.println(list.get(2));
.println(list.get(2));
list.add(1, "Saad");
list.remove(2);
3
[Ahmed, Mohamed, Ali]
Ali
[Ahmed, Saad, Mohamed, Ali]
[Ahmed, Saad, Ali]
ArrayList<int>list= new ArrayList<Int>();
ArrayList<
I
nteger>list= new ArrayList<
nteger>();
System.out.println("calculate Sum machine of salaries:");
.println("calculate Sum machine of salaries:");
System.out.println("please tell no. of employes");
.println("please tell no. of employes");
Employee manager = new Employee();
System.out.println("tell me the salaries");
.println("tell me the salaries");
System.out.println("Sum of salaries: " + manager.calculateSum());
.println("Sum of salaries: " + manager.calculateSum());
public class Employee {
int no_employes;
public Employee() {
Scanner scan = new Scanner(System.
int no_employes = scan.nextInt();
this.no_employes=no_employes;
no_employes
=no_employes;
public ArrayList<Double> scanSalaries(){
ArrayList<Double>list= new ArrayList<Double>();
for (int i =0;i<no_employes;i++) {
list.add(scan.nextDouble());
return list;
public double calculateSum() {
double sum = 0;
for (Double salary : scanSalaries()) {
sum += salary;
calculate Sum machine of salaries:
please tell no. of employes
2
tell me the salaries
Sum of salaries: 4.0
class Hello { public static void main (String[] args) {
int a = 1;
switch (a) {
case 1: System.out.println ("One");
case 2: System.out.println ("Two");
case 3: System.out.println ("Three");
default: System.out.println ("Default");
What will be the output if int x =2 instead of 1?
int x =2
1
One
Two
Three
Default
_________________
The switch statement in your code is missing break statements after each case. Without these break statements, the program will fall through and execute all subsequent case blocks, including the default block. Here's the behavior of the given code:
Output of the code:
Why this happens:
int a = 1; assigns the value 1 to a.
The switch statement evaluates a and matches it to case 1.
Since there’s no break after case 1, the program continues executing all subsequent case blocks and the default block.
Correcting the code:
If you want the program to stop executing further cases after the matched one, add break statements:
class Hello {
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
case 3:
System.out.println("Three");
default:
System.out.println("Default");
Corrected Output:
break is used to exit the switch statement after executing a specific case.
If you intentionally want multiple cases to execute, you can omit break, but this should be done with caution to avoid unintended behavior.
final
int a=10;
a++;
if (a%2==0) System.out.println ("even number");
else System.out.println ("odd number"); } }
The provided code will not compile due to an error in the following line:
Explanation:
1. final int a = 10;
The final keyword in Java makes the variable a a constant, meaning its value cannot be changed after initialization.
The line a++; attempts to increment the value of a, which is not allowed for a final variable. This results in a compilation error.
2. Compilation Error:
The compiler will throw an error similar to:
cannot assign a value to final variable 'a'
If you want the code to work, you must remove the final keyword to allow a to be modified. For example:
public static void main (String[] args) {
int a = 10;
if (a % 2 == 0)
System.out.println("even number");
else
System.out.println("odd number");
Output of the Corrected Code:
a starts at 10, increments to 11, and is then checked for evenness.
Since 11 % 2 != 0,
the output will be:
odd number
Zuletzt geändertvor 3 Tagen