Lecture 16 3 11 contents
This presentation is the property of its rightful owner.
Sponsored Links
1 / 13

Lecture 16/3/11: Contents PowerPoint PPT Presentation


  • 71 Views
  • Uploaded on
  • Presentation posted in: General

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

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


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

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.


User defined type

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)


User defined type1

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)


Functionalities

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


Car object 1 attrbutes and constructor

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 */


Car object 2 conventional getters

Car object (2): Conventional Getters

public double getEngine()

  • { return engine; }

  • public int getReg()

  • { return Reg; }

  • public String getModel()

  • { return model; }

  • }//end of class


Working with car array carr 1

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 */


Working with car array carr 2a

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;

  • }


Working with car array carr 2b

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

  • }


Working with car array carr 3

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


How to organise car model counting 1

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.


How to organise car model counting 2

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 .


  • Login