1. Five students at a local middle school volunteered to sell fresh baked cookie
ID: 3736542 • Letter: 1
Question
1. Five students at a local middle school volunteered to sell fresh baked cookies to raise funds to increase the number of computers for the computer lab. Each student reported the number of boxes he/she sold. Cost of each box of cookies was $4.50. Write a program that will output the total number of boxes of cookies sold, the total revenue generated by selling the cookies, and the average number of boxes sold by the students. Prompt the user to enter the following information: the StudentID number and the number of boxes sold.
2. In the program above you know the exact number of student volunteers. Now suppose that the programmer does not know the exact number of volunteers. Once again, assume that the data is in the following form, i.e. the student’s ID number and the number of boxes sold by the student is provided by the user. Prompt the user to enter this information as in Question 1, but here, you do not know the exact number of volunteers, instead -1 will mark the end of the data entered.
3. Finally, redo question 2, but this time declare a Boolean variable and use it to control the while loop
Q1. Use count controlled while loop or for loop
Q2. Use sentinel value controlled while loop
Q3. Use a boolean flag controlled while loop.
Explanation / Answer
Using java
Q1
import java.util.Scanner;
class fund
{
public static void main(String args[])
{
int stid[]=new int[5];
int boxsold[]= new int[5];
Scanner sc= new Scanner(System.in);
int i=0;
for(i=0;i<5;i++)
{ System.out.println("Please enter student id ");
stid[i]= sc.nextInt();
System.out.println("Please enter number of boxes sold");
boxsold[i]=sc.nextInt();
}
double totalRevenue,totalBoxesSold=0,avgBoxesSold;
for(i=0;i<5;i++)
{
totalBoxesSold = totalBoxesSold + boxsold[i];
}
totalRevenue=totalBoxesSold*4.50;
avgBoxesSold=totalBoxesSold/5;
System.out.println("total Boxes Sold by student is " +totalBoxesSold);
System.out.println("total revenue collected is "+totalRevenue);
System.out.println("Average Boxes sold"+avgBoxesSold);
}
}
question 2
import java.util.Scanner;
class fund1
{
public static void main(String args[])
{ Scanner sc= new Scanner(System.in);
System.out.println("Please enter total student ");
int n=sc.nextInt();
int stid[]=new int[n];
int boxsold[]= new int[n];
int i=0;
while(i<n)
{ System.out.println("Please enter student id ");
stid[i]= sc.nextInt();
System.out.println("Please enter number of boxes sold");
boxsold[i]=sc.nextInt();
i++;
}
double totalRevenue,totalBoxesSold=0,avgBoxesSold;
i=0;
while(i<n)
{
totalBoxesSold = totalBoxesSold + boxsold[i];
i++;
}
totalRevenue=totalBoxesSold*4.50;
avgBoxesSold=totalBoxesSold/5;
System.out.println("total Boxes Sold by student is " +totalBoxesSold);
System.out.println("total revenue collected is "+totalRevenue);
System.out.println("Average Boxes sold"+avgBoxesSold);
}
}
Question 3
import java.util.Scanner;
class fund2
{
public static void main(String args[])
{ Scanner sc= new Scanner(System.in);
System.out.println("Please enter total student ");
int n=sc.nextInt();
int stid[]=new int[n];
int boxsold[]= new int[n];
int i=0;
boolean done=false;
while(!done)
{ System.out.println("Please enter student id ");
stid[i]= sc.nextInt();
System.out.println("Please enter number of boxes sold");
boxsold[i]=sc.nextInt();
i++;
if(i==n)
done=true;
}
double totalRevenue,totalBoxesSold=0,avgBoxesSold;
i=0;
done=false;
while(!done)
{
totalBoxesSold = totalBoxesSold + boxsold[i];
i++;
if(i==n)
done=true;
}
totalRevenue=totalBoxesSold*4.50;
avgBoxesSold=totalBoxesSold/5;
System.out.println("total Boxes Sold by student is " +totalBoxesSold);
System.out.println("total revenue collected is "+totalRevenue);
System.out.println("Average Boxes sold"+avgBoxesSold);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.