Home
Time Box
Calculator
Snake
Blogs
Hacks

Question 4 • 13 min read


Question 4: Math Class (Unit 2)

Situation: You are developing a scientific calculator application where users need to perform various mathematical operations.

Part (a)

Answer (a)

the Math class serves as a utility class for performing mathematical operations. Its purpose is to provide commonly used mathematical functions and constants, allowing developers to perform various calculations without having to implement them from scratch. Here’s a breakdown of its purpose and utility

  1. pow –> used like pow(double a, double b) –> Returns a raised to the power of b.
  2. random –> used like random() –> Returns a double value greater than or equal to 0.0 and less than 1.0
  3. round –> round the number to make it Integer

Extra

double a2 = Math.log(Math.E); //1.0
double b2 = Math.log10(1000); //3.0

double e = Math.E;//2.718281828459045

Part (b)

Answer (b)

// Define a class named "calculatesquareRoot" to calculate the square root of a given number.
public class Return {
    // Define a method named "return" to return the square root of a given number.
    public static double calculatesquareRoot(double number) {
        return Math.sqrt(number);
    }
    
    // Define the main method to test the "return" method by calculating the square root of 16.
    public static void main(String[] args) {
        // Call the "return" method to calculate the square root of 16 and store the result.
        double result = calculatesquareRoot(16.0);
        // Print the calculated square root.
        System.out.println("Square root is: " + result);
    }
}
Return.main(null);

Square root is: 4.0

Connection with the java backend

public class Number {
    private String title = "Number";
    private String counter = "Index";
    private static int COUNT = 0;
    private final int number;
    private final int index;

    // Number has a zero Argument constructor
    // It initializes a random number between 3 and 36, ie the number of squirrels in class
    public Number() {
        int SIZE = 36; int MIN = 3; int RANGE = SIZE - MIN + 1;  // constants for initialization
        this.number = (int)(Math.random()*RANGE) + MIN;  // observe RANGE calculation and MIN offset
        this.index = Number.COUNT++;    // observe use of Class variable COUNT and post increment
    }

    // It contains a getter for the Random Number
    public int getNumber() {
        return this.number;
    }

    // It contains a getter for Index, or the order it was initialized
    public int getIndex() {
        return this.index;
    }

    // Make a setter to title the Random Number
    public void setTitle(String title) { this.title = title; }

    // Make a setter to name the Counter
    public void setCounter(String counter) { this.counter = counter; }

    // toString method for formatted output, this was not in requirement but is critical knowledge
    public String toString() {
        return this.title + ": " + this.getNumber() + " " + this.counter + ": " + this.getIndex();
    }

    // Write a tester method
    public static void main(String[] args) {

        // Create an ArrayList of Type Number, the ArrayList is called squirrels
        ArrayList<Number> squirrels = new ArrayList<>();

        // Initialize 10 squirrels of class type Number
        // Insert Number Object into ArrayList Squirrels in least to the greatest order using getNumber()
        final int SQUIRRELS = 10;
        for (int i = 0; i < SQUIRRELS; i++) {
            Number num = new Number();
            num.setTitle("Squirrels");
            num.setCounter("Days");
            // Insert in ordered position, this avoids sort algorithm
            for (int j = 0; true; j++) {
                // Conditions to insert
                if ((squirrels.size() == j) // empty or at end of list
                        || (num.getNumber() < squirrels.get(j).getNumber()) )  // less than current
                {
                    /* The java.util.ArrayList.add(int index, Object)
                       method inserts the specified object at the specified position in this list.
                       This is overload of the common java.util.ArrayList.add(Object)
                    */
                    squirrels.add(j, num); // insert in "j" position
                    break;  // break forever loop when inserted
                }
            }
        }

        // Added total/average, not in requirements
        int total = 0;
        for (Number squirrel : squirrels) {
            // Print a formatted message with number of Squirrels and Index
            System.out.println(squirrel);  // prints Object using toString() method
            total += squirrel.getNumber(); // running total, not in requirements
        }
        // Integer division requires cast for precision
        System.out.println("Total random squirrels: " + total + ", Number of days: " + squirrels.size());
        System.out.println("Average squirrels per day: " + Math.round(total / (double) squirrels.size()));
    }

}
Number.main(null);


Squirrels: 9 Days: 10
Squirrels: 11 Days: 11
Squirrels: 12 Days: 19
Squirrels: 14 Days: 14
Squirrels: 14 Days: 15
Squirrels: 15 Days: 18
Squirrels: 19 Days: 12
Squirrels: 25 Days: 16
Squirrels: 31 Days: 13
Squirrels: 36 Days: 17
Total random squirrels: 186, Number of days: 10
Average squirrels per day: 19