I need help writting a program in Java it is from How to Program tenth eidtion c
ID: 2246364 • Letter: I
Question
I need help writting a program in Java it is from How to Program tenth eidtion chapter 2.
This program should calculate the packing of Candles. First read:
The number of candles
The number of candles that fit in each case
Then calculate and print:
The number of full cases
The number of candles left over (partial case)
If this order is large (more than 5 full cases needed)
An extra message at the end to say you are done
Requirements:
Put System.out.println() after each call to nextInt().This will help you match the Test Program and make it easier to use it.
Even though you can do this with less variables, make sure you declare and use a variable for the number of full cases and another for the number left over
Sample Run #1: (the highlighted text is what the user types)
Num candles? 20
Candles per case? 8
2 cases 4 leftover
Done!
Sample Run #2: (the highlighted text is what the user types)
Num candles? 40
Candles per case? 6
6 cases 4 leftover
*** large order
Done!
Extra Notes:
Did you correctly name the package/folder?
Did you correctly name the class/file?
Did you include comments?
Explanation / Answer
Program :
import java.util.Scanner;
class candles{
// main function defination
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter Number of candles: ");
int noc = in.nextInt(); //read number of candles
System.out.println("Enter Number of candles per case: ");
int candles_per_case = in.nextInt(); //read Number of candles per case:
int numcases= noc/candles_per_case ; // calculate full cases
int partialcase = noc-numcases*candles_per_case ; //calculate left over cases
System.out.println(numcases+"cases "+partialcase +" leftover"); //dispaly output
if(numcases<=5)
System.out.println("Done!");
else
{
System.out.println("*** large order");
System.out.println("Done!");
}
}
}
Output :
Enter Number of candles:
60
Enter Number of candles per case:
8
7 cases 4 leftover
*** large order
Done!