package part3 import javaio import javautil public class Cer

package part3;
import java.io.*;
import java.util.*;

public class CerealRanker {

   public static float getMaxValue(float[] numbers){
       float maxValue = numbers[0];
       for(int i=1;i < numbers.length;i++){
           if(numbers[i] > maxValue){
               maxValue = numbers[i];
           }
       }
       return maxValue;
}
      
   public static float getMinValue(float[] numbers) {
       float minValue = numbers[0];
       for (int i = 1; i < numbers.length; i++) {
           if (numbers[i] < minValue) {
               minValue = numbers[i];
           }
       }
       return minValue;
   }
  
   public static float getNormalize(float value, float min, float max) {
       return (value - min) / (max - min);
   }
  
                                       // Trap for Input/Output errors
   public static void main(String[] args) throws java.io.IOException{

       // set up the buffered reader to read from the text file}
       Scanner fileScanner = new Scanner(new FileReader(\"CerealData.txt\"));
      
       /*
       -   Name
       -   Manufacturer
       -   Calories   - Low
       -   Protein   - High
       -   Fat       - Low
       -   Sodium   - Low
       -   Fiber       - High
       -   Carbs       - Low
       -   Sugars       - Low
       -   Potassium   - Ignore
       -   Vitamins   - High
       */
       String[] name = new String[74];
       String[] mfr = new String[74];
       float[] calories = new float[74];
       float[] protein = new float[74];
       float[] fat = new float[74];
       float[] sodium = new float[74];
       float[] fiber = new float[74];
       float[] carbs = new float[74];
       float[] sugars = new float[74];
       float[] potassium = new float[74];
       float[] vitamins = new float[74];

      
       // 1. Read all the data
       for (int i = 0; i < 74; i++) {
               name[i] = fileScanner.next();
               mfr[i] = fileScanner.next();
               calories[i] = fileScanner.nextFloat();
               protein[i] = fileScanner.nextFloat();      
               fat[i] = fileScanner.nextFloat();
               sodium[i] = fileScanner.nextFloat();
               fiber[i] = fileScanner.nextFloat();
               carbs[i] = fileScanner.nextFloat();
               sugars[i] = fileScanner.nextFloat();
               potassium[i] = fileScanner.nextFloat();
               vitamins[i] = fileScanner.nextFloat();
       }
      
       fileScanner.close();
      
       // 2. Calculate the min and max for each nutrient
       float minCalories = getMinValue(calories);
       float maxCalories = getMaxValue(calories);
      
       float minProtein = getMinValue(protein);
       float maxProtein = getMaxValue(protein);
      
       float minFat = getMinValue(fat);
       float maxFat = getMaxValue(fat);
       float minSodium = getMinValue(sodium);
       float maxSodium = getMaxValue(sodium);
       float minFiber = getMinValue(fiber);
       float maxFiber = getMaxValue(fiber);
       float minCarbs = getMinValue(carbs);
       float maxCarbs = getMaxValue(carbs);
       float minSugars= getMinValue(sugars);
       float maxSugars = getMaxValue(sugars);
       float minPotassium = getMinValue(potassium);
       float maxPotassium = getMaxValue(potassium);
       float minVitamins = getMinValue(vitamins);
       float maxVitamins = getMaxValue(vitamins);
      
       // 3. Calcuate the rankings for each cereal
       float[] rankings = new float[74];
      
       float minRanking=0, maxRanking=0;
       int minRankingIndex=0, maxRankingIndex=0;
      
       for (int i=0; i < 74; i++) {
           rankings[i] = -getNormalize(calories[i], minCalories, maxCalories)
                   + getNormalize(protein[i], minProtein, maxProtein)
                   - getNormalize(fat[i], minFat, maxFat)
                   - getNormalize(sodium[i], minSodium, maxSodium)
                   + getNormalize(fiber[i], minFiber, maxFiber)
                   - getNormalize(carbs[i], minCarbs, maxCarbs)
                   - getNormalize(sugars[i], minSugars, maxSugars)
                   + getNormalize(vitamins[i], minVitamins, maxVitamins);
           System.out.println(name[i] + \"\\t\" + mfr[i] + \"\\t\" + rankings[i]);
          
           // 4. Keep track of the cereal with the highest and lowest ranking
           if (i == 0) {
               minRanking = rankings[i];
               maxRanking = rankings[i];
           } else {
               if (minRanking > rankings[i]) {
                   minRanking = rankings[i];
                   minRankingIndex = i;
               }
               if (maxRanking < rankings[i]) {
                   maxRanking = rankings[i];
                   maxRankingIndex = i;
               }
           }  
       }
      
       // Reveal the results...
       System.out.println(\"HEALTHIEST CEREAL \" + name[maxRankingIndex] + \" made by \" + mfr[maxRankingIndex]);
       System.out.println(\"UNHEALTHIEST CEREAL \" + name[minRankingIndex] + \" made by \" + mfr[minRankingIndex]);
   }
}

Part 3: Modify Cereal Example In this part, you will modify the Cereal example to include Whole Grain in the calculation. (data used for whole grain is not real and was randomly generated). The same project that was imported in part 1 should already have a part 2 package. Open the file and complete the following tasks to add whole grains to the calculations. (note: The location of each task\'s code matters, please place them where they belong in the CerealRanker.java file) 1. Change the data file name to \"CerealData2.txt\" 2. Add \"Whole Grains- High\" to the comments describing the data 3. declare a new array of 74 floats named wholeGrains 4. scan the last data field into the wholeGrains array 5. store the min value of wholeGrains into minWholeGrains 6. store the max value of wholeGrains into maxWholeGrains 7. add whole grains to the ranking calculation. Since while grain is valued at \"High\", this should be a positive influence on the ranking calculation a.

Solution

Please follow the code and comments for description :

CODE :

import java.io.*;
import java.util.*;

public class CerealRanker {

public static float getMaxValue(float[] numbers) {
float maxValue = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > maxValue) {
maxValue = numbers[i];
}
}
return maxValue;
}

public static float getMinValue(float[] numbers) {
float minValue = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < minValue) {
minValue = numbers[i];
}
}
return minValue;
}

public static float getNormalize(float value, float min, float max) {
return (value - min) / (max - min);
}

// Trap for Input/Output errors
public static void main(String[] args) throws java.io.IOException {
// set up the buffered reader to read from the text file}
Scanner fileScanner = new Scanner(new FileReader(\"CerealData2.txt\"));

/*
- Name
- Manufacturer
- Calories - Low
- Protein - High
- Fat - Low
- Sodium - Low
- Fiber - High
- Carbs - Low
- Sugars - Low
- Potassium - Ignore
- Vitamins - High
*/
String[] name = new String[74];
String[] mfr = new String[74];
float[] calories = new float[74];
float[] protein = new float[74];
float[] fat = new float[74];
float[] sodium = new float[74];
float[] fiber = new float[74];
float[] carbs = new float[74];
float[] sugars = new float[74];
float[] potassium = new float[74];
float[] vitamins = new float[74];
float[] wholeGrains = new float[74];

// 1. Read all the data
for (int i = 0; i < 74; i++) {
name[i] = fileScanner.next();
mfr[i] = fileScanner.next();
calories[i] = fileScanner.nextFloat();
protein[i] = fileScanner.nextFloat();
fat[i] = fileScanner.nextFloat();
sodium[i] = fileScanner.nextFloat();
fiber[i] = fileScanner.nextFloat();
carbs[i] = fileScanner.nextFloat();
sugars[i] = fileScanner.nextFloat();
potassium[i] = fileScanner.nextFloat();
vitamins[i] = fileScanner.nextFloat();
wholeGrains[i] = fileScanner.nextFloat();
}

fileScanner.close();

// 2. Calculate the min and max for each nutrient
float minCalories = getMinValue(calories);
float maxCalories = getMaxValue(calories);

float minProtein = getMinValue(protein);
float maxProtein = getMaxValue(protein);

float minFat = getMinValue(fat);
float maxFat = getMaxValue(fat);
  
float minSodium = getMinValue(sodium);
float maxSodium = getMaxValue(sodium);
  
float minFiber = getMinValue(fiber);
float maxFiber = getMaxValue(fiber);
  
float minCarbs = getMinValue(carbs);
float maxCarbs = getMaxValue(carbs);
  
float minSugars = getMinValue(sugars);
float maxSugars = getMaxValue(sugars);
  
float minPotassium = getMinValue(potassium);
float maxPotassium = getMaxValue(potassium);
  
float minVitamins = getMinValue(vitamins);
float maxVitamins = getMaxValue(vitamins);
  
float minWholeGrains = getMinValue(wholeGrains);
float maxWholeGrains = getMaxValue(wholeGrains);

// 3. Calcuate the rankings for each cereal
float[] rankings = new float[74];

float minRanking = 0, maxRanking = 0;
int minRankingIndex = 0, maxRankingIndex = 0;

for (int i = 0; i < 74; i++) {
rankings[i] = -getNormalize(calories[i], minCalories, maxCalories)
+ getNormalize(protein[i], minProtein, maxProtein)
- getNormalize(fat[i], minFat, maxFat)
- getNormalize(sodium[i], minSodium, maxSodium)
+ getNormalize(fiber[i], minFiber, maxFiber)
- getNormalize(carbs[i], minCarbs, maxCarbs)
- getNormalize(sugars[i], minSugars, maxSugars)
+ getNormalize(vitamins[i], minVitamins, maxVitamins)
+ getNormalize(wholeGrains[i], minWholeGrains, maxWholeGrains);
System.out.println(name[i] + \"\\t\" + mfr[i] + \"\\t\" + rankings[i]);

// 4. Keep track of the cereal with the highest and lowest ranking
if (i == 0) {
minRanking = rankings[i];
maxRanking = rankings[i];
} else {
if (minRanking > rankings[i]) {
minRanking = rankings[i];
minRankingIndex = i;
}
if (maxRanking < rankings[i]) {
maxRanking = rankings[i];
maxRankingIndex = i;
}
}
}

// Reveal the results...
System.out.println(\"HEALTHIEST CEREAL is \" + name[maxRankingIndex] + \" made by \" + mfr[maxRankingIndex]);
System.out.println(\"UNHEALTHIEST CEREAL is \" + name[minRankingIndex] + \" made by \" + mfr[minRankingIndex]);
}
}


OUTPUT :

HEALTHIEST CEREAL is Cereals made by HUL
UNHEALTHIEST CEREAL is Packets made by ABC


The output has been a sample one so please check with the required test file and verify it.

Hope this is helpful.

package part3; import java.io.*; import java.util.*; public class CerealRanker { public static float getMaxValue(float[] numbers){ float maxValue = numbers[0];
package part3; import java.io.*; import java.util.*; public class CerealRanker { public static float getMaxValue(float[] numbers){ float maxValue = numbers[0];
package part3; import java.io.*; import java.util.*; public class CerealRanker { public static float getMaxValue(float[] numbers){ float maxValue = numbers[0];
package part3; import java.io.*; import java.util.*; public class CerealRanker { public static float getMaxValue(float[] numbers){ float maxValue = numbers[0];
package part3; import java.io.*; import java.util.*; public class CerealRanker { public static float getMaxValue(float[] numbers){ float maxValue = numbers[0];
package part3; import java.io.*; import java.util.*; public class CerealRanker { public static float getMaxValue(float[] numbers){ float maxValue = numbers[0];

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site