0% found this document useful (0 votes)
31 views14 pages

Umer Irfan Roll No: 93: Public Class Public Static Void

Uploaded by

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

Umer Irfan Roll No: 93: Public Class Public Static Void

Uploaded by

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

Umer Irfan

Roll no: 93

public class Main {


public static void main(String[] args) {
System.out.println("UMER IRFAN");
}
}

If else
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);

System.out.print("Enter a number: ");


int num = scanner.nextInt();

if(num % 2 == 0) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
}

scanner.close();
}
}

If else if
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);

System.out.print("Enter your score: ");


int score = scanner.nextInt();

char grade;

if(score >= 90) {


grade = 'A';
} else if(score >= 80) {
grade = 'B';
} else if(score >= 70) {
grade = 'C';
} else if(score >= 60) {
grade = 'D';
} else {
grade = 'F';
}

System.out.println("Your grade is: " + grade);

scanner.close();
}
}

Switch statement

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);
System.out.print("Enter the day number (1 for Monday, 2 for Tuesday, etc.): "); int dayNumber

String day;

switch (dayNumber) { case 1:


day = "Monday"; break;
case 2:
day = "Tuesday"; break;
case 3:
day = "Wednesday"; break;
case 4:
day = "Thursday"; break;
case 5:
day = "Friday"; break;
case 6:
day = "Saturday"; break;
case 7:
day = "Sunday"; break;
default:
day = "Invalid day number";
}
System.out.println("Day of the week: " + day); scanner.close();

}
}
Ǫuestion 3: Loop
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}

While loop
public class Main {
public static void main(String[] args) { int i = 1;
while (i <= 10) { System.out.println(i); i++;
}
}
}
Do while loop
public class Main {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 10);
}
}

Nested loop
public class Main {
public static void main(String[] args) {
int rows = 5;
int columns = 5;

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

for (int j = 1; j <= columns; j++)


{ System.out.print("* ");
}

System.out.println();
}
}
}
Ǫuestion 4: Arrays
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};

System.out.println("Elements of the array:");


for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}

Max and Min number in array


public class Main {
public static void main(String[] args) {
int[] numbers = {5, 10, 3, 8, 15};

int max = numbers[0];


for (int i = 1; i < numbers.length; i++)
{ if (numbers[i] > max) {
max = numbers[i];
}
}
System.out.println("The maximum number in the array is: " + max);
}
}

public class Main {


public static void main(String[] args) {
int[] numbers = {5, 10, 3, 8, 15};

int min = numbers[0];


for (int i = 1; i < numbers.length; i++)
{ if (numbers[i] < min) {
min = numbers[i];
}
}

System.out.println("The minimum number in the array is: " + min);


}
}

Ǫuestion 5: Overloading
public class Main {
public int add(int a, int b) {
return a + b;
}

public int add(int a, int b, int c) {


return a + b + c;
}

public double add(double a, double b) {


return a + b;
}

public static void main(String[] args) {


Main example = new Main();

System.out.println("Sum of 5 and 10: " + example.add(5, 10));


System.out.println("Sum of 5, 10, and 15: " + example.add(5, 10, 15));
System.out.println("Sum of 3.5 and 2.5: " + example.add(3.5, 2.5));

}
Question 1:- Examples Of Each Type of Function Overloading.
Overloading By Num Of Parameters

Question 2:- Examples Of Each Type of Constructors.


Default Constructor:

TypeCastings
Program 1:- Windening Casting automatic
Program 2:- Narrowing Casting Self

Program 3:- Final Keyword In Java.

Program 5:- String Concatenation.


public class index1{ public static void main(String[]
args){ String firstname = "Adnan"; String lastname =
"Asghar";
Program 6:- Math Functions In Java.

Program 7:- Boolean DataType In Java .

Program 8:- Another Example Of Boolean.


Program 9:- Void Method

Program 10:- References

Program 11:- Overloading By Different types of


Parameters.

Program 12:- Assigning Value to a member variable by


using object.
Program 13:- Example Of Default Constructor.

Program 14:- Example Of Parameterized Constructor

Program 15:-
class Numb {
int a;
double b;
boolean c;
String d;
String f;
}
public class Umer {
public static void main(String[] args) {
Numb practice = new Numb();
practice.a = 123;
practice.b = 3.14;
practice.c = false;
practice.d = "Umer";
practice.f = "Hello World";
System.out.println("Assign Value of int data type:" +practice.a);
System.out.println("Assign Value of double data type:" +practice.b);
System.out.println("Assign Value of Boolean data type:"+practice.c);
System.out.println("Assign Value of String data type:" +practice.d);
System.out.println("Length of Hello World is :" +
practice.f.length());
System.out.println("Uppercase of my name:" +
practice.d.toUpperCase());
System.out.println("Lowercase of my name:" +
practice.d.toLowerCase());
}
}

Program Encapsulation
class Encapsulation {
private int data;
public int getData() {
return data;
}
public void setData(int newData) {
if(newData >= 0) {
data = newData;
} else {
System.out.println("Invalid data! Value must be non- negative.");
}
}
}
public class Mian{
public static void main(String[] args) {
Encapsulation obj= new Encapsulation();
obj.setData(-50);
System.out.println("Data: " + obj.getData());
}
}

You might also like