CSE205 Object Oriented Programming and Data Structures Homework 1 :: 25 pts
1 Submission Instructions
Create a document using your favorite word processor and type your exercise solutions. At the top of the document be
sure to include your name and the homework assignment number, e.g. HW1. Convert this document into Adobe PDF
format and name the PDF file <asuriteid>.pdf where <asuriteid> is your ASURITE user id (for example, my ASURITE
user id is kburger2 so my file would be named kburger2.pdf). To convert your document into PDF format, Microsoft Office
versions 2008 and newer will export the document into PDF format by selecting the proper menu item from the File
menu. The same is true of Open Office and Libre Office. Otherwise, you may use a freeware PDF converter program, e.g.,
CutePDF is one such program.
Next, create a folder named <asuriteid> and copy <asuriteid>.pdf to that folder. Copy any Java source code files to this
folder (note: Java source code files are the files with a .java file name extension; do not copy the .class files as we do not
need those).
Next, compress the <asuriteid> folder creating a zip archive file named <asuriteid>.zip. Upload <asuriteid>.zip to the
Homework Assignment 1 dropbox by the assignment deadline. The deadline is 11:59pm Wed 26 Mar. Consult the online
syllabus for the late and academic integrity policies.
Note: not all of these exercises will be graded, i.e., random ones will be selected for grading.
2 Learning Objectives
1. Use the Integer and Double wrapper classes.
2. Declare and use ArrayList class objects.
3. Write code to read from, and write to, text files.
4. Write an exception handler for an I/O exception.
5. Write Java classes and instantiate objects of those classes.
6. Read UML class diagrams and convert the diagram into Java classes.
7. Identify and implement dependency, aggregation, inheritance, and composition relationships.
3 ArrayLists
3.1 Write code that creates an ArrayList<Integer> object named list and fills list with these numbers (using one or a
pair of for or while loops):
0 1 2 3 4 0 1 2 3 4
3.2 Consider the ArrayList<Integer> object named list containing these Integers:
list = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 }
What are the contents of list after this loop completes?
for (int i = 1; i < 10; ++i) {
list.set(i, list.get(i) + list.get(i-1));
}
3.3 Write an enhanced for loop that counts how many numbers in an ArrayList<Integer> object named list are negative.
Print the count after the loop terminates.
3.4 Write a method named arrayListSum() that has one parameter pList which is an object of the class ArrayList
<Integer>. The method shall return the sum of the elements of pList.
3.5 Write a method named named arrayListCreate() that has two int parameters pLen and pInitValue. The method
shall create and return a new ArrayList<Integer> object which has exactly pLen elements. Each element shall be
initialized to pInitValue.
Arizona State University Page 1
CSE205 Object Oriented Programming and Data Structures Homework 1 :: 25 pts
3.6 Write a void method named insertName() that has two input parameters: (1) pList which is an object of ArrayList
<String> where each element of pList is a person’s name; and (2) a String object named pName. Assume the names
in pList are sorted into ascending order. The method shall insert pName into pList such that the sort order is
maintained.
3.7 Write a void method named arrayListRemove() which has two parameters: pList is an object of the ArrayList
<Integer> class and pValue is an int. The method shall remove all elements from pList that are equal to pValue.
4 Text File Input/Output
4.1 Explain what happens if you try to open a file for reading that does not exist.
4.2 Explain what happens if you try to open a file for writing that does not exist.
4.3 Name your source code file Hw1_1.java. Write a program that prompts the user for the name of a Java
source code file. The program shall read the source code file and output the contents to a new file named the same
as the input file, but with a .txt file name extension (e.g., if the input file is foo.java then the output file shall be
named foo.java.txt). Each line of the input file shall be numbered with a line number (formatted as shown below) in
the output file. For example, if the user enters Hw1_1.java as the name of the input file and Hw1_1.java contains:
