1 / 16

Lecture 9/3/11: Contents

Lecture 9/3/11: 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”

shakti
Download Presentation

Lecture 9/3/11: Contents

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. Lecture 9/3/11: Contents • Array of user-defined objects • 2D array

  2. User defined type reading Follows 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”

  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

  4. Array of user-defined type(2) • To develop an array of applicants, we need • The ID and name arrays (A); • A named class, say Appl, with variables declared to hold all individual applicant data (B); • A constructor in this class that would take values from the arrays holding ID and name information (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

  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;} //constructor • static int[] iden(){ • int ii[]={12, 15, 22, 18, 11}; • return ii;} // method producing array of IDs • static String[] namen(){ • String jj[]={"Aa", "Bb", "Cc", "Dd", "Ee"}; • return jj;} // method producing array of names

  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(); } • } • }

  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 4

  8. 2D arrays The last subject to study in SP1

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

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

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

  12. 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;               }

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

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

  15. 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;}

  16. 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;}

More Related