Why Java?

Garbage Collecting Multi Threading

Primitives vs Non-Primitives

Naming Conventions

They all use lower case

Casting

Method to convert primitives into other primitives

Operators

    • addition
    • subtraction
    • multiplication
  • / division
  • % modulo
  • ++ increment
  • += add then assign to variable
  • -= subtract then assign to variable
  • more

They have orders of operations for each operator

Scanners

import java.util.Scanner Create Scanner object set what the scanner will read with data type

Homework 2006 Question 1, 2a, and 3a

Question 1

  1. An appointment scheduling system is represented by the following three classes: TimeInterval, Appointment, and DailySchedule. In this question, you will implement one method in the Appointment class and two methods in the DailySchedule class.

A TimeInterval object represents a period of time. The TimeInterval class provides a method to determine if another time interval overlaps with the time interval represented by the current TimeInterval object. An Appointment object contains a time interval for the appointment and a method that determines if there is a time conflict between the current appointment and another appointment. The declarations of the TimeInterval and Appointment classes are shown below.

public class TimeInterval { // returns true if interval overlaps with this TimeInterval; // otherwise, returns false public boolean overlapsWith(TimeInterval interval) { / implementation not shown / } // There may be fields, constructors, and methods that are not shown. } 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) { / to be implemented in part (a) / } // There may be fields, constructors, and methods that are not shown. }

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

(b) A DailySchedule object contains a list of nonoverlapping Appointment objects. The DailySchedule class contains methods to clear all appointments that conflict with a given appointment and to add an appointment to the schedule.

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) { / 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) { / to be implemented in part (c) / } // There may be fields, constructors, and methods that are not shown. }

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)

(c) Write the DailySchedule method addAppt. The parameters to method addAppt are an appointment and a boolean value that indicates whether the appointment to be added is an emergency. If the appointment is an emergency, the schedule is cleared of all appointments that have a time conflict with the given appointment and the appointment is added to the schedule. If the appointment is not an emergency, the schedule is checked for any conflicting appointments. If there are no conflicting appointments, the given appointment is added to the schedule. Method addAppt returns true if the appointment was added to the schedule; otherwise, it returns false. 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)

Question 1a

public boolean conflictsWith(Appointment other){
    return getTime().overlapsWith(other.getTime());
}

Question 1b

public void clearConflicts(Appointment appt){
    for (int i = 0; i < aptList.size(); i++){
        if (appt.conflictsWith((Appointment)apptList.get(i))){
            aptList.remove(i);
        }
    }
}

Question 1c

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);
}

Question 2a

(a) 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()

public double purchasePrice(){
    return (1 + taxRate) * getListPrice();
}

Question 3a

  1. Consider the following incomplete class that stores information about a customer, which includes a name and unique ID (a positive integer). To facilitate sorting, customers are ordered alphabetically by name. If two or more customers have the same name, they are further ordered by ID number. A particular customer is "greater than" another customer if that particular customer appears later in the ordering than the other customer. public class Customer { // constructs a Customer with given name and ID number public Customer(String name, int idNum) { / implementation not shown / } // returns the customer's name public String getName() { / implementation not shown / } // returns the customer's id public int getID() { / implementation not shown / } // 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) { / to be implemented in part (a) / } // There may be fields, constructors, and methods that are not shown. } (a) Write the Customer method compareCustomer, which compares this customer to a given customer, other. Customers are ordered alphabetically by name, using the compareTo method of the String class. If the names of the two customers are the same, then the customers are ordered by ID number. Method compareCustomer should return a positive integer if this customer is greater than other, a negative integer if this customer is less than other, and 0 if they are the same. For example, suppose we have the following Customer objects. Customer c1 = new Customer("Smith", 1001); Customer c2 = new Customer("Anderson", 1002); Customer c3 = new Customer("Smith", 1003); The following table shows the result of several calls to compareCustomer. Method Call Result c1.compareCustomer(c1) 0 c1.compareCustomer(c2) a positive integer c1.compareCustomer(c3) a negative integer

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)

public int compareCustomer(Customer other){
    int comparedNames = getName().compareTo(other.getName());

    if (comparedNames != 0){
        return comparedNames;
    }

    else{
        return getID() - other.getID();
    }
}