Final Lab Java
Final Lab Java
FILE
BCA-DS-
452A
Submitted By
Student Name ROHIT MAJUMDER
Roll No 22/FCA/BCA(AIML)/042
Subject JAVA LAB
Semester 4TH
Section/Group D
Department Computer Applications
Batch 2023-2025
Submitted To
Faculty Name Ritu
11
12
13
14
15
Experiment 1:- Write a program to find the average and sum of
the N numbers using Command line argument.
if (args.length == 0) {
System.out.println("Usage: java AverageAndSum <num1> <num2> <num3>
...");
return;
}
int sum = 0;
for (String arg : args)
{ try {
int num = Integer.parseInt(arg);
sum += num;
} catch (NumberFormatException e) {
System.out.println("Invalid input: " + arg + ". Please enter valid integers.");
return;
}
}
double average = (double) sum / args.length;
Output :-
java AverageAndSum 5 10 15
Sum: 30
Average: 10.0
Experiment 2:- Write a program to demonstrate type casting.
System.out.println("\n-------------------\n");
double anotherDoubleValue = 15.75;
int anotherIntValue = (int) anotherDoubleValue;
System.out.println("Explicit Type Casting (Narrowing):");
System.out.println("double value: " + anotherDoubleValue);
System.out.println("int value after casting: " + anotherIntValue);
}
}
Output:-
import java.util.Scanner;
scanner.close();
}
import java.io.*;
Output:-
*
**
EEeew
***
****
*****
Experiment 5:- Write a program to reversed pyramid using for loops &
decrement operator.
Output:-
dddsda
import java.io.*;
class GFG {
// Outer Switch
switch (x) {
// If x == 1
case 1:
// Nested Switch
switch (y) {
// If y == 2
case 2:
System.out.println("Choice is 2");
break;
// If y == 3
case 3:
System.out.println("Choice is 3");
break;
}
break;
// If x == 4
case 4:
System.out.println("Choice is 4");
break;
// If x == 5
case 5:
System.out.println("Choice is 5");
dddsda
break;
default:
System.out.println("Choice is other than 1, 2 3, 4, or 5");
}
}
}
Output:-
Choice is 2
dddsda
Circle(double radius)
{ this.radius =
radius;
}
@Override
double calculateArea() {
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape
{ private double length;
private double width;
Circle(5.0);
Rectangle rectangle = new Rectangle(4.0,
6.0); circle.display();
System.out.println("Area of Circle: " + circle.calculateArea());
rectangle.display();
System.out.println("Area of Rectangle: " + rectangle.calculateArea());
}
}
Output:-
This is a shape.
Area of Circle:
78.53981633974483 This is a
shape.
Area of Rectangle: 24.0
Experiment 8:- Write a program to find G.C.D of the number.
import java.io.*;
public class GFG {
static int gcd(int a, int b)
{
int result = Math.min(a, b);
while (result > 0) {
if (a % result == 0 && b % result == 0) {
break;
}
result--;
}
return result;
}
public static void main(String[] args)
{
int a = 98, b = 56;
System.out.print("GCD of " + a + " and " + b
+ " is " + gcd(a, b));
}
}
Output:-
GCD of 98 and 56 is 14
Experiment 9:- Write a program to design a class account using the
inheritance and static members which show all functions of a bank
(Withdrawl, deposit)
import java.util.Scanner;
class Account {
private static int nextAccountNumber = 1;
scanner.close();
}
}
Output:-
Enter withdrawal
amount: $50 Withdrawn:
$50.0
Current Balance: $1150.0
Experiment 10:- Write a program to create a simple class to find out
the area and perimeter of rectangle using super and this keyword.
import
java.util.Scanner;
class Rectangle {
protected double
length; protected
double width;
public double
calculateArea() { return
this.length * this.width;
}
public double
calculatePerimeter() { return
2 * (this.length + this.width);
}
@Override
public void
display() {
super.display()
;
System.out.println("Area: " + calculateArea());
System.out.println("Perimeter: " + calculatePerimeter());
}
}
public class RectangleProgram {
public static void main(String[] args)
{ Scanner scanner = new
Scanner(System.in);
scanner.close();
}
}
Output:-