1 / 14

SOFTWARE AND PROGRAMMING 1

SOFTWARE AND PROGRAMMING 1. An AUT action short of strike: no TEST 2 this year _________________________ Today: - 2D Arrays. 2D arrays: example. Example - week sales at each of four shops:

blithe
Download Presentation

SOFTWARE AND PROGRAMMING 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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  |----------------------------------------------------0|        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 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 2002 5 22 22 43 2002 5 22 22 43 2002 5 22 23 58 2002 5 23 00 16 2002 5 23 00 16 • 5 23 03 53 . . . . . . . . . . . . . . . . . . . . . . .(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]++; } • } • }

More Related