wood burning stoves 2.0
The moose likes Beginning Java and the fly likes examination question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "examination question" Watch "examination question" New topic

examination question

Ayukawa Madoka

Joined: Feb 28, 2004
Posts: 27
a) Define a class MyMath. Include a doubled-typed constant called PI that has a value of 3.14. Ensure that this member can be accessed without instantiating an object. [3 marks]
b) Include a method in the MyMath class called square that takes in an integer argument and returns the square of the argument. Ensure that this method can be called without instantiating an object. [2 marks]
c) Include another method called divide that takes in two integer parameters and divides the first number by the second number and returns the quotient. Note: You may not use the / and the % operators. Again, ensure that this method can be called without instantiating an object. [6 marks]
My question is...
1)how to ensure that the member can be accessed wihtout instantiating an object?
2)how do i suppose to divide the two value without using the / and % operator?

hello everyone i'm new here.
eammon bannon
Ranch Hand

Joined: Mar 16, 2004
Posts: 140
Your examiner looks like they want you to declare the PI variable public and static - so you use it without having to create a reference to a MyMath Object (e.g. MyMath.PI instead of MyMath math = new MyMath()). The question is badly worded though - because you can't use an Object without instantiating it. There are just different ways of doing this. Using a static class means you don't have to call the Object's constructor directly (e.g. MyMath math = new MyMath()) but using the variable MyMath.PI will still load the compiled class into memory (i.e. instantiate the Object) its just it will be done behind the scenes.
For question 2, have a look at the other operators you have avaliable to you that can act on primitive values. Check out the documentation for the bit shift operators.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11957

digging back to some of my abstract math days, I remember that multiplication is a form of addition.
By A Similar Argument, Division, which is the inverse Multiplication, should be a form of subtraction.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link: http://aspose.com/file-tools
subject: examination question
It's not a secret anymore!