0% found this document useful (0 votes)
4 views22 pages

Final Lab Java

The document outlines a Java lab course for BCA students at Manav Rachna International Institute, detailing various programming experiments. Each experiment includes a specific task, such as calculating averages, generating prime numbers, and demonstrating object-oriented concepts. The document also provides example code and expected outputs for each experiment.

Uploaded by

gameamazono10
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)
4 views22 pages

Final Lab Java

The document outlines a Java lab course for BCA students at Manav Rachna International Institute, detailing various programming experiments. Each experiment includes a specific task, such as calculating averages, generating prime numbers, and demonstrating object-oriented concepts. The document also provides example code and expected outputs for each experiment.

Uploaded by

gameamazono10
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/ 22

JAVA LAB

FILE
BCA-DS-
452A

Manav Rachna International Institute of Research and


Studies School of Computer Applications
Department of Computer Applications

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

SCHOOL OF COMPUTER APPLICATIONS


S.
No. Date Aim of the Experiment Signature Grade
1 Write a program to find the average and sum of the
N numbers using Command line argument.

2 Write a program to demonstrate type casting.

3 Write a program to generate prime numbers between


1 & given number

4 Write a program to generate pyramid of stars using


nested for loops

5 Write a program to reversed pyramid using for


loops & decrement operator.

6 Write a program for demonstrate Nested Switch

7 Write a program to calculate area of a circle using


radius

8 Write a program to count the number of objects


created for a class using static member function

9 Write a program to design a class account using the


inheritance and static members which show all
functions of a bank (Withdrawl, deposit)
10 Write a program to create a simple class to find
out the area and perimeter of rectangle using super
and this keyword.

11

12

13

14

15
Experiment 1:- Write a program to find the average and sum of
the N numbers using Command line argument.

public class AverageAndSum {

public static void main(String[] args) {

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;

System.out.println("Sum: " + sum);


System.out.println("Average: " + average);
}
}

Output :-
java AverageAndSum 5 10 15
Sum: 30
Average: 10.0
Experiment 2:- Write a program to demonstrate type casting.

public class TypeCastingDemo {


public static void main(String[] args) {
int intValue = 10;
double doubleValue = intValue;
System.out.println("Implicit Type Casting (Widening):");
System.out.println("int value: " + intValue);
System.out.println("double value after casting: " + doubleValue);

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:-

Implicit Type Casting


(Widening): int value: 10
double value after casting: 10.0

Explicit Type Casting


(Narrowing): double value:
15.75
int value after casting: 15
Experiment 3:- Write a program to generate prime numbers
between 1 & given number

import java.util.Scanner;

public class PrimeNumberGenerator {


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

System.out.print("Enter a number to find prime numbers up to that number: ");


int n = scanner.nextInt();

System.out.println("Prime numbers between 1 and " + n + ":");


generateAndPrintPrimes(n);

scanner.close();
}

private static void generateAndPrintPrimes(int limit) {


for (int i = 2; i <= limit; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
}

private 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:-

Enter a number to find prime numbers up to that number: 10


Prime numbers between 1 and 10:
2357
EEeew

Experiment 4:- Write a program to generate pyramid of stars using


nested for loops

import java.io.*;

public class GeeksForGeeks


{

public static void printStars(int n)


{
int i, j;
for(i=0; i<n; i++)
{
for(j=0; j<=i; j++)
{
// printing stars
System.out.print("* ");
}

// ending line after each row


System.out.println();
}
}

public static void main(String args[])


{
int n = 5;
printStars(n);
}
}

Output:-

*
**
EEeew

***
****
*****
Experiment 5:- Write a program to reversed pyramid using for loops &
decrement operator.

public class JavaExample


{
public static void main(String[] args)
{
int numberOfRows=7;
//This loop runs based on the number of rows
//In this case, the loop runs 7 times to print 7 rows
for (int i= 0; i<= numberOfRows-1; i++)
{

//This loop prints starting spaces for each row of pattern


for (int j=0; j<=i; j++)
{
System.out.print(" ");
}
//This loop prints stars and the space between stars for each row
for (int k=0; k<=numberOfRows-1-i; k++)
{
System.out.print("*" + " ");
}
//To move the cursor to new line after each row
System.out.println();
}
}
}

Output:-
dddsda

Experiment 6:- Write a program for demonstrate Nested Switch.

import java.io.*;

class GFG {

public static void main (String[] args)


{
int x = 1, y = 2;

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

Experiment 7:- Write a program to calculate area of a circle using


radius

abstract class Shape {


abstract double
calculateArea(); void
display() {
System.out.println("This is a shape.");
}
}
class Circle extends Shape
{ private double radius;

Circle(double radius)
{ this.radius =
radius;
}
@Override
double calculateArea() {
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape
{ private double length;
private double width;

Rectangle(double length, double


width) { this.length = length;
this.width = width;
}
@Override
double calculateArea()
{ return length *
width;
}
}

public class ShapeDemo {


public static void main(String[]
args) { Circle circle = new
dddsda

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;

protected int accountNumber;


protected String
accountHolder; protected
double balance;

public Account(String accountHolder, double initialBalance) {


this.accountNumber = nextAccountNumber++;
this.accountHolder = accountHolder;
this.balance = initialBalance;
}

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
System.out.println("Deposited: $" + amount);
displayBalance();
} else {
System.out.println("Invalid deposit amount.");
}
}

public void withdraw(double amount) {


if (amount > 0 && amount <= balance)
{ balance -= amount;
System.out.println("Withdrawn: $" + amount);
displayBalance();
} else {
System.out.println("Invalid withdrawal amount or insufficient funds.");
}
}

public void displayBalance() {


System.out.println("Current Balance: $" + balance);
}

public static void main(String[] args)


{ Scanner scanner = new
Scanner(System.in);

System.out.print("Enter account holder's name: ");


String accountHolder = scanner.nextLine();

System.out.print("Enter initial balance: $");


double initialBalance = scanner.nextDouble();

Account account = new Account(accountHolder, initialBalance);

// Perform bank transactions


account.displayBalance();

System.out.print("Enter deposit amount: $");


double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);

System.out.print("Enter withdrawal amount: $");


double withdrawalAmount = scanner.nextDouble();
account.withdraw(withdrawalAmount);

scanner.close();
}
}

Output:-

Enter account holder's name:


John Doe Enter initial balance:
$1000
Current Balance: $1000.0

Enter deposit amount:


$200 Deposited:
$200.0
Current Balance: $1200.0

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 Rectangle(double length, double


width) { this.length = length;
this.width = width;
}

public void display() {


System.out.println("Rectangle
Information:");
System.out.println("Length: " + length);
System.out.println("Width: " + width);
}
}

class RectangleDetails extends Rectangle {


public RectangleDetails(double length, double width) {
super(length, 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);

System.out.print("Enter length of the


rectangle: "); double length =
scanner.nextDouble();

System.out.print("Enter width of the


rectangle: "); double width =
scanner.nextDouble();

RectangleDetails rectangle = new RectangleDetails(length,


width); rectangle.display();

scanner.close();
}
}

Output:-

Enter length of the


rectangle: 5.5 Enter width
of the rectangle: 3.2
Rectangle Information:
Length: 5.5
Width: 3.2
Area: 17.6
Perimeter: 17.4

You might also like