• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

examination question

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A timing clock, fuse wire, high explosives and a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic