- 자바 애플릿(Java Applet)으로 지진 종류 시각화하기
1. EarthquakeCityMap에서 printQuakes() 메서드 구현
System.out.println()을 사용하여 1회 이상의 지진이 발생한 각 국가와 해당 국가에서 감지된 지진 수를 나열하고, 바다에서 감지된 지진의 수를 출력하여라.
- EarthquakeCityMap.java
private void printQuakes() {
int totalWaterQuakes = quakeMarkers.size();
for (Marker country : countryMarkers) {
String countryName = country.getStringProperty("name");
int numQuakes = 0;
for (Marker marker : quakeMarkers) {
EarthquakeMarker eqMarker = (EarthquakeMarker)marker;
if (eqMarker.isOnLand()) {
if (countryName.equals(eqMarker.getStringProperty("country"))) {
numQuakes++;
}
}
}
if (numQuakes > 0) {
totalWaterQuakes -= numQuakes;
System.out.println(countryName + ": " + numQuakes);
}
}
System.out.println("OCEAN QUAKES: " + totalWaterQuakes);
}
2. 다음 클래스/인터페이스 간의 상속 관계를 설명하는 클래스 다이어그램 그리기
- Marker
- AbstractMarker
- SimplePointMarker
- EarthquakeMarker
- LandQuakeMarker
- OceanQuakeMarker
- CityMarker
[다이어그램] 클래스 다이어그램(Class Diagram) - Marker 인터페이스 상속/구현 관계
- 클래스 다이어그램(Class Diagram)이란? 클래스 내부 구성요소 및 클래스 간의 관계를 도식화하여 시스템의 특정 모듈이나 일부 및 전체를 구조화 → 클래스, 인터페이스 간의 관계를 알 수 있다. -
yermi.tistory.com
3. CityMarker 클래스에서 draw() 메서드에 대한 메서드 정의
- CityMarker.java
public void draw(PGraphics pg, float x, float y) {
// Save previous drawing style
pg.pushStyle();
pg.fill(255, 0, 0);
pg.triangle(x, y-5, x-5, y+5, x+5, y+5);
// Restore previous drawing style
pg.popStyle();
}
4. LandQuakeMarker 및 OceanQuakeMarker에서 drawEarthquake() 메서드 정의
- LandQuakeMarker.java
/** Draw the earthquake as an ellipse */
@Override
public void drawEarthquake(PGraphics pg, float x, float y) {
// TODO: Implement this method
float radius = getMagnitude() * 3;
pg.ellipse(x-getMagnitude(), y-getMagnitude(), radius, radius);
}
- OceanQuakeMarker.java
/** Draw the earthquake as a square */
@Override
public void drawEarthquake(PGraphics pg, float x, float y) {
// TODO: Implement this method
float size = getMagnitude() * 2.5f;
pg.rect(x, y, size, size);
}
5. EarthquakeMarker에서 colorDetermine() 메서드 정의
- EarthquakeMarker.java
private void colorDetermine(PGraphics pg) {
//TODO: Implement this method
if(getDepth() < THRESHOLD_INTERMEDIATE) {
pg.fill(255, 255, 0);
}
else if (THRESHOLD_DEEP <= getDepth()) {
pg.fill(255, 0, 0);
}
else {
pg.fill(0, 0, 255);
}
}
6. 24시간 이내에 발생한 모든 지진 마커에 X를 그리기
- EarthquakeMarker.java
// calls abstract method drawEarthquake and then checks age and draws X if needed
@Override
public void drawMarker(PGraphics pg, float x, float y) {
// save previous styling
pg.pushStyle();
// determine color of marker from depth
colorDetermine(pg);
// call abstract method implemented in child class to draw marker shape
drawEarthquake(pg, x, y);
// IMPLEMENT: add X over marker if within past day
String age = getStringProperty("age");
if ("Past Hour".equals(age) || "Past Day".equals(age)) {
pg.strokeWeight(2);
int buffer = 2;
pg.line(x-(radius+buffer), y-(radius+buffer), x+radius+buffer, y+radius+buffer);
pg.line(x-(radius+buffer), y+(radius+buffer), x+radius+buffer, y-(radius+buffer));
}
// reset to previous styling
pg.popStyle();
}