0% found this document useful (0 votes)
3 views

Lab Report 2

Lab_Report_2

Uploaded by

samirgolder2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab Report 2

Lab_Report_2

Uploaded by

samirgolder2
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Dhaka International University

Dept of CSE

Lab Report - 2

Course Name : Object-Oriented Programming Language


Course Number : 0613-202

Submitted By
Nasir Ahammad Suvo
Batch D-85
Roll 39

Submitted To

Majharul Hasan Lecturer,


Dept of Computer Science & Engineering
Dhaka International University
Introduction:

In this lab, we aim to develop a simple Java program that demonstrates the use of basic
input/output operations, arithmetic calculations, and comparison functions. The program
takes five integer inputs from the user, calculates the sum and average of these values, and
determines the largest number among them using Java’s built-in functions.

Objective:

To write a Java program that takes five integers as input, calculates their sum, average,
and determines the maximum value among them.

Code Overview:

The program is structured as follows:

1. Imports: The Scanner class from the java.util package is imported to allow user input.
2. Main Method:
• A Scanner object is created to take input from the user.
• Five integers (a, b, c, d, e) are read from the console using nextInt() method of the
Scanner.
• The sum of the five integers is calculated.
• The average is calculated by dividing the sum by 5. To avoid integer division, the
sum is cast to float.
• The maximum value is determined using the Math.max() method. It compares all
five numbers using nested Math.max() calls.
• The results for the sum, average, and maximum value are printed to the console
using System.out.println().

Code:
1. import java.util.Scanner;
2.
3. public class Shuvo420 {
4. public static void main(String[] args) {
5. Scanner scan = new Scanner(System.in);
6.
7. int a = scan.nextInt();
8. int b = scan.nextInt();
9. int c = scan.nextInt();
10. int d = scan.nextInt();
11. int e = scan.nextInt();
12.
13. int sum = a + b + c + d + e;
14. float avg = (float) sum / 5;
15.
16. // Finding maximum value among the five numbers
17. int max = Math.max(a, Math.max(b, Math.max(c, Math.max(d, e))));
18.
19. // Printing results
20. System.out.println(sum);
21. System.out.println(avg);
22. System.out.println(max);
23. }
24. }
Explanation of Key Concepts:

• Scanner: Used for taking input from the console.


• Math.max(): A built-in Java method that returns the greater of two numbers. It is used in
a nested manner to compare multiple numbers.
• Type Casting (float): Casting the sum to a float ensures that the division result will
include decimal places if necessary.

Execution:
When this code runs, it prompts the user to input five integers. After entering the numbers,
it displays:

1. The sum of the numbers.


2. The average value.
3. The maximum value.

Sample Input/Output:

• Input:

➢ 15
➢ 20
➢ 14
➢ 18
➢ 12

• Output:

➢ 79
➢ 15.8
➢ 20

Conclusion:

The program successfully takes five integer inputs from the user and calculates the sum,
average, and maximum of those numbers. The nested Math.max() function is an efficient way
to determine the largest of the five numbers. This solution is simple and demonstrates basic
Java input/output operations, arithmetic calculations, and comparison functions.

You might also like