KEMBAR78
Java GSJ Final | PDF | Constructor (Object Oriented Programming) | Programming
0% found this document useful (0 votes)
20 views37 pages

Java GSJ Final

This document is a practical file for Java programming submitted by Gaurav Singh Joon under the guidance of Dr. Ranjeeta Kaur. It includes a comprehensive index of programming tasks such as calculating Fibonacci series, checking for prime numbers, and implementing various Java concepts like inheritance and method overloading. Each task is accompanied by code examples and outputs demonstrating the functionality of the programs.
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)
20 views37 pages

Java GSJ Final

This document is a practical file for Java programming submitted by Gaurav Singh Joon under the guidance of Dr. Ranjeeta Kaur. It includes a comprehensive index of programming tasks such as calculating Fibonacci series, checking for prime numbers, and implementing various Java concepts like inheritance and method overloading. Each task is accompanied by code examples and outputs demonstrating the functionality of the programs.
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/ 37

Practical file

Of
“Java programming language”

Submitted by:
Gaurav Singh Joon

00996702022
Under the guidance of –
Dr. Ranjeeta kaur

Kamal Institute Of Higher Education and Advanced Technology


(Affiliated to Guru Gobind Singh Indraprstha University)
K-1 Extension, Mohan Garden,
New Delhi – 110059
INDEX
S. No. TITLE Sign.
Q1 WAP to print Java Program to Display Fibonacci Series.

Q2 WAP to calculate the sum of digits of any number.

Q3 WAP to store string in array.

Q4 WAP to swap two numbers without using a third variable

Q5 WAP in JAVA to Find prime numbers.

Q6 WAP in Java program to check palindrome number.

Q7 Write a program to print list of even numbers from 1 to 100.

Q8 WAP in Java to Check the given year is Leap Year or not.

Q9 WAP in Java to check if a given Number is Positive or Negative


using if else.
Q10 Write a program to Illustrate the use of for-each loop in Java.

Q11 WAP in java to Check Whether a Number is Even or Odd.

Q12 WAP in Java Program to Generate Multiplication Table.

Q13 WAP in in JAVA to create a half pyramid pattern using nested


loops.
Q14 Write a Java program to concatenate Two strings.

Q15 Write a Java program to find out Java String length.

Q16 WAP in JAVA to Count Number of Words in a String.


Q17 WAP in Java To Reverse a String.
Q18 WAP in Java to illustrate the use of Break and continue
statement.
Q19 WAP in Java to find largest and smallest number from the array.

Q20 WAP in JAVA for the Implementation of Abstract classes in JAVA.

Q21 WAP in JAVA for Implementing single level inheritance.

Q22. WAP in JAVA to Implement multiple inheritance.

Q23. WAP to illustrate 2 D array in Java .

Q24. WAP in JAVA Method Overloading.

Q25. WAP in java to find the sum of Natural Numbers using Recursion.

Q26. WAP to implement default constructor & parameterized


constructor.
Q27. WAP in Java to illustrate the use of threads in JAVA.

Q28. WAP in java to Create File and Write in the file.

Q29. WAP in Java to Read the Content of the File, Line by Line.

Q30. WAP in Java to Delete a file.


Question 1 : WAP to print Java Program to Display Fibonacci
Series.
Code:
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = scanner.nextInt();
int a = 0, b = 1;
System.out.print("Fibonacci Series up to " + n + " terms:
"); for (int i = 1; i <= n; ++i) {
System.out.print(a + " ");
int sum = a + b;
a = b;
b = sum;
}
}
}

Output :
Question 2 : WAP to calculate the sum of digits of any
number.
Coding:
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args)
{ Scanner scanner = new
Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int sum = calculateSumOfDigits(number);
System.out.println("Sum of digits of " + number + " is: " + sum);
scanner.close();
}
public static int calculateSumOfDigits(int number) {
int sum = 0;
// Loop through each digit of the number
while (number != 0) {
int digit = number % 10;
sum += digit;
number /= 10;
}
return sum;
}
}

Output:
Question 3 : WAP To Store String In
Array
Coding
import java.util.Scanner;
public class StringArray {
public static void main(String[] args)
{ Scanner scanner = new
Scanner(System.in);

// Get the number of strings to store


System.out.print("Enter the number of strings: ");
int n = scanner.nextInt();

// Create an array to store the strings


String[] strings = new String[n];

// Get strings from the user and store them in the


array for (int i = 0; i < n; i++) {
System.out.print("Enter string " + (i + 1) + ": ");
strings[i] = scanner.next();
}

// Print the strings stored in the array


System.out.println("Strings stored in the array:");
for (int i = 0; i < n; i++) {
System.out.println("String " + (i + 1) + ": " + strings[i]);
}

scanner.close();
}
}
Output:
Question 4 : WAP to swap two numbers without using a third
variable Coding:
import java.util.Scanner;

public class SwapNumbers {

public static void main(String[] args) {


Scanner scanner = new
Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
System.out.println("Before swapping: num1 = " + num1 + ", num2 = " +
num2);
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
System.out.println("After swapping: num1 = " + num1 + ", num2 = " +
num2);
}
}
Output:
Question 5 : WAP in JAVA to Find prime
numbers. Coding:
import java.util.Scanner;
public class PrimeNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = scanner.nextInt();
System.out.println("Prime numbers up to " + n + " are:");
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
}
public static boolean isPrime(int num)
{ if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
Output:
Question 6 : WAP in Java program to check palindrome
number. Coding:
import java.util.Scanner;

public class PalindromeNumber {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int originalNum = num;
int reversedNum = 0;
while (num != 0) {
int digit = num % 10;
reversedNum = reversedNum * 10 +
digit; num /= 10;
}
if (originalNum == reversedNum) {
System.out.println(originalNum + " is a palindrome number.");
} else {
System.out.println(originalNum + " is not a palindrome number.");
}
}
}

Output:
Question 7 Write a program to print list of even numbers from 1 to 100.
Coding:
public class EvenNumbers {
public static void main(String[] args) {
System.out.println("Even numbers from 1 to 100:");
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
System.out.print(i + " ");
}
}
}
}

Output:
Question 8: WAP in Java to Check the given year is Leap Year or
not. Coding:
import java.util.Scanner;

public class LeapYear {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
boolean isLeapYear = (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
if (isLeapYear) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}

Output:
Question 9: WAP in Java to check if a given Number is Positive or
Negative
using if else.
Coding:
import java.util.Scanner;

public class PositiveNegative {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (num > 0) {
System.out.println(num + " is positive.");
} else if (num < 0) {
System.out.println(num + " is negative.");
} else {
System.out.println("The number is zero.");
}
}
}

Output:
Question 10:Write a program to Illustrate the use of for-each loop in Java.
Coding:
public class ForEachLoopExample
{ public static void main(String[]
args) {
int[] numbers = {1, 2, 3, 4, 5};
System.out.print("Numbers in the array: ");
for (int number : numbers) {
System.out.print(number + " ");
}
}
}

Output:
Question 11: WAP in java to Check Whether a Number is Even or
Odd. Coding:
import java.util.Scanner;

public class EvenOdd {


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.");
}
}
}

Output:
Question 12: WAP in Java Program to Generate Multiplication
Table. Coding:
import java.util.Scanner;

public class MultiplicationTable {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
System.out.println("Multiplication table of " + num + ":");
for (int i = 1; i <= 10; i++) {
System.out.println(num + " x " + i + " = " + (num * i));
}
}
}

Output:
Question 13: WAP in in JAVA to create a half pyramid pattern
using loops.
nested
Coding:
public class HalfPyramid {
public static void main(String[] args)
{ int rows = 5;
for (int i = 1; i <= rows; i++)
{ for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}

Output:
Question 14 : Write a Java program to concatenate Two
strings.
Coding:
public class StringConcatenation {
public static void main(String[] args)
{
String str1 = "Hello";
String str2 = "World";
String concatenatedStr = str1 + " " + str2;
System.out.println("Concatenated string: " + concatenatedStr);
}
}

Output:
Question 15: Write a Java program to find out Java String length.
Coding:
public class StringLength {
public static void main(String[] args)
{ String str = "Hello, World!";
int length = str.length();
System.out.println("Length of the string: " + length);
}
}

Output:
Question 16: WAP in JAVA to Count Number of Words in a
String.
Coding:
public class WordCount {
public static void main(String[] args) {
String str = "This is a sample string with multiple words.";
String[] words = str.split("\\s+");
int wordCount = words.length;
System.out.println("Number of words in the string: " + wordCount);
}
}

Output:
Question 17: WAP in Java To Reverse a
String.
Coding:
import java.util.Scanner;

public class ReverseString {


public static void main(String[] args)
{ Scanner scanner = new
Scanner(System.in);
System.out.print("Enter a string: ");
String str = scanner.nextLine();
String reversedStr = "";
for (int i = str.length() - 1; i >= 0; i--)
{ reversedStr += str.charAt(i);
}
System.out.println("Reversed string: " + reversedStr);
}
}

Output:
Question 18: WAP in Java to illustrate the use of Break and
continue
statement.
Coding:
public class BreakContinueExample
{ public static void main(String[]
args) {
for (int i = 1; i <= 10; i++)
{ if (i == 5) {
break; // Terminate the loop if i equals 5
}
if (i % 2 == 0) {
continue; // Skip even numbers
}
System.out.println(i);
}
}
}

Output:
Question 19:WAP in Java to find largest and smallest number from
the
array.
Coding:
public class LargestSmallestNumber
{ public static void main(String[] args) {
int[] numbers = {10, 5, 8, 20, 3};
int largest = numbers[0];
int smallest = numbers[0];
for (int number : numbers) {
if (number > largest)
{ largest = number;
}
if (number < smallest)
{ smallest = number;
}
}
System.out.println("Largest number: " + largest);
System.out.println("Smallest number: " + smallest);
}
}

Output:
Question 20: WAP in JAVA for the Implementation of Abstract classes
in
JAVA.
Coding:
abstract class Shape
{ abstract void
draw();
}
class Circle extends Shape
{ void draw() {
System.out.println("Drawing Circle");
}
}
class Rectangle extends Shape
{ void draw() {
System.out.println("Drawing Rectangle");
}
}
public class AbstractClassExample
{ public static void main(String[]
args) {
Shape circle = new Circle();
Shape rectangle = new Rectangle();
circle.draw();
rectangle.draw();
}
}

Output:
Question 21:WAP in JAVA for Implementing single level
inheritance.
Coding:
class Animal
{ void eat() {
System.out.println("Animal is eating...");
}
}

class Dog extends Animal


{ void bark() {
System.out.println("Dog is barking...");
}
}

public class SingleInheritance {


public static void main(String[] args)
{ Dog dog = new Dog();
dog.eat();
dog.bark();
}
}

Output:
Question 22: WAP in JAVA to Implement multiple
inheritance.
Coding:
interface Animal
{ void eat();
}

interface Mammal
{ void move();
}

class Dog implements Animal, Mammal


{ public void eat() {
System.out.println("Dog is eating...");
}
public void move()
{ System.out.println("Dog is running...");
}
}
public class MultipleInheritance {
public static void main(String[] args)
{ Dog dog = new Dog();
dog.eat();
dog.move();
}
}

Output:
Question 23: WAP to illustrate 2 D array in Java
.
Coding:
public class TwoDArrayExample
{ public static void main(String[]
args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++)
{ System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}

Output:
Question 24: WAP in JAVA Method Overloading.
Coding:
public class MethodOverloading
{ void display(int num) {
System.out.println("Number: " + num);
}

void display(String str)


{ System.out.println("String: " + str);
}

public static void main(String[] args)


{ MethodOverloading obj = new
MethodOverloading(); obj.display(10);
obj.display("Hello");
}
}

Output:
Question 25: WAP in java to find the sum of Natural Numbers
using
Recursion.
Coding:
public class SumOfNaturalNumbers
{ static int sum(int n) {
if (n == 1) {
return 1;
} else {
return n + sum(n - 1);
}
}

public static void main(String[] args)


{ int n = 5;
int result = sum(n);
System.out.println("Sum of first " + n + " natural numbers: " + result);
}
}

Output:
Question 26: WAP to implement default constructor &
parameterized
constructor.
Coding:
public class ConstructorExample
{ int num;
String name;

// Default Constructor
ConstructorExample() {
num = 10;
name = "John";
}

// Parameterized Constructor
ConstructorExample(int n, String s) {
num = n;
name = s;
}

public static void main(String[] args)


{ ConstructorExample obj1 = new
ConstructorExample();
ConstructorExample obj2 = new ConstructorExample(20, "Alice");
System.out.println("Default Constructor:");
System.out.println("num: " + obj1.num);
System.out.println("name: " + obj1.name);
System.out.println("\nParameterized Constructor:");
System.out.println("num: " + obj2.num);
System.out.println("name: " + obj2.name);
}
}
Output:
Question 27: WAP in Java to illustrate the use of threads in
JAVA.
Coding:
class MyThread extends Thread
{ public void run() {
for (int i = 0; i < 5; i++)
{ System.out.println(Thread.currentThread().getName() + ": " + i);
try {
Thread.sleep(500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(e);
}
}
}

public static void main(String[] args)


{ MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
t1.start();
t2.start(); }
}

Output:
Question 28: WAP in java to Create File and Write in the
file.
Coding:
// importing the File class
import java.io.File;

class Main {
public static void main(String[] args) {

// create a file object for the current location


File file = new File("JavaFile.java");

try {

// create a new file with name specified


// by the file object
boolean value = file.createNewFile();
if (value) {
System.out.println("New Java File is created.");
}
else {
System.out.println("The file already exists.");
} }
catch(Exception e) {
e.getStackTrace();
} }
}

Output:
Question 29:WAP in Java to Read the Content of the File, Line by
Line.
Coding:
import java.io.FileWriter; // Import the FileWriter class
import java.io.IOException; // Import the IOException class to handle errors

public class WriteToFile {


public static void main(String[] args)
{ try {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Files in Java might be tricky, but it is fun enough!");
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e)
{ System.out.println("An error occurred.");
e.printStackTrace();
}
}
}

Output:
Question 30:WAP in Java to Delete a
file.
Coding:
import java.io.File; // Import the File class

public class DeleteFile {


public static void main(String[] args)
{ File myObj = new
File("filename.txt"); if (myObj.delete())
{
System.out.println("Deleted the file: " + myObj.getName());
} else {
System.out.println("Failed to delete the file.");
}
}
}

Output:

You might also like