EXTRA_CREDIT_4 - Illinois Institute of Technology

advertisement
CS116---EXTRA CREDIT # 4- NOT GRADABLE until end of Semester
Objectives:
Use vectors
Use Multidimentional arrays and ArrayList.
A. EXERCISE
Use the class Drivers from the solution of Extra Credit 3 exercise. Since Drivers uses DriverType enum,
you would also need this class.
Create the class MyVector to be placed in package Driverclass1. In the main method:

Create a Vector object

Create 3 Drivers objects (choose data from sample output below) using the non default
constructor of the class.

Insert the 3 objects into the vector.

FIRST DISPLAY: Create a for loop and iterate through the vector and for each object display its
attributes by using toString method. Data can be accessed from a Vector via the method get(i)
where I is the index of the vector where the data is located.

Now create a 4th Drivers object with new values. Add the new object at index 1 of the vector but
do not delete the present object at index 1 but rather move it one index down (which method of
the vector class does just that?).

Create an integer object for number 15 and insert into the vector.

SECOND DISPLAY:Create a for loop and iterate through the vector. Output the values of the
attributes of all of the objects by using their toString method as they are retrieved from the
vector during each iteration.

THIRD DISPLAY: Find the objects of Drivers type that are equal , if any, and display their id
numbers. Make sure that youexclude from the comparison the Integer object at the bottom of
the vector.

FOURTH DISPLAY: Now clear the vector of all objects. Display the size of the vector.

SEE SAMPLE OUTPUT AND MATCH IT.

NOTE: Vector class has method size() which returns the size of the vector as an int.

NOTE: Disregard the compiler message:
---------- Java compiler ----------
1
Note: MyVector.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
B. PRACTICE EXERCISE


Create a class called TwoDimMin that has a main method.
In the main method:
o Declare a two dimensional array of int data type. We will use the technique of entering data
directly into the array and then the system can instantiate its size from the data we entered (as
opposed to instantiating the array to specific sizes and then entering the data using for loops).
o Enter the two rows of data: { 9, 8, 7, 6} and {10,20,30,40}
o Now create the two for loops needed to read the contents of the array row by row and display it.
In the second for loop (that reads the columns of each row) add code to capture the min of the
int numbers read and display its value.
Use the lecture notes and or your text as reference!!
SAMPLE OUTPUT
Part A:
C:\CS116\SPRING2015\EXTRACREDIT\ExtraCredit4\SolutionPracticeProgram4\PartA>java
Driverclass1.MyVector
FIRST DISPLAY
The name of the driver is: John The age is: 24 The license number is: 345 The years of driving is: 5.5 The
driver type is: Adult_Male_DriverThe object
id is 1
The name of the driver is: Mary The age is: 18 The license number is: 945 The years of driving is: 2.5 The
driver type is: Teenager_Female_DriverThe o
bject id is 2
The name of the driver is: John The age is: 44 The license number is: 345 The years of driving is: 15.5 The
driver type is: Adult_Male_DriverThe objec
t id is 3
5
SECOND DISPLAY
The name of the driver is: John The age is: 24 The license number is: 345 The years of driving is: 5.5 The
driver type is: Adult_Male_DriverThe object id is 1
The name of the driver is: John The age is: 24 The license number is: 345 The years of driving is: 5.5 The
driver type is: Adult_Male_DriverThe object id is 4
The name of the driver is: Mary The age is: 18 The license number is: 945 The years of driving is: 2.5 The
driver type is: Teenager_Female_DriverThe object id is 2
The name of the driver is: John The age is: 44 The license number is: 345 The years of driving is: 15.5 The
driver type is: Adult_Male_DriverThe object id is 3
15
THIRD DISPLAY
2
Object with id= 1 is equal with object of id= 4
Object with id= 1 is equal with object of id= 3
Object with id= 4 is equal with object of id= 3
FOURTH DISPLAY
The size of the vector is now= 0
PART B
The minimum is 6
MAKE SURE YOU GET PERMISSION BEFORE YOU UPLOAD ON BLACKBOARD>
Copyright: George Koutsogiannakis , 2015, Illinois Institute of Technology
3
Download