Home
Time Box
Calculator
Snake
Blogs
Hacks

Question 1 • 17 min read


Question 1

(a) Definitions and Examples In Java, there are two types of data types: primitive types and reference types.

Primitive types are the basic types of data. They store simple values directly in the location they occupy in memory. Java has 8 primitive types: byte, short, int, long, float, double, boolean, and char. For example, int age = 30; is a primitive type storing the integer value 30.

Reference types store references (or addresses) to the actual data. The reference points to a location in memory where the data is stored. Classes, interfaces, arrays, and enums are reference types. An example of a reference type is String name = “John Doe”; where name is a reference to a string object stored in memory.

(b) Memory Allocation and Usage Primitive types are stored in the stack memory, which allows for fast access but has limited size. Each variable directly contains its value, and when you assign it to another variable, a fresh copy of the value is created and stored.

Reference types are stored in the heap memory, which is used for dynamic memory allocation where the size of the memory needed doesn’t need to be known before runtime. When you assign a reference type to another variable, both variables now point to the same data in memory rather than creating a new copy. If you modify the data using one reference, the change is visible through all references.

Code

class Customer {
    // Assume other properties like name, ID, etc., are defined here
}

public class BankingApplication {
    /**
     * Calculates the interest for a given principal amount for a customer.
     *
     * @param principal The principal amount.
     * @param customer  The customer for whom the interest is calculated.
     * @return The interest amount calculated.
     */
    public double calculateInterest(double principal, Customer customer) {
        // Assuming a fixed interest rate of 5%
        final double interestRate = 0.05;
        double interest = principal * interestRate;
        
        // Further processing with the customer information if necessary
        
        return interest;
    }
}



Question 2

(a) Concept and Example Scenario Iterating over a 2D array involves traversing the array row by row (or column by column) and accessing each element individually. A common use case for 2D arrays is representing matrices, grids, or any situation where you need a row and column structure. For instance, in a game development context, a 2D array can represent a game board or grid where each cell holds information like player scores across different levels and attempts.

Code

public class GameApplication {
    /**
     * Calculates the total score from a 2D array of scores.
     *
     * @param scores The 2D array containing player scores.
     * @return The total sum of all scores.
     */
    public int calculateTotalScore(int[][] scores) {
        int totalScore = 0;
        
        // Iterate over each row
        for (int i = 0; i < scores.length; i++) {
            // Iterate over each column in the row
            for (int j = 0; j < scores[i].length; j++) {
                totalScore += scores[i][j]; // Add the score to the total
            }
        }
        
        return totalScore;
    }
}

Hacks for Monday

public class StackHeapTest {
    public int n = 5; // primitive data type on the heap
    public static void changeInt(int nValue, int nRefN, StackHeapTest nRef){
        System.out.println("PRE-CHANGE: ===============");
        System.out.println("nValue : " + nValue);
        System.out.println("nRefn  : " + nRefn);
        System.out.println("nRef.n : " + nRef.n);

        nValue += 10;
        nRefn += 10;
        nRef.n += 10;

        System.out.println("POST-CHANGE: ==============");
        System.out.println("nValue : " + nValue);
        System.out.println("nRefn  : " + nRefn);
        System.out.println("nRef.n : " + nRef.n);
    }
    
    public static void main(String[] args){
        int n = 5; // primitive data type on the stack
        StackHeapTest nRef = new StackHeapTest();

        System.out.println("PRE-METHOD: ===============");
        System.out.println("n      : " + n);
        System.out.println("Hash for n  : " + System.identityHashCode(n));
        System.out.println("nRef   : " + nRef);
        System.out.println("Hash  for nRef : " + System.identityHashCode(nRef));
        System.out.println("nRef.n : " + nRef.n);
        System.out.println("Hash for nRef.n  : " + System.identityHashCode(nRef.n));

        changeInt(n, nRef.n, nRef); // stack by value, heap by value, heap by reference

        System.out.println("POST-METHOD: ===============");
        System.out.println("n      : " + n);
        System.out.println("Hash for n  : " + System.identityHashCode(n));
        System.out.println("nRef   : " + nRef);
        System.out.println("Hash  for nRef : " + System.identityHashCode(nRef));
        System.out.println("nRef.n : " + nRef.n);
        System.out.println("Hash for nRef.n  : " + System.identityHashCode(nRef.n));

    }
}

StackHeapTest.main(null)
|   public class StackHeapTest {

|       public int n = 5; // primitive data type on the heap

|       public static void changeInt(int nValue, int nRefN, StackHeapTest nRef){

|           System.out.println("PRE-CHANGE: ===============");

|           System.out.println("nValue : " + nValue);

|           System.out.println("nRefn  : " + nRefn);

|           System.out.println("nRef.n : " + nRef.n);

|   

|           nValue += 10;

|           nRefn += 10;

|           nRef.n += 10;

|   

|           System.out.println("POST-CHANGE: ==============");

|           System.out.println("nValue : " + nValue);

|           System.out.println("nRefn  : " + nRefn);

|           System.out.println("nRef.n : " + nRef.n);

|       }

|       

|       public static void main(String[] args){

|           int n = 5; // primitive data type on the stack

|           StackHeapTest nRef = new StackHeapTest();

|   

|           System.out.println("PRE-METHOD: ===============");

|           System.out.println("n      : " + n);

|           System.out.println("Hash for n  : " + System.identityHashCode(n));

|           System.out.println("nRef   : " + nRef);

|           System.out.println("Hash  for nRef : " + System.identityHashCode(nRef));

|           System.out.println("nRef.n : " + nRef.n);

|           System.out.println("Hash for nRef.n  : " + System.identityHashCode(nRef.n));

|   

|           changeInt(n, nRef.n, nRef); // stack by value, heap by value, heap by reference

|   

|           System.out.println("POST-METHOD: ===============");

|           System.out.println("n      : " + n);

|           System.out.println("Hash for n  : " + System.identityHashCode(n));

|           System.out.println("nRef   : " + nRef);

|           System.out.println("Hash  for nRef : " + System.identityHashCode(nRef));

|           System.out.println("nRef.n : " + nRef.n);

|           System.out.println("Hash for nRef.n  : " + System.identityHashCode(nRef.n));

|   

|       }

|   }

Unresolved dependencies:

   - variable nRefn
public class Main {
    public static void main(String[] args) {
        String originalString = "abc";
        int indexToChange = 0; // Index of the character to change
        
        if (indexToChange < 0 || indexToChange >= originalString.length()) {
            System.out.println("Index is out of range");
            return;
        }

        // Convert the character at the specified index to uppercase
        char newChar = Character.toUpperCase(originalString.charAt(indexToChange));

        // Create a new string with the modified character
        String modifiedString = originalString.substring(0, indexToChange) + newChar + originalString.substring(indexToChange + 1);
        System.out.println(originalString.substring(0, 1));

        System.out.println("Original String: " + originalString);
        System.out.println("Modified String: " + modifiedString);
    }
}
Main.main(null)
a
Original String: abc
Modified String: Abc