0% found this document useful (0 votes)
50 views21 pages

Neha Java

Uploaded by

jaydityashopy
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)
50 views21 pages

Neha Java

Uploaded by

jaydityashopy
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/ 21

ENGINEERING COLLEGE BIKANER

JAVA PRACTICAL FILE

DATE OF SUBMISSION: 09-10-2023

SUBMITTED TO: SUBMITTED BY:


MR. RAHUL AGGARWAL NAME: NEHA PAREEK
(Assistant Professor) BRANCH: CSE ‘B’
ROLLNo: 21EEBCS074
INDEX
1. Print Hello World.
2. Sum of two numbers.
3. Print Prime number series.
4. Check weather the given number is Even OR Odd.
5. Print Square pa ern using for Loop.
6. Use of all Operators.
7. Print Fibonacci Series
8. Write program to execute the Control Statements.
9. Find the K th odd number from the series provided
by user without Using array.
10. Without using array find the 2 nd largest number f
from the inputs Provided by user (end of inputs
should be with -1). *without using array
11. Output the number of dis nct elements from the
shorted Sequence provided by user (end of inputs
should be with -1)
12. Write a program of 2D and 3D array
13. Write a java code to explain single and mul ple
inheritance
14. Write a program of excep on handeling (try catch
throw)
15. Write a program of Method overriding
1. Print Hello World.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

OUTPUT:
Hello, World!
2. Sum of two numbers.

import java.util.Scanner;

public class SumOfTwoIntegers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first integer: ");
int firstNumber = scanner.nextInt();

System.out.print("Enter the second integer: ");


int secondNumber = scanner.nextInt();
int sum = firstNumber + secondNumber;

System.out.println("The sum of " + firstNumber + " and " +


secondNumber + " is " + sum);
}
}

OUTPUT:
Enter the first integer: 5
Enter the second integer: 10
The sum of 5 and 10 is 15
3. Print Prime number series.

public class PrimeNumberSeries {


public static void main(String[] args) {
int limit = 100;
System.out.println("Prime numbers up to " + limit + ":");
for (int num = 2; num <= limit; num++) {
boolean isPrime = true;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(num + " ");
}
}
}
}

OUTPUT:
Prime numbers up to 20 : 2 3 5 7 11 13 17
4. Check weather the given number is Even OR Odd.

import java.util.Scanner;

public class EvenOrOdd {


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

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


int number = scanner.nextInt();

if (isEven(number)) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}

public static boolean isEven(int number) {


return number % 2 == 0;
}
}

OUTPUT:
Enter a number: 17
number is odd
5. Print Square pattern using for Loop.

import java.util.Scanner;

public class SquarePattern {


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

System.out.print("Enter the size of the square: ");


int size = scanner.nextInt();

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


for (int j = 0; j < size; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}

OUTPUT:
Enter the size of the square: 5
*****
*****
*****
*****
*****
6. Use of all Operators.
public class OperatorExample {
public static void main(String[] args) {
int a = 10;
int b = 5;

int addition = a + b;
boolean greaterThan = (a > b);
boolean andOperator = (true && false);
int x = 10;
x += 5;
int counter = 5;
counter++;
int bitwiseAnd = a & b;
int max = (a > b) ? a : b;

System.out.println("Addition: " + addition);


System.out.println("Greater Than: " + greaterThan);
System.out.println("And Operator: " + andOperator);
System.out.println("Incremented Counter: " + counter);
System.out.println("Bitwise AND: " + bitwiseAnd);
System.out.println("Max: " + max);
}
}

OUTPUT:
Addition: 15
Greater Than: true
And Operator: false
Incremented Counter: 6
Bitwise AND: 0
Max: a
7. Print Fibonacci Series .
public class FibonacciSeries {
public static void main(String[] args) {
int numTerms = 10;
int prev = 0, current = 1;

System.out.print("Fibonacci Series: ");

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


System.out.print(prev + " ");
int next = prev + current;
prev = current;
current = next;
}
}
}

OUTPUT:
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
8. Write program to execute the Control Statements.

import java.util.Scanner;

public class ControlStatementsExample {


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

// If statement
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}

// Switch statement
System.out.print("Enter a day of the week (1-7): ");
int day = scanner.nextInt();
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;
default:
System.out.println("Invalid day");
}

// While loop
System.out.print("Enter a positive number: ");
int count = scanner.nextInt();
while (count > 0) {
System.out.println("Countdown: " + count);
count--;
}

// For loop
System.out.print("Enter a number for the multiplication table: ");
int tableNumber = scanner.nextInt();
System.out.println("Multiplication table for " + tableNumber + ":");
for (int i = 1; i <= 10; i++) {
System.out.print(tableNumber * i + “ “);
}
}
}

OUTPUT:
Enter the number: 4
The number is positive.
Enter a day of the week (1-7): 3
Wednesday
Enter a positive number: 4
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Enter a number for the multiplication table: 2
Multiplication table for 2 :
2 4 6 8 10 12 14 16 18 20
9. Find the K th odd number from the series provided by
user without Using array.

import java.util.Scanner;

public class KthOddNumberInUserSeries {


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

System.out.print("Enter the series of numbers (space-separated): ");


String input = scanner.nextLine();

System.out.print("Enter the value of K: ");


int k = scanner.nextInt();

int count = 0;
int kthOdd = -1;
int start = 0;

for (int i = 0; i < input.length(); i++) {


if (input.charAt(i) == ' ' || i == input.length() - 1) {
int number;
if (i == input.length() - 1) {
number = Integer.parseInt(input.substring(start));
} else {
number = Integer.parseInt(input.substring(start, i));
}

if (number % 2 != 0) {
count++;
if (count == k) {
kthOdd = number;
break;
}
}

start = i + 1;
}
}

if (kthOdd != -1) {
System.out.println("The " + k + "th odd number in the series is: " +
kthOdd);
} else {
System.out.println("There are less than " + k + " odd numbers in the
series.");
}
}
}

OUTPUT:
Enter the series of numbers (space-separated): 3 5 2 8 7 6 9
Enter the value of K: 3
The 3rd odd number in the series is: 7
10. Without using array find the 2 nd largest number
from the inputs Provided by user (end of inputs should be
with -1). *without using array.

import java.util.Scanner;

public class SecondLargestNumber {


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

System.out.println("Enter a series of numbers (-1 to end):");

int largest = Integer.MIN_VALUE;


int secondLargest = Integer.MIN_VALUE;
int input;

while (true) {
input = scanner.nextInt();

if (input == -1) {
break;
}
if (input > largest) {
secondLargest = largest;
largest = input;
} else if (input > secondLargest && input != largest) {
secondLargest = input;
}
}

if (secondLargest != Integer.MIN_VALUE) {
System.out.println("The second largest number is: " + secondLargest);
} else {
System.out.println("There is no second largest number.");
}
}
}

OUTPUT:
Enter a series of numbers (-1 to end): 7 4 9 2 6 -1
The second largest number is: 7
11. Output the number of distinct elements from the
shorted Sequence provided by user (end of inputs should
be with -1).

import java.util.Scanner;
public class CountDistinctElements {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a sorted sequence of numbers (-1 to end):");

int prev = Integer.MIN_VALUE;


int distinctCount = 0;
while (true) {
int input = scanner.nextInt();

if (input == -1) {
break;
}

if (input != prev) {
distinctCount++;
}

prev = input;
}
System.out.println("The number of distinct elements is: " +
distinctCount);
}
}

OUTPUT:
Enter a sorted sequence of numbers (-1 to end): 2 2 3 5 5 5 7 7 7 7 7 –1
The number of distinct elements is: 4
12. Write a program of 2D and 3D array .

// 2D ARRAY
public class TwoDArrayExample {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int element = matrix[1][2];


System.out.println("Element at (1, 2): " + element);
}
}

// 3D ARRAY
public class ThreeDArrayExample {
public static void main(String[] args) {
int[][][] cube = {
{
{1, 2, 3},
{4, 5, 6}
},
{
{7, 8, 9},
{10, 11, 12}
}
};

int element = cube[1][0][2];


System.out.println("Element at (1, 0, 2): " + element);
}
}

OUTPUT:
Element at (1, 2): 6
Element at (1, 0, 2): 9
13. Write a java code to explain single and mulƟple
inheritance .

// single inheritance
class Parent {
void display() {
System.out.println("This is the parent class.");
}
}

class Child extends Parent {


void show() {
System.out.println("This is the child class.");
}
}

public class SingleInheritanceExample {


public static void main(String[] args) {
Child child = new Child();
child.display(); // Inherited from Parent
child.show(); // Defined in Child
}
}

// multiple inheritance (Using Interfaces)


interface A {
void methodA();
}
interface B {
void methodB();
}
class MyClass implements A, B {
public void methodA() {
System.out.println("Method A implementation.");
}

public void methodB() {


System.out.println("Method B implementation.");
}
}

public class MultipleInheritanceExample {


public static void main(String[] args) {
MyClass obj = new MyClass();
obj.methodA(); // Implemented from interface A
obj.methodB(); // Implemented from interface B
}
}

OUTPUT:
This is the parent class.
This is the child class.
Method A implementation.
Method B implementation.
14. Write a program of exception handeling (try catch
throw) .

public class ArithmeticExceptionExample {


public static void main(String[] args) {
try {
int result = 10 / 0; // Attempt to divide by zero
} catch (ArithmeticException ex) {
System.out.println("An ArithmeticException occurred: " +
ex.getMessage());
}
}
}

OUTPUT:
An ArithmeticException occurred : java.lang.ArithmeticException: / by
zero
15. Write a program of Method overriding .
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


@Override
void makeSound() {
System.out.println("Dog barks");
}
}

public class MethodOverridingExample {


public static void main(String[] args) {
Animal animal = new Animal();
Animal dog = new Dog();
animal.makeSound();
dog.makeSound();
}
}

OUTPUT:
Animal makes a sound
Dog barks

You might also like