Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Quiz 1 Worksheet - Object Oriented Programming II | CMSC 132, Quizzes of Computer Science

Material Type: Quiz; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Quizzes

Pre 2010

Uploaded on 02/13/2009

koofers-user-1mi
koofers-user-1mi 🇺🇸

10 documents

1 / 2

Toggle sidebar

Related documents


Partial preview of the text

Download Quiz 1 Worksheet - Object Oriented Programming II | CMSC 132 and more Quizzes Computer Science in PDF only on Docsity!

CMSC 132 Quiz 1 Worksheet

The first quiz for the course will be on Wednesday, Sept 13 during your lab session. The following list provides more information about the quiz:

  • The quiz will be a written quiz (no computer).
  • Closed book, closed notes quiz.
  • Answers must be neat and legible. We recommend that you use pencil and eraser.

The following exercises cover the material to be included in this quiz. Solutions to these exercises will not be provided, but you are welcome to discuss your solutions with the TA or instructor during office hours.

Exercises

  1. What is encapsulation? How does it relate to abstraction?
  2. What is the difference between procedural abstraction and data abstraction?
  3. Define an enumerate type named Day that represents the days of the week. Using the enhanced for loop construct, write a code fragment that prints all the days of the week.
  4. Using the Day enumerated type you defined above, define a static method called randomDay that returns a random day.
  5. Is the following code fragment legal? Briefly explain.

ArrayList L1 = new ArrayList(); ArrayList L2 = L1;

  1. The Student class is defined as follows:

public class Student { private String name; private int id;

public Student(String name, int id) { this.name = name; this.id = id; }

public String toString() { return "Name: " + name + " Id: " + id; } }

Rewrite the following code fragment using generics and the new for loop construct.

ArrayList L = new ArrayList();

L.add(new Student("Mary", 10)); L.add(new Student("John", 5)); L.add(new Student("Kelly", 7));

for (int i=0; i<L.size(); i++) System. out .println(L.get(i));

  1. The WebSite class is defined as follows:

public class WebSite { private String name; private int links;

public WebSite(String name, int links) { this.name = name; this.links = links; }

public String toString() { return "Name: " + name + " Links: " + links; } }

a. Modify the class so it implements the Comparable interface, allowing you to compare WebSite objects based on their name.

b. Implement a comparator class that allow us to compare WebSite objects based on the number of links.

  1. Implement a method named compareStrArrays that has the following prototype:

public static boolean compareStrArrays(String[][] array1, String[][] array2);

The method will return true if the arrays have the same dimensions and the same

String values. Keep in mind that each array can have rows with different lengths.