0% found this document useful (0 votes)
18 views2 pages

ACFrOgCs1Zgd759aESjwzaTt2ZUYR YqN1XyFU8OBmi8G32kopbSQ3VJt58 xEsKObM3 - ktJaS6ReZf6gHAH8zDoJ 1BYcRXwEbgq8ZnWEooR14EMPqx7AUT - QGBL eBTcYSZ8znRtDDwmNyvCT7KuAYOUXls9gZGB2rcfZuTQ

Uploaded by

techprai77
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)
18 views2 pages

ACFrOgCs1Zgd759aESjwzaTt2ZUYR YqN1XyFU8OBmi8G32kopbSQ3VJt58 xEsKObM3 - ktJaS6ReZf6gHAH8zDoJ 1BYcRXwEbgq8ZnWEooR14EMPqx7AUT - QGBL eBTcYSZ8znRtDDwmNyvCT7KuAYOUXls9gZGB2rcfZuTQ

Uploaded by

techprai77
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
You are on page 1/ 2

Practical-2

Aim: Implement the concept of Command line arguments and Scanner Class in
Java.
a. WAP to find the average and sum of the N numbers Using Command line
arguments.
b. WAP to calculate the Simple Interest and Input by the user.

Theory: In Java, input types are typically handled through different methods depending
on the context of the application. Here are two common approaches:

Java command-line argument is an argument i.e. passed at the time of running the Java
program. In Java, the command line arguments passed from the console can be
received in the Java program and they can be used as input. The users can pass the
arguments during the execution bypassing the command-line arguments inside the
main() method.

Part A : Program:

import java.util.Scanner;

public class SumAverageCalculator {


public static void main(String[] args) {
// Part A: Using Command Line Arguments
if (args.length > 0) {
int sum = 0;
for (String arg : args) {
try {
int num = Integer.parseInt(arg);
sum += num;
} catch (NumberFormatException e) {
System.out.println(arg + " is not a valid number.");
return;
}
}
double average = (double) sum / args.length;
System.out.println("Using Command Line Arguments:");
System.out.println("Sum: " + sum);
System.out.println("Average: " + average);
} else {
System.out.println("Please provide numbers as command line arguments.");
}

// Part B: Using Scanner Class


WAP to calculate the Simple Interest and Input by the user.
 Second method is getting user input using scanner class.
 The Scanner class is used to get user input, and it is found in the java.util package.
 To use the Scanner class, create an object of the class and use any of the available
methods found in the Scanner class documentation. In our example, we will use
the nextLine() method, which is used to read String

// Part B: Using Scanner Class


Scanner scanner = new Scanner(System.in);

System.out.println("\nUsing Scanner Class:");


System.out.print("Enter the Principal amount: ");
double principal = scanner.nextDouble();

System.out.print("Enter the Rate of Interest: ");


double rate = scanner.nextDouble();

System.out.print("Enter the Time period (in years): ");


double time = scanner.nextDouble();

double simpleInterest = (principal * rate * time) / 100;

System.out.println("Principal: " + principal);


System.out.println("Rate of Interest: " + rate);
System.out.println("Time Period: " + time);
System.out.println("Simple Interest: " + simpleInterest);

scanner.close();
}
}

Output:
Conclusion: Thus, we have performed Command line arguments and Scanner Class in
Java.

You might also like