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?
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.
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com