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 a data types that come with the original Java language itself and don’t really refer to any memory location and are stored directly.
Examples:
int
double
boolean
char
Reference types use objects in memory rather than storing the values themselves
Examples:
- Classes
- Arrays
- Interfaces
- Enumerations
(b) Explain the differences between primitive types and reference types in terms of memory allocation and usage in Java programs.
The differences are mainly in the fact that primitives store the data in the Stack and the references store it in the Heap (where the objects are created) and are referred to by the stored variables. Primitives also hold the actual values but references are stored in variables
(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.
class Customer {
private double interestRate;
// constructor to set the interest rate for the customer
public Customer(double interestRate) {
this.interestRate = interestRate;
}
// getters for interest rate getting from customer object
public double getInterestRate() {
return interestRate;
}
}
public class BankApp {
public double calculateInterest(double principal, Customer customer) {
double interestRate = customer.getInterestRate(); // gets the interest rate out of the customer object
double interest = principal * interestRate; // storing via primitive
return interest;
}
public static void main(String[] args) {
Customer customer = new Customer(0.07); // tester data
double principal = 2000.0;
System.out.println("Principal: " + principal);
Bank bank = new Bank();
double interest = bank.calculateInterest(principal, customer);
System.out.println("Interest: " + interest);
}
}
BankApp.main(null)
Principal: 2000.0
Interest: 140.0
Question 3: Array (Unit 6)
Situation: You are developing a student management system where you need to store and analyze the grades of students in a class.
(a) Define an array in Java. Explain its significance and usefulness in programming.
An array is a data structure that can be used to store elements that are the same type and serve as a convenient and easy way to store and access multiple values
(b) Code:
You need to implement a method calculateAverageGrade
that takes an array grades
of integers representing student grades and returns the average of all the elements in the array. Write the method signature and the method implementation. Include comments to explain your code.
public class GradeAnalyzer {
public double calculateAverageGrade(int[] grades) { // calculate average grade method using the array of grades
int sum = 0; // setting inital sum to 0
for (int grade : grades) { // iterating that for grade in the array grades, add the sum with the grade and go to the next
sum += grade;
}
double average = (double) sum / grades.length; // divide by array length to get average
return average;
}
public static void main(String[] args) { // main method for testing data
int[] studentGrades = {85, 90, 75, 95, 80};
GradeAnalyzer analyzer = new GradeAnalyzer(); // creates new instance for the main class
double averageGrade = analyzer.calculateAverageGrade(studentGrades); // calling method
System.out.println("Average Grade: " + averageGrade);
}
}
GradeAnalyzer.main(null)
Average Grade: 85.0
Question 4: Math Class (Unit 2)
Situation: You are developing a scientific calculator application where users need to perform various mathematical operations.
(a) Discuss the purpose and utility of the Math class in Java programming. Provide examples of at least three methods provided by the Math class and explain their usage.
The math function provides different methods to perform math with and can help in calculating different thins, actions in games and other applications that require calculations
(b) Code:
You need to implement a method calculateSquareRoot
that takes a double
number as input and returns its square root using the Math class. Write the method signature and the method implementation. Include comments to explain your code.
public class ScientificCalculator {
public double calculateSquareRoot(double number) { // calculate square root method
double squareRoot = Math.sqrt(number); // math.sqrt used to get the squareroot of a number and double because square roots some times arent whole #
return squareRoot;
}
public static void main(String[] args) { // main method to test the data with
double number = 400;
ScientificCalculator calculator = new ScientificCalculator(); // creating an instance of the class
double squareRoot = calculator.calculateSquareRoot(number); // calling method for the calculator
System.out.println("Square root of " + number + " is: " + squareRoot);
}
}
ScientificCalculator.main(null)
Square root of 400.0 is: 20.0