Objects First With Java

Download Report

Transcript Objects First With Java

Lecture 16/3/11: Contents
• Example of an array of user-defined
objects: Car and Carr
• Constructor
• Providing functionalities for a userdefined object array
1
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.
2
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)
3
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)
4
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
5
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 */
6
Car object (2): Conventional Getters
public double getEngine()
•
{ return engine; }
•
•
public int getReg()
{ return Reg; }
•
•
public String getModel()
{ return model; }
• }//end of class
7
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 */
8
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;
•
}
9
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();
•
}
10
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
11
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.
12
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 .
13