software and programming 1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
SOFTWARE AND PROGRAMMING 1 PowerPoint Presentation
Download Presentation
SOFTWARE AND PROGRAMMING 1

Loading in 2 Seconds...

play fullscreen
1 / 14

SOFTWARE AND PROGRAMMING 1 - PowerPoint PPT Presentation


  • 66 Views
  • Uploaded on

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:

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

PowerPoint Slideshow about 'SOFTWARE AND PROGRAMMING 1' - blithe


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

SOFTWARE AND PROGRAMMING 1

An AUT action short of strike:

no TEST 2 this year

_________________________

Today:

- 2D Arrays

2d arrays example
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
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
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)

2d arrays processing
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;               }
2d arrays different row lengths
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);
two dimensional arrays
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
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 book1
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

original loganalyzer chapter 4 11 bluej book p 101
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]++; }
  • }
  • }
2d month hour log counts in loganalyzer
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]++;

}

}

2d month hour counts with loganalyzer 2
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 loganalyzer1
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

2d modified loganalyzer
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]++; }
  • }
  • }