1 / 13

Lecture 16/3/11: Contents

Lecture 16/3/11: Contents. Example of an array of user-defined objects: Car and Carr Constructor Providing functionalities for a user-defined object array. Adverts. I: The deadline for turning in Assignment 2 has been extended to 31 March 2011

demetra
Download Presentation

Lecture 16/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 16/3/11: Contents • Example of an array of user-defined objects: Car and Carr • Constructor • Providing functionalities for a user-defined object array

  2. Adverts I: The deadline for turning in Assignment 2 has been extended to 31 March 2011 II: Two last questions from Test 2, n9 and n10, are withdrawn from the marking, because the average mark on each of them is less than the pass rate of 40% . They will be taken care of in today’s lab. Consequently, the total mark will be corrected by dividing over 0.7 to make it back, per cent.

  3. User defined type Task: supplementary to primitive number types, introduce a CAR type This is not part of JAVA; we do it by ourselves !!! First, define attributes that you want to be maintained by the type (depend on what tasks are to be addressed): Attributes:model (String) Ford, Toyota, Vauxhall engine volume (double) registration year (integer)

  4. User defined type Task: supplementary to primitive number types, introduce a CAR type and set an array of Car type instances One must do it by themselves !!! First, specify attributes that you want to be maintained by the type (depend on what tasks are to be addressed): Attributes:model (String) Ford, Toyota, Vauxhall engine volume (double) registration year (integer)

  5. Functionalities Attributes:model (String) Ford, Toyota, Vauxhall engine volume (double) registration year (integer) Classes Car and Carr should provide functionalities for: (a) initialising an array of Cars, (b) printing data of the array, and (c) informing of the numbers of different car models in the array

  6. Car object (1): Attrbutes and Constructor • /* a class to keep data of a car: model, engine size, registration year */ • public class Car{ • private int Reg; • private double engine; • public String model; • public String[] mmm={"Ford", "Toyota", "Vauxhall"}; • public Car(double e, int r) //constructor • { Reg = r; • engine = e; • int choice=(int) (3*Math.random()); • model=mmm[choice]; //random assignment • } • /* An instance must have all the variables initialised – constructor • should provide for this */

  7. Car object (2): Conventional Getters public double getEngine() • { return engine; } • public int getReg() • { return Reg; } • public String getModel() • { return model; } • }//end of class

  8. Working with Car array: Carr (1) • //this class is for working with an array of user-defined Car variables • //that have attributes of engine size, registration and colour • import java.util.*; • public class Carr{ • static Scanner sc=new Scanner(System.in); • public static void main(String[] args){ • System.out.println(“Enter an integer for the number of cars"); • int nc=sc.nextInt(); • Car[] arCar=crCar(nc); // initialising array of Car's • dataCar(arCar);//printing data of the array • int[] st=modstat(arCar); // statistics of models • } /*Put in main method all the functions that the class should provide: (a) initialising an array of Cars, method crCar, (b) printing data of the array, method dataCar, and (c) informing of the numbers of different brand cars in the array, method modstat */

  9. Working with Car array: Carr (2a) • //-------method crCar to create a Car array of a given length // a with entering the attributes automatically --- • public static Car[] crCar(int a){ //a is the array's length • Car[] carar=new Car[a]; • for(int i=0;i<a;i++){ • double eng=Math.round(10*(Math.random()+1))/10.0; //random between 1 and 2 • int re=(int)(10*Math.random()); • //random between 0 and 9 • carar[i]=new Car(eng,re); • } • return carar; • }

  10. Working with Car array: Carr (2b) • //--------------method dataCar to print data of the array ---- • public static void dataCar(Car[] a){ • for(int i=0;i<a.length; i++){ • System.out.println("Data of car "+i+ ":"); • printCar(a[i]); • } • } • //---------- method printCar for printing data of a car • public static void printCar(Car a){ • System.out.println("Car's engine: " + a.getEngine()); • System.out.println(" Its registration year: " + a.getReg()); • System.out.println("Car's Model: " + a.model); • System.out.println(); • }

  11. Working with Car array: Carr(3) • public static int[] modstat(Car[] a){ • int[] count= new int[a.length]; • String[] mm={"Ford","Toyota","Vauxhall"}; • for(int i=0;i<a.length;i++){ • String b=a[i].model; • for(int j=0;j<3;j++){ • if(b.equals(mm[j])) • count[j]++; • } • } • for(int j=0;j<3;j++) • System.out.println("There are "+count[j]+" of model "+mm[j]); • return count; • } • }//end of class

  12. How to organise Car model counting? (1) 1st Approach (many loops over Array) Take a Model, say, “Vauxhall”; run a loop over the array, each time checking whether the entry’s model is “Vauxhall”; if YES, add 1 to counter. Take next model, do same. The loop runs as many times as there are models.

  13. How to organise Car model counting? (2) 2nd Approach (one loop over Array) Run a loop over Car array; for each entry, check its model and add 1 to the corresponding counter (an array of counters are needed). I do this in Carr with method modstat .

More Related