|
Will this C++ program compile? If the code shown in not legal, what's wrong with it and how will you fix it?
class X { int n; public: void X( int i ) { n = i; } }; int main() { X xobj( 100 ); }
Will this Java program compile? If the code shown in not legal, what's wrong with it and how will you fix it?
class X { private int n; public void X( int i ) { n = i; } } class Test { public static void main( String[] args ) { X xobj = new X(100); } }
Do you see any parallels between the children's riddle: "Who is bigger? Mr. Bigger or Mr. Bigger's little baby?" and the question "Which is bigger? A class or a class's little baby (meaning a subclass)?"
Provide C++ and Java definitions for an Account class that could be used for a bank account. The class would need at least four data members: name, balance, accountNumber, and interestRate. Specify a constructor and a print function for the class. Give some thought to whether you'd want these data members to be public, private, or protected. You'd obviously not want to make all the class members public since that'd destroy the privacy of an account.
Let the Account class be the parent class of the subclasses named SavingsAccount and CheckingAccount. The subclass SavingsAccount should have an additional data member signifying whether the savings can to be used for short-term high-risk investments or long-term low-risk investments. The subclass CheckingAccount would need a data member signifying the minimum balance to be maintained in the account. As with the parent class, your subclasses would also need constructors and print functions.
Your homework should show the class definitions. Your homework should also show some specific objects created from the classes and successful invocations of the print functions for the different types of objects.
The following Java class does something rather "peculiar"; in line (A) it sets its data member y equal to the value of the data member x. The class comes with two constructors, in lines (B) and (C). The constructor in line (B) supplies a value for each of the two data members of the class. On the other hand, the constructor in line (C) initializes only the data member x. What will be printed out by the statements in lines (D) and (E)? [Note: You'll probably be surprised by the correct answer to the question, which you can find out by compiling and running the program. The discussion in Section 7.3 will help you understand the behavior of the program.]
class X { private int x; private int y = x; //(A) X( int xx, int yy ) { x = xx; y = yy; } //(B) X( int xx ) { x = xx; } //(C) public String toString() { return " " + x + " " + y; }; public static void main( String[] args ) { X xobj = new X( 100, 200 ); System.out.println( xobj ); //(D) xobj = new X( 300 ); System.out.println( xobj ); //(E) } }
The print ( X* ) function defined below is not able to do its job because m and n are in the private section of the class X? How can this situation be fixed with a single additional declaration in the definition of class X? (You are not allowed to change the access control property for any of the members of X.)
class X { int m; int n; public: X( int mm, int nn ) { m = mm; n = nn; } }; void print( X* ptr ) { cout << ptr->m << " " << ptr->n << endl; }
Here is an example of a nested interface in Java. Would this code fragment compile?
interface X { interface Y { void doSomething_Y(); } void doSomething_X(); } class Z implements X { public void doSomething_X() {} public void doSomething_Y() {} }
The following Java program does not compile. The compiler reports a problem with the statement in line (A). How will you fix it? (Let's say that you really need to construct an object of type Y in line (A). So commenting out that statement is not an option.)
interface X { interface Y { void doSomething_Y(); } void doSomething_X(); } class Z implements X { public void doSomething_X() {} public void doSomething_Y() {} public static void main( String[] args ) { X x = new Z(); Y y = new Z(); //(A) } }
|