FIF's 코딩팩토리

자바 기초 NCS교육과정(15)-클래스 문제풀이 본문

Back-End/국비 NCS교과과정

자바 기초 NCS교육과정(15)-클래스 문제풀이

FIF 2019. 7. 29. 21:18
반응형

이번 시간은 문제풀이 시간 입니다.

코드를 바로 보지 마시고, 충분한 시간을 가지고 고민해 보세요!

생각하는 시간이 많을수록 실력은 향상됩니다.

 

 

 

Prob01_class.java

피자의 반지름을 10, 도넛의 반지름은 2로 한다.
   
  [실행결과]
  자바피자의 면적은 314.0
  자바도넛의 면적은 12.56
 

 

 

 

 

 

 

 

 

 

 

 

 

class Circle {
int radius; // 원의 반지름을 저장하는 멤버 변수
String name; // 원의 이름을 저장하는 멤버 변수
public double getArea() { // 멤버 메소드
return 3.14 * radius * radius;
}
}// end class////////////////////////
public class Prob01_class {
public static void main(String[] args) {
// 여기를 구현하세요.////////////////////
Circle ca = new Circle();
ca.name = "자바피자";
ca.radius = 10;
Circle cb = new Circle();
cb.name = "자바도넛";
cb.radius = 2;
System.out.println(ca.name + "의 면적은 " + ca.getArea());
System.out.println(cb.name + "의 면적은 " + cb.getArea());
}// end main()
}// end class

 

Prob02_class.java

 [실행결과]를 참조하여  main() 메소드에 로직에 추가하세요.
   
  [실행결과]
  생성자 호출됨
  Bible 작자미상
 

 

 

 

 

 

 

 

 

 

 

 

 

class Book2 {
String title;
String author;
void show() { System.out.println(title + " " + author); }
public Book2() {
this("", "");
System.out.println("생성자 호출됨");
}
public Book2(String title) {
this(title, "작자미상");
}
public Book2(String title, String author) {
this.title = title;
this.author = author;
}
}
public class Prob02_class {
public static void main(String[] args) {
Book2 javaBook = new Book2("Java", "황기태");
Book2 bible = new Book2("Bible");
Book2 emptyBook = new Book2();
/////////여기에 구현하세요.
bible.show();
}//end main()
}//end class

 

Prob03_class.java

  draw()메소드를   [실행결과]를 참조하여 구현하세요.
   
  [실행결과]
  &&&&&&&&&&
  %%%%%%%%%%%%%%%%%%%%
  %%%%%%%%%%%%%%%%%%%%
  %%%%%%%%%%%%%%%%%%%%
 

 

 

 

 

 

 

 

 

 

 

 

 

class Box {
private int width, height; // 박스의 너비와 높이
private char fillChar; // 박스를 그리는 데 사용하는 문자
public Box() { // 매개 변수 없는 생성자
this(10, 1); // this() 이용
}
public Box(int width, int height) { // 너비외 높이의 2 매개 변수를 가진 생성자
this.width = width;
this.height = height;
}
public void draw() { // 박스 그리는 메소드
///////////// 여기에서 구현하세요.
for(int j=1;j<=height;j++){
for(int i=1;i<=width;i++){
System.out.print(fillChar);
}
System.out.println();
}
}// end draw()
public void fill(char c) { // 박스를 그리는데 사용하는 문자 설정
this.fillChar = c;
}
}
public class Prob03_class {
public static void main(String[] args) {
//여기를 구현하세요.
Box a = new Box(); // 10x1 사각형
Box b = new Box(20, 3); // 20x3 사각형
a.fill('&'); // box를 그릴 때 사용하는 문자 '&'
b.fill('%'); // box를 그릴 때 사용하는 문자 '%'
a.draw(); // 박스 그리기
b.draw(); // 박스 그리기
}//end main()
}//end class

 

Prob04_class.java

1 Office클래스를 이용한 객체를 생성할때 chk멤버변수에 2로 초기값을 할당하시오.
    1이면 합격 2이면 불합격
     
  2 출력결과 
   번호         점수         합격여부
    1                90                 1
    2                65                 2
    3                85                 1
 

 

 

 

 

 

 

 

 

 

 

 

 

 

class Office {
int num;
int jumsu;
int chk;
public Office(int chk) {
this.chk = chk;
}
// 프로그램을 컴파일할때 정상적으로 수행이 되도록 생성자를 정의하시오.
public Office(int num, int jumsu) {
this.num = num;
this.jumsu = jumsu;
}
public void count() {
// 점수가 80이상이면 chk에 1로 변경한다.
if (jumsu >= 80) {
chk = 1;
} else {
chk = 2;
}
}
public void prn() {
System.out.printf("%d %d %d\n", num, jumsu, chk);
}
public void process() {
count();
prn();
}
}
public class Prob04_class {
public static void main(String[] args) {
// 문제를 풀때는 주석을 해제후 한다.
Office p1 = new Office(1, 90);
Office p2 = new Office(2, 65);
Office p3 = new Office(3, 85);
p1.process();
p2.process();
p3.process();
}// end main()
}// end class
반응형
Comments