Java
Java

 

 

Introduction:

In this program, we aim to create a simple Java application that displays a random quote from a predefined list of quotes. This type of program helps to learn basic Java programming concepts such as working with arrays, using the Random class, and displaying output to the user.

Objective:

The objective of this exercise is to demonstrate how to implement randomness in Java. Specifically, the program will randomly select one quote from an array of quotes and display it each time it is executed.

Code:

        // RandomQuote.java
        import java.util.Random;

        public class RandomQuote {
            public static void main(String[] args) {
                // Array of quotes
                String[] quotes = {
                    "The only way to do great work is to love what you do. - Steve Jobs",
                    "Life is what happens when you're busy making other plans. - John Lennon",
                    "To be yourself in a world that is constantly trying to make you something else is the greatest accomplishment. - Ralph Waldo Emerson",
                    "In the end, it's not the years in your life that count. It's the life in your years. - Abraham Lincoln",
                    "Success is not final, failure is not fatal: It is the courage to continue that counts. - Winston Churchill"
                };

                // Create a Random object
                Random random = new Random();

                // Generate a random index to select a quote
                int randomIndex = random.nextInt(quotes.length);

                // Display the randomly selected quote
                System.out.println("Random Quote: ");
                System.out.println(quotes[randomIndex]);
            }
        }

Explanation of Program Structure:

The program consists of the following parts:

  • Import Statement: We import the Random class from java.util package to generate random numbers.
  • Array of Quotes: The program stores a list of quotes in a String array. Each quote is a string element in the array.
  • Random Object: A Random object is created to generate random numbers.
  • Random Index: The nextInt() method of the Random class is used to generate a random index between 0 and the length of the quotes array.
  • Output: The randomly selected quote is displayed to the user using System.out.println().

How to Run the Program:

  1. Step 1: Open your preferred Java development environment (IDE) or text editor.
  2. Step 2: Create a new file and save it as RandomQuote.java.
  3. Step 3: Copy and paste the provided code into your file.
  4. Step 4: Compile the program by running the following command in your terminal or IDE:
    javac RandomQuote.java
  5. Step 5: Run the compiled program with the command:
    java RandomQuote
  6. Step 6: You should see a randomly selected quote displayed on the console each time you run the program.
© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)