Assignment 4b: modify this java code for creating an emotion[ ] arrays as well.
ID: 3688152 • Letter: A
Question
Assignment 4b: modify this java code for
creating an emotion[ ] arrays as well.
import java.util.Scanner;
public class Exercise4b {
public static String Define_Emotion(String sound){
String emotion="none"; //init a string variable called emotion
switch(sound){
case "laugh":
emotion = "happy";
break;
case "sneeze":
emotion = "worried";
break;
case "sigh":
emotion = "hopeful";
break;
case "yummy":
emotion = "hungry";
break;
case "cheers":
emotion = "festive";
break;
//default:System.out.println("Invalid sound");
}//switch
return emotion;
}//end of the Define_Emotion method
public static void main(String[] args){
String sound = "none";
String emotion = "none";
String[] sounds = new String[20];
int userInput = 0;
Scanner input = new Scanner(System.in);
System.out.print("Provide a sound = ");
sound = input.next();
while((!(sound.equals("whistle")))&&(userInput<20)){
sounds[userInput]=sound;
sound = sounds[userInput];
emotion = Define_Emotion(sound);
System.out.println("The emotion is "+ emotion);
userInput++; //userInput = userInput + 1;
System.out.print("Provide a sound = ");
sound = input.next();
}
}//end of the main method
}
Explanation / Answer
Exercise4b.java
package org.students;
import java.util.Scanner;
public class Exercise4b {
public static String Define_Emotion(String sound){
String emotion="none"; //init a string variable called emotion
switch(sound){
case "laugh":
emotion = "happy";
break;
case "sneeze":
emotion = "worried";
break;
case "sigh":
emotion = "hopeful";
break;
case "yummy":
emotion = "hungry";
break;
case "cheers":
emotion = "festive";
break;
//default:System.out.println("Invalid sound");
}//switch
return emotion;
}//end of the Define_Emotion method
public static void main(String[] args){
String sound = "none";
String emotion = "none";
String emotions[]=new String[20];
String[] sounds = new String[20];
int userInput = 0;
Scanner input = new Scanner(System.in);
System.out.print("Provide a sound = ");
sound = input.next();
while((!(sound.equals("whistle")))&&(userInput<20)){
sounds[userInput]=sound;
sound = sounds[userInput];
emotion = Define_Emotion(sound);
emotions[userInput]=emotion;
//emotions
System.out.println("The emotion is "+ emotion);
userInput++; //userInput = userInput + 1;
System.out.print("Provide a sound = ");
sound = input.next();
}
//Loop which displays the sounds which are present in an array
System.out.println("The Sounds given by the user are ::");
for(int i=0;i<userInput;i++)
{System.out.println(sounds[i]);}
//Loop which displays the generated emotions which are present in an array
System.out.println("The list of Emotions generated are ::");
for(int i=0;i<userInput;i++)
{System.out.println(emotions[i]);}
}//end of the main method
}
_______________________________________________________________________________________
output:
until the user gave the sound 'whistle' the program will take the inputs or untill the user gave 20 sounds.
Provide a sound = cheers
The emotion is festive
Provide a sound = sigh
The emotion is hopeful
Provide a sound = yummy
The emotion is hungry
Provide a sound = sneeze
The emotion is worried
Provide a sound = laugh
The emotion is happy
Provide a sound = whistle
The Sounds given by the user are ::
cheers
sigh
yummy
sneeze
laugh
The list of Emotions generated are ::
festive
hopeful
hungry
worried
happy
____________________________________________________________________________________