Home Workshop 1 (FRQ1) Hacks
Post
Cancel

Workshop 1 (FRQ1) Hacks

Question 1: Primitive Types vs Reference Types (Unit 1)

Situation: You are developing a banking application where you need to represent customer information. You have decided to use both primitive types and reference types for this purpose.

(a) Define primitive types and reference types in Java. Provide examples of each.

  • primitive types are the simplest types that are usually built into the programming language. Examples include integer, float, and boolean. Primitive types are usually stored directly in memory
  • reference types hold references to other data in memory. reference types store the memory address where the data is stored whereas primitive types store the actual value. Examples include arrays, objects, classes.


(b) Explain the differences between primitive types and reference types in terms of memory allocation and usage in Java programs.

  • Primitive types store the actual value of the data in memory where as reference types hold references to data in the memory.
  • Primitive types have less memory usage since they only store the actual value. reference types have more memory usage since they store the actual data and the reference to the data


(c) Code:


You have a method calculateInterest that takes a primitive double type representing the principal amount and a reference type Customer representing the customer information. Write the method signature and the method implementation. Include comments to explain your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public double calculateInterest(double amount, Customer customer) {

    //use a method from class Customer (assuming its something like getCreditScore)
    int creditScore = customer.getCreditScore();

    double interestRate;

    //just a if statement to assign interest rate based on the credit score from the customer object
    if (creditScore >= 700) {
        interestRate = 0.05; 
    } else if (creditScore >= 600) {
        interestRate = 0.07;
    } else {
        interestRate = 0.1;  
    }

    //calculates the interest based on the principal amount and interest rate
    double interest = principalAmount * interestRate;

    return interest;

}

Question 2: Iteration over 2D arrays (Unit 4)

Situation: You are developing a game where you need to track player scores on a 2D grid representing levels and attempts.

(a) Explain the concept of iteration over a 2D array in Java. Provide an example scenario where iterating over a 2D array is useful in a programming task.

  • a 2D array is an array of arrays so it requires a nested loop to iterate over each row and column of the array. 2D arrays are useful in game development. For example, in my groups current project we use 2D arrays to represent our game maps. Iterating over the array helps in tasks like collision detection, and path finding.


(b) Code:


You need to implement a method calculateTotalScore that takes a 2D array scores of integers representing player scores and returns the sum of all the elements in the array. Write the method signature and the method implementation. Include comments to explain your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public int calculateTotalScore(int[][] scores) {

    //initializes total
    int totalScore = 0;

    //nested for loop iterates through array
    for(int i=0; i < scores.length; i++) {
        for (int j=0; j < scores[i].length; j++) {
            totalScore += scores[i][j];
        }
    }

    return totalScore;

}

Question 5: If, While, Else (Unit 3-4)

Situation: You are developing a simple grading system where you need to determine if a given score is passing or failing.

(a) Explain the roles and usage of the if statement, while loop, and else statement in Java programming. Provide examples illustrating each.

  • if statement checks for a condition and whether or not the condition is true, an action is taken. It can be used with else or else if for more complex decision making
  • while loop lets you to repeatedly run code as long as a specified condition is true. It is useful when the number of iterations is unknown.
  • The else statement is used with an if statement to execute a block of code if the if condition is false.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//if statement example
int x = 10;
if (x > 0) {
    System.out.println("x is positive");
}

//while loop example
String[] fruits = {"strawberry", "lemon", "grapes", "apple", "watermelon"};

int i = 0;
while (i < fruits.length) {
    System.out.println(fruits[i]);
    i++;
}

//else example
int x = -9;
if (x > 0) {
    System.out.println("x is positive");
}
else {
    System.out.println("x is negative");
}

1
2
3
4
5
6
7
x is positive
strawberry
lemon
grapes
apple
watermelon
x is negative

(b) Code:


You need to implement a method printGradeStatus that takes an integer score as input and prints “Pass” if the score is greater than or equal to 60, and “Fail” otherwise. Write the method signature and the method implementation. Include comments to explain your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//use void because i don't want to return anything
public void printGradeStatus(int score) {

    //if score is greater than or equal to 60 print pass
    if (score >= 60) {
        System.out.println("Pass");
    }
    //else print fail
    else {
        System.out.println("Fail");
    }

}

printGradeStatus(55);
printGradeStatus(89);
1
2
Fail
Pass
1
This post is licensed under CC BY 4.0 by the author.

Data Types and Control Structures Lesson

FRQ 2 - Writing Classes