Question: / * * * @author Srimathi Vadivel * @author Sarah Benedicto * / public class FruitCosts { / * * * Main function to execute
@author Srimathi Vadivel
@author Sarah Benedicto
public class FruitCosts
Main function to execute the program
@param args commandline arguments, where args is the file name to read from
public static void mainString args
Do not remove this line, it opens the file for reading.
StdIn.setFileargs;
Reads amount of fruits from the input file
int numberOfFruits StdIn.readInt;
Creates parallel arrays for fruits and costs
String fruitNames new StringnumberOfFruits;
double fruitCosts new doublenumberOfFruits;
Populate the arrays
for int i ; i numberOfFruits; i
fruitNamesi StdIn.readString; Read fruit name
fruitCostsi StdIn.readDouble; Read fruit cost
Find fruits with the lowest costs
int firstMinIndex ;
int secondMinIndex ;
Ensure indices are correct
if fruitCosts fruitCosts
firstMinIndex ;
secondMinIndex ;
For loop in the array
for int i ; i numberOfFruits; i
if fruitCostsi fruitCostsfirstMinIndex
secondMinIndex firstMinIndex;
firstMinIndex i;
else if fruitCostsi fruitCostssecondMinIndex
secondMinIndex i;
Output the fruit names and costs of lowest
System.out.printlnfruitNamesfirstMinIndex fruitCostsfirstMinIndex;
System.out.printlnfruitNamessecondMinIndex fruitCostssecondMinIndex;
Calculates and prints the total cost
double totalCost fruitCostsfirstMinIndex fruitCostssecondMinIndex;
System.out.printfTotal f
totalCost;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
