Why Java?

  • Simpler syntax
  • Automatic Garbage collection
  • OOP paradigm is more flexible and efficient
  • Can run on any platform because of javac compiler
  • Multithreading, secure

What are Primitives?

Booleans, integers, doubles, char, float, long, etc.

  • Predefined
  • Lowercase values only
  • Can't call methods
  • Can't have null value
  • Size varies

Naming conventions

Lowercase, then 2nd word is capitalized Final key word will make it so you will not change a value later Casting: manual vs automatic Change value from one type to another, can narrow or widen Need to declare narrowing bc of overflow error

Operators

Addition: + Subtraction: = Division: / Modulus: % Multiplication: * ++ ex ++x: x = x+1 -- ex --x: x = x-1 And so on...

On the exam:

  • Combines basic and compound operators
  • Evaluate variable's value after expression
  • Compound operators can replace regular ones

User input

Import java.util.Scanner Use Scanner scan=new Scanner(System.in); Scanner = new data type

Homework

2a. Multiplying the tax rate + 1 gives the actual multiplier we want to use for the new price, since it includes the tax and existing price.

public interface Item
   {
      double purchasePrice();
   }
public abstract class TaxableItem implements Item
{
   private double taxRate;
   public abstract double getListPrice();
   public TaxableItem(double rate)
   { taxRate = rate; }

   // returns the price of the item including the tax
   public double purchasePrice()
   { return (1 + taxRate) * getListPrice();}
}

3a. Returning nameCompare if it's not 0, subtracting IDs otherwise

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)
   { 
      int nameCompare = getName().compareTo(other.getName());
      if (nameCompare != 0) {
         return nameCompare;
      }
      else {
         return getID() - other.getID();
      }
    }
   // There may be fields, constructors, and methods that are not shown.
}