Question

Complete method printPopcornTime(), with int parameter bagOunces, and void return type. If bagOunces is less than...

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 void printPopcornTime(int bagOunces) {

/* Your solution goes here */

}

public static void main (String [] args) {
PopcornTimer popcornBag = new PopcornTimer();
popcornBag.printPopcornTime(7);
}
}

Answer in JAVA where it says solution goes here

Homework Answers

Answer #1

The complete Java source code is given below.

import java.util.Scanner;

public class PopcornTimer
{
    public void printPopcornTime(int bagOunces)
    {
        //if loop to check whether bagOunces value is less than 3 or not
        //if it is true, then if stmt will execute otherwise else part will execute
        if(bagOunces<3)
            System.out.println("Too small");    //print a message
        else
        {   System.out.print(6*bagOunces+" seconds\n");     //print the output
          
        }
    }

    public static void main (String [] args)
    {
        PopcornTimer popcornBag = new PopcornTimer();
        popcornBag.printPopcornTime(7);
    }
}

If the indendations are not clear, then please refer the screenshot of the code given below.

The screenshot of the output is also given below.

Explanation

  • The printPopcornTime function takes an argument bagOunces which is an integer.
  • There are two conditions as per the problem. So, an if else statement can be used. If the "if" condition is true, then the statements under it will get executed and the else part will be ignored completely. But if the "if" condition is false, then the statements under the else gets executed.
  • If the value of bagOunces is less than 3, then a message "Too small" will be displayed using the println function.
  • If the above condition was not satisfied, then it will print the value 6 * bagOunces and a "seconds" message will be also displayed.

Hope this helps. Doubts, if any, can be asked in the comment section.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Print "userNum1 is negative." if userNum1 is less than 0. End with newline.Convert userNum2 to 0...
Print "userNum1 is negative." if userNum1 is less than 0. End with newline.Convert userNum2 to 0 if userNum2 is greater than 10. Otherwise, print "userNum2 is less than or equal to 10.". End with newline. public class UserNums {    public static void main (String [] args) {       int userNum1;       int userNum2;       userNum1 = -1;       userNum2 = 13; //solution goes here//       System.out.println("userNum2 is " + userNum2);    } }
IN JAVA Print "userNum1 is negative." if userNum1 is less than 0. End with newline. Convert...
IN JAVA Print "userNum1 is negative." if userNum1 is less than 0. End with newline. Convert userNum2 to 0 if userNum2 is greater than 10. Otherwise, print "userNum2 is less than or equal to 10.". End with newline. public class UserNums { public static void main (String [] args) { int userNum1; int userNum2; userNum1 = 0; userNum2 = 7; *insert* System.out.println("userNum2 is " + userNum2); } }
in JAVA Write a switch statement that checks origLetter. If 'a' or 'A', print "Alpha". If...
in JAVA Write a switch statement that checks origLetter. If 'a' or 'A', print "Alpha". If 'b' or 'B', print "Beta". For any other character, print "Unknown". Use fall-through as appropriate. End with newline. import java.util.Scanner; public class ConvertToGreek { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); char origLetter; origLetter = scnr.next().charAt(0); /* Your solution goes here */ } }
4.9.1: Nested loops: Indent text. JAVA Print numbers 0, 1, 2, ..., userNum as shown, with...
4.9.1: Nested loops: Indent text. JAVA Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints: 0 1 2 3 Please use my template import java.util.Scanner; public class...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1 2 3 4 import java.util.Scanner; public class ForLoops { public static void main (String [] args) { int userNum; int i; Scanner input = new Scanner(System.in); userNum = input.nextInt(); for (/* Your code goes here */) { System.out.print(i + " "); } } } Need to fill out the "for" solved in JAVA
Start with the code below and complete the getInt method. The method should prompt the user...
Start with the code below and complete the getInt method. The method should prompt the user to enter an integer. Scan the input the user types. If the input is not an int, throw an IOException; otherwise, return the int. In the main program, invoke the getInt method, use try-catch block to catch the IOException. import java.util.*; import java.io.*; public class ReadInteger { pubilc static void main() { // your code goes here } public static int getInt() throws IOException...
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
Write an application that asks a user to enter an integer. Display a statement that indicates...
Write an application that asks a user to enter an integer. Display a statement that indicates whether the integer is even or odd. ------------------------------------------ import java.util.Scanner; class EvenOdd {     public static void main(String[] args) {         // Write your code here     }     public static boolean isEven(int number) {     } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read the input string String input = sc.nextLine(); BalancedParentheses bp = new BalancedParentheses(); // Print whether the string has balanced parentheses System.out.println(bp.hasBalancedParentheses(input)); } } class BalancedParentheses { public boolean hasBalancedParentheses(String input) { // Remove this and implement code throw new UnsupportedOperationException(); } }
Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over...
Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over 50. Print the result to the console using System.out.println(); The file will only have numbers. code below import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; public class ReadingFiles { public static void main(String[] args) throws FileNotFoundException { System.out.println(totalEven); } }