Exercise: Paintball Halloween It’s Halloween. You hate Halloween. You decide to
ID: 3821084 • Letter: E
Question
Exercise: Paintball Halloween
It’s Halloween.
You hate Halloween.
You decide to shoot unpleasant paintballs at people who enter your front gate. You have two laser-targeted paintball turrets by your door. You call them Stinker and Red. Stinker shoots balls filled with liquid artificial skunk stench. Red shoots balls filled with artificial blood.
You decide every adult who enters your gate will get three skunkballs, and five bloodballs. Every teenage will get four skunkballs, and three bloodballs. Every tween will get one skunkball, and one bloodball. Younger kids won’t be shot at. Their turn will come.
You use high-quality paintballs. Skunkballs cost you $0.25 each, and bloodballs $0.07 each.
Write a program to compute the number of each ball type you’ll need and their total cost, given a number of adults, teens, and tweens. Here’s a screenshot:
Round the cost to the neatest cent. It’s OK to leave off the last digit if it’s zero, as in the sample output above. Check the rounding numbers hint; it might be useful.
All inputs should be numeric, and not less than zero. Your program should show an error message and stop when there’s an error. For example:
Your program can stop immediately when it detects an error, or show all applicable error messages and then stop. Your choice.
You must use subroutines. Use three at least: input, processing, and output. You may use more if you want.
The usual coding standards apply.
1 Halloween Planner 3 Adults: 12 32 4 Teenagers: 5 Tweens: 39 Run 9 Skunk balls: 203 10 Blood balls: 195 12 Total cost: 64.4Explanation / Answer
import java.util.Scanner;
class PaintBall
{
//all the variables
int adults, teenagers, tweens = 0;
int skunkBalls, bloodBalls = 0;
double skunkBallPrice = 0.25;
double bloodBallsPrice = 0.07;
double totalPrice = 0.0;
//take input from user
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("How many adults ? ");
adults = sc.nextInt();
System.out.println("How many teenagers ?");
teenagers = sc.nextInt();
System.out.println("How many Tweens ?");
tweens = sc.nextInt();
}
//process input and calculate number of balls and cost
public boolean processing()
{
if(teenagers < 0)
{
System.out.println("Teenagers cant be less than 0");
return false;
}else{
for(int i=0; i<teenagers; i++){
skunkBalls += 4;
bloodBalls += 3;
}
}
if(adults < 0)
{
System.out.println("Adults cant be less than 0");
return false;
}else{
for(int i=0; i<adults; i++){
skunkBalls += 3 ;
bloodBalls += 5;
}
}
if(tweens < 0)
{
System.out.println("Tweens cant be less than 0");
return false;
}else{
for(int i=0; i<tweens; i++){
skunkBalls += 1;
bloodBalls += 1;
}
}
double temp = 0;
temp = skunkBalls * skunkBallPrice;
double temp1 = 0;
temp1 = bloodBalls * bloodBallsPrice;
totalPrice = temp + temp1;
return true;
}
//display
public void output()
{
System.out.println("Total skunkBalls needed: " + skunkBalls);
System.out.println("Total bloodBalls needed: " + bloodBalls);
System.out.println("Total cost: " + totalPrice);
System.out.println("Total Roundoff cost: " + Math.round(totalPrice) );
}
}
public class MainClass
{
public static void main(String[] args)
{
PaintBall p = new PaintBall(); //create object of class
p.input(); //call
boolean b = p.processing(); //returns TRUE if and onlf if eveything is correct.
if(b) //if true, show output
p.output();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.