Docsity

BCA JAVA PROGRAMMING LAB ASSIGNMENT WITH CONSTRUCTOR AND OUTPUTS, Assignments of Java Programming

Toggle sidebar
bg1
Assignment no:- 01 Date:-
Write a program to find the factorial of a given number using constructor.
Program:-
import java.util.Scanner;
class Factorial {
int n;
long fact = 1;
Factorial(int num) {
n = num;
for (int i = 1; i <= n; i++) {
fact *= i;
}
System.out.println("Factorial of " + n + " is: " + fact);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
new Factorial(number);
sc.close();
}
}
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download BCA JAVA PROGRAMMING LAB ASSIGNMENT WITH CONSTRUCTOR AND OUTPUTS and more Assignments Java Programming in PDF only on Docsity!

Assignment no:- 01 Date:- Write a program to find the factorial of a given number using constructor. Program:- import java.util.Scanner; class Factorial { int n; long fact = 1; Factorial(int num) { n = num; for (int i = 1; i <= n; i++) { fact *= i; } System.out.println("Factorial of " + n + " is: " + fact); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int number = sc.nextInt(); new Factorial(number); sc.close(); } }

Output Teacher’s Signature:-………………………………….

Output Teacher’s Signature:-……………………………………

Assignment no:- 03 Date:- Write a program to find the sum of any number of integers entered as command line arguments. Program:- class SumCommandLine { public static void main(String[] args) { int sum = 0; for (int i = 0; i < args.length; i++) { sum += Integer.parseInt(args[i]); } System.out.println("Sum of integers: " + sum); } }

Assignment no:- 04 Date:- Write a program Divide By Zero that takes two numbers a and b as input, computes a/b, and invokes Arithmetic Exception to generate a message when the denominator is zero. Program:- import java.util.Scanner; class DividedByZero { public static void main(String[] args) { Scanner sc = new Scanner(System.in); try { System.out.print("Enter number a: "); int a = sc.nextInt(); System.out.print("Enter number b: "); int b = sc.nextInt(); int res = a / b; System.out.println("Result: " + res); } catch (ArithmeticException e) { System.out.println("Error: Arithmetic Exception - Division by zero is not allowed."); } sc.close(); } }

Output Teacher’s Signature:-………………………………………

Output Teacher’s Signature:-………………………………………

Assignment no:- 06 Date:- Write a program to find maximum of n numbers using command line arguments. Program:- class MaxNumber { public static void main(String[] args) { if (args.length == 0) { System.out.println("Please provide numbers!"); return; } int max = Integer.parseInt(args[0]); for (int i = 1; i < args.length; i++) { int current = Integer.parseInt(args[i]); if (current > max) { max = current; } } System.out.println("Maximum number is: " + max); } }

Assignment no:- 07 Date:- Write a program to calculate area of different shape using interface. Program:- interface Shape { void area(); } class Circle implements Shape { public void area() { double r = 5.0; System.out.println("Circle area: " + (3.14 * r * r)); } } class Rectangle implements Shape { public void area() { int l = 10, b = 5; System.out.println("Rectangle area: " + (l * b)); } } class AreaInterface { public static void main(String args[]) { Shape s1 = new Circle(); Shape s2 = new Rectangle(); s1.area(); s2.area(); } }

Output

Teacher’s Signature:-………………………………………

Output

Teacher’s Signature:-………………………………………

Assignment no:- 09 Date:- Write a program to find S=1+%+%.....+1/n with the help of the constructor. Program:- class SeriesSum { double sum = 0; SeriesSum(int n) { for (int i = 1; i <= n; i++) { sum += 1.0 / i; } System.out.println("Sum of the series is: " + sum); } public static void main(String[] args) { int n = 5; new SeriesSum(n); } }

Assignment no:- 10 Date:- Write a program to calculate the volume of different shape using method overloading. Program:- class VolumeCalc { void volume(int s) { System.out.println("Volume of Cube: " + (s * s * s)); } void volume(int l, int b, int h) { System.out.println("Volume of Box: " + (l * b * h)); } void volume(double r) { System.out.println("Volume of Sphere: " + (1.33 * 3.14 * r * r * r)); } public static void main(String[] args) { VolumeCalc obj = new VolumeCalc(); obj.volume(5); obj.volume(10, 5, 2); obj.volume(3.0); } }

Output Teacher’s Signature:-………………………………………