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

Java Lab Exercises 1

Uploaded by

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

Java Lab Exercises 1

Uploaded by

ARYA BHAGAT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Date – 06-02-2024

Use Command Line Arguments and Scanner Class for Input


Exercise 1: Reverse and Print Arguments
Problem Statement:

• Write a Java program that takes an arbitrary number of command-line arguments and
prints them in reverse order.
• If no arguments are provided, the program should print a message indicating that no
arguments were passed.

Learning Objective:

• Understand how to access and manipulate elements of the args array.

Sample Execution:

Input: java ReverseArguments One Two Three

Output:

Three Two One

Exercise 2: Calculate Average of Numbers


Problem Statement:

• Create a Java program that accepts a series of integers as command-line arguments and
calculates their average.
• The program should handle any incorrect input (non-integer values) gracefully by
displaying an appropriate error message.
• If no integers are provided, it should inform the user.

Learning Objective:

• Practice type conversion.


Sample Execution:

• Input: java AverageNumbers 4 8 15 16 23 42


• Output: Average: 18.0

Exercise 3: Check for Palindrome


Problem Statement:

• Develop a Java program that takes a single string as a command-line argument and
checks whether the string is a palindrome (a string that reads the same backward as
forward).
• The program should print a message indicating whether the input string is a palindrome
or not.
• If no argument or more than one argument is provided, the program should display a
usage message.

Learning Objective:

• Utilize string manipulation techniques and understand command-line argument


validation.

Sample Execution:

• Input: java PalindromeChecker racecar


• Output: racecar is a palindrome.

You might also like