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

Lab Exercise in Java: Generics and Privacy in CpSc 215L, Fall 2008, Lab Reports of Computer Science

A lab exercise for cpsc 215l students in the fall 2008 semester. The lab focuses on gaining experience with generics in java and implementing best practices to prevent privacy leaks. The exercise consists of two parts: the first part is collaborative and involves creating classes ink and pen, while the second part is individual and requires modifying the stack interface and implementation to be generic. Students are expected to write safe copy constructors, parameterized constructors, and getter and setter methods for the classes ink and pen, and to test their implementation by changing the quantity of the ink object and checking if the values are the same.

Typology: Lab Reports

Pre 2010

Uploaded on 07/28/2009

koofers-user-4wj
koofers-user-4wj 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CpSc 215L, Section 002 Fall 2008
Lab #6:
Generics and Privacy Leaks
Sravanthi Dandamudi and Hao Jiang
September 30th, 2008
1 Objectives
The purpose of this lab is to give you some experience working with generics in Java as well as using best
practices to avoid “privacy leak”.
2 Lab Description
For this lab, you are required to have the Stack interface as well as one of your interface implementations.
The lab is divided into two parts. The first part should be done collab oratively in pairs. The second part
should be done individually.
2.1 Part One
For the collaborative part, you are required to implement the class Ink which includes the following member
variables:
quantity
color
Additionally, you are required to create the class Pen that has the following member variables:
ink, which is an object of the class Ink.
length
color
For both classes, you are required to use the Color enumeration covered in the code handouts provided in
class. For the Ink class, provide a safe copy constructor that takes an object of the same class as argument.
For the Pen class, provide a safe parameterized constructor that takes the different member variables as
arguments. For both classes provided safe getter and setter methods for the different member variables.
To test your implementation, in your main method do the following following:
Create an object of the class Ink.
Create an object of class Pen passing the ink object as one of the parameters to the constructor.
Change the quantity of the ink object.
Get the quantity of the ink member variable in the pen object.
Get the quantity of the ink object.
Are they the same value? Is your implementation safe?
Note: Make sure all your member variables are private.
1
pf2

Partial preview of the text

Download Lab Exercise in Java: Generics and Privacy in CpSc 215L, Fall 2008 and more Lab Reports Computer Science in PDF only on Docsity!

CpSc 215L, Section 002 Fall 2008

Lab #6:

Generics and Privacy Leaks

Sravanthi Dandamudi and Hao Jiang

September 30th, 2008

1 Objectives

The purpose of this lab is to give you some experience working with generics in Java as well as using best practices to avoid “privacy leak”.

2 Lab Description

For this lab, you are required to have the Stack interface as well as one of your interface implementations. The lab is divided into two parts. The first part should be done collaboratively in pairs. The second part should be done individually.

2.1 Part One

For the collaborative part, you are required to implement the class Ink which includes the following member variables:

  • quantity
  • color

Additionally, you are required to create the class Pen that has the following member variables:

  • ink, which is an object of the class Ink.
  • length
  • color

For both classes, you are required to use the Color enumeration covered in the code handouts provided in class. For the Ink class, provide a safe copy constructor that takes an object of the same class as argument. For the Pen class, provide a safe parameterized constructor that takes the different member variables as arguments. For both classes provided safe getter and setter methods for the different member variables.

To test your implementation, in your main method do the following following:

  • Create an object of the class Ink.
  • Create an object of class Pen passing the ink object as one of the parameters to the constructor.
  • Change the quantity of the ink object.
  • Get the quantity of the ink member variable in the pen object.
  • Get the quantity of the ink object.
  • Are they the same value? Is your implementation safe?

Note: Make sure all your member variables are private.

CpSc 215L, Section 002 Fall 2008

2.2 Part Two

For the individual part, you are required to change the stack interface that you have previously developed to be generic. Additionally, change one of your stack implementation classes to implement the interface and be generic as well.

To test your implementation, in your main method do the following:

  • Create a stack of Ink.
  • In a loop, push several ink objects on the stack.
  • Create a stack of Strings.
  • In a loop, push several Strings on the stack.
  • For both stacks, pop and print out all the elements of the stack. For the ink, you will need to provide the toString method that outputs both the quantity and the color of the ink.

Good Luck!