Objects First With Java
Download
Report
Transcript Objects First With Java
Lecture 10/3/10: Contents
• Example of an array of user-defined
objects: Car
• Constructor
• Functionalities of a class of userdefined object
1
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)
2
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 */
3
Car object (2): Conventional Getters
public double getEngine()
•
{ return engine; }
•
•
public int getReg()
{ return Reg; }
•
•
public String getModel()
{ return model; }
• }//end of class
4
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 as well as 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[] =crCar(nc); // initialising array of Car's
dataCar(arCar);//printing data of the array
int[] st=modstat(arCar);
}
/*Put in main method all the functions that the class should provide:
(a) initialising an array of Cars, (b) printing data of the array, and
(c) informing of the numbers of different brand cars in the array */
5
Working with Car array: Carr (2a)
• //-------method crCar to create a Car array of a given length
a --• 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;
•
}
6
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();
•
}
7
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
8
How to organise Car brand counting?
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.
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. 9