Complete method printPopcornTime(), with int parameter bagOunces, and void retur
ID: 3821881 • Letter: C
Question
Complete method printPopcornTime(), with int parameter bagOunces, and void return type. If bagOunces is less than 3, print "Too small". If greater than 10, print "Too large". Otherwise, compute and print 6 * bagOunces followed by "seconds". End with a newline. Example output for ounces = 7: 42 seconds import java.util.Scanner; public class PopcornTimer {public static void printPopcornTime(int bagOunces) {/* Your solution goes here */} public static void main (String ? args) {printPopcornTime(7); return;}}|Explanation / Answer
import java.util.Scanner;
public class PopcornTimer //This the class PopcornTimer
{
public static void printPopcornTime(int bagOunces) //Defined method
{
if(bagOunces<3) //if condition for bagBouces lessthan 3 it takes the input and checks if it lessthan 3 or not
{
System.out.println("Too Small"); //It prints Too Small if input is less than 3
}
else if(bagOunces>=3 && bagOunces<=10) //If the above condition fails this statement takes the input and checks if value is in between 3 and 10
{
int a = 6*bagOunces; // It computes the time takes for popcorn using give condtion 6*bagOunces
System.out.println(+a+"seconds");
}
else
{
System.out.println("Too Large"); //It the input is larger than 10 then it prints Too Large
}
}
public static void main(String[] args) // main statement
{
Scanner s = new Scanner(System.in); //Scanner class is used to take the input from user through keyboard,assined to a temp variable here "s" to the input is stored in s
System.out.println("Enter your bagOunces:"); //It prints the specified statemet in ""
int bagOunces = s.nextInt(); //Takes the integer entered by the user and stores it in bagOunces type
printPopcornTime(bagOunces); // call the method printPopcornTime
return;
}
}
sample output :
5
30 seconds
Note : save the file with PopcornTimer.java and compile else remove the pulic from the class statement
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.