posted 14 years ago
a) Define a class MyMath. Include a doubledtyped 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?
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.
posted 14 years ago
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.
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.
posted 14 years ago
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.
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 offbyone errors
grapes are vegan food pellets. Eat this tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
