Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Exercises: Java Command Line Arguments

 

[post-views]

In this lesson, you will learn.

  • Exercises

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.

 


End of the lesson….enjoy learning

Student Ratings and Reviews

Submit a Review