学校Java Week7
Week7
W7L1
Java Virtual Machine (JVM)
- JDK (Development Kit)
- JRE (Runtime Environment)
- JDB (Debugger)
.java -> [javac compiler] -> .class -> [JVM] -> Underlying OS (Operating System)
Stack vs. Heap (栈 堆)
-
the stack is used to store the order of method execution and local variables
-
primitive type variables is stored on the stack
-
the heap memory stores the objects and it uses dynamic memory allocation and deallocation.
-
the stack holds a pointer to the object on the heap. When setting a reference type variable equal to another reference type variable, a copy of only the pointer is made.
Object
-
primitive data types that are built in Java
-
Non-primitive data type - everything else is reference type (Objects)
-
Primitives don’t have methods
-
Objects have methods
-
objects have states and behaviour
-
Java is an Object Oriented language
-
Programs are for doing stuff
-
Methods do stuff
-
Variables hold or represent data
-
Objects are groups of data and processing
String Methods
- myString.length();
- myString.toLowerCase();
- myString.toUpperCase();
- myString(int start, int end);
- myString.equals(anotherString);
- myString.split(String delimiter);
- ......
Class
no main methods
Constructors
special methods in Class
Create
public class Person() {
String name;
String gender;
// constructors have the same name of class
public Person() {
// this.Reference
this.gender = "male";
}
public Person(String name) {
this.name = name; /* 'name' is a local variable
Declare an instance variable
*/
}
}
Public and Private - data hiding
- Public or private, both work within a class
- can create public void, or private void methods
- With multiple classes, there is a difference
- If it is PRIVATE, can only be accessed by that class
- Useful for internal methods
- something that only needs to be done within the objects
- If it is PUBLIC, then it can be accessed by other classes
- Useful for object methods that need to be accessed
use getter methods to get data
static
Static means that there is only one instance for the entire class
the new declaration overwrites the data before
Variable pass
value and reference
objects are pointers
W7L2
public class Clock {
private int hours;
private int minutes;
// CW1 #7.1
// Creates a clock whose initial time is h hours and m minutes.
public Clock(int h, int m) {
this.hours = h;
this.minutes = m;
}
// CW1 #7.2
// Creates a clock whose initial time is specified as a string, using the format HH:MM.
public Clock(String s) {
String[] t = s.split(":");
this.hours = Integer.parseInt(t[0]);
this.minutes = Integer.parseInt(t[1]);
}
// CW1 #7.3
// Returns a string representation of this clock, using the format HH:MM.
public String toString() {
if (this.hours<10) {
if (this.minutes<10) {
return "0" + this.hours + ":0" + this.minutes;
} else {
return "0" + this.hours + ":" + this.minutes;
}
} else {
if (this.minutes<10) {
return this.hours + ":0" + this.minutes;
} else {
return this.hours + ":" + this.minutes;
}
}
}
// CW1 #7.4
// Is the time on this clock earlier than the time on that one?
public boolean isEarlierThan(Clock that) {
if (this.hours < that.hours) {
return true;
} else if (this.hours == that.hours) {
if (this.minutes < that.minutes) {
return true;
} else {
return false;
}
} else {
return false;
}
}
// CW1 #7.5
// Adds 1 minute to the time on this clock.
public void tick() {
this.minutes += 1;
if (this.minutes == 60) {
this.minutes = 0;
if (this.hours == 23) {
this.hours = 0;
} else {
this.hours += 1;
}
}
}
// CW1 #7.6
// Adds delta minutes to the time on this clock.
public void tock(int delta) {
int addhours = 0;
while (delta < 0 || delta >= 60) {
delta -= 60;
addhours ++;
}
if ((this.minutes + delta) >= 60) {
delta -= 60;
addhours++;
}
this.minutes += delta;
while ((this.hours + addhours) >= 24) {
addhours -= 24;
}
this.hours += addhours;
}
// Test client
}
public class ColorHSB {
int hue;
int saturation;
int brightness;
public ColorHSB(int h, int s, int b) {
this.hue = h;
this.saturation = s;
this.brightness = b;
}
public String toString() {
return "(" + hue + ", " + saturation + ", " + brightness + ")";
}
public boolean isGrayscale() {
if (saturation==0 || brightness==0) {
return true;
}
return false;
}
// min{ (h1−h2)^2, (360−|h1−h2|)^2 } + (s1−s2)^2 + (b1−b2)^2
public int squareDist(ColorHSB that) {
double min = 0;
double distance = 0;
double se1 = Math.pow((this.hue-that.hue), 2);
double se2 = Math.pow(360 - Math.abs(this.hue-that.hue), 2);
if (se1 < se2) {
min = se1;
} else {
min = se2;
}
distance = min + Math.pow((this.saturation-that.saturation), 2) + Math.pow((this.brightness-that.brightness), 2);
int dis = (int)distance;
return dis;
}
}
ex
public class W7L2_ex {
public static void main(String[] args) {
ColorHSB green = new ColorHSB(100, 100, 50);
ColorHSB orange = new ColorHSB(25, 100, 100);
ColorHSB gray = new ColorHSB(0, 0, 50);
System.out.println(green);
System.out.println(orange.isGrayscale());
System.out.println(gray.isGrayscale());
int distGreenOrange = green.squareDist(orange);
System.out.println(distGreenOrange);
System.out.println(gray.squareDist(orange));
}
}
CW1
public class W7_CW1 {
public static void main(String[] args) {
Clock clock1 = new Clock(1, 0);
Clock clock2 = new Clock("02:30");
Clock clock3 = new Clock("02:30");
Clock clock4 = new Clock("02:31");
Clock clock5 = new Clock("02:29");
Clock clock6 = new Clock("01:30");
Clock clock7 = new Clock("03:30");
Clock clock8 = new Clock("01:31");
Clock clock9 = new Clock("01:29");
Clock clock10 = new Clock("03:31");
Clock clock11 = new Clock("03:29");
Clock clock12 = new Clock("23:59");
Clock clock13 = new Clock("22:59");
Clock clock14 = new Clock("00:00");
Clock clock15 = new Clock("23:00");
Clock clock16 = new Clock("23:59");
Clock clock17 = new Clock("22:59");
Clock clock18 = new Clock("00:00");
Clock clock19 = new Clock("23:59");
Clock clock20 = new Clock("00:00");
Clock clock21 = new Clock("00:00");
System.out.println(clock1);
System.out.println(clock2);
System.out.println(clock1.isEarlierThan(clock2));
// System.out.println(clock2.isEarlierThan(clock3));
// System.out.println(clock2.isEarlierThan(clock4));
// System.out.println(clock2.isEarlierThan(clock5));
// System.out.println();
// System.out.println(clock2.isEarlierThan(clock6));
// System.out.println(clock2.isEarlierThan(clock7));
// System.out.println();
// System.out.println(clock2.isEarlierThan(clock8));
// System.out.println(clock2.isEarlierThan(clock9));
// System.out.println();
// System.out.println(clock2.isEarlierThan(clock10));
// System.out.println(clock2.isEarlierThan(clock11));
clock1.tick();
// clock12.tick();
// clock13.tick();
// clock14.tick();
// clock15.tick();
clock2.tock(100);
clock14.tock(100);
clock18.tock(1540);
clock13.tock(100);
clock17.tock(1540);
clock12.tock(100);
clock16.tock(1540);
clock19.tock(1);
clock20.tock(60);
clock21.tock(260);
System.out.println(clock1);
// System.out.println(clock12);
// System.out.println(clock13);
// System.out.println(clock14);
// System.out.println(clock15);
System.out.println(clock2);
System.out.println(clock14);
System.out.println(clock18);
System.out.println();
System.out.println(clock13);
System.out.println(clock17);
System.out.println();
System.out.println(clock12);
System.out.println(clock16);
System.out.println();
System.out.println(clock19);
System.out.println(clock20);
System.out.println(clock21);
}
}