SOFTWARE AND PROGRAMMING 1

1 / 14

# SOFTWARE AND PROGRAMMING 1 - PowerPoint PPT Presentation

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:

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'SOFTWARE AND PROGRAMMING 1' - blithe

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### 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:

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

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}              }

2D arrays: accessing

Reaching a component:

sales[2][5] = 23

or

int aa = 2;

int bb = 5;

sales[aa][bb]=23

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];
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];
• int summa=sum(Twodarray);
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
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)

Log counts in LogAnalyzer (Chapter 4.11 BlueJ book)

BlueJ project “weblog-analyzer” consists of four classes:

LogEntry reads a line from the log file

LoglineTokenizer tokenizes a line into its constituting items

LogAnalyzer counts statistics from line items

Original LogAnalyzer (Chapter 4.11 BlueJ book, p. 101)
• public class LogAnalyzer {

private int[] hourCounts; //Hourly access counts.

public LogAnalyzer() //constructor

{hourCounts = new int[24]; // array for the hourly access counts

• }

public void analyzeHourlyData()

• int hour = entry.getHour();
• hourCounts[hour]++; }
• }
• }
2D month-hour log counts in LogAnalyzer

This method in LogAnalyzer should be updated for the purpose: public void analyzeHourlyData()

{

int hour = entry.getHour();

//getHour, a method in LogEntry to get hour

hourCounts[hour]++;

}

}

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)

2D month-hour log counts in LogAnalyzer

Now - an analogue to analyzeHourlyData:

public void analyzeHourMonthlyData()

{

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

2D Modified LogAnalyzer
• public class LogAnalyzer {

private int[][] hourmonthCounts; //Hour-Monthly access counts.

public LogAnalyzer() //constructor

{hourmonthCounts = new int[24][12]; // h.-monthly access counts

• }

public void analyzeHourMonthlyData()