Hi All,
I have written an application for traffic Study which takes traffic load for seven days, inputting traffic load during morning,lunch,evening and night for each day. The output im getting is this
Morning Lunch Evening Night Average
Day 0 : 5 5 5 5 5
Day 1 : 5 5 5 5 5
Day 2 : 5 5 5 5 5
Day 3 : 5 5 5 5 5
Day 4 : 5 5 5 5 5
Day 5 : 5 5 5 5 5
Day 6 : 5 5 5 5 5
Average : 5 5 5 5
But i don't understand how im getting this output. Please explain
Here is my code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package traffic;
import java.util.Scanner;
public class Traffic {
private int[][] traffic = new int[7][4];
private int[] dailyAvg = new int[7];
private int[] timeAvg = new int[4];
public void collectData(Scanner sc)
{
int row;
int col;
for (row = 0; row < traffic.length; row++)
{
for (col = 0; col < traffic[row].length; col++)
traffic[row][col] = sc.nextInt();
}
}
/**
Analyze data
*/
public void analyzeData()
{
int row;
int col;
int sum;
for (row = 0; row < traffic.length; row++)
{
sum = 0;
for (col = 0; col < traffic[row].length; col++)
sum = sum + traffic[row][col];
dailyAvg[row]
= Math.round(sum / traffic[row].length);
}
for (col = 0; col < timeAvg.length; col++)
{ sum = 0;
for (row = 0; row < traffic.length; row++)
sum = sum + traffic[row][col];
timeAvg[col]
= Math.round(sum / traffic. length);
}
}
/**
Create study report as a String
@return study report
*/
public String createTrafficReport()
{
int row;
int col;
String str;
str =
"\t\tMorning\t\tLunch\t\tEvening\tNight\t\tAverage\n";
for (row = 0; row < traffic.length; row++)
{
str = str + "Day "+ row + " :\t\t";
for (col = 0; col < traffic[row].length; col++)
str = str + traffic[row][col] + "\t\t";
str = str + dailyAvg[row]+ "\n";
}
str = str + "Average"+ " :\t";
for (row = 0; row < timeAvg.length; row++)
str = str + timeAvg[row] + "\t\t";
return str;
}
}
package trafficstudyapplication;
import traffic.Traffic;
import java.util.*;
import java.io.*;
public class TrafficStudyApplication
{
public static void main (String[] args) throws
FileNotFoundException, IOException
{
Scanner input = new Scanner(System.in);
Traffic trafficStudy = new Traffic();
Scanner ScannedInfo
= new Scanner(new File("D:\\inputs\\Traffic.dat"));
PrintWriter output
= new PrintWriter(new FileWriter
("D:\\inputs\\report.dat"));
trafficStudy.collectData(ScannedInfo);
trafficStudy.analyzeData();
output.println(trafficStudy.createTrafficReport());
output.close();
}
}
The thing i dontt understand is I initiated Str as First statement below .
and i used str in the following statements 2 to 6.
1. str = "\t\tMorning\t\tLunch\t\tEvening\tNight\t\tAverage\n";
2.str = str + "Day "+ row + " :\t\t" // output is looking like str = "Day "+ row + " :\t\t"
3.str = str + traffic[row][col] + "\t\t" // output is looking like str = traffic[row][col] + "\t\t"
4.str = str + dailyAvg[row]+ "\n" // output is looking like str = dailyAvg[row]+ "\n"
5.str = str + "Average"+ " :\t" // output is looking like str = "Average"+ " :\t"
6.str = str + timeAvg[row] + "\t\t" // output is looking like str = timeAvg[row] + "\t\t"
In output file why intiated value of str (1st statement) is missing for statements 2 to 6.
For instance in the output file for the second statement why didn't i get something like this,
morning lunch evening night average day 0 5 5 5 5 //for str = str + "day" + row + " :\t\t";
because "str" has been intiated with "\t\tMorning\t\tLunch\t\tEvening\tNight\t\tAverage\n";
how did i get in the output file like below in second row instead of above
day 0 5 5 5 5 5 ( why is the Str intiated value (morning evening night average ..) gone ?)
and this question goes to 3 to 6 statements as well.