Presentation is loading. Please wait.

Presentation is loading. Please wait.

SOFTWARE AND PROGRAMMING 1

Similar presentations


Presentation on theme: "SOFTWARE AND PROGRAMMING 1"— Presentation transcript:

1 SOFTWARE AND PROGRAMMING 1
An AUT action short of strike: no TEST 2 this year _________________________ Today: - 2D Arrays

2 2D arrays: example Example - week sales at each of four shops:
                                      Days         0        1     2      3     4      5      6   | |        22     49     4     93     0     12     32   | 1|         3        8     67   51     5      3     63 | 2|         14      8     23   14     5     23     16   | 3|         54      0     76   31     4      3     99

3 2D arrays: actions Declaration and initialisation (with zeros):
int[ ][ ] sales = new int[4][7];   Filling in: sales = {                 {22, 49, 4, 93, 0, 12, 32},                   ………………………,                 {54, 0, 76, 31, 4, 3, 99}               }

4 2D arrays: accessing Reaching a component: sales[2][5] = 23 or
int aa = 2; int bb = 5; sales[aa][bb]=23 sales[bb][aa] = ? (answer: error)

5 2D arrays: processing Summary sales: int sum =0;
    for (int shop = 0; shop < 4; shop ++)     for(int day = 0; day < 7; day ++)     sum = sum + sales[shop][day]; As a method:  public int sum( int[][] a) {  int total = 0; for (int row = 0; row < a.length; row ++)  for( int col = 0; col < a[0].length; col ++)  total = total + a [row][col];                     return total;               }

6 2D arrays: different row lengths
int[ ] Twodarray={{1, 1, 1}, {1, 3}, {4,5,4,5}} Modifying the summation method:  public int sum( int[][] a) {  int total = 0; for (int row = 0; row < a.length; row ++)   for( int col = 0; col < a[row].length; col ++)   total = total + a[row][col];         return total;               } int summa=sum(Twodarray);

7 Two-dimensional arrays
Summary sales for Wednesday:         int sum =0;         for (int row=0; row< 4; row ++)         sum = sum + sales[row][2]; Further problems: methods: summary sales by week-day summary sales by shop methods for finding the minimum and the maximum

8 Log counts in LogAnalyzer (Chapter 4.11 BlueJ book)
Part of a real log file in a web server (register of accesses), weblog.txt Year Month Day Hour Minute (3749 rows of May 2002)

9 Log counts in LogAnalyzer (Chapter 4.11 BlueJ book)
BlueJ project “weblog-analyzer” consists of four classes: LogfileReader reads a log file LogEntry reads a line from the log file LoglineTokenizer tokenizes a line into its constituting items LogAnalyzer counts statistics from line items

10 Original LogAnalyzer (Chapter 4.11 BlueJ book, p. 101)
public class LogAnalyzer { private int[] hourCounts; //Hourly access counts. private LogfileReader reader; // to access the data public LogAnalyzer() //constructor {hourCounts = new int[24]; // array for the hourly access counts reader = new LogfileReader(); // Reader to obtain the data } public void analyzeHourlyData() { while(reader.hasMoreEntries()) { LogEntry entry = reader.nextEntry(); int hour = entry.getHour(); hourCounts[hour]++; }

11 2D month-hour log counts in LogAnalyzer
This method in LogAnalyzer should be updated for the purpose: public void analyzeHourlyData() { while(reader.hasMoreEntries()) { LogEntry entry = reader.nextEntry(); int hour = entry.getHour(); //getHour, a method in LogEntry to get hour hourCounts[hour]++; }

12 2D month-hour counts with LogAnalyzer (2)
We need a method in class LogEntry for getting month as well, to be added: public int getMonth() { return dataValues[Month]; } //dataValues is the array in LogEntry holding //all five data entries (year,…,minute)

13 2D month-hour log counts in LogAnalyzer
Now - an analogue to analyzeHourlyData: public void analyzeHourMonthlyData() { while(reader.hasMoreEntries()) { LogEntry entry = reader.nextEntry(); int hour = entry.getHour(); int month=entry.getMonth(); //getHour, a method in LogEntry to get hour hourmonthCounts[month][hour]++; } }//hourmonth[ ][ ] must be declared and //initialized beforehand

14 2D Modified LogAnalyzer
public class LogAnalyzer { private int[][] hourmonthCounts; //Hour-Monthly access counts. private LogfileReader reader; // to access the data public LogAnalyzer() //constructor {hourmonthCounts = new int[24][12]; // h.-monthly access counts reader = new LogfileReader(); // Reader to obtain the data } public void analyzeHourMonthlyData() { while(reader.hasMoreEntries()) { LogEntry entry = reader.nextEntry(); int hour = entry.getHour(); int month = entry.getMonth(); //method getMonth() must be added to LogEntry hourmonthCounts[hour][month]++; }


Download ppt "SOFTWARE AND PROGRAMMING 1"

Similar presentations


Ads by Google