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 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();
int x [] = {10,20,30};
for(int i=0;i<x.length;i++){
System.out.println(x[i]);
.println(x[i]);
for(int s : x) {
System.out.println(s);
.println(s);
https://youtu.be/_2n7Q4uKAEM?si=TZOtsLP75pktxEzh
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;
char grade;
System.out.println("Enter Your Grade");
grade = scan.next().charAt(0);
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
}}
instead of make this : char grade = scan.nextchar();
char grade = scan.nextchar();
make this
……….
the diffrence between ‘‘ and ““
Single quotes =….. , Double quotes = ……
character
string
EX
case “eyad”:
System.out.println("Monday");
.println("Monday");
………………..
case ‘A‘:
double Y; // Y is …. but not …..
double Y = 0; // Y is … and ……
declared
initialized
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
int x = 5;
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");
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);
Note
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;
invoke=….
Call or mention
➤ …… is a style of programming, a way of thinking about software construction.
A programming paradigm
ex for procedural (a type in Paradigms)
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
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);
myBook.displayBookInfo(" OOP C++", "yasser", "9780446310789");
public Book(String title, String author, String isbn)
public void
displayBookInfo(String title, String author, String isbn)
// Instance variables (attributes) of theBook
Title: OOP C++
Author: yasser
Title: null
ISBN: null
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
// 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
@Override // we can delete it
@Override
// we can delete it
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();
Last changed2 days ago