SOFTWARE AND PROGRAMMING 1

Slides:



Advertisements
Similar presentations
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Advertisements

Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Grouping objects Arrays and for loops. Fixed-size collections Sometimes the maximum collection size can be pre-determined. Programming languages usually.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
A First Book of ANSI C Fourth Edition
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming.
SOFTWARE AND PROGRAMMING 1 Lecture 7/5/8: Review of concepts involved in lectures and exam Lecture 14/5/8: Review of questions in previous exam papers.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
1 Original Source : and Problem and Problem Solving.ppt.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Lecture 9/3/11: Contents Array of user-defined objects 2D array.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Fixed-sized collections Introduction to arrays 6.0.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays Chapter 7.
CompSci 230 S Programming Techniques
Objects First with Java CITS1001
Two Dimensional Array Mr. Jacobs.
Fixed-sized collections
Inheritance and Encapsulation
Exploiting Parallelism
Case Study 2 – Marking a Multiple-choice Test
PRG 420 Education for Service-- prg420.com. PRG 420 Week 1 Coding a Simple “Hello, world!” Java™ Program(New Syllabus) For more course tutorials visit.
Chapter Three - Implementing Classes
CNG 140 C Programming (Lecture set 8)
MIS2502: Review for Exam 1 JaeHwuen Jung
Classes & Objects: Examples
EKT150 : Computer Programming
Review for Final Exam.
The four rules of number
Chapter 8 Multidimensional Arrays
Choose a passage from your assigned chapter.
CIS162AD - C# Arrays ch08.ppt.
MSIS 655 Advanced Business Applications Programming
F II 5. Grouping Objects Objectives
Multidimensional Arrays
Multidimensional Arrays
Chapter 10 Counting Methods 2012 Pearson Education, Inc.
COS 260 DAY 11 Tony Gauvin.
Collections and iterators
Review for Final Exam.
1D Arrays and Lots of Brackets
Multi-Dimensional Arrays
Creative Commons Attribution Non-Commercial Share Alike License
Year 2 Spring Term Week 11 Lesson 3
Year 2 Spring Term Week 11 Lesson 3
Thanachat Thanomkulabut
Tuesday May 15, 2018 (??).
Collections and iterators
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
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 sales[bb][aa] = ? (answer: error)  

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 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 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 23 58 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: 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) 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 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) 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() { 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 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]++; }