Data Structures and Programming, Java Final Exam solution

1.   A (5 points)  Write a Java program that:à        Asks for an integer value n; à        Then asks for n number(s) from the user; à        Finally displays the average of all the numbers typed in by the user.        B (5 points)  Write a program segment in Java that sets the value of “int Number1” randomly from the following set:   {2, 4, 6, 20, 22}.        C (5 points)  Write a method Power(base, exponent) that returns the value of        (Assume that base is a positive, non-zero double and exponent is a double.)
2.   A (2 points)Explain autoboxing in Java and give an example.       B (8 points)Write a program that rolls a FOUR-sided die 10 times and stores results in ascending order in int a[10].       C (3 points)What does the following program print? Why? Explain with your own words.   public class Program {            public static void main (String [] args)            {                final int arraySize =5;                int a[] = { 1, 2, 3, 4, 5 };                int result = whatIsThis(a, arraySize);                System.out.printf("Result is : %d.\n", result);            }            public static int whatIsThis(int b[], int size)            {                if ( size == 1 )                      return b[ 0 ];                else                      return 4 * (b[ size - 1 ] )   + whatIsThis(b, size - 1 );            } }      3.  A (11 points)  (Complex Numbers) Create a class called Complex for performing arithmetic with complex numbers.  Complex numbers have the form   realPart + imaginaryPart * i   where i is    Write a program to test your class.  Use floating-point variables to represent the private data of the class.  Provide a constructor that enables an object of this class to be initialized when it is declared.  Provide a no-argument constructor with default values in case no initializers are provided.  Provide public methods that perform the following operations: a)      Add two Complex numbers: the real parts are added together and the imaginary parts are added together. Hint: For this exercise, please consider the use of this reference (see from subchapter 8.4).  A template looks like:   // Add two Complex numbers public Complex add( Complex Right ) {    /*  Write code here */ } b)      Subtract two Complex numbers: the real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand. c)      Print Complex numbers in the form (a, b), where a is the real part and b is the imaginary part.         B (6 points)  Write a program that inputs five numbers, each of which is between 10 and 100, inclusive.  As each number is read, display it only if it is not a duplicate of a number already read.   4.   A (4 points)  What is a subclass?  Can a subclass access the members of its superclass?  If yes, how? If not, why? Explain.        B (4 points)  A method can access the superclass’s private members, but it can not access the subclass private members.  What can be inferred about the method?   C (6 points)  Consider class Point provided in APPENDIX A (1 file).                       Inherit class Square. A Square is just a Point and a side.  It should include methods for getting and setting the side as well as calculating the area.                       Based on class Square, inherit class Cube.  A Cube is a Square extended into three dimensions.  It requires no additional data members.                      Redefine the calculation of the area to calculate the surface area and add a member method to calculate the volume.  The surface area can be calculated with the formula side * side * 6, and the volume may be calculated with the formula side * side * side.  Finally, write a short driver program to show the features of your code.      5.  A (16 points) Construct the class of non-negative 3x3 Matrices containing non-negative double elements with the following functionalities: à        Determinant method returns the double value of the matrix determinant:   à        isNull method returns true if the matrix is a Null Matrix (all the elements are 0). Otherwise, isNull method returns false. à        isUMatrix method returns true if the matrix is a U Matrix.  Otherwise the return value is false. Let à        Multiplication results in a 3x3 matrix and takes a constant of double type as the only input parameter.  The resulting matrix R is:   à        Addition method returns a 3x3 sum of two matrices (it takes the addend matrix as an input parameter).  The resulting matrix R is: Implement the member methods of class of 3x3 matrices with the functionalities above.         B (5 points) Given matrix X and matrix Y.  Write a program segment that instantiates matrix Z and sets its elements:  
6.   A (10 points) Write a program that creates a queue of integers.  After that it loads in 20 random integers between 1 and 6, and after the 20th number it prints out the content of the queue.  Finally, the program calculates and prints out the average of the values without losing the values from the queue!   (For the queue structure, consider Chapter 21, Data Structures.)   7. A (10 points) Create a class of Circle with attributes radius, X and Y that represent the Cartesian coordinates of the center point. Radius default value is 1, it must be positive for every circle.  Provide a member method Cross that returns true if two circles cross each other.  Otherwise it returns false.  Cross has a Circle object input parameter.
Powered by