-
쉽게 배우는 자바 프로그래밍 2판 4장 프로그래밍 문제 (6번 ~ 8번)JAVA/연습문제 2022. 10. 8. 22:05반응형
# 개발 공부하는 초보자가 작성한 답안이니 정답이 아닐 수 있습니다!!!
쉽게 배우는 자바 프로그래밍 2판 4장 프로그래밍 문제 (1번 ~ 5번)
# 개발 공부하는 초보자가 작성한 답안이니 정답이 아닐 수 있습니다!!! 1. 삼각형을 나타내는 Triangle 클래스를 작성하라. 삼각형의 속성으로는 실숫값의 밑변과 높이를, 동작으로는 넓이 구하기
sewoni.tistory.com
1번 ~ 5번 문제와 정답은 위쪽 클릭
6. 복소수를 모델링한 Complex 클래스를 작성하고, 다음 프로그램으로 테스트하라.
📢주어진 값📢 public class ComplexTest { public static void main(String[] args) { Complex c1 = new Complex(2.0); c1.print(); Complex c2 = new Complex(1.0, 2.5); c2.print(); } }
🧤정답🧤 public class Complex { double bokSoSu, bokSoSu2; Complex(double bokSoSu){ // 변수가 1개인 것을 위해 this.bokSoSu = bokSoSu; } Complex(double bokSoSu, double bokSoSu2){ // 변수가 2개인 것을 위해 this.bokSoSu = bokSoSu; this.bokSoSu2 = bokSoSu2; } void print(){ System.out.printf("%.1f + %.1fi\n", bokSoSu, bokSoSu2); } } public class ComplexTest { public static void main(String[] args) { Complex c1 = new Complex(2.0); c1.print(); Complex c2 = new Complex(1.5, 2.5); c2.print(); } }
7. 골프채를 모델링한 GolfClub 클래스를 작성하고, 다음 프로그램으로 테스트하라.
📢주어진 값📢 public class GolfClubTest { public static void main(String[] args) { GolfClub g1 = new GolfClub(); g1.print(); GolfClub g2 = new GolfClub(8); g2.print(); GolfClub g3 = new GolfClub("퍼터"); g3.print(); } }
🧤정답🧤 public class GolfClub { int number; String Name = "아이언"; void print(){ if (Name.equals("아이언")) { System.out.printf("%d번 %s입니다.\n", number, Name); } else System.out.printf("%s입니다.\n", Name); } GolfClub(String Name){ this.Name = Name; } GolfClub(int number){ this.number = number; } GolfClub(){ number = 7; } } public class GolfClubTest { public static void main(String[] args) { GolfClub g1 = new GolfClub(); g1.print(); GolfClub g2 = new GolfClub(8); g2.print(); GolfClub g3 = new GolfClub("퍼터"); g3.print(); } }
8. 주사위를 나타내는 Dice 클래스를 작성하고, 다음 코드를 사용해 테스트하라.
📢주어진 조건📢
Dice 클래스에는 6개의 면이라는 속성과 굴리기라는 동작이 있다.
Math.random() 메서드는 0.0이상 1.0미만의 double 타입의 무작위 실수를 반환한다.📢주어진 값📢 public class DiceTest { public static void main(String[] args) { Dice d = new Dice(); System.out.println("주사위의 숫자 : " + d.roll()); } }
🧤정답🧤 public class Dice { int face; // 속성=변수 int roll(){ return face; } Dice(){ face = (int) (Math.random() * 6) + 1; } } public class DiceTest { public static void main(String[] args) { Dice d = new Dice(); System.out.println("주사위의 숫자 : " + d.roll()); } }
반응형'JAVA > 연습문제' 카테고리의 다른 글
쉽게 배우는 자바 프로그래밍 2판 5장 연습문제 Part.1 (1번 ~ 5번) (0) 2022.10.13 쉽게 배우는 자바 프로그래밍 2판 5장 연습문제 Part.2 (6번 ~ 11번) (1) 2022.10.13 쉽게 배우는 자바 프로그래밍 2판 4장 프로그래밍 문제 (1번 ~ 5번) (1) 2022.10.07 쉽게 배우는 자바 프로그래밍 2판 4장 연습문제 (1) 2022.10.07 쉽게 배우는 자바 프로그래밍 2판 4장 도전문제 (2) 2022.10.06