Question: JAVA: How do I write this program using Mod % and Shift >> to print out each possible pizza with the following price? Here is

JAVA: How do I write this program using Mod % and Shift >> to print out each possible pizza with the following price? Here is my code.. import java.util.*; // this includes some stuff we need public class pizza04 // this is the name of the program... the file we save this code in must match the class name { public static void main(String args[]) // the 'main' method of the program { int i, j; // a variable to hold a whole number, or integer int n; double pizzaCount, thisPizzaCost, baseCost; System.out.println("Welcome to thde Pizza Food Cost Program."); System.out.println("Please enter the number of ingredients: "); n = InputUtils.GetInt(); // get input from the keyboard // now that we know what 'n' is, we can calculate pizzaCount pizzaCount = Math.pow(2,n); // .. and we can create storage space for the ingredient name String ingredients[]; ingredients = new String[n]; //n is length of ingredients // .. and we can create storage space for the ingredient costs double costs[]; costs = new double[n]; System.out.println("What is the base cost of a pizza: "); baseCost = InputUtils.GetDbl(); // now, loop n times to get the ingredient names and costs for ( i = 0; i < n; i++ ) { System.out.println("Please enter the name of ingredient " + i + ": "); ingredients[i] = InputUtils.GetStr(); System.out.println("Thanks. Now, please enter the food cost of " + ingredients[i] + ": "); costs[i] = InputUtils.GetDbl(); } // let's loop (2 to the n) times in order to analyze each possible pizza... for ( i = 0; i < pizzaCount; i++ ) { System.out.println("looking at pizza number: " + i ); thisPizzaCost = baseCost; // look at each possible ingredient (there are 'n' of them) on this pizza for ( j = 0; j < n; j++ ) { // Here we are using mod rather than & operater. //Left shift is the same as multiplying by 2, while right shift is integer divide by 2 if ( ( i % (int)Math.pow(2,j) + 1) > 0 ) // isolate bit j { System.out.println(ingredients[j] + " is on the pizza"); thisPizzaCost += costs[j]; } } // end ingredient loop System.out.println("Pizza " + i + " costs " + thisPizzaCost); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!