Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

examination question

 
Ayukawa Madoka
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
eammon bannon
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic