Primitives Notebook
Captures Key Learnings Primitives Lesson
Primitives
- String Literals vs Literals, Exceptions,
- OOP, compiles into javac first and into byte than runs on JVM
-
Garbage Collection, Multithreading - multiple programs at once
-
primitives - predefined, cannot call methods: Boolean, Int(2-3bits), Double(64bits), Char, Float, Long
-
Naming conventions
-
lowercase at start, words after is capital ( toTheMoon )
-
Casting
- Manual casting vs automatic
-
Narrowing bigger -> smaller, Widening smaller -> bigger
-
Operator precedents
-
Left -> Right, PEMDAS
-
int x= 23;
- x *= 2;
- x %= 10;
-
System.out.println(x);
-
Scanners
- import java.util.Scaner;
- Scanner scan = Scanner(System.in);
public class Appointment
{
// returns the time interval of this Appointment
public TimeInterval getTime()
{ /* implementation not shown */ }
// returns true if the time interval of this Appointment
// overlaps with the time interval of other;
// otherwise, returns false
public boolean conflictsWith(Appointment other){
if(getTime().overlapsWith(other.getTime())){
return true
}
else{
return false
}
}
// There may be fields, constructors, and methods that are not shown.
}
public class DailySchedule
{
// contains Appointment objects, no two Appointments overlap
private ArrayList apptList;
public DailySchedule()
{ apptList = new ArrayList(); }
// 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){
int i = 0;
while (i < apptList.size()){
if (appt.conflictsWith((Appointment)(apptList.get(i)))){
apptList.remove(i);
}
else{
i++;
}
}
}
/* to be implemented in part (b) */ }
// 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);
}
public double purchasePrice()
{
return (1 + taxRate) * getListPrice();
}
public int compareCustomer(Customer other){
int nameCompare = getName().compareTo(other.getName());
if (nameCompare != 0){
return nameCompare;
}
else{
return getID() - other.getID();
}
}