learning source
two types of error
compiler-time error: syntax error
run-time error: logic error
run-time error: logic error
algorithms
- unambiguous
- executable
- terminating
methods
- accessor ( don’t change property)
- mutator
object
you ask object to do work. you don’t know how they do that
what, not how
what, not how
comment //
documentation and api
stringObject.replace(char target, char replacement)
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D;
code mold
public class XX
{
public static void main(String args[])
{
System.out.println();
}
}
public interface
public void addFriend(Person friend)
public void unFriend(Person nonfriend)
public String getFriend()
public void unFriend(Person nonfriend)
public String getFriend()
Variables
Instance variables (non-static variables)
Class variables( Static variables)
Local variables
example
/**
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
overflow
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(System.in);
int age= in. nextInt();
java.lang.Math
Math.pow(a, n);
Math.sqrt(100);
Math.max(a,b);
decision
if ()
{}
else if()
{}
else
{}
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 ==
loop
for (int i=1;i<=6;i++){} // i is local variable
for (int value:values){} // values is an array
Debugger
- break point
- single step
- 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:
- use
package
statement in the first line in the source file,thenjavac -d . file_name.java
- use
javac -d Destination_folder file_name.java
To use the classes in a package:
import Destination_folder.*
import java.util.
Scanner, ArrayList, Arrays, Random
Interface
public interface Drawable
{
void draw(); // automatically pubic, no implementation
}
public class house implements Drawable