‫د‬

KINGDOM OF SAUDI ARABIA Ministry of Higher Education Prince Sultan University College of Computer and Information Sciences

‫ا‬ ‫ا‬

‫ن‬ ‫ﻣت‬

‫وا‬

‫ا‬ ‫ا‬ ‫وزارة ا‬ ‫ﻣ ا ﻣ‬ ‫ " ما‬#

Quiz 03 Programming II (CS 102) Java Collections Framework NO Authorized Document Duration: 50 minutes

Dr. Anis Koubaa Student ID Student Name Student Section Questions are generally independent. You can respond to questions in any order

Be very clear in your answer. Learning Outcome Evaluation • Understand the concepts of Java Collection Framework • Be able to analyze the output of Collection methods • Be able to write a program using Java Collection Framework • Be able to write a class that implement a comparable interface

Grading Review Questions Programming Output Collections Programming Total

3 points 2 points 5 points 10 points

There are five pages Dr. Anis Koubâa

1/5

December 2012

Review Questions (3 points)

(10 minutes)

1. What is a wrapper class? Describe the difference between an int and an Integer? (1 point) Java wrappers are classes that wrap up primitive values in classes that offer utility methods to manipulate the values.

2. What is an iterator? Why are iterators often used with linked lists? (1 point) An iterator allows for iteration over a collection. Iterator takes the place of Enumeration in the Java collections framework.

3. How is a queue different from a list? (1 point) A queue is governed by the law first in first out. It has two main methods push and pop. A list is does not have that constraint by is a data structure for storing and accessing objects through an iterator.

4. Write a method called removeZeroes that takes as a parameter an ArrayList of integers and eliminates any occurrences of the number 0 from the list. For example, if the list stores the values (0, 7, 2, 0, 0, 4, 0) before the method is called, it should store the values (7, 2, 4) after the method finishes executing. (1 point) public static void removeZeroes(ArrayList list) { for (int i = list.size() - 1; i >= 0; i--) { if (list.get(i) == 0) { list.remove(i); } } }

Dr. Anis Koubâa

2/5

December 2012

Exercise 2

(2 points)

(20 minutes)

1. What is the output of this program? 1 import java.util.*; 2 3 public class TestMap { 4 public static void main(String[] args) { 5 // Create a HashMap 6 7 8 hashMap.put("Anderson", 31); 9 hashMap.put("Lewis", 29); 10 hashMap.put("Cook", 29); 11 12 System.out.println("Display entries in HashMap"); 13 System.out.println(hashMap + "\n"); 14 15 // Create a TreeMap from the previous HashMap 16 = 17 18 System.out.println("Display entries in ascending order of key"); 19 System.out.println(treeMap); 20 21 // Create a LinkedHashMap 22 23 24 linkedHashMap.put("Smith", 30); 25 linkedHashMap.put("Anderson", 31); 26 linkedHashMap.put("Lewis", 29); 27 linkedHashMap.put("Cook", 29); 28 29 // Display the age for Lewis 30 System.out.println("The age for " + "Lewis is " + 31 linkedHashMap.get("Lewis").intValue()); 32 33 System.out.println("\nDisplay entries in LinkedHashMap"); 34 System.out.println(linkedHashMap); 35 } 36 }

Display entries in HashMap {Cook=29, Smith=30, Lewis=29, Anderson=31} Display entries in ascending order of key {Anderson=31, Cook=29, Lewis=29, Smith=30} The age for Lewis is 29 Display entries in LinkedHashMap {Smith=30, Anderson=31, Cook=29, Lewis=29}

Dr. Anis Koubâa

3/5

December 2012

Exercise 3 (5 points)

(20 minutes)

Write a program that meets the following requirements: • Define a class named Point with two data fields x and y to represent a point’s x and y coordinates. Implement the Comparable interface for comparing the points on x-coordinates and on y-coordinates if x-coordinates are identical. (2 points) /** Define a class for a point with x- and y- coordinates */ static class Point implements Comparable { double x; double y; Point(double x, double y) { this.x = x; this.y = y; } @Override public int compareTo(Point p2) { if (this.x < p2.x) return -1; else if (this.x == p2.x) { // Secondary order on y-coordinates if (this.y < p2.y) return -1; else if (this.y == p2.y) return 0; else return 1; } else return 1; } @Override public String toString() { return "(" + x + ", " + y + ")"; } }

Dr. Anis Koubâa

4/5

December 2012



Define a class named CompareY that implements Comparator. Implement the compare method to compare two points on their y-coordinates and on their x-coordinates if y-coordinates are identical.

/** * A comparator for comparing points on their y-coordinates. If ycoordinates are the same, compare their x-coordinator. */ static class CompareY implements Comparator { public int compare(Point p1, Point p2) { if (p1.y < p2.y) return -1; else if (p1.y == p2.y) { // Secondary order on x-coordinates if (p1.x < p2.x) return -1; else if (p1.x == p2.x) return 0; else return 1; } else return 1; } } }



Randomly create 100 points in an ArrayList then sort them using a TreeSet structure. Display the TreeSet. Implement all classes and interfaces. public class Exercise22_04 { // Each row in points represents a point private double[][] points; public static void main(String[] args) { Point[] points = new Point[100]; for (int i = 0; i < points.length; i++) { points[i] = new Point(Math.random() * 100, Math.random() * 100); } System.out.println("Sort on x-coordinates"); Arrays.sort(points); for (int i = 0; i < points.length; i++) { System.out.println(points[i]); } System.out.println("Sort on y-coordinates"); Arrays.sort(points, new CompareY()); for (int i = 0; i < points.length; i++) { System.out.println(points[i]); } }

Dr. Anis Koubâa

5/5

December 2012