/* 클래스 */
public class Animal {
...
}
/* 객체와 인스턴스 */
public class Main {
public static void main(String[] args) {
Animal cat, dog; // '객체'
// 인스턴스화
cat = new Animal(); // cat은 Animal 클래스의 '인스턴스'(객체를 메모리에 할당)
dog = new Animal(); // dog은 Animal 클래스의 '인스턴스'(객체를 메모리에 할당)
}
}
https://gmlwjd9405.github.io/2018/09/17/class-object-instance.html
```java
package timetest;
import java.util.Scanner;
public class timetest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("시/분/초 입력: ");
int hour = sc.nextInt();
int min = sc.nextInt();
int sec = sc.nextInt();
time first_time = new time(10, 10, 10);
time second_time = new time(hour, min, sec);
time add_time = first_time.add(second_time); // add 메소드 호출
System.out.println("두 시간의 합은");
add_time.print_time( ); // print_time 메소드 호출
time sub_time = first_time.sub(second_time); // sub 메소드 호출
System.out.println("두 시간의 차는");
sub_time.print_time( ); // print_time 메소드 호출
int result = first_time.compare(second_time); // compare 메소드 호출
System.out.println(result); // compare 결과 출력
}
public static class time {
static int day = 0;
int hour;
int min;
int sec;
int max = 24 * 60 * 60;
public time() {
this.hour = 0;
this.min = 0;
this.sec = 0;
}
public time(int hour, int min, int sec) {
this.hour = hour;
this.min = min;
this.sec = sec;
}
public int compare(time time) {
int a = convert(this.hour, this.min, this.sec);
int b = convert(time.hour, time.min, time.sec);
if (a > b) {
return 1;
}
else if (a == b) {
return 0;
}
else {
return -1;
}
}
public int convert(int hour, int min, int sec) {
int result;
result = hour * 3600 + min * 60 + sec;
return result;
}
public time add(time time) {
int addingTime = convert(time.hour, time.min, time.sec);
int curTime = convert(this.hour, this.min, this.sec);
int result = addingTime + curTime;
if (result >= time.max) {
day = 1;
result = result - time.max;
int cur_hour = (int) Math.ceil(result / 3600);
int remain = result % 3600;
time time_add = new time(cur_hour, (int) Math.ceil(remain / 60), remain % 60);
return time_add;
}
else {
day = 0;
int cur_hour = (int) Math.ceil(result / 3600);
int remain = result % 3600;
hour = cur_hour;
min = (int) Math.ceil(remain / 60);
sec = remain % 60;
time time_add = new time(cur_hour, (int) Math.ceil(remain / 60), remain % 60);
return time_add;
}
}
public time sub(time time) {
int addingTime = convert(time.hour, time.min, time.sec);
int curTime = convert(this.hour, this.min, this.sec);
int result = curTime - addingTime;
if (result < 0) {
day = -1;
result = result + time.max;
int cur_hour = (int) Math.ceil(result / 3600);
int remain = result % 3600;
hour = cur_hour;
min = (int) Math.ceil(remain / 60);
sec = remain % 60;
}
else {
day = 0;
int cur_hour = (int) Math.ceil(result / 3600);
int remain = result % 3600;
hour = cur_hour;
min = (int) Math.ceil(remain / 60);
sec = remain % 60;
}
time time_sub = new time(hour, min, sec);
return time_sub;
}
public void print_time() {
if (this.day >= 1) {
System.out.println("후일 " + this.hour + "시 " + this.min + "분 " + this.sec + "초 " + "입니다");
}
else if (this.day == 0) {
System.out.println("당일 " + this.hour + "시 " + this.min + "분 " + this.sec + "초 " + "입니다");
}
else {
System.out.println("전일 " + this.hour + "시 " + this.min + "분 " + this.sec + "초 " + "입니다");
}
}
}
}
클래스 만들어봄
그 this로 하면 값 계속 변하는거 조심
class안에 class 2개 만들수 있음
package secondPractice;
import java.util.Scanner;
public class secondPractice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String t = sc.nextLine();
Tr P = new Select(s,t);
Tr Q = ((Select)P).check();
System.out.println(((Select)P).connect(Q));
}
public static class Tr {
public String s;
public String t;
public Tr() {}
public Tr(String s, String t) {
this.s = s;
this.t = t;
}
}
public static class Strcmp extends Tr {
public Strcmp(String s, String t) {
super(s, t);
}
public int cmp() {
return this.s.compareTo(this.t);
}
}
public static class Numcmp extends Tr {
double a, b; int result;
public Numcmp(String s, String t) {
super(s, t);
}
public int cmp() {
a = Double.parseDouble(this.s);
b = Double.parseDouble(this.t);
if (a > b) result = 1;
else if (a < b) result = -1;
else result = 0;
return result;
}
}
public static class Select extends Tr {
public Select(String s, String t) {
super(s, t);
}
public Tr check () {
char neg_sign = '-';
char s_first_val = this.s.charAt(0);
char t_first_val = this.t.charAt(0);
int i = 0;
int j = 0;
if (s_first_val == neg_sign) {
i = 1;
}
if (t_first_val == neg_sign) {
j = 1;
}
for (int k = i; k < this.s.length(); k++) {
char val = this.s.charAt(i);
char point = '.';
if ((val == point) | (Character.isDigit(val))) {
continue;
}
else {
return new Strcmp(this.s, this.t);
}
}
for (int x = j; x < this.t.length(); x++) {
char val = this.t.charAt(x);
char point = '.';
if ((val == point) | (Character.isDigit(val))) {
continue;
}
else {
return new Strcmp(this.s, this.t);
}
}
return new Numcmp(this.s, this.t);
}
public int connect(Tr tr) {
if (tr instanceof Numcmp) {
Numcmp num = new Numcmp(s, t);
return num.cmp();
} else {
Strcmp str = new Strcmp(s, t);
return str.cmp();
}
}
}
}
자바는 너무 바보다.
객체지향은 누가 만들었을까.
자바를 하기 싫다.
근데 해야된다.
이걸 어떡하냐.