COPPIN STATE COLLEGE
COSC 420: JAVA PROGRAMMING
TEST 2 - Spring 2004
Name _______________________________
1. Short Questions - 30pts
a. (4pts) Write the differences between constructors and methods.
b. (4pts) Like properties and methods, the constructors of a superclass are inherited in the subclass – is the statement TRUE or False? ___________. How constructors of the superclass can be invoked from the subclass? Explain.
c. (4pts) When do you need to write the no-parameter (a default) constructor and when is it defined by Java?
d. (2pts) If a superclass defines constructors other than a default constructor, how a subclass can use the default constructor of the superclass?
e. (4pts) What does the modifier static mean and under what circumstances should a data be declared using the modifier static? Can you define a class with the modifier static? Explain.
f. (4pts) Write the differences between a method overloading and a method overriding.
g. (4pts) Explain what will be the consequences: (i) if a class is defined using final modifier? (ii) if a method is defined using final modifier?, and (iii) if a data is defined using final modifier?
h. (4pts) Write the main differences between an abstract class and an interface. Write the signatures of an abstract class named Shape and interface named Employee.
2. (10pts) Indicate True or False for the following statements:
a. In Java, parameters are passed to methods using pass by value and pass by references, because for a parameter of a primitive type, the actual value is passed; for a parameter of a reference type, the reference for the object is passed. ________________.
b. A static method is a method that can be invoked without using instances of the class that contains the static method. ___________________.
c. Abstract classes are like regular classes with data and methods, so you can create instances of abstract classes using the new operator. _______________.
d. A class that contains abstract methods must declared abstract. __________________
e. An abstract class can be extended. __________________.
f. A subclass of a nonabstarct superclass cannot be abstract. _______________.
g. A class can extend one or more superclass, but a class can implement only one interfaces. ______.
h. An interfaces can extend one or more interfaces. _______________.
i. In Java, the order in which modifiers appear before a method is important. ______________
j. A protected datum or method can be accessed by its subclass in any package. ___________
3. (2.5pts) Are the following statements correct?
Cylinder cylinder = new Cylinder (1, 1) ;
Circle circle = cylinder ;
4. (2.5pts) Are the following statements correct?
Circle circle = new Circle (1) ;
Cylinder cylinder = (Cylinder) circle ;
5. Write the necessary codes for the following super class Person and subclass Student
public class Person
{
// data fields
private String name;
private int age;
private String ssn;
// Write the default constructor and the constructor for the three data members.
// Output metod
public void printPerson( ) {
System.out.println(“Name: “ + name) ;
System.out.println( “Age: “ + age);
System.out.println( “Social Security: “ + ssn);
}
}
public ……………………………… Person
{
// data fields
private String major;
private double GPA;
// methods
public Student( String n, int a, String s, double g, String m) {
}
public void StudentInfo( ) {
…………………. …. // call the method of parent class
System.out.println(“Major: “ + major) ;
System.out.println( “GPA: “ + GPA);
}
}
6. Write the necessary Accessors and Mutators for the superclass Person.
7. Write the appropriate test class named StudentTest for the above super and subclass so that you should be able to printout GPA 3.7 for a student named Joe Smith of age 24, whose social security number is 999 99 9999, and whose major is Chemistry.
B. The GeometricObject is an abstract class and is represented in UML notation as below:
The methods findArea and findPerimeter are not implemented because their implementation is dependent on the specific type of geometric objects or class (say, Rectangle, Circle, triangle etc). Others are common to all the subclasses of GeometricObject class. So, they could be implemented in the class GeometricObject.
(2pts.) Write the signature of the abstract class Geometric Object (that is just declare the class in Java syntax):
__________________________________________________________________
(8pts) Write a class named Triangle that extends GeometricObject. The outline of the class is given below. Complete the program as instructed.
class Triangle _________________________ // fillup the blank.
{
// declare the data members
// Write the default constructor
// Write the constructor of the triangle with the specified sides
// Implement the abstract method findArea in GeometricObject
public double findArea()
{
}
// Write the abstract method findCircumference in GeometricObject
}
The formula for computing the area is as follows:
s = (side1 + side2 + side3) / 2;
area = Ö (s(s - side1)(s - side2)(s - side3))
C. (25pts)
Define a class Money that has type int data field for dollars and cents. Write a constructor with zero parameters, one parameter (the value of dollars), and two parameters. Write accessors and mutators and a toString() method that returns a string of the form $dollars.cents. Also, write a method findcents() that calculates the total value in cents. (Write the program in separate pages).
4. Writing method's algorithm - 10pts
You want to write a program to create, assign, and check math problems of various difficulty levels for students. At the design phase, you identified all the required classes and data and methods of each class. One of the methods is getAndCheckAnswer () in class MathDrill. Write the algorithm (in steps) for the method getAndCheckAnswer (). [Hint: you want to check student's answer with your solution; give congratulatory message if the student's answer is correct, otherwise give the correct answer. Also keep track of the total number of incorrect answers and correct answers given by the student. You are free to add additional steps].
Algorityhm for getAndCheckAnswer () of class MathDrill
cosc420/Test2/2004