Manish Java
Manish Java
Ques 1. Write a program to demonstrate the use of Scanner class, command line
arguments and BufferedReader for taking input from the user.
(a) Multiply a number by 2
(b) Add three numbers take input using BufferedReader
(c) Check If a Number Is Even or
Odd. Solution:
(a) Multiply a number by 2
import java.util.*;
class mul{
public static void main (String[] args)
{ System.out.println("Name: Vansh Kakkar");
System.out.println("Enroll No.: 01413702024");
System.out.print("Enter a number: ");
int n1;
Scanner scanner=new Scanner(System.in);
n1=scanner.nextInt();
n1=n1*2;
System.out.println("Desired number: " + n1);
}
}
1
01413702024 BCA104P OOPJ Lab
2
01413702024 BCA104P OOPJ Lab
3
01413702024 BCA104P OOPJ Lab
Ques 2. Write a program to demonstrate the use of loops, break and continue
statements.
(a) Calculate the factorial of a number.
(b) Create an infinite loop and use break to exit based on a condition, such as user
input or a specific value.
(c) Print Odd or Even Numbers: Loop through numbers and use continue to skip
numbers that don’t match the condition (e.g., skip even numbers when printing
odd numbers).
(d) Find Non-Divisible Numbers: Loop through numbers in a range and skip
numbers divisible by a given value using continue.
Solution:
(a) Calculate the factorial of a number.
import java.util.Scanner;
public class LoopDemo{
public static void main(String[] args)
{ Scanner scanner = new
Scanner(System.in);
System.out.println("Name: Vansh Kakkar");
System.out.println("Enroll No.: 01413702024");
System.out.print("Enter number: ");
int num = scanner.nextInt();
long factorial = 1;
for (int i = 1; i <= num; i++)
{ factorial *= i;
}
System.out.println("Factorial of " + num + " is: " + factorial);
scanner.close();
}
}
4
01413702024 BCA104P OOPJ Lab
(b) Create an infinite loop and use break to exit based on a condition, such as user
input or a specific value.
import java.util.Scanner;
public class LoopDemo1{
public static void main(String[] args)
{ Scanner scanner = new
Scanner(System.in);
System.out.println("Name: Vansh Kakkar");
System.out.println("Enroll No.: 01413702024");
System.out.println("Infinite loop - type 'exit' to stop:");
while (true) {
System.out.print("Enter something (type 'exit' to stop): ");
String input = scanner.next();
if (input.equalsIgnoreCase("exit")) {
break;
}
System.out.println("You entered: " + input);
}
scanner.close();
}
}
5
01413702024 BCA104P OOPJ Lab
(c) Print Odd or Even Numbers: Loop through numbers and use continue to skip
numbers that don’t match the condition (e.g., skip even numbers when printing
odd numbers).
import java.util.Scanner;
public class evenno{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Name: Vansh Kakkar");
System.out.println("Enroll No: 01413702024");
System.out.println("Printing Even Numbers from 1 to 10: ");
for (int i = 1; i <= 10; i++) {
if (i % 2 != 0) {
continue;
}
System.out.print(i + " ");
}
System.out.println();
scanner.close();
}
}
6
01413702024 BCA104P OOPJ Lab
(d) Find Non-Divisible Numbers: Loop through numbers in a range and skip
numbers divisible by a given value using continue.
import java.util.Scanner;
public class skipdivisor {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Name: Vansh Kakkar");
System.out.println("Enroll No.: 01413702024");
System.out.print("Enter a number to skip multiples of: ");
int skipDivisor = scanner.nextInt();
System.out.println("Numbers from 1 to 20 not divisible by " + skipDivisor + ":");
for (int i = 1; i <= 20; i++) {
if (i % skipDivisor == 0) {
continue;
}
System.out.print(i + " ");
}
scanner.close();
}
}
7
01413702024 BCA104P OOPJ Lab
8
01413702024 BCA104P OOPJ Lab
(b) Copy Elements from One Array to Another: Create a new array and copy
elements from the original array.
import java.util.Arrays;
9
01413702024 BCA104P OOPJ Lab
if (isPalindrome) {
System.out.println("The array is a palindrome.");
} else {
System.out.println("The array is not a palindrome.");
}
}
}
10
01413702024 BCA104P OOPJ Lab
11
01413702024 BCA104P OOPJ Lab
12
01413702024 BCA104P OOPJ Lab
13
01413702024 BCA104P OOPJ Lab
Ques 4. Write a program to demonstrate classes, objects, member functions and data
members:
(a) Declaring a class Rectangle with data member’s length and breadth and member
functions Input, Output and CalculateArea.
(b) Create a class employee which have name, age and address of employee, include
methods getdata() and showdata(), getdata() takes the input from the user,
showdata() display the data in following format:
Name:
Age:
Address:
Solution:
(a) Declaring a class Rectangle with data member’s length and breadth and member
functions Input, Output and CalculateArea.
import java.util.Scanner;
class Rectangle {
double length, breadth;
void input() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter length: ");
length = sc.nextDouble();
System.out.print("Enter breadth: ");
breadth = sc.nextDouble();
}
void output()
{ System.out.println("Length: " +
length);
System.out.println("Breadth: " + breadth);
}
void calculateArea() {
double area = length * breadth;
System.out.println("Area of Rectangle: " + area);
}
14
01413702024 BCA104P OOPJ Lab
15
01413702024 BCA104P OOPJ Lab
(b) Create a class employee which have name, age and address of employee, include
methods getdata() and showdata(), getdata() takes the input from the user,
showdata() display the data in following format:
Name:
Age:
Address:
import java.util.Scanner;
class Employee {
String name, address;
int age;
void getdata() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name: ");
name = sc.nextLine();
System.out.print("Enter Age: ");
age = sc.nextInt();
sc.nextLine();
System.out.print("Enter Address:
"); address = sc.nextLine();
}
void showdata()
{ System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Address: " +
address);
}
public static void main(String[] args)
{ System.out.println("Name: Vansh Kakkar");
System.out.println("Enroll No.: 01413702024");
Employee emp = new Employee();
emp.getdata();
16
01413702024 BCA104P OOPJ Lab
emp.showdata();
}
}
17
01413702024 BCA104P OOPJ Lab
18
01413702024 BCA104P OOPJ Lab
(b) add two integers, three integers, and two double numbers
class Adder {
int add(int a, int b)
{ return a + b;
}
int add(int a, int b, int c)
{ return a + b + c;
}
double add(double a, double b)
{ return a + b;
}
public static void main(String[] args)
{ System.out.println("Name: Vansh Kakkar");
System.out.println("Enroll No.: 01413702024");
Adder obj = new Adder();
19
01413702024 BCA104P OOPJ Lab
Ques 6. Write a program to demonstrate the use of static variable, static method and
static block.
(a) Maintain a shared counter across all instances of a class. Create a StudentCount
class where a static variable count keeps track of the total number of students.
Each time a new student object is created, the count increases.
(b) Create an ArithmeticUtilityClass to add, subtract, divide, and multiply two
numbers using static methods.
Create a static block to demonstrate that static block is executed automatically, and that
too before invoking the constructor of the class.
Solution:
(a) Maintain a shared counter across all instances of a class. Create a StudentCount
class where a static variable count keeps track of the total number of students.
Each time a new student object is created, the count increases.
class StudentCount
{ static int count =
0; StudentCount() {
count++;
System.out.println("Student created. Current student count: " + count);
}
public static void main(String[] args)
{ System.out.println("Name: Vansh Kakkar");
System.out.println("Enroll No.: 01413702024");
StudentCount s1 = new StudentCount();
StudentCount s2 = new StudentCount();
StudentCount s3 = new StudentCount();
StudentCount s4 = new StudentCount();
System.out.println("Total number of students: " + StudentCount.count);
}
}
20
01413702024 BCA104P OOPJ Lab
public ArithmeticUtilityClass()
{ System.out.println("Constructor
executed.");
}
21
01413702024 BCA104P OOPJ Lab
22
01413702024 BCA104P OOPJ Lab
Student() {
name = "Not Provided";
age = 0;
course = "Undecided";
}
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Course: " +
23
01413702024 BCA104P OOPJ Lab
course); System.out.println();
24
01413702024 BCA104P OOPJ Lab
System.out.println("Student 1 (Default):");
s1.display();
System.out.println("Student 2 (Parameterized):");
s2.display();
}
}
25
01413702024 BCA104P OOPJ Lab
26
01413702024 BCA104P OOPJ Lab
Car() {
brand = "Unknown";
model =
"Unknown"; year =
0;
}
Car(String b, String m) {
brand = b;
model = m;
year = 2024;
}
void display()
{ System.out.println("Brand: " +
brand); System.out.println("Model: " +
model); System.out.println("Year: " +
27
01413702024 BCA104P OOPJ Lab
year); System.out.println();
}
28
01413702024 BCA104P OOPJ Lab
System.out.println("Car 1 (Default):");
car1.display();
29
01413702024 BCA104P OOPJ Lab
30
01413702024 BCA104P OOPJ Lab
(b) show how “this” can be used to call another constructor within the same class,
allowing constructor chaining.
class Book
{ String title;
String author;
double price;
Book(String title) {
this(title, "Unknown", 0.0);
}
void display()
{ System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Price: $" + price);
System.out.println();
}
31
01413702024 BCA104P OOPJ Lab
b1.display();
b2.display();
b3.display();
}
}
32
01413702024 BCA104P OOPJ Lab
2. Package1 – ClassB
package package1;
import package2.ClassC;
import package2.ClassD;
33
01413702024 BCA104P OOPJ Lab
34
01413702024 BCA104P OOPJ Lab
3. Package2 – ClassC
package package2;
import package1.ClassA;
35
01413702024 BCA104P OOPJ Lab
4. Package2 – ClassD
package package2;
public class ClassD {
public void display() {
System.out.println("Inside ClassD from packagetwo (public class).");
}
}
Compiling Files
Accessing default and private variables from ClassA (Package1) to ClassC (Package2)
36
01413702024 BCA104P OOPJ Lab
37
01413702024 BCA104P OOPJ Lab
38
01413702024 BCA104P OOPJ Lab
}
}
40
01413702024 BCA104P OOPJ Lab
41
01413702024 BCA104P OOPJ Lab
}
}
42
01413702024 BCA104P OOPJ Lab
Ques 11.
(a) Write a program to demonstrate method overriding. Create an employee class
and a programmer class extending employee class. Override method of employee
class in programmer class.
(b) Write a program to demonstrate the use of super keyword.
To refer immediate parent class instance variable.
To invoke parent class method inside overriding method of subclass.
To invoke parent class constructor, enabling constructor
chaining. Solution:
(a) Write a program to demonstrate method overriding. Create an employee class
and a programmer class extending employee class. Override method of employee
class in programmer class.
class Employee {
String name = "Generic Employee";
double salary = 50000;
public void showDetails()
{ System.out.println("Employee Name: " +
name); System.out.println("Salary: $" + salary);
}
}
class Programmer extends Employee
{ String language = "Java";
public void showDetails()
{ System.out.println("Programmer Name: " + name);
System.out.println("Salary: $" + salary);
System.out.println("Programming Language: " + language);
}
}
p.showDetails();
}
}
44
01413702024 BCA104P OOPJ Lab
45
01413702024 BCA104P OOPJ Lab
46
01413702024 BCA104P OOPJ Lab
47
01413702024 BCA104P OOPJ Lab
48
01413702024 BCA104P OOPJ Lab
49
01413702024 BCA104P OOPJ Lab
51
01413702024 BCA104P OOPJ Lab
void display() {
System.out.println("This is a
shape.");
}
}
s.draw();
System.out.println();
53
01413702024 BCA104P OOPJ Lab
Ques 15. Write program to create and implement an interface. Also, demonstrate the
concept of multiple inheritance in Java.
Solution:
interface Printable {
void print();
}
interface Showable
{ void show();
}
class Document implements Printable, Showable
{ public void print() {
System.out.println("Printing document...");
}
public void show()
{ System.out.println("Showing
document...");
}
}
public class InterfaceDemo {
public static void main(String[] args)
{ System.out.println("Name: Vansh Kakkar");
System.out.println("Enroll No.: 01413702024");
Document doc = new Document();
doc.print(); // from Printable
doc.show(); // from Showable
}
}
54
01413702024 BCA104P OOPJ Lab
55
01413702024 BCA104P OOPJ Lab
56
01413702024 BCA104P OOPJ Lab
57
01413702024 BCA104P OOPJ Lab
58
01413702024 BCA104P OOPJ Lab
59
01413702024 BCA104P OOPJ Lab
(e) Write primitive datatypes like int, byte, char, double to a file
using DataOutputStream.
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class
WriteDataOutputStream{ public static
void main(String[] args){
System.out.println("Name: Vansh Kakkar");
System.out.println("Enroll No.: 01413702024");
try (DataOutputStream dout = new DataOutputStream(new
FileOutputStream("sample.txt"))) {
dout.writeByte(72);
dout.writeInt(101);
dout.writeChar('A');
dout.writeDouble(23.33);
dout.writeBoolean(true);
}catch (IOException e) {
System.out.println("Error writing file:"+ e.getMessage());
}
}
}
60
01413702024 BCA104P OOPJ Lab
(f) Read primitive datatypes like int, byte, char, double from a file
using DataInputStream.
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.EOFException;
public class
ReadDataInputStream{ public static
void main(String[] args){
System.out.println("Name: Vansh Kakkar");
System.out.println("Enroll No.: 01413702024");
try(DataInputStream din = new DataInputStream(new FileInputStream("sample.txt")))
{ while (true) {
System.out.println(din.readByte());
System.out.println(din.readInt());
System.out.println(din.readChar());
System.out.println(din.readDouble());
System.out.println(din.readBoolean());
}
}catch(EOFException e){
System.out.println("EOFException caught End of file reached.");
}catch (IOException e){
System.out.println("IOException occurred:" + e.getMessage());
}
}
}
61