• Writing Classes
  • Wrapper Classes
  • Value & Reference Types

    Part 1

    public class Person {
        String name;
        int age;
        int height;
        String job;
    
        public Person(String name, int age, int height, String job) {
            this.name = name;
            this.age = age;
            this.height = height;
            this.job = job;
        }
    }
    
    public static void main(String[] args) {
        Person person1 = new Person("Carl", 25, 165, "Construction Worker");
        Person person2 = new Person("Adam", 29, 160, "Truck Driver");
        Person person3 = person1;
        int number = 16;
        System.out.println(number);
    }
    main(null);
    
    16
    

    Answer the following questions based on the code above:

    1. What kind of types are person1 and person2?

      Answer: They are reference types, because they are instantiations of classes.

    2. Do person1 and person3 point to the same value in memory?

      Answer: Yes, because they are referencing the same value stored in the heap memory.

    3. Is the integer “number” stored in the heap or in the stack?

      Answer: The value type is stored in the stack.

    4. Is the value that “person1” points to stored in the heap or in the stack?

      Answer: The value is stored in stack, but the reference is stored in the heap.

    Part 2

    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.

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

    // A: Primitive types are stored in the stack. Reference types are stored in the heap. 
    
    // B: Primitive types point to one value only. Multiple reference types can reference one value.AbstractCollection
    
    public class DifferentTypes {
        private int num = 0; // Primitive value type
        public DifferentTypes refer = new DifferentTypes(); // Reference type
    }
    
    // C: Banking application with Customer class that has a calculateInterest() method.
    
    public class Customer {
    
        private double amount; // Primitive value type
    
        public Customer(double _amount) {
            amount = _amount;
        }
    
        double calculateInterest(double interest, Customer customer /* Reference type */) {
            return customer.amount * interest / 100; // Referencing the primitive value through the reference type
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Customer customer = new Customer(100);
            double interest = customer.calculateInterest(9, customer);
            System.out.println(interest);
        }
    }
    Main.main(null);
    
    // Lesson learnt: Don't put access modifiers on methods unncessarily, or in this case, at all.
    
    9.0
    

    Writing Classes

    // A: Below is a class with comments defining the three key parts in order of writing any class
    
    public class PlainDefaultClass {
    
        // Variables are the first key component
        private int numberValue;
        private double decimalValue;
        private String message;
        private boolean condition;
    
        // Constructors are the second key component
        public PlainDefaultClass(int numberValue, double decimalValue, String message, boolean condition) {
            this.numberValue = numberValue;
            this.decimalValue = decimalValue;
            this.message = message;
            this.condition = condition;
        }
    
        // Methods are the third key component
        int getNumValue() {
            return this.numberValue;
        }
    
        double getDecimalValue() {
            return this.decimalValue;
        }
    
        String getMessage() {
            return this.message;
        }
    
        boolean getCondition() {
            return this.condition;
        }
    }
    
    // B: Below is the BankAccount class with all the instructions completed
    
    public class BankAccount {
    
        // Variables
        private String accountHolder;
        private double balance;
        
        // Constructor
        public BankAccount(String accountHolder, double balance) {
            this.accountHolder = accountHolder;
            this.balance = balance;
        }
    
        // Methods
        String setAccountHolder(String name) {
            this.accountHolder = name;
            return this.accountHolder;
        }
    
        double deposit(double amount) {
            this.balance += amount;
            return this.balance;
        }
    
        double withdraw(double amount) {
            if (amount <= this.balance) {
                this.balance -= amount;
                return this.balance;
            } else {
                System.out.println("You do not have enough to withdraw");
                return this.balance;
            }
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            BankAccount account = new BankAccount("None", 100);
            System.out.println(account.setAccountHolder("Paaras"));
            System.out.println(account.deposit(25));
            System.out.println(account.withdraw(50));
            System.out.println(account.withdraw(175));
        }
    }
    
    Main.main(null);
    
    // Lessons learnt: First, always make sure all variables everywhere have an identifier. Second, make sure you don't forget to actually write the main method.
    
    Paaras
    125.0
    75.0
    You do not have enough to withdraw
    75.0
    

    Wrapper Classes

    Popcorn Hack: Fill out the below.

    • byteByte
    • shortShort
    • intInteger
    • longLong
    • floatFloat
    • doubleDouble
    • charCharacter
    • booleanBoolean

    A. Wrapper classes are used to make reference types out of primitive types. This can be useful if you want multiple references stored in the heap so they can point to the same value in the stack.

    // B: The Temperature class that represents a temperature in Celsius
    
    public class Temperature {
        private Double temp;
    
        public Temperature(double temp) {
            this.temp = temp; // Autoboxing
        }
    
        double getTemperature() {
            return this.temp;
        }
    
        void setTemperature(double value) {
            this.temp = value; // Autoboxing
        }
    
        double toFahrenheit() {
            double fahrenheit = (this.temp * 1.8) + 32; 
            return fahrenheit;
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Temperature temp = new Temperature(54);
            System.out.println(temp.getTemperature());
            temp.setTemperature(108);
            System.out.println(temp.getTemperature());
            System.out.println(temp.toFahrenheit());
        }
    }
    Main.main(null);
    
    54.0
    108.0
    226.4