Monday, August 22, 2016

Introduction to Java programming

learning source

two types of error

compiler-time error: syntax error
run-time error: logic error


  1. unambiguous
  2. executable
  3. terminating


  1. accessor ( don’t change property)
  2. mutator


you ask object to do work. you don’t know how they do that
what, not how
comment //

documentation and api

stringObject.replace(char target, char replacement)
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

code mold

public class XX
  public static void main(String args[])

public interface

public void addFriend(Person friend)
public void unFriend(Person nonfriend)
public String getFriend()


Instance variables (non-static variables)
Class variables( Static variables)
Local variables


   A simulated car that consumes gas as it drives.
public class Car
    private double milesDriven;
    private double gasInTank;
    private double milesPerGallon;

       Constructs a car with a given fuel efficiency.
    public Car(double mpg)
        milesDriven = 0;
        gasInTank = 0;
        milesPerGallon = mpg;

      add gas to the Tank of car
      @param amount the amount of gas added into the Tank

    public void addGas(double amount)
        gasInTank = gasInTank + amount;

      Gets the current amount of gas in the tank of this car.
      @return the current gas level
    public double getGasInTank()
        return gasInTank;

      Drives this car by a given distance.
      @param distance the distance to drive
    public void drive(double distance)
        this.milesDriven = this.milesDriven + distance;
        double gasConsumed = distance / this.milesPerGallon;
        this.gasInTank = this.gasInTank - gasConsumed;

      Gets the current mileage of this car.
      @return the total number of miles driven
    public double getMilesDriven()
        return milesDriven;
public class Friend
  private String name;
  private String friends;
  public void addFriend(Person friend)
  public void unFriend(Person nonfriend)
  public String getFriend()

fundamental data type

doubles are fuzzy
cast: (int)(3.35)
grayscale: Y=0.2126R+0.7152G+0.0722*B
sunset effect: +25
final int MAX_RED=255;
System.out.printf(“%8.2f\n”, price); // 8 character, 2 decimal points, float type

import java.util.Scanner;

Scanner in= new Scanner(;
int age= in. nextInt();


Math.pow(a, n);


if ()
else if()
public static int SECONDS_PER_MINUTE=60;
final int SECONDS_PER_MINUTE=60;

they are never exactly the same

String.equal(); // not ==
final double EPSILON=1e-12;
Math.abs(x-y)<EPSILON; // not ==


for (int i=1;i<=6;i++){} // i is local variable
for (int value:values){} // values is an array


  1. break point
  2. single step
  3. inspect variables

ArrayList vs Array

ArrayList values= new ArrayList();
method: get(), set(),add()
double[] values=new double[10];
double[] values={32,54,67.5,29,35};

create a package

Basically, there are two ways:
  1. use package statement in the first line in the source file,then
    javac -d .
  2. use javac -d Destination_folder
To use the classes in a package:
import Destination_folder.*
import java.util.
​ Scanner, ArrayList, Arrays, Random


public interface Drawable
  void draw();  // automatically pubic, no implementation
public class house implements Drawable