Arrays, user-defined type arrays. - Department of Computer Science

Download Report

Transcript Arrays, user-defined type arrays. - Department of Computer Science

Lecture 23/2/11
• Working with an array
• Array of user-defined objects (if time
permits)
2 MARCH, next Wednesday: Reading week No lecture 6-7.30, but – room B12 Cruciform
is available from 7.30- 9.00: Martin O’Shea
will consult over any questions on SP1
1
Advert
• In-class open-book Test 9/3/11
subjects
– Strings
– Methods
– Arrays
2
Test 1 results
•
•
•
•
39 or less
40 through 70
71 or more
Absent
5
21
9
17
• Total
52
Those with marks 50 or less should put more effort:
(a) Look through previous lecture slides before
lectures
(b) Try ask questions and do not stop if anything
remains unclear;
(c) Visit all lab sessions;
(d) Try do the same tasks by yourself in DCSIS labs;
3
Array
Array is an indexed list of elements of the same
type; the index is supplied by default (!)
A string array nam[ ]: contains both entries and
index.
String[] nam ={“John”,“Paul”,“George”,“Ringo”};
Index:
0
1
2
3
Length (the number of entries) is 4
An integer array age[ ]:
int age[ ]= {23, 32, 19, 30, 25, 25, 23, 30};
Index:
0
1 2 3 4 5 6 7
Length is 8
4
Work with arrays(1)
Data of 5 students:
double height[ ]={1.56, 1.72, 1.80, 1.85, 1.90};
//in m
double weight[ ]={65.3,80.0,78.1,76.5,112.8};
// in kg
Problem: compute the body mass index for all
the students, bmi=weight/height2
(in the US, those with bmi between 20 and 25
are considered of normal weight)
5
Work with arrays(2)
Loop for is natural with arrays: the index used as the
counter
double bmi[ ]=new double[5];
for (int I = 0; I < 5; I + +)
bmi[I]=weight[I] / (height[I]height[I]);
If length of student arrays is not known or is
variable, put array’s length whatever it is:
double bmi[ ]=new double[height.length];
for (int I = 0; I < height.length; I + +)
bmi[I]=weight[I] / (height[I]height[I]);
6
Work with arrays(3)
The same result with a method for the bmi:
double[ ] bmiind(double h[ ], double w[ ]){
double in[ ];
for (int ii = 0; ii < h.length; ii = ii+1)
in[ii]=h[ii]/(w[ii]w[ii]);
return in; }
Method bmiind is just a frame box; to make it work,
one needs to put within a class this:
double[ ] bmi=bmiind(weight, height);
7
Work with arrays(4)
Still, no result on the screen!!! Printing arrays is
not easy in Java: no innate methods for that!
double[ ] bmip(double h[ ], double w[ ]){
double in[ ];
for (int a = 0; a < h.length; a++){
in[a]=h[a]/(w[a]w[a]);
System.out.println(a + “ ’s bmi is “+in[a]);}
return in; }
double[ ] b=bmip(weight, height);
8
//Outputs and Prints
Finding a maximum in an array
double x[ ]; //assume taken from somewhere
int place=-1; //index of the max entry
double maxim=-1000;
for (int i = 0; i < x.length; i = i+1){
if (x[i] > maxim)
{maxim=x[i];
place=i;}
}
Question: Make it into a method.
9
Finding maximum : a method
int place= -1; //for keeping the index of max entry
double findMax(double x[ ]){ //method’s wrap-up
double maxim= -1000;
for (int i = 0; i < x.length; i = i+1){
if (x[i] > maxim)
{maxim=x[i];
place=i;}
}
}
// Note: a trick with “place”
10
Finding the average in an array
double x[ ]={1.1, 1.2, 1.6, 2.0,1.1};
double average=0; //to accumulate the sum
for (int i = 0; i < x.length; i++)
average=average+x[i];
//after this, average=7.0;
average=average/x.length;
//average=7.0/5=1.4;
Question: Make it into a method.
11
Finding the average: method
public static void main(String[ ] args){
double x[ ]={1.1, 1.2, 1.6, 2.0,1.1};
double average=MetAv(x);} //average=1.4
public static double MetAv(double[ ] a){
double av=0;
for (int i = 0; i < a.length; i++)
av=av+a[i];
av=av/a.length;
return av; }
12
Constrained average: method
public static void main(String[ ] args){
double x[ ]={1.1, 1.2, 0, 1.6, 2.0, 0}; //av. of no 0s
double average=ConAv(x);}
public static double ConAv(double[ ] a){
double av=0;
int counter=0;
for (int i = 0; i < a.length; i++)
{ if(a[i]!=0){ counter++;
av=av+a[i];}
}
return av/counter; }
13
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.36.6, Pohl and McDowell, “Java by
dissection”
- Employee and TwoEmployee, p. 96-112,
Ch.3, Farrell, “Java programming”
14
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
•
15
Array of user-defined type(2)
• To develop an array of applicants, we need
– A named class, say Appl, with variables declared to hold
all individual applicant data (A);
– A constructor in this class that would take values from the
arrays holding id and name information (B);
– Representation of the id and name arrays (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.
16
Array of user-defined type(3)
• class Appl{
•
•
•
•
•
public int ids;
public String nms;
public Appl(int iden, String nnn){
ids=iden;
nms=nnn;}
•
•
•
static int[] iden(){
int ii[]={12, 15, 22, 18, 11};
return ii;}
•
•
•
static String[] namen(){
String jj[]={"Aa", "Bb", "Cc", "Dd", "Ee"};
return jj;}
•
17
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(); }
•
}
• }
18
Array of user-defined type(5)
Question:
• Identify which parts of class Appl
correspond
• to tasks A, B, C, D and E on slide 16
19