Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB: Every summer, Americans travel by road to various locations across the c

ID: 3572625 • Letter: M

Question

MATLAB: Every summer, Americans travel by road to various locations across the country. With low fuel prices this summer, there will be lots of travel. The purpose of this program is to determine the total miles of travel and compute the cost of travel using various travel route options. The excel sheet named project3data.xlsx has data for this work. The table represents 55 families travel plans. Each family is identified by a family ID which is in column 1. Each travel plan consists of 10 road segments whose distances are given in miles. Colum 11 is the average speed of the family and column 12 is the fuel efficiency of the family’s vehicle. For example, for family 1, the average speed is 65mph and they have a vehicle that does 35 miles per gallon. Write a function called summertravel that uses a for loop to generate the table below. Your table should show all the outputs from 1 to 55. Use $2.30/gallon as the average fuel cost.

1 40 150 70 100 140 202 51 44 12 95 65 35 2 100 121 57 105 66 182 54 87 8 141 75 20 3 66 147 64 115 206 100 57 78 45 135 68 25 4 89 88 67 130 25 99 60 98 120 158 65 26 5 90 125 68 150 58 68 63 78 145 141 70 25 6 45 102 78 175 157 159 66 41 22 107 75 33 7 60 99 77 205 149 171 69 15 23 84 77 30 8 91 218 81 140 204 21 72 25 36 108 79 32 9 24 192 79 140 116 228 75 47 99 87 49 28 10 57 188 82 150 150 57 78 102 97 88 66 29 11 89 140 75 160 139 140 81 55 72 45 60 20 12 97 167 76 170 179 125 84 48 42 77 75 19 13 67 183 119 180 170 122 87 77 20 98 76 60 14 54 125 113 190 104 109 90 99 5 45 58 52 15 100 98 85 200 128 210 93 87 55 23 63 22 16 120 82 86 210 137 214 91 78 88 12 65 21 17 96 75 87 220 111 183 94 79 48 10 60 20 18 201 69 87 230 215 122 97 69 78 9 55 18 19 77 132 90 55 159 221 100 63 74 77 58 19 20 123 59 139 50 137 194 103 158 41 9 59 66 21 143 57 100 60 160 212 106 45 40 12 76 25 22 119 160 131 108 44 91 109 60 145 120 77 54 23 102 123 104 112 17 59 112 91 22 88 74 25 24 70 75 105 116 120 157 115 24 23 45 71 24 25 72 123 109 120 175 207 113 57 36 98 70 26 26 74 52 112 124 111 138 116 89 99 32 69 27 27 76 93 116 128 159 181 119 97 97 78 66 29 28 78 89 119 132 162 179 122 67 72 79 58 30 29 80 74 122 136 154 166 125 54 42 69 59 31 30 82 63 128 140 44 157 128 100 20 63 63 35 31 84 339 127 144 45 14 131 120 5 158 60 32 32 86 227 133 148 66 87 134 96 55 45 58 36 33 88 243 133 152 36 88 137 105 22 60 55 34 34 44 78 142 156 30 47 140 93 45 91 54 41 35 92 209 125 160 28 45 143 89 78 24 71 22 36 94 125 125 58 156 50 141 45 98 57 72 20 37 96 144 75 154 90 84 102 113 12 89 65 18 38 98 158 52 172 200 55 147 33 100 97 69 17 39 100 56 44 176 172 68 150 77 78 67 66 19 40 102 90 25 180 207 4 153 47 79 100 67 32 41 104 78 134 92 96 75 156 128 98 87 55 19 42 106 144 58 94 163 62 159 59 78 52 55 22 43 108 258 144 96 178 87 162 389 45 65 58 25 44 110 243 87 98 164 9 121 58 52 49 59 24 45 55 243 90 100 108 98 123 45 47 48 63 21 46 47 58 99 87 95 117 125 54 22 43 60 20 47 116 100 150 11 171 69 127 78 111 50 58 23 48 118 22 47 106 189 87 129 89 20 66 55 33 49 120 58 87 108 194 77 131 65 69 69 54 35 50 22 25 8 60 45 59 133 47 86 61 71 30 51 48 78 165 10 116 45 135 102 38 63 72 30 52 20 98 17 114 88 130 87 12 72 48 65 36 53 102 78 158 87 195 192 77 201 55 88 69 19 54 66 44 163 48 144 122 141 125 65 73 70 22 55 132 89 171 120 207 47 44 88 60 70 69 28

Explanation / Answer

for this use poi-3.9 jar file

import java.awt.BorderLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import javax.swing.*;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;//usermodel.XSSFWorkbook;

public class TravellCost1 {

public static void main(String[] args) throws FileNotFoundException, IOException {
  
  new TravellCost1().summertravel();
}

public void summertravel() throws FileNotFoundException, IOException {
      
          FileInputStream fis = new FileInputStream( new File("D:/COPS_PROD/CF31_DEATH_CLAIM/Data/project3data.xls"));
          Workbook workbook = new HSSFWorkbook(fis);
          Sheet firstSheet = workbook.getSheetAt(0);
        
          String col [] = {"familyId","roadSeg1","roadSeg2","roadSeg3","roadSeg4","roadSeg5","roadSeg6","roadSeg7","roadSeg8","roadSeg9","roadSeg10","avgSpeed","milesPerGallon","totalTravel","cost"};
          String tableData[][] = new String[55][15];
                  
          Iterator<Row> itr = firstSheet.iterator();
          int rowNo = 0;
          while (itr.hasNext()) {
            Row row = itr.next();
      
           
            int familyId =(int) row.getCell(0).getNumericCellValue();
            int roadSeg1 = (int) row.getCell(1).getNumericCellValue();
            int roadSeg2 = (int) row.getCell(2).getNumericCellValue();
            int roadSeg3 = (int) row.getCell(3).getNumericCellValue();
            int roadSeg4 = (int) row.getCell(4).getNumericCellValue();
            int roadSeg5 = (int) row.getCell(5).getNumericCellValue();
            int roadSeg6 = (int) row.getCell(6).getNumericCellValue();
            int roadSeg7 = (int) row.getCell(7).getNumericCellValue();
            int roadSeg8 = (int) row.getCell(8).getNumericCellValue();
            int roadSeg9 = (int) row.getCell(9).getNumericCellValue();
            int roadSeg10 = (int) row.getCell(10).getNumericCellValue();
            int avgSpeed = (int) row.getCell(11).getNumericCellValue();
            int milesPerGallon = (int) row.getCell(12).getNumericCellValue();
           
            int totalTravel = (roadSeg1+roadSeg2+roadSeg3+roadSeg4+roadSeg5+roadSeg6+roadSeg7+roadSeg8+roadSeg9+roadSeg10);
            double cost = (totalTravel / avgSpeed * 2.30);
           
            tableData[rowNo][0]=Integer.toString(familyId);
            tableData[rowNo][1]=Integer.toString(roadSeg1);
            tableData[rowNo][2]=Integer.toString(roadSeg2);
            tableData[rowNo][3]=Integer.toString(roadSeg3);
            tableData[rowNo][4]=Integer.toString(roadSeg4);
            tableData[rowNo][5]=Integer.toString(roadSeg5);
            tableData[rowNo][6]=Integer.toString(roadSeg6);
            tableData[rowNo][7]=Integer.toString(roadSeg7);
            tableData[rowNo][8]=Integer.toString(roadSeg8);
            tableData[rowNo][9]=Integer.toString(roadSeg9);
            tableData[rowNo][10]=Integer.toString(roadSeg10);
            tableData[rowNo][11]=Integer.toString(avgSpeed);
            tableData[rowNo][12]=Integer.toString(milesPerGallon);
            tableData[rowNo][13]=Integer.toString(totalTravel);
            tableData[rowNo][14]=Integer.toString((int) cost);
           rowNo++;
         
          }
         
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         
          JTable table = new JTable(tableData,col);

          JScrollPane scrollPane = new JScrollPane(table);
          frame.add(scrollPane, BorderLayout.CENTER);
          frame.setSize(800, 300);
          frame.setVisible(true);

     }
}