Java Bottles

Report 5 Downloads 43 Views
 

TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________ Name:

Date:

Period:

Java - Bottles Directions​ : After working through the Scratch version of the ​ Bottles​ program, it’s time to see if we can do it in Java. First, take a look at the pseudocode and then the corresponding flowchart. Once you feel you understand the steps involved in the algorithm, try to write the program in Java. Mr. Colestock has provided you with a skeleton program (and hints) you can use. Complete it using the NetBeans IDE, then fill in the missing code in the space provided below. Pseudocode set ​ bottles​ to 10 initialize ​ suffix if ​ bottles​ is greater than or equal to 0 if ​ bottles​ is equal to 1 set value of ​ suffix​ to “” else set value of ​ suffix​ to “s” display value of ​ bottles​ + “bottle” + value of ​ suffix​ + “ of Root Beer on the wall, “ + value of ​ bottles ​ + “ bottle” + value of ​ suffix​ + “ of Root Beer“ decrease ​ bottles​ by 1 if ​ bottles​ is less than 0 print “Go to the store, buy some more, 10 bottles of Root Beer on the wall” else if ​ bottles​ is equal to 1 set value of ​ suffix​ to “” else set value of ​ suffix​ to “s” print “Take one down, pass it around, " + value of ​ bottles ​ + “bottle” + value of ​ suffix​ + “ of Root Beer on the wall“ repeat

Page 1 of 3

 

TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________ Here is the flowchart which shows the step-by-step solution to the algorithm.

Page 2 of 3

 

TURN INTO YOUR CLASS’ BASKET BY THE BEGINNING OF CLASS ON _________________ Let’s Do it in Java! Write a class named ​ Bottles​ that implements the algorithm. Place your class in the com.colestock.example​ package and place all of your code inside the ​ main​ method. Bottles.java package com.colestock.example;    public class Bottles {       public static void main(String[] args) {                                                  }    }    Hint​ : Use a ​ while​ loop to solve this problem. The syntax and an example are listed below. while (​ ​ ) {        }    while (counter > 0){     System.out.println("Counter is: " + counter);     counter­­;  } Page 3 of 3