Java Internal Lab
Java Internal Lab
class Celebrity {
// Constructor
this.name = name;
this.profession = profession;
this.achievement = achievement;
System.out.println(name + " is a famous " + profession + " known for " + achievement + ".");
// Displaying information
footballCelebrity.displayInfo();
actorCelebrity.displayInfo();
// Closing Scanner
scanner.close();
OUTPUTS:
Ronaldo
Salman khan
Ronaldo is a famous football player known for goals.
Command-Line Arguments:
import java.util.Scanner;
// Displaying information
System.out.println(footballPlayer + " and " + actor + " are most famous in their respective fields.");
// Closing Scanner
scanner.close();
}
OUTPUT:
Enter the name of the most famous football player:
Messi
Amir
Output:
Value of num: 50
Constant:
Output:
System.out.println("Elements of array:");
Output:
Elements of array:
10 20 30 40 50
Strings:
Output:
Vectors:
import java.util.Vector;
public class VectorsExample {
names.add("Harsh");
names.add("chirag");
names.add("Jay");
System.out.println("Names in vector:");
System.out.println(name);
Output:
Names in vector:
Harsh
chirag
Jay
Wrapper Classes:
Output:
Character Value: A
Typecasting:
Output:
After typecasting: 10
Q3. Write a separate Java Code to implement each of the following operators:
Arithmetic operator, Relational operator, Logical operator, Assignment
operator, Increment& Decrement operator, Conditional operator, Bitwise
operator ?: Operator.
Arithmetic operator
public class ArithmeticOperatorExample {
public static void main(String[] args) {
int a = 25;
int b = 15;
Output:
Sum: 40
Difference: 10
Product: 375
Quotient: 1
Remainder: 10
Relational operator:
public class RelationalOperatorExample {
public static void main(String[] args) {
int a = 25;
int b = 15
Output:
a > b true
a < b false
a >= b true
a <= b false
a == b false
a != b true
Logical operator:
public class LogicalOperatorExample {
boolean a = true;
boolean b = false;
Output:
a AND b: false
a OR b: true
NOT a: false
NOT b: true
Assignment operator
public class AssignmentOperatorExample {
int a = 25;
int b = 15;
a += b;
a -= b;
a *= b;
a %= b;
Output:
a: 25
a += b: 40
a -= b: 25
a *= b: 375
a /= b: 25
a %= b: 10
int x = 20;
x++; // Increment
}
Output:
Initial value of x: 20
Conditional operator
public class ConditionalOperatorExample {
int a = 20;
int b = 15;
Output:
Maximum value: 20
Bitwise operator
public class BitwiseOperatorExample {
Output:
a & b: 9
a | b: 31
a ^ b 22
~a: -26
a << 1: 50
a >> 1: 12
Ternary Operator:
public class ConditionalOperatorExample {
int a = 25;
int b = 15;
}
}
Output:
Maximum value: 25
Q4. Write a separate Java code to implement each of the following control
statements.
If-Else Statement:
public class IfElseExample {
if (number % 2 == 0) {
} else {
Output:
10 is even.
Switch Statement:
public class SwitchExample {
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName = "Wednesday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
case 7:
dayName = "Saturday";
break;
default:
}
System.out.println("Day is: " + dayName);
Output
Loops statement:
For loop:
public class ForLoopExample {
Output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Do-while Loop:
public class DoWhileLoopExample {
int i = 1;
do {
i++;
Output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Break statements
public class BreakStatementExample {
if (i == 5) {
break;
Output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Continue Statement:
public class ContinueStatementExample {
if (i == 3) {
continue;
Output:
Iteration 1
Iteration 2
Iteration 4
Iteration 5
Q5. Write a separate Java Code to implement each of the following sorting:
Bubble Sort:
public class BubbleSortExample {
printArray(arr);
bubbleSort(arr);
printArray(arr);
int n = arr.length;
arr[j + 1] = temp;
System.out.println();
Output:
64 34 25 12 22 11 90
11 12 22 25 34 64 90
Selection Sort:
public class SelectionSortExample {
printArray(arr);
selectionSort(arr);
printArray(arr);
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
minIndex = j;
arr[minIndex] = arr[i];
arr[i] = temp;
System.out.println();
Output:
64 25 12 22 11
Insertion Sort:
public class InsertionSortExample {
printArray(arr);
insertionSort(arr);
printArray(arr);
int n = arr.length;
int j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
System.out.println();
Output:
64 25 12 22 11
11 12 22 25 64
Merge sort:
public class MergeSortExample {
printArray(arr);
mergeSort(arr, 0, arr.length - 1);
printArray(arr);
public static void merge(int[] arr, int left, int mid, int right) {
int i = 0, j = 0;
int k = left;
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
k++;
arr[k] = leftArr[i];
i++;
k++;
j++;
k++;
System.out.println();
Output:
38 27 43 3 9 82 10
3 9 10 27 38 43 82
Class, Object:
ronaldo.displayInfo();
messi.displayInfo();
String name;
int goalsScored;
// Constructor
this.name = name;
this.goalsScored = goalsScored;
OUTPUTS:
Player: Ronaldo
Player: Messi
Constructors Method
public class MyClass {
// Constructor
public MyClass() {
value = 90;
return value;
OUTPUTS:
The value is 90
Method Overloading:
class Calculator { public static void main(String[] args) {
Calculator calc = new Calculator();
return a + b;
return a + b + c;
OUTPUTS:
class Bank {
int getRateOfInterest() {
Method Overriding:
class Player {
void speak() {
System.out.println("I am a player.");
ronaldo.speak();
salman.speak();
//@Override
void speak() {
System.out.println("I am a footballer.");
//@Override
void speak() {
OUTPUTS:
I am a footballer.
I am a film star.
Final Variable
public class FinalVariableExample {
OUTPUT:
Final Class
final class FinalClassExample {
Final method
public class Parent {
}*/
Abstract class
abstract class OlympicGames {
void participatingCountries() {
void hostCountry() {
tokyoOlympics.hostCountry();
tokyoOlympics.participatingCountries();
OUTPUT:
Host country: Japan
Abstract method
abstract class Sports {
void typeOfGame() {
basketball.typeOfGame();
OUTPUT:
Concrete Method:
abstract class Sport {
void players() {
void typeOfGame() {
football.typeOfGame();
football.players();
OUTPUT:
Number of players: 11
Q8. . Write a separate Java Code to implement each of the following OOPs
concepts:
Abstraction:
abstract class OlympicGame {
void hostCountry() {
void participatingCountries() {
olympics.hostCountry();
olympics.participatingCountries();
OUTPUT:
Polymorphism
class OlympicGame {
void display() {
System.out.println("This is an Olympic game.");
void display() {
void display() {
summer.display();
winter.display();
OUTPUT:
This is a Summer Olympic game.
Encapsulation
class Olympic {
return hostCountry;
this.hostCountry = hostCountry;
return participatingCountries;
this.participatingCountries = participatingCountries;
tokyo2020.setHostCountry("Japan");
tokyo2020.setParticipatingCountries(200);
OUTPUT:
Inheritance
class Olympic {
void display() {
void hostCountry() {
void participatingCountries() {
System.out.println("Number of participating countries: 200");
tokyoOlympics.display();
tokyoOlympics.hostCountry();
tokyoOlympics.participatingCountries();
OUTPUT:
Exception handling with Try, Catch, Throw, Throws, and Finally Multiple catch
statement with the following exceptions: Arithmetic Exception, Array Out Of
Bounds Exception and Array Store Exception
class ExceptionHandlingExample {
try {
} catch (ArithmeticException e) {
System.out.println("ArithmeticException caught: " + e.getMessage());
} catch (ArrayIndexOutOfBoundsException e) {
} catch (ArrayStoreException e) {
} finally {
System.out.println("Finally ");
// Cause ArithmeticException
try {
System.out.println(arr[5]); // ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
} finally {
System.out.println("Finally ");
// Cause ArrayStoreException
try {
} catch (ArrayStoreException e) {
System.out.println("Finally:");
return a / b;
OUTPUT:
Finally
Finally
Finally
a) Interface
interface OlympicGames {
}
class TokyoOlympics implements OlympicGames {
// Interface example
olympics.hostedBy("Japan");
olympics.numberOfCountries(206);
OUTPUT: