A two-part Java problem that I am having a hard time with... sorry it\'s so long
ID: 3738603 • Letter: A
Question
A two-part Java problem that I am having a hard time with... sorry it's so long, I'm really lost:(
1) Write the following static methods. Assume the reference variable input has already been declared for the Scanner class:
a) A method called setName that prompts for a child’s name and returns it:
b) A method called setSpecialPresent that accepts a child’s name, prompts for the present that the child wants the most and returns the present. Example: What is the present Xxxxxxxx wants the most? where the Xs is the child’s name.
c) A method called behavior that receives a child’s name, prompts for the child’s behavior, and returns whether the child has been good or bad as a boolean. Example: Has Xxxxxxxx been good? Enter ‘Y’ or ‘N’: where the Xs is the child’s name.
d) A method called letterGrade that receives the child’s name and prompts for the letter grade of all combined schoolwork. The letter grade is returned. Example: What is Xxxxxxxx’s letter grade for all combined schoolwork? where the Xs is the child’s name.
e) A method called authorizeBDayParty that accepts a letter grade, the child’s name, and behavior to print the following when:
The letter grade is an ‘A’ and the behavior is good:where the first set of Xs is the child’s name & the 2nd set the special present.
Xxxxxxxx gets to have an awesome birthday party this year!You’ll be getting a(n) Xxxxxxxx!!!
The letter grade is a ‘B’ and the behavior is good:where the Xs is the child’s name.
Xxxxxxxx gets to have a birthday party this year with a small group of friends!
The letter grade is a ‘C’ and the behavior is good:where the Xs is the child’s name.
Xxxxxxxx, we’re having a family birthday celebration for you
Any letter grade under a ‘C’ regardless of behavior:where the Xs is the child’s name.
Xxxxxxxx, your overall grade or behavior has been poor, so there’ll be NO birthday party for you this year!
2) Write statements that will call each of the methods you coded above.OR write only 2 Java statements that call 4 of the 5 methods with the assumption that the 5th method is called in one of the 5.
a.
b.
c.
d.
e.
Explanation / Answer
Java program for the given requirement:
import java.util.*;
public class BirthDayParty
{
static Scanner in = new Scanner(System.in);
public static void main(String[] args)
{
//a) A method called setName that prompts for a child’s name and returns it:
String childName=setName();
String present=setSpecialPresent(childName);
boolean behavior=behavior(childName);
char letterGrade=letterGrade(childName);
authorizeBDayParty(childName,behavior,letterGrade);
}
public static String setName()
{
//prompts for a child's name
System.out.print("Enter child's name:");
//storing
String name=in.nextLine();
//returning
return name;
}
public static String setSpecialPresent(String childName)
{
//prompts for present
System.out.print("What is the present "+childName+" wants the most?");
//reading and storing
String present=in.nextLine();
//returning
return present;
}
public static boolean behavior(String childName)
{
//prompts for present
System.out.print("Has "+childName+" been good?");
//reading and storing
char behavior=in.nextLine().charAt(0);
boolean res=false;
if(behavior=='Y' || behavior=='y')
res=true;
else if(behavior=='N' || behavior=='n')
res=false;
//returning
return res;
}
public static char letterGrade(String childName)
{
//prompts for present
System.out.print("What is "+childName+"'s letter grade for all combined schoolwork?");
//reading and storing
char letterGrade=in.nextLine().charAt(0);
//returning
return letterGrade;
}
public static void authorizeBDayParty(String childName,boolean behavior,char letterGrade)
{
//if the letterGrade is 'A' and the behavior is good
if(letterGrade=='A' && behavior==true )
{
System.out.println(childName+" gets to have an awesome birthday party this year!You’ll be getting a(n) awesome!!!");
}
//if the letter grade is a ‘B’ and the behavior is good
else if(letterGrade=='B' && behavior==true )
{
System.out.println(childName+" gets to have a birthday party this year with a small group of friends!");
}
//if the letter grade is a ‘C’ and the behavior is good
else if(letterGrade=='C' && behavior==true )
{
System.out.println(childName+", we’re having a family birthday celebration for you.");
}
else
{
System.out.println(childName+", your overall grade or behavior has been poor, so there’ll be NO birthday party for you this year!");
}
}
}
Output:
Enter child's name:Jean
What is the present Jean wants the most?Birth Day Dress
Has Jean been good?Y
What is Jean's letter grade for all combined schoolwork?A
Jean gets to have an awesome birthday party this year!You’ll be getting a(n) awesome!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.