Java/과제 Java/과제 2022. 9. 2. chapter 10) 예외처리example - Exercise11 두 개의 정수를 입력받아 나눗셈을 하는 프로그램을 만들어 보자.(나눗셈의 결과는 정수로 나타낸다.) 어떤 수를 0으로 나눌 때와 정수가 아닌 다른 타입을 입력받는 경우 try-catch문을 사용하여 예외처리를 해준다. 어떤 수를 0으로 나눌 때에는 "0으로 나눌 수 없습니다."라고 출력하고 다시 입력받게 만든다. 정수가 아닌 다른 타입을 입력받는 경우에는 "잘못된 입력입니다."를 출력하고 다시 입력받게 만든다. package practice4; import java.util.Scanner; public class Exercise11 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); boolean resu.. Java/과제 2022. 9. 2. chapter 10) 예외처리example - BankAccount 클래스 인터페이스 과제에서 만들었던 BankAccount 클래스를 아래 내용을 참고하여 변경하여 보자. package practice4; public abstract class BankAccount { //필드 protected int balance; //잔액변수 //생성자 public BankAccount(int balance){ this.balance=balance; //잔액변수 } //추상메소드 public abstract String getAccountType(); /** * 계좌의 종류를 반환하는 메소드 * @return 계좌의 종류(저축예금, 당좌예금) */ //메소드 public int getBalance() { return this.balance; } public void deposit(int a.. Java/과제 2022. 8. 31. chapter 08) 인터페이스example - Book 클래스 아래 클래스 다이어그램을 참고하여 책을 대여해주는 업체를 위한 Book 클래스와 서브클래스들을 만들어 보자. package practice3; public abstract class Book { private int number; private String title; private String author; private static int countOfBooks = 0; public Book(String title, String author) { this.title = title; this.author = author; number = countOfBooks; countOfBooks++; } public int getNumber() { return number; } public void setNumb.. Java/과제 2022. 8. 31. chapter 08) 인터페이스example - BankAccount 클래스 변경 상속 과제에서 만들었던 BankAccount 클래스를 추상클래스로 변경하여 보자. package practice3; public abstract class BankAccount { public abstract String getAccountType(); protected int balance; public BankAccount(int balance) { this.balance = balance; } public int getBalance() { return balance; } public void deposit(int amount) { balance += amount; } public boolean withdraw(int amount) { if (balance >= amount) { balance -= .. Java/과제 2022. 8. 31. chapter 08) 인터페이스example - Shape 클래스 변경하기 7장 상속 과제 섹션 3에서 만들었던 도형 관련 클래스인 Shape 클래스를 추상클래스로 만들어 보자. package practice3; public abstract class Shape implements Comparable { String name; public abstract double area(); public abstract double perimeter(); @Override public int compareTo(Shape o) { if (area() > o.area()) { return 1; } else if (area() < o.area()) { return -1; } else { return 0; } } @Override public String toString() { return St.. Java/과제 2022. 8. 29. chapter 07) 상속example - 은행 관련 클래스 확장하기 아래 클래스 다이어그램을 참고하여 섹션 4에서 만들었던 은행 관련 클래스들을 확장해보자. package chapter07.exercise.advanced; public class BankAccount { /** * 계좌의 잔액 */ protected int balance; public BankAccount(int balance) { this.balance = balance; } public int getBalance() { return balance; } /** * 입금 메소드 * @param amount 입금할 금액 */ public void deposit(int amount) { balance += amount; } /** * 출금 메소드 * @param amount 출금할 금액 * @return 출.. Java/과제 2022. 8. 26. chapter 07) 상속example - 도형 관련 클래스 만들기 아래의 클래스 다이어그램을 참고하여 도형 관련 클래스들을 만드시오. 원- package practice; public class Circle extends Shape { private double radius; public Circle(double radius) { this.radius = radius; } @Override public double area() { return radius * radius * Math.PI; } @Override public double perimeter() { return radius * 2 * Math.PI; } public String toString() { return "도형의 종류: 원, 둘레: " + perimeter() + "㎝" + ", 넓이: " + ar.. Java/과제 2022. 8. 26. chapter 07) 상속example - Student 클래스 만들기 아래의 클래스 다이어그램을 참고하여 Human 클래스를 상속받는 Student 클래스를 만드시오. package practice; public class Human { private String name; private int age; public Human(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String.. Java/과제 2022. 8. 24. chapter06) 클래스 example - NewCar 아래 내용 클래스 다이어그램과 내용을 참고하여 Car 클래스를 NewCar 클래스로 새롭게 만들고 테스트 해 보자. 테스트 코드는 앞의 Car 클래스에서 테스트했던 내용을 그대로 사용하여 결과가 동일하게 나오는지 확인한다. package practice; public class NewCar { // 필드 private double speed; private String color; private static final double MAX_SPEED = KilloToMile(200.0); // 생성자 public NewCar() { } NewCar(String color) { this.color = color; } // 메소드 public double getSpeed() { return MileToKill.. Java/과제 2022. 8. 24. chapter06) 클래스 example - Car 아래의 클래스 다이어그램에 해당하는 Car 클래스를 만들고, 테스트 코드로 Car 클래스를 테스트 해 보자. package practice; public class Car { // 필드 private double speed; private String color; private static final double MAX_SPEED = 200.0; // 생성자 public Car() { } public Car(String color) { this.color = color; } // 메소드 public double getSpeed() { return speed; } public void setSpeed(double speed) { this.speed = speed; } public String getColo.. 이전 1 2 3 다음