Unit 1: Primitive Types

Question 1

Which of the following is a valid declaration of a variable of type int in Java?
a) int 123variable;
b) int variable123;
c) int variable#123;
d) int variable 123;

Answer: b) int variable123;

// Q1 Hack: Define variables according to Java naming conventions.
// For instance, is it snake_case, camelCase, or PascalCase? camelCase

int variable123 = 123;
System.out.println(variable123);

Question 2

What is the value of the following expression in Java: 5 / 2?
a) 2.5
b) 3
c) 2
d) 2.0

Answer: c) 2

// Q2.1 Hack: Show in code difference between integer and floating point division.
// Q2.2 Hack: Show in code the differnt number types in Java and how they behave.
// Behave means definition and assignment.

System.out.println("Integer division: 5 / 2 = " + 5/2);
System.out.println("Floating point division: 5 / 2 = " + 5.0/2);

int num1 = 3;
double num2 = 5.5;
System.out.printf("num1: %d, num2: %f\n", num1, num2);
Integer division: 5 / 2 = 2
Floating point division: 5 / 2 = 2.5
num1: 3, num2: 5.500000





java.io.PrintStream@49026f4b

Question 3

Which primitive type is used to represent a single character in Java?
a) char
b) String
c) int
d) byte

Answer: a) char

// Q3.1 Hack: Show in code all the the non-number Java primitive data types and how they behave.
// Q3.2 Hack: Show in code the String data type and how it behaves.

char character = 'c';
String str = "Hello, World";
boolean flag = true;

System.out.printf("character: %c, str: %s, flag: ", character, str);
System.out.println(flag);
character: c, str: Hello, World, flag: true

Question 4

Answer the following questions based on the code cell:

  • a) What kind of types are person1 and person2?
  • Answer: Person class
  • b) Do person1 and person3 point to the same value in memory?
  • Answer: Yes both are pointers to the actual data
  • c) Is the integer “number” stored in the heap or in the stack?
  • Answer: stored in stack, because it is main
  • d) Is the value that “person1” points to stored in the heap or in the stack?
  • Answer: the value of person1 is stored in heap
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); // This is required in Jupiter Notebook to run the main method.

Question 5

(a) Define primitive types and reference types in Java. The application is for banking, where you need to represent customer information.

(b) Add comments for primitive types and reference types. In terms of memory allocation, discuss concepts like instance, stack, and heap where it adds value.

(c) To assist in requirements, here are some required elements:

  • Create multiple customers from the public class Account.
  • Consider key class variables that a Bank may require: name, balance, accountNumber.
  • Create a two argument constructor using name and balance.
  • Consider in constructor how you will create a unique account number using static int lastAccountNumber
  • Define a method calculateInterest that works with getting and setting double balance using private static double interestRate.
public class Account {
    private String name;
    private double balance;
    private int accountNumber;

    private static int lastAccountNumber = 0;
    private static double interestRate = 0.04;

    public Account(String name, double balance) {
        this.name = name;
        this.balance = balance;
        accountNumber = lastAccountNumber++;
    }

    public double calculateInterest() {
        return balance *= interestRate;
    }
}