Name: ______

DePaul ID: ______

CSC-211 Final Exam –Spring Quarter, 2002

Mendelsohn

Unless specifically required to do so, you do not have to provide ‘#include’ directives, or to declare main() or any class declarations. Simply include the appropriate code. Be sure that your writing is legible!!!

N.B.: Use your book as a reference, not as a crutch. Do not try to search for answers in your book as it can often lead to incorrect solutions on your exam. For a few (but not all) of these problems, you may want to make liberal use of Appendix M in your book which contains a partial Java API.

*************************************************

1)[5 points] Write out the output of the following method:

public static void doStuff() {

for (int i=1; i<=2; i++) {

for (int j=1; j<=3; j++)

System.out.print(“*\\”);

//Note: print (not println)

System.out.println();

}

}

2)[5 points] Write a method that calculates the sum of a series of integers from 1 to ‘n’ (where ‘n’ is a formal parameter of the method). The method should return the sum of all the values from 1 to ‘n’. Be sure to include visibility modifiers, etc. Sample output:

System.out.println( calcSum(4) );

//outputs: 10 (i.e. 4+3+2+1)

3)[13 points] Write the Java code for a GroceryItem class with the following specifications:

(a) Fields: itemName, unitCost, and quantityInStock. (quantityInStock refers to the quantity of items currently in stock). One additional variable of type ‘boolean’ called ‘inStock’ which would be true whenever there is more than one item in stock. (In other words, whenever a change is made to ‘quantityInStock’, you must decide whether to make a change to ‘inStock’).

(b) A constructor to initialize all the variables in (a).

(c) A public method that returns itemName.

(d) A toString() method

(e) Accessor methods for ‘quantityInStock’, ‘inStock’, and ‘unitCost’.

(f) Write a main method to test the GroceryItem class that you

created in (a)-(d). You may put the main method in the GroceryItem class.

Extra space (if needed) for problem #3.

4)Write Java statements to accomplish the following:

a)[2 points] Display the 14thelement (not index) of an integer array ‘f’ (i.e. Assuming you have an array called ‘f’, output the 14th element)

Sample output: The 14th element of this array is: 2343

b)[3 points] Write a statement to initialize every element of a char array called ‘myArray’ to the letter ‘q’. Note: You do not know the actual length of the array.

c)[5 points] Write a method (including the header) that accepts an array of integers as its parameter, and returns the average of all the numbers in that array. Watch your data types! Note: You may not necessarily know the number of elements in the array. Your method should be able to handle an array of size 2 or size 2000. Your output should like like mine below.

Sample Output: The average of the 20 elements in this array is 33.7.

5)[5 points] Write a bit of code that will prompt the user for a word of text, and then count the number of of instances of the letter ‘e’ (upper OR lower case) in that string.

Sample Output:

You entered the string: “Hello Ebert!”

The character ‘e’ occurs 3 times in that string.

6)[5 points] Write a method called ‘numberOfEvens’ that accepts as an argument, an array (of integers). The method should then step through the entire array and count the number of even numbers present in the array, and return that value. You may assume that the array is completely full of integer values.

7)Attached is a class called ‘Die’ that represents a dice object. The class itself is quite simple. Please use it to answer the following questions. Please answer all questions in the space provided.

a)[2 points] Will the statement ‘d1.faceValue = 6;’ in the main method result in an error? Hint: Where is the main method located? Explain.

b)[2 points] Are there any other possible errors in the code, or will it compile cleanly? If you believe there will be errors, explain.

c)[3 points] For now, please assume that the program would compile cleanly (which may or may not be the case). What would be the output of the last line in the main method?

public class Die

{

private final int MAX_SIDES = 6;

private int faceValue; // current value showing on the die

public Die ()

{

faceValue = 1;

} //end of default constructor

public Die (int theSide)

{

if (theSide > MAX_SIDES)

face_value = 6;

else

faceValue = theSide;

} //end of Die(int theSide) constructor

public void roll ()

{

faceValue = (int) (Math.random() * 6) + 1;

} //end of method ‘roll’

public void setFaceValue(int face)

{

if (face > MAX_SIDES)

face_value = 6;

else

faceValue = face;

} //end of method ‘setFaceValue’

public int getFaceValue ()

{

return faceValue;

} //end of method ‘getFaceValue’

public static void main(String[] args) {

Die d1 = new Die(6);

Die d2 = new Die();

d2.MAX_SIDES = 4; //a pyramidal die

d1.setFaceValue(5);

d2.roll();

d2.faceValue=3;

d2 = d1;

System.out.println(d2.getFaceValue());

} //end of method main()

} // end of class Die

Scrap paper…

1