In Java You are awarded from the SDSU Bookstore 1000 You can
In Java,
You are awarded from the SDSU Bookstore $10.00! You can spend this money on pencils, pens, and notebooks. They currently have n pencils, m pens, and k notebooks is stock.
The prices are as follows:
pencils: $0.12
pens: $0.90
notebooks: $3.50
How many different combinations of items can be purchased? Not all of the money has to be spend, a combination is valid as long as the total price is less than or equal to the $10.00 limit, this includes not purchasing anything at all.
Solving the problem in steps:
Read in integer values for the number of pencils, pens, and notebooks in stock.
(For pencils you would use: int n = scan.nextInt();)
You don\'t need to worry about the composition of the combinations just the number of combinations so you will want to use a counter(for example int count = 0;) to track when a valid combination has occurred.
You will be iterating through each combination to check if it is valid. Note: this assignment is to use nested loops, in the end of this program you will have 3 nested for loops. That is the loops will be inside each other.
Start with a loop for pencils. You can have a range of 0-n pencils. Therefore, your loop will start at 0 and end once n has been exceeded: for(int pencils = 0; pencils <=n; pencils++)
for loop explained:
pencils starts at 0; checks that pencil is less than or equal to n (goes through loop in the case of true); then increments the value of pencil after the iteration
Note: the loop ends when the middle parameter fails (results in false). In this case, the loop stops when the value of pencils is equal to n+1 (when pencils <= n fails because pencils is greater than n).
The next loop will track the number of pens and will be inside the pencil for loop. The range for pens is 0-m. Write the loop that will iterate through the number of pens.(Hint: modify the for loop parameters in the previous step to work for pens.)
The last loop is for the notebooks and will be inside the pens for loop. The range for notebooks is 0-k. Write the loop that will iterate through the number of notebooks.
Inside the most inner loop, you will increment your counter if (Hint: it\'s bold for a reason) the total price of the purchase is less than or equal to the $10.00. The expression that compares the total price to the $10.00 is:
(pencils*12 + pens*90 + notebooks*350) <= 1000
Print out the number of combinations with this print statement:
System.out.println(\"The number of combinations: \" + counter);
Solution
/*
* The java program SDSUBookStore that promtps for number of penciels,
* pen and notebooks. Then finds the number of combinations of award
* price ,10 for given input and print to console.
* */
//SDSUBookStore.java
import java.util.Scanner;
public class SDSUBookStore {
public static void main(String[] args) {
final double PENCIL_COST=0.12;
final double PEN_COST=0.90;
final double NOTEBOOK_COST=3.50;
//award price is 10 dollars
final int AWARD_PRICE=10;
Scanner scanner=new Scanner(System.in);
//set to zero
int numPencils=0;
int numPens=0;
int numNoteooks=0;
int counter=0;
System.out.println(\"Enter number of penciles : \");
numPencils=Integer.parseInt(scanner.nextLine());
System.out.println(\"Enter number of pen\'s : \");
numPens=Integer.parseInt(scanner.nextLine());
System.out.println(\"Enter number of Notebooks : \");
numNoteooks=Integer.parseInt(scanner.nextLine());
//outer for loop for pencil
for (int pencil = 1; pencil <=numPencils; pencil++) {
//middle for loop for pen
for (int pen = 1; pen <=numPens; pen++) {
//inner for loop for notebook
for (int notebook = 1; notebook <=numNoteooks; notebook++) {
//check if total cost is less than 10dollar (1000 in cents)
if(pencil*PENCIL_COST+pen*PEN_COST+notebook*NOTEBOOK_COST<=AWARD_PRICE*10)
counter++;
}
}
}
//print number of combinations that match the given awarded price
System.out.println(\"The number of combinations: \" + counter);
}
}
Sample output:
Enter number of penciles :
10
Enter number of pen\'s :
5
Enter number of Notebooks :
2
The number of combinations: 100

