728x90
1.
일단 입력받는 것 자체가 오랜만이라 제대로 입력받는지 확인을 해보자.
제대로 작동한다..!! 아직은 안헤매도 된당 ㅎㅎ
package org.opentutorials.javatutorials.numberstring;
import java.util.Scanner;
class Date{
int Y, M, D;
public Date(int Y, int M, int D) {
this.Y=Y;
this.M=M;
this.D=D;
}
public void print1() {
System.out.println(Y+","+M+","+D);
}
public void print2() {
String[] Month = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "Octorber", "November", "December"};
System.out.println(Month[--M]+" "+D+", "+Y);
}
}
public class Number {
public static void main(String[] args) {
int Y, M, D;
Scanner sc = new Scanner(System.in);
System.out.print("연도, 월, 일을 입력하시오. : ");
Y = sc.nextInt();
M = sc.nextInt();
D = sc.nextInt();
Date today= new Date(Y, M, D);
today.print1();
today.print2();
}
}
print1메소드는 그냥 입력받은 값을 순서에 맞게 출력만 하면된다. 쉽게 성공!
print2메소드는 입력받은 월에 맞춰서 영어를 출력해야 한다. 쉽게 배열을 사용해서 January부터 December까지 입력해준다. 그리고 입력받은 월을 배열주소로 활용해서 순서에 맞게 출력한다. (월은 1로 시작하는 것과 달리 배열 주소는 0부터이기 때문에 --M을 하여 출력하게 해야한다. ) 그럼 성공!
2.
일단 클래스 상속을 사용해야 하는 거 같다. https://sh1256.tistory.com/8
상속(개념, 클래스 상속, 부모 생성자 , 다형성, 추상 클래스)
1. 상속, 클래스 상속 자바에는 부모클래스와 자식클래스가 존재한다. 자식 클래스는 부모 클래스를 통해 그 부모의 멤버를 상속받아 쓸 수 있다. 예제를 보면서 클래스 상속을 사용해 보자. packa
sh1256.tistory.com
복습 겸 다시 보고 오자! 자식클래스 NamedCircle 안에 부모생성자 super()을 이용해서 원의 반지름을 입력받은 뒤 main 함수에서 부모 클래스 Circle의 GetRadius()함수를 이용해 출력하면 된다! 성공!
package org.opentutorials.javatutorials.numberstring;
class Circle {
private int radius;
public Circle(int radius) {
this.radius = radius;
}
public int getRadius() {
return radius;
}
}
class NamedCircle extends Circle{
public NamedCircle(int r) {
super(r);
}
}
public class Number {
public static void main(String[] args) {
NamedCircle W = new NamedCircle(5);
System.out.println("Waffle, 반지름 = " + W.getRadius());
}
}
'자바 공부' 카테고리의 다른 글
프로세스(process), 쓰레드(thread) 생성 (0) | 2021.02.26 |
---|---|
인터페이스(역할, 사용, 상속) (0) | 2021.02.25 |
상속(개념, 클래스 상속, 부모 생성자 , 다형성, 추상 클래스) (0) | 2021.02.16 |
this와 접근 제어자 (1) | 2021.01.31 |
객체지향이란? / 클래스선언, 클래스 구성, 변수, 메소드 등 (2) | 2021.01.25 |