Unit 1 - Primitive Data Types
Notes
-
Why Java?
- More flexible OOP
- Run an any platform
- Garbage collection --> memory management, when a variable is no longer used it's
- Multi-threading: able to acces different
-
Primitives
- Predefined
- lovercase
- ex: boolean, int, double, char, long, short
- Non-Primitives
- Strings
- capital
- How to name
- letters, numbers, or underscores
-
final
makes variable constant - Casting --> changing data types
- int to a double
- Narrowing is going from bigger data to smaller
- Operators
- +, -, , /, %, ++, --, +=, -=, =, /=, %=
- Mod returns the remainder
- overflow errors --> left to right (PEMDAS)
- Incremet and decrement operators --> adds 1 and subract 1
- Coding Challenge
- Scanner
- import
Scanner
class
- import
public class Appointment
{
public boolean conflictsWith (int appointmentTimeInterval, int timeInterval)
{
if(appointmentTimeInterval < timeInterval)
{
return true;
}
else
{
return false;
}
}
}
Appointment appointment = new Appointment();
boolean conflict = appointment.conflictsWith(10, 2);
System.out.printf("Appointment is at 10\nTime interval is 2\n");
System.out.printf("Is there a conflict with appointment?: %b\n", conflict);
1)
b.
public class Appointment
{
public void clearConflicts(Appointment appointment)
{
int loop = 0;
while (loop < appointmentList.size())
if (((Appointment)appointmentList.get(loop)).conflictsWith(appointment))
{
appointmentList.remove(loop);
}
else
{
loop++;
}
}
}
1)
c.
public class Appointment
{
public boolean addAppointment(Appointment appointment, boolean emergency)
{
if (emergency)
{
clearConflicts(appointment);
appointmentList.add(appointment);
return true;
}
for (int loop = 0; loop < appointmentList.size(); loop++)
if (((Appointment)appointmentList.get(i)).conflictsWith(appointment))
{
return false;
}
appointmentList.add(appointment);
return true;
}
}
2)
a.
double initialPrice = 6.50;
public static double purchasePrice (double tax)
{
tax *= 1.1;
return tax;
}
System.out.printf("Before tax: $%.2f\n", initialPrice);
double finalPrice = purchasePrice(initialPrice);
System.out.printf("After tax: $%.2f\n", finalPrice);
3)
a.
public class Customer
{
private String _name;
private int _ID;
public Customer(String name, int IDNum)
{
_name = name;
_ID = IDNum;
}
public String getName()
{
return _name;
}
public int getID()
{
return _ID;
}
public int compareCustomer (Customer compare)
{
int compareIDs = _ID - compare._ID;
return compareIDs;
}
}
// main method
Customer customer1 = new Customer("Smith", 1001);
Customer customer2 = new Customer("Anderson", 1002);
Customer customer3 = new Customer("Smith", 1003);
int compareCustomer1 = customer1.compareCustomer(customer1);
int compareCustomer2 = customer1.compareCustomer(customer2);
int compareCustomer3 = customer1.compareCustomer(customer3);
System.out.printf("Customer 1 compared to Customer 1: %d\n", compareCustomer1);
System.out.printf("Customer 1 compared to Customer 2: %d\n", compareCustomer2);
System.out.printf("Customer 1 compared to Customer 3: %d\n", compareCustomer3);