23BCP113 Java File
23BCP113 Java File
SCHOOL OF TECHNOLOGY
SESSION 2024-25
SUBMITTED BY
NAME : Harsh Rana
SUBMITTED TO
Assistant Professor
****************
INDEX
2|Page
LAB 1
Code:
class lab1_1
{
public static void main(String[] args)
{ System.out.print("coding is fun , enjoy
it"); }}
OUTPUT:
System.out.println(sum);
}
}
OUTPUT:
5|Page
LAB 2
Study language features of Java (variables, data types, declarations, loop
and branch constructs, etc.)
I. You are developing a mathematical tool that requires generating a list
of prime numbers. How would you implement a Java program to
generate the first n prime numbers?
CODE:
import java.util.Scanner;
class lab2_1 {
public static void main(String[] args)
{ Scanner myObj = new
Scanner(System.in);
System.out.println("Enter the number up to you want to find prime
numbers");
int prime = myObj.nextInt();
for (int i = 2; i <= prime; i++) {
boolean isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(i + " ");
}
}
}
}
6|Page
OUTPUT:
CODE:
import java.util.Scanner;
class lab2_3{
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
System.out.println("Enter the 1st number :");
float a = num.nextFloat();
System.out.println("Enter the 2nd number :");
float b = num.nextFloat();
System.out.println("Enter 1 if you want to do addition operation\nEnter 2 if
you want to do subtraction operation\nEnter 3 if you want to do
multiplication operation\nEnter 4 if you want to do division operation\n");
float c = num.nextFloat();
if(c==1)
{
System.out.println(a+"+" + b + "=" + (a+b));
}
else if (c==2)
{
7|Page
OUTPUT:
8|Page
CODE:
import java.util.Scanner;
class lab2_2 {
public static void main(String[] args)
{ Scanner num = new
Scanner(System.in);
System.out.println("Enter the 1st number you want to
compare:"); int a = num.nextInt();
System.out.println("Enter the 2nd number you want to compare:");
int b = num.nextInt();
System.out.println("Enter the 3rd number you want to compare:");
int c = num.nextInt();
}
}
OUTPUT:
9|Page
IV. You're working on a text analysis feature that counts the number of
vowels
and consonants in a given line of text. Write a program to accept a line
and check how many consonants and vowels are there in line.
CODE:
import java.util.Scanner;
class lab2_4 {
public static void main(String[] args) { Scanner
scanner = new Scanner(System.in);
System.out.println("Enter a line of
text:"); String line = scanner.nextLine();
int vowelCount = 0;
int consonantCount = 0;
line = line.toLowerCase();
scanner.close();
}
}
OUTPUT:
CODE:
import java.util.Scanner;
}
}
OUTPUT:
12 | P a g e
CODE:
largest = numbers[i];
OUTPUT:
13 | P a g e
CODE:
int[][] matrixA = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] matrixB = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
product[i][j] = 0;
System.out.println("Sum of the
matrices:"); for (int i = 0; i < 3; i++) {
System.out.println();
System.out.println("Product of the
matrices:"); for (int i = 0; i < 3; i++) {
System.out.println();
OUTPUT:
16 | P a g e
LAB 3
CODE:
import java.util.Scanner;
class lab3_1{
feet=sc.nextInt();
inch=sc.nextInt();
int ans=feet*12;
float ans1=inch/12;
class distance{
d1.distance_method();
d1.feet_inch();
d1.inch_feet();
OUTPUT:
ii. Modify the “distance” class by creating constructor for assigning values
(feet and inches) to the distance object. Create another object and assign
CODE:
import java.util.Scanner;
this.feet = feet;
this.inch = inch;
public lab3_2() {
feet = sc.nextInt();
inch = sc.nextInt();
System.out.println("The distance of " + feet + " feet in inches is: " + ans);
System.out.println("The distance of " + inch + " inches in feet is: " + ans1);
@Override
try {
return super.clone();
} catch (CloneNotSupportedException e)
{ e.printStackTrace();
return null;
System.out.println("Original object:");
d1.feetToInch();
d1.inchToFeet();
System.out.println("Assigned object:");
20 | P a g e
d2.feetToInch();
d2.inchToFeet();
System.out.println("Cloned object:");
d3.feetToInch();
d3.inchToFeet();
OUTPUT:
iii. Write a program to show the difference between public and private
access specifiers. The program should also show that primitive data
types are passed by value and objects are passed by reference and to
learn use of final keyword
Code:
class Example {
public int publicVar = 10;
private int privateVar = 20;
final int finalVar = 30;
21 | P a g e
void changeValue(int x) {
x = 100;
}
}
Output:
iv. Write a program that implements two constructors in the class. We call
the other constructor using ‘this’ pointer, from the default constructor of the
class.
Code:
class MyClass {
int value;
MyClass() {
this(100);
}
MyClass(int value) {
this.value = value;
System.out.println("Value: " + this.value);
}
}
Code:
class SuperClass {
int number;
SuperClass(int num) {
number = num;
System.out.println("Superclass constructor called. Number: " + number);
}
}
Output:
vi. Write a program in Java to develop overloaded constructor. Also develop the
copy constructor to create a new object with the state of the existing object.
Code:
class Example {
int number;
Example() {
number = 0;
}
25 | P a g e
Example(int num) {
number = num;
}
Example(Example obj) {
this.number = obj.number;
}
}
Output:
26 | P a g e
LAB-4
Code:
class Object{
String color;
int size;
public void area(){
System.out.println("It displays area");
}
}
class Triangle extends Object {
public void area (int l,int h)
{
System.out.println(1/2*l*h);
}
Output:
28 | P a g e
ii. Java Program to demonstrate the real scenario (e.g., bank) of Java
Method Overriding where three classes are overriding the method of a
parent class. Creating a parent class.
Code:
class BankAccount {
void accountType() {
void accountType() {
void accountType() {
29 | P a g e
void accountType() {
savings.accountType();
checking.accountType();
business.accountType();
OUTPUT:
30 | P a g e
iii. Write a java program for the use of super and this keyword.
Code:
class Parent {
int value = 10;
}
Child() {
System.out.println("Child Constructor");
}
void showValues() {
System.out.println("Parent value: " + super.value);
System.out.println("Child value: " + this.value);
}
}
child.showValues();
}
}
Output:
keyword. Code:
Output:
32 | P a g e
LAB-5
Code:
class Animal {
void sound() {
System.out.println("Some sound");
void sound() {
System.out.println("Bark");
void sound() {
System.out.println("Yelp");
animal1.sound();
animal2.sound();
animal3.sound();
Output:
Code:
return false;
return combined.contains(s2);
}
34 | P a g e
String s1 = "java";
String s2 = "avaj";
System.out.println(isRotation(s1, s2));
Output:
35 | P a g e
LAB-6
i. Describe abstract class called Shape which has three subclasses say
Triangle, Rectangle, Circle. Define one method area() in the abstract
class and override this area() in these three subclasses to calculate for
specific object i.e. area() of Triangle subclass should calculate area of
triangle etc. Same for Rectangle and Circle.
Code:
abstract class Shape {
abstract double area();
}
double area() {
return 0.5 * base * height;
}
}
this.length = length;
this.width = width;
}
double area() {
return length * width;
}
}
Circle(double radius) {
this.radius = radius;
}
double area() {
return Math.PI * radius * radius;
}
}
Output:
ii. Write a Java program to create an abstract class Employee with abstract
methods calculateSalary() and displayInfo(). Create subclasses Manager
and Programmer that extend the Employee class and implement the
respective methods to calculate salary and display information for each
role.
Code:
abstract class Employee {
abstract void calculateSalary();
abstract void displayInfo();
}
void calculateSalary() {
System.out.println("Manager Salary: " + (baseSalary + 20000));
}
void displayInfo() {
38 | P a g e
System.out.println("Role: Manager");
}
}
void calculateSalary() {
System.out.println("Programmer Salary: " + (baseSalary + 10000));
}
void displayInfo() {
System.out.println("Role: Programmer");
}
}
Output:
iii. Write a Java program to create an interface Shape with the getArea()
method. Create three classes Rectangle, Circle, and Triangle that
implement the Shape interface. Implement the getArea() method for each
of the three classes.
Code:
interface Shape {
double getArea();
}
Circle(double radius) {
this.radius = radius;
}
Output: