




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





1) (6 pts) Describe a situation where it would be reasonable to use a static variable and a static method within a class. 2) (12 pts) Consider creating a Complex_Number class that allowed the user to perform routine arithmetic for complex numbers. Let the instance variables of the class be two doubles: real and imaginary. Write a boolean instance method reciprocal that changes a Complex_Number object into it's reciprocal. If the operation is possible, your method should complete the operation and return true. If it is impossible to take the reciprocal of the Complex_Number object the method is called upon, just return false. The prototype is provided below: public boolean reciprocal() { }
5) (15 pts) Write a static method that takes in two one dimensional character arrays and returns true if the contents of each corresponding element are equal and false otherwise. (If the lengths of the arrays are not equal, automatically return false.) public static boolean equal(char[] word1, char[] word2) { } 6) (10 pts) Let a dictionary be stored in a 2 dimensional character array. Think of the array as an array of arrays, where each element of the array is a different 1-D array. Write a static method search that takes in a dictionary and a character array as parameters and returns true if the word stored in the character array appears in the dictionary. (Hint: use the equal method from question 4.) public static boolean search(char[][] dictionary, char[] word) { }
7) (10 pts) Consider the following abstract class below: public abstract class Publication { protected String title; protected int num_pages; protected String language; public Publication(String name, int pages, String lang) { title = name; num_pages = pages; languages = lang; } public abstract void buy(); public abstract void recycle(); } Write a class Magazine that inherits from this abstract class. This class should include one extra instance variable: a double called cost. Write an appropriate constructor for the class. The method buy should simply print out the message, "Can buy almost anywhere." The method recycle should simply print out the message, "Put in the recycle bin on Mondays." Finally, add a method discount which takes in a double rate in between 0 and 1 and discounts the cost of the Magazine object by the rate passed to the method.
10) (10 pts) Write a code segment that iterates through all of the Publication objects from the vector v and calls the buy and recycle method on each of them. (Your code should work regardless of the number of items in the v. Furthermore, there is no need to remove the items from v.) 11) (5 pts) What is the output of the code segment from question 9? 12) (1 pt) What two countries fought in the Russo-Japanese War?
Extra Page - If you would like any work on this page graded, please label your work clearly.