lecture 3/3/10: contents

15
1 Lecture 3/3/10: Contents Array of user-defined objects 2D array

Upload: vernon-vance

Post on 03-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

Lecture 3/3/10: Contents. Array of user-defined objects 2D array. User defined type reading. Follows such classes as: - Oblong and OblongTester, sections 6.3 and 7.2, Charatan and Kans, “Java in two semesters” - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 3/3/10: Contents

1

Lecture 3/3/10: Contents

• Array of user-defined objects• 2D array

Page 2: Lecture 3/3/10: Contents

2

User defined type readingFollows such classes as: - Oblong and OblongTester,

sections 6.3 and 7.2, Charatan and Kans, “Java in two semesters”

- Counter and CounterTest, sections 6.3-6.6, Pohl and McDowell, “Java by dissection”

- Employee and TwoEmployee, p. 96-112, Ch.3, Farrell, “Java programming”

Page 3: Lecture 3/3/10: Contents

3

Array of user-defined type(1)

• The setting: We have a number of applicants for whom we have separate, but matching, lists of names and id’s organised as arrays.

• We would like to develop a new type for an Applicant to hold all individual data of the applicant, in this case just id and name, but it can have as many attributes as it takes.

• With this new type, we would like to organise a list of applicants as an array of this type

Page 4: Lecture 3/3/10: Contents

4

Array of user-defined type(2)

• To develop an array of applicants, we need

– A named class, say Appl, with variables declared to hold all individual applicant data (A);

– A constructor in this class that would take values from the arrays holding id and name information (B);

– Representation of the id and name arrays (C);

– Generation of an instance of array of new type, Appl (D);

– Feeding the Id’s and names into the Appl array (E);

• We can show that this does work by printing out data of all the entries in the Appl array

Page 5: Lecture 3/3/10: Contents

5

Array of user-defined type(3)

• class Appl{

• public int ids;• public String nms;• public Appl(int iden, String nnn){• ids=iden;• nms=nnn;}

• static int[] iden(){• int ii[]={12, 15, 22, 18, 11};• return ii;}

• static String[] namen(){• String jj[]={"Aa", "Bb", "Cc", "Dd", "Ee"};• return jj;}

Page 6: Lecture 3/3/10: Contents

6

Array of user-defined type(4)

• public static void main(String[] args){• int id[]=iden();• String name[]=namen();• Appl[] many=new Appl[id.length];• for(int i=0;i<id.length;i++)• many[i]=new Appl(id[i],name[i]);• //many – array of Appl type objects. Check:• for(int i=0;i<name.length;i++){• System.out.println(i +"th applicant data:");• System.out.println("Id: "+many[i].ids);• System.out.println("Name: "+many[i].nms);• System.out.println(); }• }• }

Page 7: Lecture 3/3/10: Contents

7

Array of user-defined type(5)

Question:

• Identify which parts of class Appl correspond

• to tasks A, B, C, D and E on slide 17

Page 8: Lecture 3/3/10: Contents

8

2D arrays: example

Example - week sales at each of four shops:  

           Sunday                     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

Page 9: Lecture 3/3/10: Contents

9

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

Page 10: Lecture 3/3/10: Contents

10

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)  because sales[5][2] does not exist

Page 11: Lecture 3/3/10: Contents

11

2D arrays: processingSummary 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;               }

Page 12: Lecture 3/3/10: Contents

12

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);//application of the method

Page 13: Lecture 3/3/10: Contents

13

Two-dimensional arrays

Summary sales for Wednesday (4th day): •         int sum =0; •         for (int row=0; row< 4; row +

+) •         sum = sum + sales[row][3]; Further problems: • methods:

– summary sales on Wednesday (next page)– summary sales by week-day (after next

page)– summary sales by shop

Page 14: Lecture 3/3/10: Contents

14

Summary sales on any day

Method: summary sales in sales[][] on any day

  int sss(sales[][], int day){ int sum =0;    for (int r=0; r<sales.length; r++)       sum = sum + sales[r][day]; return sum;}

Page 15: Lecture 3/3/10: Contents

15

Summary sales on all days: method

   int[] sss(sales[][], int day){//note: output is an

array!

int[] sum =new int[sales[0].length]; for (int c=0; c<sales[0].length; c++)

   for (int r=0; r<sales.length; r++) sum[c] = sum[c] + sales[r][c]; return sum;}