Вы находитесь на странице: 1из 12

1.

00 Tutorial 3
Agenda
Quiz 1 Logistics
Classes & Objects
Array
Insertion Sort
Quiz 1 Logistics
Quiz 1 will be in class time
Covering Lecture1-Lecture 11
Open book, open notes
Quiz 1 Review
Classes and Objects
-A Student Class
Write a simple Student class with following
attributes, a String variable name, an int variable
age.
Write two constructors
The first constructor takes one argument a String type
argument as the value for name, and assign the age
variable value 19.
The Second constructor takes two arguments - a String
argument as the value for name and a int argument as
the value for age.
Write a print method to print out the attributes
StudentTest Class
Write a main class StudentTest
In the main method,
1. creates 3 students objects by using both
constructors,
2. print out their attributes, and
3. calculate the average age of the students.
Arrays of primitive types
Three things to do:
declare the array
create the array (arrays are objects)
fill the array
int[] myArray;
myArray = new int[32];
myArray[0] = 1;
myArray[1] = 10;
Arrays of objects
Again, three things to do:
Declare the array
Create the array
Create/initialize each object in the array
Integer[] myInts;
myInts = new Ints[2];
myInts[0] = new Integer(1);
myInts[1] = new Integer(2);
Shortcuts
Can do all three at once (sometimes)
int[] powers = {0,1,10,100};
String[] profs = { "Harward",
"Lerman"};
Integer[] ints = { new
Integer(1), new Integer(10)
};
Use powers.length to get the # elements
in the array
Array Question 1
What is the value of a and b after the following
code segment?
int [] a = {1, 2, 3, 4, 5};
int [] b = a;
b[4]=0;
a[1]= b[4];
Array Question 2
What is the value of c after the following
code segment?
int [] a = {1, 2, 3, 4, 5};
int [] b = {11, 12, 13};
int [] c = new int [4];
for (int j=0; j<3; j++)
c[j]=a[j]+b[j];
Insertion Sort (1)
A series number 5, 6, 9, 11, 6, 2, 4 is
given, how do you sort them so that they are
in the increasing(decreasing) order? Show
the step by step process on the black board
(each pair). How many comparison do you
make? How many swaps do you make?
You are given some cards, show the step by
step process of your insertion sorting.
Insertion Sort (2)
Students stand in one line at random order
One student sorts the line so that the line is
in increasing or decreasing order of height.

Вам также может понравиться