Posts

Showing posts with the label Java

Write code that will take three Integer values, in1, in2 and in3, compute their average and store the average in a new Integer av. Remember that Integer is a wrapper class (an object), and is not the same as int. To obtain the int value stored in an Integer, use intValue( ).

Write code that will take three Integer values, in1, in2 and in3, compute their average and store the average in a new Integer av.  Remember that Integer is a wrapper class (an object), and is not the same as int.  To obtain the int value stored in an Integer, use intValue( ). Answer:               int average = (in1.intValue( ) + in2.intValue( ) + in3.inValue( )) / 3;               Integer av = new Integer(average

Define an interface class that contains two int constants, X = 5 and Y = 10 and one int method called useXY which receives no parameters. Call your interface class XYClass

Define an interface class that contains two int constants, X = 5 and Y = 10 and one int method called useXY which receives no parameters.  Call your interface class XYClass Answer:               public interface XYClass               {                             public final int X = 5;                             public final int Y = 10;                             public int useXY( );               }

Explain or provide an example showing how each of the following Exceptions could arise. a) ArithmeticException b) NullPointerException c) NumberFormatException d) IndexOutOfBoundsException

Explain or provide an example showing how each of the following Exceptions could arise. a)       ArithmeticException b)       NullPointerException c)       NumberFormatException d)       IndexOutOfBoundsException Answer: a)       An arithmetic operation could not take place because of an illegal action such as division by zero or square root of a negative number b)       An object was not instantiated (it had the value null) before a message was passed to it c)       An expected value for a numeric operation was not a legal number, such as trying to parse a String value to create an equivalent int where the String contains a non-digit character d)       A numeric parameter used as an index into an object such as an array, String or Vector was out of range, such as charAt(-1)

Assume a class Foo implements Comparable. Without knowing anything else about the Foo class, write an equal method that returns true if the Foo parameter passed to this Foo is equal to this Foo as determined by using the implementation of Comparable.

Assume a class Foo implements Comparable.  Without knowing anything else about the Foo class, write an equal method that returns true if the Foo parameter passed to this Foo is equal to this Foo as determined by using the implementation of Comparable. Answer:                             public boolean equals(Foo a)                             {                                           if(compareTo(a)) = = 0) return true; else return false;

Provide a reason why a method would be declared static.

Provide a reason why a method would be declared static. Answer:  A method is declared static if it might be used, not by the current object, but by any objects of the class.  That is, the method does not require access to any single object’s instance data.  Consider a class called Point that consists of an x and y coordinate.  To determine the distance between two Points, we could make a static computeDistance method.  It is passed two Points as parameters and computes the distance.  Another example is the Keyboard class from the textbook, which does not itself have any instance data.

Provide a reason why an instance data would be declared static.

Provide a reason why an instance data would be declared static.  Answer:  If an instance data is to be shared among all objects of the class, the instance data would be static.  As an example, an instance data that counts the number of times something has happened across all objects of the class would be made static.  If we are writing a chess game and have a ChessPiece class that includes a method called movePiece, we would want to know how many moves a player has made, but not necessarily how many times a single piece has been moved.  So, all of the ChessPieces share a numberOfTimesMoved instance data that is incremented by any ChessPiece whenever it is moved.

Consider the condition (x = = y). How is this handled if x and y are primitive types? How is this handled if x and y are objects?

Consider the condition (x = = y).  How is this handled if x and y are primitive types?  How is this handled if x and y are objects? Answer:  If x and y are primitive types, then the values stored in x and y are compared and true is returned if the two values are equal.  If x and y are objects, then the object that x and y reference are compared.  If they are the same object, it returns true, otherwise it returns false.

Explain what a NullPointerException is and how it can arise.

Explain what a NullPointerException is and how it can arise. Answer:  A NullPointerException is an exception that indicates that an operation was attempted on some object that is equal to null.  For instance, ChessPiece c; declares c to refer to a ChessPiece, but c has not yet been instantiated, so any message passed to c at this point will cause a NullPointerException.

Explain why aliases can cause trouble for programmers who are not aware of them.

Explain why aliases can cause trouble for programmers who are not aware of them. Answer:  Aliases arise in Java when variables refer to the same object.  If one of those variables is used to pass a message to the object in such a way that the object is changed, then the other variables now refer to a changed object.  Unless the programmer is careful, he/she may not realize that a change has been made to the object.  Consider a class Foo that has an instance data x and a method incr that adds one to x.  Assume a and b are both of type Foo, where a’s x is 10.  Consider the following two instructions:  b = a;  b.incr( );  The result is that a/b’s value x is now 11, but if the programmer is not aware of the aliasing, then he/she might expect a’s x to still be 10.