Question 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 variables where, when called, the variable will call the value directly. An int will give the integer when you call it. A refrence type will return the location of the value when called rather than the value itself. Objects do that

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

(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.

public class Nei 
{
    private String name;  //define customer name and money
    private double money;

    public Nei(String name, double money)  //constructor
    {
        this.name = name;
        this.money = money;
    }
    public double getMonety()
    {
        return this.money; //getter for money only because i dont care about the customers name
    }

    public static double calculateInterest(double num, Nei customer) //calculates the given interest for a given customer
    {
        double insterest = 1 + num;   //adds 1 to the given interest so I onnly have to multiply 
        return insterest * customer.getMonety();  //return the interest multiplied by the customer's money to get the money+interest
    }
    public static void main(String[] args) 
    {
        Nei customer1 = new Nei("John Smith", 20);
        System.out.println(calculateInterest(0.1, customer1));
        System.out.println(calculateInterest(0.5, customer1));
    }
}
Nei.main(null)
22.0
30.0

Question 5

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.

The if statement is like a lever that switches the direction that the code is going. It’s very helpful when you have code that depends on certain conditions to work. The while loop is an infinite loop that needs to be broken to continue the code. It’s great for repeating code a large amount of times. The else statement is the same as the if statement, it’s an alternative if the dependencies for the if statment are not met.

(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.

public class gtrrawfe {

    public static void printGradeStatus(int score) {
        if (score >= 60) {  // if the grade is greater or equal to 60
            System.out.println("Pass");  // print pass
        } else {  // if the grade is less than 60
            System.out.println("Fail");  // print fail
        }
    }
    public static void main(String[] args) {
        printGradeStatus(80);
        printGradeStatus(60);
        printGradeStatus(30);
    }
}
gtrrawfe.main(null)
Pass
Pass
Fail