1. Tv 클래스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package constructorex; class Tv { String brand; int year; int size; public Tv() {} Tv(String brand, int year, int size) { this.brand = brand; this.year = year; this.size = size; } void show() { System.out.print(brand + "에서 만든 "); System.out.print(year + "년 "); System.out.print(size + "인치 TV"); } } public class ConTest04 { public static void main(String[] args) { Tv myTv = new Tv("LG", 2018, 65); myTv.show(); } } | cs |
2. Book 클래스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package constructorex; class Book { String title; String author; public Book() {} Book(String title) { this(title, "작자 미상"); } Book(String title, String author) { this.title = title; this.author = author; } } public class ConTest05 { public static void main(String[] args) { Book littlePrince = new Book("어린왕자", "생텍쥐페리"); Book loveStory = new Book("춘향전"); System.out.println(littlePrince.title + " " + littlePrince.author); System.out.println(loveStory.title + " " + loveStory.author); } } | cs |
3. Song 클래스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package constructorex; class Song { String title; String singer; int year; String country; public Song() {} Song(String title, String singer, int year, String country) { this.title = title; this.singer = singer; this.year = year; this.country = country; } void show() { System.out.print(year + "년 "); System.out.print(country + " 국적의 "); System.out.print(singer + "가 부른 "); System.out.print(title); } } public class ConTest06 { public static void main(String[] args) { Song myFavorite = new Song("Dancing Queen", "ABBA", 1978, "스웨덴"); myFavorite.show(); } } | cs |
4. Grade 클래스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | package constructorex; import java.util.Scanner; class Grade { int math; int science; int english; double average; public Grade() {} Grade(int math, int science, int english) { this.math = math; this.science = science; this.english = english; } String average() { average = (math + science + english) / 3.0; return String.format("%.1f", average); } } public class ConTest07 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("수학점수 입력: "); int math = sc.nextInt(); System.out.print("과학점수 입력: "); int science = sc.nextInt(); System.out.print("영어점수 입력: "); int english = sc.nextInt(); Grade hgd = new Grade(math, science, english); System.out.println("평균은 " + hgd.average()); sc.close(); } } | cs |
5. Circle 클래스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package constructorex; // 객체 배열 class Circle { int radius; public Circle() {} Circle(int radius) { this.radius = radius; } double getArea() { return 3.14 * radius * radius; } } public class ConTest08 { public static void main(String[] args) { Circle c1 = new Circle(0); Circle c2 = new Circle(1); Circle c3 = new Circle(2); Circle c4 = new Circle(3); Circle c5 = new Circle(4); System.out.println("c1의 면적: " + c1.getArea()); System.out.println("c2의 면적: " + c2.getArea()); System.out.println("c3의 면적: " + c3.getArea()); System.out.println("c4의 면적: " + c4.getArea()); System.out.println("c5의 면적: " + c5.getArea()); } } | cs |
6. Cir (Circle2) 클래스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package constructorex; class Cir { int radius; public Cir() {} Cir(int radius) { this.radius = radius; } double getArea() { return 3.14 * radius * radius; } } public class ConTest09 { public static void main(String[] args) { int[] arr = new int[5]; Cir[] myCir = new Cir[5]; // c[0] = new Cir(0); // c[1] = new Cir(1); // c[2] = new Cir(2); // c[3] = new Cir(3); // c[4] = new Cir(4); for (int i=0; i<myCir.length; i++) { myCir[i] = new Cir(i); } for (int i=0; i<myCir.length; i++) { System.out.println("myCir[" + i + "]: " + myCir[i].getArea()); } } } | cs |
7. Book (Booking) 클래스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | package constructorex; import java.util.Scanner; class Booking { String title; String author; Booking() {} Booking(String title, String author) { this.title = title; this.author = author; } void show() { System.out.println("책 이름: " + title + " / 저자: " + author); } } public class ConTest10 { public static void main(String[] args) { Booking[] myBook = new Booking[3]; Scanner sc = new Scanner(System.in); for (int i=0; i<myBook.length; i++) { System.out.print((i+1) + ". 책의 이름을 입력하세요: "); String title = sc.nextLine(); System.out.print((i+1) + ". 책의 저자를 입력하세요: "); String author = sc.nextLine(); myBook[i] = new Booking(title, author); } for (int i=0; i<myBook.length; i++) { myBook[i].show(); } } } | cs |
8. Phone 클래스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package constructorex; import java.util.Scanner; class Phone { String name; int number; public Phone() {} Phone(String name, int number) { this.name = name; this.number = number; } } public class ConTest11 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("저장할 인원 수를 입력하세요: "); int userNum = sc.nextInt(); Phone[] myPhone = new Phone[userNum]; for(int i=0; i<myPhone.length; i++) { System.out.print((i + 1) + ". 이름을 입력하세요: "); String name = sc.next(); System.out.print((i + 1) + ". 전화번호를 입력하세요: "); int number = sc.nextInt(); myPhone[i] = new Phone(name, number); } System.out.println("저장을 완료하였습니다."); while (true) { System.out.print("검색할 이름을 입력하세요: "); String search = sc.next(); for (int i = 0; i < myPhone.length; i++) { if (search.equals(myPhone[i].name)) { System.out.println(myPhone[i].name + "의 전화번호: " + myPhone[i].number); } } if (search.equals("stop")) { break; } } // while } } | cs |
'JAVA > JAVA1' 카테고리의 다른 글
JAVA1_day20 | 클래스 Class 연습문제 (4) (ATM) (0) | 2018.01.09 |
---|---|
JAVA1_day19 | 클래스 Class 연습문제 (3) (PhoneBook) (0) | 2018.01.09 |
JAVA1_day17 | 추가공부 (0) | 2018.01.02 |
JAVA1_day17 | 클래스 Class 연습문제 (2) (0) | 2018.01.02 |
JAVA1_day17 | 메서드 오버로딩 (Method Overloading) (0) | 2018.01.02 |