[Go to site: main page, start]

0% found this document useful (0 votes)
5 views16 pages

Java Assignment

The document contains a series of Java programming practicals by Amit Nautiyal, focusing on various tasks such as calculating sums of digits, factorials, areas of geometric shapes, and implementing classes with inheritance. Each practical includes a problem statement, objective, source code, and sample outputs demonstrating the functionality of the programs. The practicals cover input validation, method overloading, and the use of jagged arrays.

Uploaded by

Googly
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Java Assignment

The document contains a series of Java programming practicals by Amit Nautiyal, focusing on various tasks such as calculating sums of digits, factorials, areas of geometric shapes, and implementing classes with inheritance. Each practical includes a problem statement, objective, source code, and sample outputs demonstrating the functionality of the programs. The practicals cover input validation, method overloading, and the use of jagged arrays.

Uploaded by

Googly
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

AMIT NAUTIYAL

MCA ‘D’
ROLL NO – 2401038

Practical:1
Problem Statement: Write a Java program to calculate the sum of last two digit of given number.

Objective: The objective of the Java program is to calculate the sum of last two digit of given number. It
should display the sum.

Source code :
import [Link];

public class SumOfLastDigits {


public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a two-digit number: ");


int number = [Link]();

if (number < 10 || number > 99) {


[Link]("Please enter a valid two-digit number.");
} else {
int lastDigit = number % 10;
int secondLastDigit = number / 10;
int sum = lastDigit + secondLastDigit;

[Link]("The sum of the last digits is: " + sum);


}
[Link]();
}}
AMIT NAUTIYAL

MCA ‘D’
ROLL NO – 2401038
OUTPUT

Enter a two-digit number: 14


The sum of the last digits is: 5
AMIT NAUTIYAL

MCA ‘D’
ROLL NO – 2401038

Practical:2
Problem Statement: Write a Java program that takes an 8-digit number as input and calculates the
sum of its even digits. The program should ensure that the user enters exactly an 8-digit number and
display an error message if the input is invalid.

Objective: The objective of this program is to develop a Java application that correctly processes an 8-
digit number by extracting its digits, identifying the even ones, and summing them up. The program
should include proper input validation to ensure that only 8-digit numbers are accepted.

Source code :

import [Link];
class EvenSum
{ public static void main(String[]args)
{

Scanner scanner = new Scanner([Link]);


[Link]("Enter a 8 digit number");
int num = [Link]();
int sum = 0;
if (num < 10000000 || num > 99999999)
{ [Link]("Please enter a valid 8-digit number.");

} else {
while (num > 0) {
int rem = (int) (num % 10);
if (rem % 2 == 0) {
sum += rem; }
num /= 10; }

[Link]("Sum of even digits = " + sum);

}}

}
AMIT NAUTIYAL

MCA ‘D’
ROLL NO – 2401038
OUTPUT

Enter a 8 digit number

24711013

Sum of even digits = 6

Enter a 8 digit number

247199

Please enter a valid 8-digit number.


AMIT NAUTIYAL

MCA ‘D’
ROLL NO – 2401038

Practical:3
Problem Statement: : The program takes two integer inputs from the user and calculates the sum
of their last digits.

Objective: The objective of this program is to develop a simple Java program takes two integer inputs
from the user and calculates the sum of their last digits. It extracts the last digit of each number using
the modulus operator (%), adds them together, and displays the result.

Source code :
import [Link];
class LastDigitSum {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter the first number:");


int n = [Link]();

[Link]("Enter the second number:");


int m = [Link]();

int sum = (n % 10) + (m % 10);

[Link]("Sum of the last digits = " + sum);

}
AMIT NAUTIYAL

MCA ‘D’
ROLL NO – 2401038
OUTPUT

Enter the first number:

707

Enter the second number:

395

Sum of the last digits = 12


AMIT NAUTIYAL

MCA ‘D’
ROLL NO – 2401038

Practical:4
Problem Statement: Write a Java Program to calculate the factorial of a number given by user.
Objective: The objective is to calculate the factorial of a number.

Source code :
import [Link];

public class FactorialCalculator {


public static void main(String[] args)
Scanner scanner = new Scanner([Link]);
[Link]("Enter a non-negative integer to calculate its factorial: ");
int number = [Link]();

if (number < 0) {
[Link]("Factorial is not defined for negative numbers.");
} else {
long factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
} [Link]("The factorial of " + number + " is: " + factorial);
} [Link]();
}
}
AMIT NAUTIYAL

MCA ‘D’
ROLL NO – 2401038
OUTPUT

Enter a non-negative integer to calculate its factorial: 5


The factorial of 5 is: 120
AMIT NAUTIYAL

MCA ‘D’
ROLL NO – 2401038

Practical:5
Problem Statement: This Java application provides a straightforward solution for calculating the
area of various geometric shapes, including circles, squares, and rectangles, based on user-defined
dimensions.

Objective: The objective of this Java application is to demonstrate method overloading by providing
multiple “calculateArea” methods that handle different geometric shapes (circle, square, and rectangle).
This allows users to easily calculate the areas of these shapes based on their inputs, enhancing usability
and flexibility in area calculations.

Source code :
import [Link];

public class ShapeAreaCalculator {


public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Select a shape to calculate the


area:");
[Link]("1. Circle");
[Link]("2. Square");
[Link]("3. Rectangle");
[Link]("Enter your choice (1-3): ");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter the radius of the circle: ");
double radius = [Link]();
double circleArea = [Link] * [Link](radius, 2);
AMIT NAUTIYAL

MCA ‘D’

ROLL NO – 2401038

[Link]("The area of the circle is:" + circleArea);

break;

case 2:
[Link]("Enter the side length of the square:");
double side = [Link]();
double squareArea = [Link](side, 2);
[Link]("The area of the square is:" +
squareArea);
break;
case 3:
[Link]("Enter the length of the rectangle: ");
double length = [Link]();
[Link]("Enter the width of the rectangle: ");
double width = [Link]();
double rectangleArea = length * width;
[Link]("The area of the rectangle is: " +
rectangleArea);
break;
default:
[Link]("Invalid choice. Please select a valid
option (1-3).");
}
[Link]();
}}
AMIT NAUTIYAL

MCA ‘D’
ROLL NO – 2401038
OUTPUT

Select a shape to calculate the area:


1. Circle
2. Square
3. Rectangle
Enter your choice (1-3): 2
Enter the side length of the square: 6
The area of the square is: 36.0

Enter your choice (1-3): 1


Enter the radius of the circle: 3
The area of the circle is: 28.274333882308138

Enter your choice (1-3): 3


Enter the length of the rectangle: 2
Enter the width of the rectangle: 3
The area of the rectangle is: 6.0
AMIT NAUTIYAL
AMIT NAUTIYAL
MCA ‘D’

ROLL NO – 2401038

Practical:6
Problem Statement: create a class name animal which include methods like eat and
sleep create a child class of animal named Bird and overwrite the parent class method test
the function of Bird and animal class in the separate main method.

Objective: The objective of this program is to create an class name animal and then we
have to overwrite the class with another class name bird.

Source code :
class animal
{
void [Link]();
{
[Link]("for eating");
}
void [Link]();
{
[Link]("for eating");
}
} class birds { void eats() {
[Link]("Bird is eating");
}void sleeps() {
[Link]("Bird is sleeping");
}
}
class test{
public static void
main(String[]args){
birds b=new birds();
[Link]();
[Link]();
}
}
AMIT NAUTIYAL
MCA ‘D’

ROLL NO – 2401038
OUTPUT

Bird is eating

Bird is sleeping
AMIT NAUTIYAL
MCA ‘D’

ROLL NO – 2401038

Practical -7
Problem Statement: create an jagged array and calculate the sum of the even element and
odd elements of jagged array.
Objective: The user defines the number of rows and the number of elements in each
individual row, allowing for rows of varying lengths. the user enters the integer values
that populate the array.
Source code:
import [Link];

public class jag {

public static void main(String[] args) {


int[][] arr = new int[4][];
arr[0] = new int[3];
arr[1] = new int[4];
arr[2] = new int[2];
arr[3] = new int[5];

for (int i = 0; i < [Link]; i++) {


if (arr[i] != null) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = i + j;
}}
}

for (int i = 0; i < [Link]; i++) {


if (arr[i] != null) {

for (int j = 0; j < arr[i].length; j++) {


[Link](arr[i][j] + " "); }
[Link](); }
} }}
AMIT NAUTIYAL

MCA ‘D’

ROLL NO – 2401038
OUTPUT

012

1234
23

34567

You might also like