Primitives vs. Non-Primitives -

  • primitives -
    • predefined
    • lowercase
    • "Primitives"
    • can't call methods
    • has a value
    • has different sizes according to type
  • non-primitives -
    • defined by you
    • uppercase
    • "Reference Types"
    • can call methods
    • can be null
    • all the same size

Primitives Review -

  • boolean
    • true/false only
    • 1 bit
  • int
    • integer values
    • 2-3 bits
  • double
    • decimal values
    • 64 bits
  • additional -
    • char
    • float
    • long
  • more info - casting
    • widening is when you're going from a smaller data type to a bigger one
    • narrowing is going from a bigger data type to a smaller one

Operators Review -

  • (+) addition
  • (-) subtraction
  • (/) division
  • (%) finds the remainder
  • (*) multiplication
  • (++) adds 1
  • (--) subtracts 1
  • (++x) adds x
  • (--x) subtracts x
  • compound operators - do a math operation and then assign it back to the variable
    • (+=) adds
    • (-=) subtracts
    • (*=) multiplies
    • (/=) divides
    • (%=) finds the remainder
public class CompOpsDemo {
    public static void main(String[] args) {
        int x = 6;
        x += 7;
        x -= 3;
        x *= 10;
        x /= 5;
        x %= 3;
        System.out.println("x = " + x);
    }
}
CompOpsDemo.main(null);
x = 2
public class CompOpsDemo {
    public static void main(String[] args) {
        int x = 2;
        int y = 7;
        x++;
        y--;
        System.out.println("x = " + x);
        System.out.println("y = " + y);
    }
}
CompOpsDemo.main(null);
x = 3
y = 2

Question and Answer -

  1. the value of z = 2
  2. a and d
  3. val = 6
  4. i = 96
  5. j = 458
public class CompOpsDemo {
    public static void main(String[] args) {
        int i = 5;
        int p = 27;
        for(int l = 23; l < p; l++) {
            i *= (l-22);
        }
        System.out.println("i = " + i);
    }
}
CompOpsDemo.main(null);
i = 120

HW - 2006 FRQ Q1

// part a:
/*
 * Write the Appointment method conflictsWith. 
 * If the time interval of the current appointment 
 * overlaps with the time interval of the appointment other, 
 * method conflictsWith should return true, otherwise, 
 * it should return false. 
 * Complete method conflictsWith below.
 * returns true if the time interval of this Appointment
 * overlaps with the time interval of other;
 * otherwise, returns false
 */
public boolean conflictsWith(Appointment other) {
    return getTime().overlapsWith(other.getTime());
} 

// part b:
/*
 Write the DailySchedule method clearConflicts. Method clearConflicts removes all
 appointments that conflict with the given appointment.
 In writing method clearConflicts, you may assume that conflictsWith works as specified,
 regardless of what you wrote in part (a).
 Complete method clearConflicts below.
 removes all appointments that overlap the given Appointment
 postcondition: all appointments that have a time conflict with
 appt have been removed from this DailySchedule  
 */
public void clearConflicts(Appointment appt) {
    for (int i = apptList.size()-1; i >= 0; i--) {
        if (appt.conflictsWith((Appointment)apptList.get(i))) {
            apptList.remove(i); 
        }
    }
}

// part c:
/*
 In writing method addAppt, you may assume that conflictsWith and clearConflicts work
 as specified, regardless of what you wrote in parts (a) and (b). 
 Complete method addAppt below.
 if emergency is true, clears any overlapping appointments and adds
 appt to this DailySchedule; otherwise, if there are no conflicting
 appointments, adds appt to this DailySchedule;
 returns true if the appointment was added;
 otherwise, returns false
 */
public boolean addAppt(Appointment appt, boolean emergency) {
    if (emergency) {
        clearConflicts(appt);
    }
    else {
        for (int i = 0; i < apptList.size(); i++) {
            if (appt.conflictsWith((Appointment)apptList.get(i))) {
                return false;
            }
        }
    }
    return apptList.add(appt); 
}
/*
 Write the TaxableItem method purchasePrice. The purchase price of a TaxableItem is its
 list price plus the tax on the item. The tax is computed by multiplying the list price by the tax rate. For
 example, if the tax rate is 0.10 (representing 10%), the purchase price of an item with a list price of $6.50
 would be $7.15.
 Complete method purchasePrice below.
 returns the price of the item including the tax 
 */
public double purchasePrice() {
    return (1 + taxRate) * getListPrice();
}
/*
 Complete method compareCustomer below.
 returns 0 when this customer is equal to other;
 a positive integer when this customer is greater than other;
 a negative integer when this customer is less than other 
 */
public int compareCustomer(Customer other) {
    int nameCompare = getName().compareTo(other.getName());
    if (nameCompare != 0) {
        return nameCompare;
    }
    else {
        return getID() - other.getID();
    }
}

Grade Calculator

import java.util.Scanner;  // Import the Scanner class
int separate = 0;
double g = 96; // Current Class Grade
double percentFinal = 30;
double finalScore = 0;
int t = 80; // test percent of the grade
int p = 300; // point in the test category
int f = 100; // points that the final is worth
double a = 92; // percent of points scored within the test category
double w = 90; // wanted grade
Scanner finalQ = new Scanner(System.in);  // Create a Scanner object
System.out.println("Is the final in its own category? \n 1 - True \n 2 - False");
separate = finalQ.nextInt();
if(separate == 1){
    System.out.println("What is your current grade?");
    g = finalQ.nextDouble();
    System.out.println("How much percent of your grade is the final?");
    percentFinal = finalQ.nextDouble();
    System.out.println("What is your desired grade?");
    w = finalQ.nextDouble();
    finalScore = (w -g * (100.0- percentFinal)/100) / (percentFinal/100);
    System.out.print("You need to get at least a " + String.format("%.2f", (finalScore/f)*100) + "% on you final to get a " + w);
}
else if(separate == 2){
    System.out.print("What is your current grade? ");
    g = finalQ.nextDouble();
    System.out.println(g);
    System.out.print("How much is your test category worth? ");
    t = finalQ.nextInt();
    System.out.println(t);
    System.out.print("How many point are in the test category? ");
    p = finalQ.nextInt();
    System.out.println(p);
    System.out.print("How many point is the final? ");
    f = finalQ.nextInt();
    System.out.println(f);
    System.out.print("What is your grade in the test category? ");
    a = finalQ.nextDouble();
    System.out.println(a);
    System.out.print("What is your desired grade? ");
    w = finalQ.nextDouble();
    System.out.println(w);
    finalScore = ((0.01*a*f*t)-(f*g)+(f*w)-(g*p)+(p*w))/t;
    System.out.print("You need to get at least a " + String.format("%.2f", (finalScore/f)*100) + "% on you final to get a " + w);
}
else{
    System.out.print("Unexpected choice, try again.");
}
Is the final in its own category? 
 1 - True 
 2 - False
What is your current grade?
How much percent of your grade is the final?
What is your desired grade?
You need to get at least a 87.20% on you final to get a 88.0