Description: A variation of the first MultiGreeter assignment, this time working
ID: 3722456 • Letter: D
Question
Description:
A variation of the first MultiGreeter assignment, this time working with lists.
Assignment:
Create a MultiGreeter class with the following capabilities:
Feature
Signature
Requirement
Constructors
None needed
Methods
public static List<String>
getGreetings(List<String> names)
Returns a list of greetings of the form “Hello, name!” where the name is from the list of names provided. The order of names in the greetings will correspond to the order of names provided.
If the provided names list is empty, the list of greetings will contain only the default greeting “Hello, World!”
If the provided names list is null, the method will throw a NullPointerException.
Feature
Signature
Requirement
Constructors
None needed
Methods
public static List<String>
getGreetings(List<String> names)
Returns a list of greetings of the form “Hello, name!” where the name is from the list of names provided. The order of names in the greetings will correspond to the order of names provided.
If the provided names list is empty, the list of greetings will contain only the default greeting “Hello, World!”
If the provided names list is null, the method will throw a NullPointerException.
Explanation / Answer
package chegg;
import java.util.*;
public class MultiGreeter {
static int num;
public static String list(ArrayList<String> name) //list method which will return the name in a list
{
for(String s:name){
return s;
}
return null;
}
public static void getGreetings(ArrayList<String> name)
{
for(int i=0;i<num;i++)
{
System.out.println("hello "+name.get(i).toString()); // getGreeting method will return greeting to every name in a list
}
}
public static void main(String args[])
{
System.out.println("how many names you want to enter"); //enter the number of names enterd by the user
Scanner scan=new Scanner(System.in);
num=scan.nextInt();
System.out.println("please enter the list of names"); //entering the names by user
ArrayList<String> name=new ArrayList<String>();
for(int i=1;i<=num;i++)
{
name.add(scan.next());
}
list(name);
getGreetings(name);
}
}
// i am little bit confused whether you want a solution using linked list or array list. i can provide solution using linked list also and if you dont understand anything or if you want some change in a code kindly comment and please give a thumbs up to the answer :)