Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description: The classic “HelloWorld” program is a simple program used to illust

ID: 3841123 • Letter: D

Question

Description:

The classic “HelloWorld” program is a simple program used to illustrate the basic syntax of a programming language. In its simplest form, the program displays the text “Hello, World!” to the user. It can serve as a great starting point to explore and learn more complex concepts.

Assignment:

Create a MultiGreeter class with the following capabilities:

Feature

Signature

Requirement

Constructors

MultiGreeter()

No-argument constructor; greeter will have 1 default greeting “Hello”;

MultiGreeter(int maxGreetings)

Creates a greeter that can hold up to maxGreetings greeting strings. MaxGreetings must be non-negative--it can be 0.

Methods

getMaxGreetings() : int

Returns the maxGreetings, that is, the maximum number of greetings that can be added to this Greeter

getNumGreetings(): int

Returns the actual number of greetings added to this greeter.

addGreeting(String greeting) : boolean

Adds a greeting to this Greeter. If Greeter already has maxGreetings added, the greeting will not be added and the method will return false; otherwise the method will return true.

getGreetings(): String []

Returns an array containing the greeting strings added to this Greeter

greet(String name) : String

Returns a salutation in the form “greetingString, name!”, where

greetingString is one of the greeting strings added to the Greeter picked at random. If not greetings have been added, the system will use the default greeting "Hello"

name – a provided name.

greet() : String

As specified above, however the system will use the default name, “World”

Feature

Signature

Requirement

Constructors

MultiGreeter()

No-argument constructor; greeter will have 1 default greeting “Hello”;

MultiGreeter(int maxGreetings)

Creates a greeter that can hold up to maxGreetings greeting strings. MaxGreetings must be non-negative--it can be 0.

Methods

getMaxGreetings() : int

Returns the maxGreetings, that is, the maximum number of greetings that can be added to this Greeter

getNumGreetings(): int

Returns the actual number of greetings added to this greeter.

addGreeting(String greeting) : boolean

Adds a greeting to this Greeter. If Greeter already has maxGreetings added, the greeting will not be added and the method will return false; otherwise the method will return true.

getGreetings(): String []

Returns an array containing the greeting strings added to this Greeter

greet(String name) : String

Returns a salutation in the form “greetingString, name!”, where

greetingString is one of the greeting strings added to the Greeter picked at random. If not greetings have been added, the system will use the default greeting "Hello"

name – a provided name.

greet() : String

As specified above, however the system will use the default name, “World”

Explanation / Answer

Here is your program: -

import java.util.ArrayList;
import java.util.Random;

public class MultiGreeter {
   ArrayList<String> greetings;
   private static String defGreeting = "Hello";
   private int maxCapacity;

   MultiGreeter() {
       greetings = new ArrayList<>();
   }

   MultiGreeter(int maxGreetings) {
       greetings = new ArrayList<>(maxGreetings);
       maxCapacity = maxGreetings;
   }

   public int getMaxGreetings() {
       return maxCapacity;
   }

   public int getNumGreetings() {
       return greetings.size();
   }

   public boolean addGreeting(String greeting) {
       if (greetings.size() == maxCapacity) {
           return false;
       } else {
           greetings.add(greeting);
           return true;
       }
   }

   public String[] getGreetings() {
       return greetings.toArray(new String[greetings.size()]);
   }

   public String greet(String name) {
       if (greetings.size() > 0) {
           Random rand = new Random();
           int index = rand.nextInt(greetings.size());
           return greetings.get(index) + ", " + name + "" + "!";
       } else {
           return defGreeting+", " + name + "" + "!";
       }
   }

   public String greet() {
       if (greetings.size() > 0) {
           Random rand = new Random();
           int index = rand.nextInt(greetings.size());
           return greetings.get(index) + ", World" + "!";
       } else {
           return defGreeting+", World" + "!";
       }
   }
  
   public static void main(String[] args) {
       MultiGreeter grt = new MultiGreeter();
       MultiGreeter grt1 = new MultiGreeter(3);
       System.out.println(grt.greet());
       System.out.println(grt.greet("Sam"));
      
       if(!grt1.addGreeting("Voila!")) {
           System.out.println("Maximum Greetings are added");
       }
       if(!grt1.addGreeting("Namaste!")) {
           System.out.println("Maximum Greetings are added");
       }
       if(!grt1.addGreeting("Hii!")) {
           System.out.println("Maximum Greetings are added");
       }
       if(!grt1.addGreeting("Khushamedeed!")) {
           System.out.println("Maximum Greetings are added");
       }
      
       System.out.println(grt1.greet());
       System.out.println(grt1.greet("Sam"));
   }
}

Sample Output: -

Hello, World!
Hello, Sam!
Maximum Greetings are added
Hii!, World!
Voila!, Sam!

2.

Hello, World!
Hello, Sam!
Maximum Greetings are added
Voila!, World!
Namaste!, Sam!