Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Only 4 pennies You are programming a children s game where the most money they can hold is 4 pennies. Which of the following is

Only 4 pennies
You are programming a childrens game where the most money they can hold is 4 pennies. Which of the following is the most elegant way to print out a full list of store items the player can afford based on the number of pennies they have?
While elegant code has no firm definition, assume for now it means "uses cleverness to accomplish something in much less code, but in a way thats readable". It is also assumed that the code works as intended (which some of the below do not).
A. switch(pennies){
case 1: System.out.println("Marble (1 penny)"); break;
case 2: System.out.println("Pet rock (2 pennies)");
System.out.println("Marble (1 penny)"); break;
case 3: System.out.println("Jump rope (3 pennies)");
System.out.println("Pet rock (2 pennies)");
System.out.println("Marble (1 penny)"); break;
case 4: System.out.println("Goldfish (4 pennies)");
System.out.println("Jump rope (3 pennies)");
System.out.println("Pet rock (2 pennies)");
System.out.println("Marble (1 penny)"); break;
default: System.out.println("It looks like you need some pennies!");
}
B. switch(pennies){
case 1: System.out.println("Marble (1 penny)");
case 2: System.out.println("Pet rock (2 pennies)");
case 3: System.out.println("Jump rope (3 pennies)");
case 4: System.out.println("Goldfish (4 pennies)"); break;
default: System.out.println("It looks like you need some pennies!");
}
c. switch(pennies){
case 4: System.out.println("Goldfish (4 pennies)");
case 3: System.out.println("Jump rope (3 pennies)");
case 2: System.out.println("Pet rock (2 pennies)");
case 1: System.out.println("Marble (1 penny)"); break;
default: System.out.println("It looks like you need some pennies!");
}
D. switch(pennies){
case 4: System.out.println("Goldfish (4 pennies)"); break;
case 3: System.out.println("Jump rope (3 pennies)"); break;
case 2: System.out.println("Pet rock (2 pennies)"); break;
case 1: System.out.println("Marble (1 penny)"); break;
default: System.out.println("It looks like you need some pennies!");
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions