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

Assignment 1 for Arrays, Collections, and the Class Object | CS 110, Assignments of Computer Science

Material Type: Assignment; Class: Intro to Computer Science I; Subject: Computer Science; University: University of San Francisco (CA); Term: Spring 2008;

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-li1
koofers-user-li1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 110: Introduction to Computer Science Spring 2008
Arrays, Collections, and the class Object
In this lesson, we’ll learn more about built-in arrays, ArrayList,
and Java library classes in general. To do this, we’ll talk about
how the ArrayList class in the Java library is implemented.
IntArray implementation
ArrayList is a class that 'wraps' Java's built-in array, providing a
list that can grow. It is generic, in that one can use ArrayList for a
list with elements of any type.
We’ll begin with an IntArray class that wraps a built-in integer
array.
Instructor coding: Write an IntArray class with append and get:
1. Define a built-in array as a ‘private’ data member
2. Define an additional data member: the number of elements.
3. Write the methods ‘add’, ‘get’, and ‘toString’
4. Write a main which tests these functions
Our IntArray is limited, relative to ArrayList.
It doesn’t grow as needed
It is type-specific
To make it grow as needed, we’ll need to modify the ‘add’
method:
If we’ve reached the size limit of the array:
1. create a new array of twice the size
2. copy the old data to the new array
3. point the data member of intArray to the new array
pf2

Partial preview of the text

Download Assignment 1 for Arrays, Collections, and the Class Object | CS 110 and more Assignments Computer Science in PDF only on Docsity!

CS 110: Introduction to Computer Science Spring 2008

Arrays, Collections, and the class Object

In this lesson, we’ll learn more about built-in arrays, ArrayList, and Java library classes in general. To do this, we’ll talk about how the ArrayList class in the Java library is implemented.

IntArray implementation

ArrayList is a class that 'wraps' Java's built-in array, providing a list that can grow. It is generic, in that one can use ArrayList for a list with elements of any type. We’ll begin with an IntArray class that wraps a built-in integer array. Instructor coding : Write an IntArray class with append and get:

  1. Define a built-in array as a ‘private’ data member
  2. Define an additional data member: the number of elements.
  3. Write the methods ‘add’, ‘get’, and ‘toString’
  4. Write a main which tests these functions Our IntArray is limited, relative to ArrayList.  It doesn’t grow as needed  It is type-specific To make it grow as needed, we’ll need to modify the ‘add’ method: If we’ve reached the size limit of the array:
  5. create a new array of twice the size
  6. copy the old data to the new array
  7. point the data member of intArray to the new array

CS 110: Introduction to Computer Science Spring 2008 In-class assignment 1: Modify IntArray so that it will grow as needed.

class Object

The Java library provides a special class called 'Object' which can be thought of as a generic placeholder. If you define a formal parameter to be of type 'Object', then you can send an object of any class type as the actual parameter. So if in class Foo there is a function defined as: void func(Object object) it can be called with: Foo f = new Foo(); f.func(person); f.func(car) f.func(token) where person, car, and token are objects of the types Person, Car, or Token. class Object is used to implement Java's ArrayList—the append method of ArrayList accepts an item of any type. In class Problem 2. Copy your IntArray class and name the copy 'ArrList'. Modify the code so that the built-in array data member is of type Object and the 'add' and 'get' methods work on Object instead of int. To test your new class, add your Person and Car classes into the project, and write a main in your ArrList class that creates lists of each of these.