Create another method called get_Sound (). This method will receive the sounds[
ID: 3688149 • Letter: C
Question
Create another method called get_Sound(). This method will receive the sounds[ ] array, and an emotion. It will return the sound corresponding to the given emotion.
import java.util.Scanner;
public class Exercise4a{
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 Define_Emotion method
public static void main(String[] args){
String sound = "none";
String emotion = "none";
Scanner input = new Scanner(System.in);
System.out.print("Provide an sound = ");
sound = input.next();
System.out.println("The emotion is "+ Define_Emotion(sound));
}
}
Explanation / Answer
Here is the code for you:
import java.util.*;
public class SoundAndEmotion{
//Create another method called get_Sound()
//This method will receive the sounds[] array, and an emotion.
//It will return the sound corresponding to the given emotion.
public static String get_Sound(String[] sounds, String emotion)
{
for(int i = 0; i < sounds.length; i++) //For each sound.
if(Define_Emotion(sounds[i]) == emotion) //If that sound corresponds to the given emotion.
return sounds[i]; //Return the sound at that index.
return "None";
}
//Takes the sound as input, and returns the emotion.
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 Define_Emotion method
public static void main(String[] args){
String sound = "none";
String emotion = "none";
Scanner input = new Scanner(System.in);
System.out.print("Provide an sound = ");
sound = input.next();
System.out.println("The emotion is "+ Define_Emotion(sound));
System.out.print("Enter the emotion: ");
emotion = input.next();
String[] Emotions = {"laugh", "worried", "hopeful", "hungry", "festive"};
System.out.println("The sound is "+ get_Sound(Emotions, emotion));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.