HW3/HW3.doc
CS 219 - HOMEWORK 3
(Due: Sun, 9/20/2015. Total: 20 pts; Extra credit: 2 pts)
HOMEWORK SUBMISSION POLICIES:
· Put all of your work, even project source code and screenshots of project output, into a single homework document, and submit that document into the course website's drop box by midnight of the day that the homework is due. Put your name at the top of that document. If you completed any of the extra credit work, add a paragraph to the beginning of your document explaining the part you attempted and the amount of extra credit you are claiming. Please also comment the corresponding part of your code with the phase “Extra credit work: …”
· Make sure that the problems in your homework document are in the same order as the problems in the homework assignment.
· In your homework document, make sure that you use monospace font (such as Courier New) for your project source code and project output. The monospace font ensures that text that's supposed to be aligned stays aligned when it's printed. If your printed source code is not aligned properly, you will lose style points.
Submit answers for all of the following questions.
EXERCISES (20 pts + 2 pts Extra Credit):
Java source code files for the following problems are in HW3.zip. Note that every given Java file already compiles and generates result.
1. (4 pts) (The complete code to use is in P1.java) Complete the tracing we did in class on Wed. Sep 9 (week 4) on the second test case. Use Eclipse’s debugging mode to step through the code to verify your tracing result.
(Trace execution on paper): Consider the following method:
10 public static void mystery(ArrayList<Integer> list)
11 {
12 for (int i = list.size() - 1; i > 0; i--)
13 {
14 if (list.get(i) < list.get(i - 1))
15 {
16 int element = list.get(i);
17 list.remove(i);
18 list.add(0, element);
19 }
20 } // end for
21 System.out.println(list);
22 } // end mystery
Write the output produced by the method when passed the following ArrayLists:
[30, 20, 10, 60, 50, 40]
a. (1 pt) The output is: ____________________________________
b. (2 pts) tracing table
|
line# |
List |
size() |
I |
i>0? |
get(i)<get(i-1)? |
element |
|
11 |
[30, 20, 10, 60, 50, 40] |
6 |
|
|
|
|
|
12 |
|
|
5 |
|
|
|
|
12 |
|
|
|
T |
|
|
|
14 |
|
|
|
|
get(5)<get(4)? 40<50? T |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c. (1 pts) Two screenshots of the Variables panel () when running this program (P1.java) in Eclipse under Debugging mode:
i) when execution is at the first line in mystery() i.e. for loop heading line. Click open each [i] to show integer value inside (as shown below);
ii) when execution reaches the println(list) line in mystery(). Again click open each [i] to have the integer value shown on your screenshot (as shown below).
2. (5 pts) (Shell code in P2_MaxLength.java) Write a method maxLength that takes an ArrayList of Strings as a parameter and that returns the length of the longest string in the list. If your method is passed an empty list, it should return 0.
Hint:
· There are build-in ArrayList method to check if an ArrayList object is empty or not.
· There are build-in String method to return the length of a String object.
3. (4 pts) (Shell code in P3_DoubleList.java) Write a method doubleList that takes an ArrayList of Strings as a parameter and that replaces every string with two of that string. For example, if the list stores the values {"how", "are", "you?"} before the method is called, it should store the values {"how", "how", "are", "are", "you?", "you?"} after the method finishes executing.
4. (5 pts) (Shell code in P4_RemoveDuplicate.java) Write a method removeDuplicates that takes as a parameter a sorted ArrayList of Strings and that eliminates any duplicates from the list. For example, suppose that a variable called list contains the following values: {"be", "be", "is", "not", "or", "question", "that", "the", "to", "to"}. After calling removeDuplicates(list); the list should store the following values: {"be", "is", "not", "or", "question", "that", "the", "to"}
Because the values will be sorted, all of the duplicates will be grouped together.
5. (Extra Credit: 2 pts) (Shell code in P5_EC_FilterRange.java) Write a method filterRange that accepts an ArrayList of integers and two integer values min and max as parameters and removes all elements whose values are in the range min through max (inclusive) from the list. For example, if a variable called list stores the values:
[4, 7, 9, 2, 7, 7, 5, 3, 5, 1, 7, 8, 6, 7]
The call of filterRange(list, 5, 7); should remove all values between 5 and 7, therefore it should change the list to store [4, 9, 2, 3, 1, 8]. If no elements in range min-max are found in the list, the list's contents are unchanged. If an empty list is passed, the list remains empty. You may assume that the list is not null.
6. (1/2 pts) Answer this question AFTER you’ve completed this assignment:
What’s the hardest part of this assignment for you? Please explain.
Overall comment your program appropriately. Pay attention to standard stuff like coding style, indention, heading, and curly braces. Double check your code for indentation and alignment after you paste it into your homework document.
Submission: one single document including answers to all exercise questions
· Exercise 1: output result; tracing table; two screenshots
· Exercise 2 – 4 (and EC 5 if included): source code of the method (just that method, not the whole java file); screenshot of running program
· Use the rubric below to check the completeness of your work before turning it in.
Rubric: CS219, HW3
|
Item |
Points (Max) |
Points (recvd) |
|
Exercise (20 pts + 2 EC) |
|
|
|
1. mystery: output result; tracing table; two screenshots |
4 |
|
|
2. maxLength: source code of the method; screenshot |
5 |
|
|
3. doubleList: source code of the method; screenshot |
4 |
|
|
4. removeDuplicates: source code of the method; screenshot |
5 |
|
|
(Extra Credit) 5. filterRange: source code of the method; screenshot |
(2) |
|
|
6. HW feedback |
½ |
|
|
|
|
|
|
General (1½ pts) |
|
|
|
Your project compiles and runs |
½ |
|
|
|
|
|
|
Programming style · Meaningful names for constants and variables · Correct indentation: 2 spaces for each level |
½
|
|
|
Comments · Prolog · End of section comments: end of class, end of method, end of loop etc. · Document each variable · Proper comments in the program |
½ |
|
|
|
|
|
|
Penalty |
|
|
|
Extra instance/class variables/methods |
(- 1) |
|
|
No copy of code in document |
(- 1) |
|
|
No screenshot of execution result |
(- 1) |
|
|
|
|
|
|
Total: |
20+2EC |
|
- END -
HW3/HW3_code/P1.java
HW3/HW3_code/P1.java
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Arrays
;
// use Arrays.asList() method
public
class
P1
{
public
static
void
main
(
String
[]
args
)
{
// case 1
Integer
[]
intArr
=
new
Integer
[]{
2
,
6
,
1
,
8
};
// first create an array of Integers: automatic boxing happens here
// automatic boxing happens here: convert each int to Integer obj and then initialize array of Integer
ArrayList
<
Integer
>
values
=
new
ArrayList
<
Integer
>
(
Arrays
.
asList
(
intArr
));
// create an ArrayList obj from given Integer array
mystery
(
values
);
// case 2
Integer
[]
intArr2
=
new
Integer
[]{
30
,
20
,
10
,
60
,
50
,
40
};
// first create an array of Integers: automatic boxing happens here
// automatic boxing happens here: convert each int to Integer obj and then initialize array of Integer
ArrayList
<
Integer
>
values2
=
new
ArrayList
<
Integer
>
(
Arrays
.
asList
(
intArr2
));
// create an ArrayList obj from given Integer array
mystery
(
values2
);
/*
// case 3
Integer[] intArr3 = new Integer[]{-4, 16, 9, 1, 64, 25, 36, 4, 49};
// first create an array of Integers: automatic boxing happens here
// automatic boxing happens here: convert each int to Integer obj and then initialize array of Integer
ArrayList<Integer> values3 = new ArrayList<Integer>(Arrays.asList(intArr3)); // create an ArrayList obj from given Integer array
mystery(values3);
*/
}
// end main
public
static
void
mystery
(
ArrayList
<
Integer
>
list
)
{
for
(
int
i
=
list
.
size
()
-
1
;
i
>
0
;
i
--
)
{
if
(
list
.
get
(
i
)
<
list
.
get
(
i
-
1
))
{
int
element
=
list
.
get
(
i
);
list
.
remove
(
i
);
list
.
add
(
0
,
element
);
}
}
// end for
System
.
out
.
println
(
list
);
}
// end mystery
}
// end class P1
HW3/HW3_code/P2_MaxLength.java
HW3/HW3_code/P2_MaxLength.java
/*************************************************************************
* CS219 Fall 2015
* HW3
*
* (shell code)
* maxLength
* Write a method maxLength that takes an ArrayList of Strings as a
* parameter and that returns the length of the longest string in the
* list. If your method is passed an empty list, it should return 0.
**************************************************************************/
import
java
.
util
.
ArrayList
;
// to use ArrayList class
import
java
.
util
.
Arrays
;
// to use Arrays.toString(), Arrays.asList() methods
// Arrays is a pre-defined class which contains various methods for
// manipulating arrays
// Java API: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
public
class
P2_MaxLength
{
public
static
void
main
(
String
[]
args
)
{
// testing case 1
String
[]
arr1
=
{
"to"
,
"be"
,
"or"
,
"not"
,
"to"
,
"be"
,
"hamlet"
};
int
result1
=
6
;
test
(
arr1
,
result1
);
// testing case 2
String
[]
arr2
=
{
"to"
,
"be"
,
"or"
,
"not"
,
"to"
,
"be"
};
int
result2
=
3
;
test
(
arr2
,
result2
);
// testing case 3
String
[]
arr3
=
{
"biggest"
,
"next"
,
"not"
};
int
result3
=
7
;
test
(
arr3
,
result3
);
// testing case 4
String
[]
arr4
=
{
"Only one really long string"
};
int
result4
=
27
;
test
(
arr4
,
result4
);
// testing case 5
String
[]
arr5
=
{};
// empty list
int
result5
=
0
;
test
(
arr5
,
result5
);
}
// end main
//**********************************************************************
// a testing shell to print input and testing result
public
static
void
test
(
String
[]
list
,
int
expectedResult
)
{
ArrayList
<
String
>
listArrList
=
new
ArrayList
<
String
>
(
Arrays
.
asList
(
list
));
// create ArrayList out of array
System
.
out
.
println
(
"Testing: "
+
listArrList
);
// print content of array
System
.
out
.
println
(
"expected: "
+
expectedResult
);
// print expected result
int
actualResult
=
maxLength
(
listArrList
);
// call method that solves the problem
System
.
out
.
println
(
"actual: "
+
actualResult
);
// print result
System
.
out
.
println
((
expectedResult
==
actualResult
)
?
"pass"
:
"fail"
);
// testing status: pass or fail
System
.
out
.
println
();
// empty line between testing cases
}
// end test
//**********************************************************************
// actual method that solve the problem
// maxLength: returns the length of the longest string in the ArrayList parameter
public
static
int
maxLength
(
ArrayList
<
String
>
strs
)
{
// ADD CODE
}
// end maxLength
}
// end class P2_MaxLength
HW3/HW3_code/P3_DoubleList.java
HW3/HW3_code/P3_DoubleList.java
/*************************************************************************
* CS219 Fall 2015
* HW3
*
* (shell code)
* doubleList
* Write a method doubleList that takes an ArrayList of Strings as a
* parameter and that replaces every string with two of that string.
**************************************************************************/
import
java
.
util
.
ArrayList
;
// to use ArrayList class
import
java
.
util
.
Arrays
;
// to use Arrays.toString(), Arrays.asList() methods
// Arrays is a pre-defined class which contains various methods for
// manipulating arrays
// Java API: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
public
class
P3_DoubleList
{
public
static
void
main
(
String
[]
args
)
{
// testing case 1
String
[]
arr1
=
{
"how"
,
"are"
,
"you?"
};
String
[]
result1
=
{
"how"
,
"how"
,
"are"
,
"are"
,
"you?"
,
"you?"
};
test
(
arr1
,
result1
);
// testing case 2
String
[]
arr2
=
{
"I"
,
"am"
,
"great,"
,
"thanks!"
};
String
[]
result2
=
{
"I"
,
"I"
,
"am"
,
"am"
,
"great,"
,
"great,"
,
"thanks!"
,
"thanks!"
};
test
(
arr2
,
result2
);
// testing case 3
String
[]
arr3
=
{
"One string only"
};
String
[]
result3
=
{
"One string only"
,
"One string only"
};
test
(
arr3
,
result3
);
// testing case 4
String
[]
arr4
=
{
"1"
,
"4"
,
"3"
};
String
[]
result4
=
{
"1"
,
"1"
,
"4"
,
"4"
,
"3"
,
"3"
};
test
(
arr4
,
result4
);
// testing case 5
String
[]
arr5
=
{};
String
[]
result5
=
{};
test
(
arr5
,
result5
);
}
// end main
//**********************************************************************
// a testing shell to print input and testing result
public
static
void
test
(
String
[]
list
,
String
[]
expectedResult
)
{
ArrayList
<
String
>
listArrList
=
new
ArrayList
<
String
>
(
Arrays
.
asList
(
list
));
// create ArrayList out of array
String
expectedResultStr
=
Arrays
.
toString
(
expectedResult
);
// save expected result as string
System
.
out
.
println
(
"Testing: "
+
listArrList
);
// print content of array
System
.
out
.
println
(
"expected: "
+
expectedResultStr
);
// print expected result
doubleList
(
listArrList
);
// call method with actual functionality
System
.
out
.
println
(
"actual: "
+
listArrList
);
// print result
System
.
out
.
println
((
expectedResultStr
.
equals
(
""
+
listArrList
))
?
"pass"
:
"fail"
);
// testing status: pass or fail
System
.
out
.
println
();
// empty line between testing cases
}
// end test
//**********************************************************************
// actual method that solve the problem
// doubleList: replaces every string from ArrayList parameter with two of that string
public
static
void
doubleList
(
ArrayList
<
String
>
strs
)
{
// ADD CODE
}
// end doubleList
}
// end class P3_DoubleList
HW3/HW3_code/P4_RemoveDuplicates.java
HW3/HW3_code/P4_RemoveDuplicates.java
/*************************************************************************
* CS219 Fall 2015
* HW3
*
* (shell code)
* removeDuplicates
* Write a method removeDuplicates that takes as a parameter a sorted
* ArrayList of Strings and that eliminates any duplicates from the list.
* Because the values will be sorted, all of the duplicates will be
* grouped together.
**************************************************************************/
import
java
.
util
.
ArrayList
;
// to use ArrayList class
import
java
.
util
.
Arrays
;
// to use Arrays.toString(), Arrays.asList() methods
// Arrays is a pre-defined class which contains various methods for
// manipulating arrays
// Java API: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
public
class
P4_RemoveDuplicates
{
public
static
void
main
(
String
[]
args
)
{
// testing case 1
String
[]
arr1
=
{
"be"
,
"be"
,
"is"
,
"not"
,
"or"
,
"question"
,
"that"
,
"the"
,
"to"
,
"to"
};
String
[]
result1
=
{
"be"
,
"is"
,
"not"
,
"or"
,
"question"
,
"that"
,
"the"
,
"to"
};
test
(
arr1
,
result1
);
// testing case 2
String
[]
arr2
=
{
"duplicate"
,
"duplicate"
,
"duplicate"
,
"duplicate"
,
"duplicate"
};
String
[]
result2
=
{
"duplicate"
};
test
(
arr2
,
result2
);
// testing case 3
String
[]
arr3
=
{
"unique"
};
String
[]
result3
=
{
"unique"
};
test
(
arr3
,
result3
);
// testing case 4
String
[]
arr4
=
{
"be"
,
"is"
,
"not"
,
"or"
,
"question"
,
"that"
,
"the"
,
"to"
};
String
[]
result4
=
{
"be"
,
"is"
,
"not"
,
"or"
,
"question"
,
"that"
,
"the"
,
"to"
};
test
(
arr4
,
result4
);
// testing case 5
String
[]
arr5
=
{};
String
[]
result5
=
{};
test
(
arr5
,
result5
);
}
// end main
//**********************************************************************
// a testing shell to print input and testing result
public
static
void
test
(
String
[]
list
,
String
[]
expectedResult
)
{
ArrayList
<
String
>
listArrList
=
new
ArrayList
<
String
>
(
Arrays
.
asList
(
list
));
// create ArrayList out of array
String
expectedResultStr
=
Arrays
.
toString
(
expectedResult
);
// save expected result as string
System
.
out
.
println
(
"Testing: "
+
listArrList
);
// print content of array
System
.
out
.
println
(
"expected: "
+
expectedResultStr
);
// print expected result
removeDuplicates
(
listArrList
);
// call method with actual functionality
System
.
out
.
println
(
"actual: "
+
listArrList
);
// print result
System
.
out
.
println
((
expectedResultStr
.
equals
(
""
+
listArrList
))
?
"pass"
:
"fail"
);
// testing status: pass or fail
System
.
out
.
println
();
// empty line between testing cases
}
// end test
//**********************************************************************
// actual method that solve the problem
// removeDuplicates: eliminates any duplicates from sorted ArrayList parameter
public
static
void
removeDuplicates
(
ArrayList
<
String
>
strs
)
{
// ADD CODE
}
// end removeDuplicates
}
// end class P4_RemoveDuplicates
HW3/HW3_code/P5_EC_FilterRange.java
HW3/HW3_code/P5_EC_FilterRange.java
/*************************************************************************
* CS219 Fall 2015
* HW3
*
* (shell code)
* (Extra Credit) filterRange
* Write a method filterRange that accepts an ArrayList of integers and
* two integer values min and max as parameters and removes all elements
* whose values are in the range min through max (inclusive) from the
* list. If no elements in range min-max are found in the list, the
* list's contents are unchanged. If an empty list is passed, the list
* remains empty. You may assume that the list is not null.
**************************************************************************/
import
java
.
util
.
ArrayList
;
// to use ArrayList class
import
java
.
util
.
Arrays
;
// to use Arrays.toString(), Arrays.asList() methods
// Arrays is a pre-defined class which contains various methods for
// manipulating arrays
// Java API: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Arrays
;
// use Arrays.asList() method
public
class
P5_EC_FilterRange
{
public
static
void
main
(
String
[]
args
)
{
// case 1
Integer
[]
intArr1
=
new
Integer
[]{
4
,
7
,
9
,
2
,
7
,
7
,
5
,
3
,
5
,
1
,
7
,
8
,
6
,
7
};
int
min1
=
5
,
max1
=
7
;
// range
Integer
[]
result1
=
new
Integer
[]{
4
,
9
,
2
,
3
,
1
,
8
};
test
(
intArr1
,
min1
,
max1
,
result1
);
// case 2
Integer
[]
intArr2
=
new
Integer
[]{
-
8
,
3
,
5
,
-
2
,
12
,
0
,
-
4
,
1
,
-
10
};
int
min2
=
-
5
,
max2
=
5
;
// range
Integer
[]
result2
=
new
Integer
[]{
-
8
,
12
,
-
10
};
test
(
intArr2
,
min2
,
max2
,
result2
);
// case 3
Integer
[]
intArr3
=
new
Integer
[]{
4
,
7
,
9
,
2
,
7
,
7
,
5
,
3
,
5
,
1
,
7
,
8
,
6
,
7
};
int
min3
=
1
,
max3
=
9
;
// range
Integer
[]
result3
=
new
Integer
[]{};
test
(
intArr3
,
min3
,
max3
,
result3
);
// case 4
Integer
[]
intArr4
=
new
Integer
[]{
4
,
1
,
9
,
11
,
3
,
10
,
14
,
2
,
12
};
int
min4
=
5
,
max4
=
8
;
// range
Integer
[]
result4
=
new
Integer
[]{
4
,
1
,
9
,
11
,
3
,
10
,
14
,
2
,
12
};
test
(
intArr4
,
min4
,
max4
,
result4
);
// case 5
Integer
[]
intArr5
=
new
Integer
[]{};
int
min5
=
0
,
max5
=
3
;
// range
Integer
[]
result5
=
new
Integer
[]{};
test
(
intArr5
,
min5
,
max5
,
result5
);
}
// end main
//**********************************************************************
// a testing shell to print input and testing result
public
static
void
test
(
Integer
[]
list
,
int
min
,
int
max
,
Integer
[]
expectedResult
)
{
ArrayList
<
Integer
>
listArrList
=
new
ArrayList
<
Integer
>
(
Arrays
.
asList
(
list
));
// create ArrayList out of array
String
expectedResultStr
=
Arrays
.
toString
(
expectedResult
);
// save expected result as string
System
.
out
.
println
(
"Testing: "
+
listArrList
+
", min "
+
min
+
", max "
+
max
);
// print content of array
System
.
out
.
println
(
"expected: "
+
expectedResultStr
);
// print expected result
filterRange
(
listArrList
,
min
,
max
);
// call method with actual functionality
System
.
out
.
println
(
"actual: "
+
listArrList
);
// print result
System
.
out
.
println
((
expectedResultStr
.
equals
(
""
+
listArrList
))
?
"pass"
:
"fail"
);
// testing status: pass or fail
System
.
out
.
println
();
// empty line between testing cases
}
// end test
//**********************************************************************
// actual method that solve the problem
// filterRange: eliminates any duplicates from sorted ArrayList parameter
public
static
void
filterRange
(
ArrayList
<
Integer
>
nums
,
int
min
,
int
max
)
{
// ADD CODE
}
// end filterRange
}
// end class P5_EC_FilterRange