01. Circle 클래스 객체 생성 및 활용
º 반지름과 이름을 가진 Circle 클래스를 작성하고, Circle 클래스의 객체를 생성하시오.
- 클래스의 이름은 Circle 클래스로 정의
- String 타입 name, int 타입 radius 두 개의 필드
- main() 메소드를 작성하여 객체를 두 개 생성하고, 이 객체에 대한 레퍼런스 변수명을 pizza, donut으로 하시오.
- pizza의 이름(name 필드)을 "자바 피자", 반지름(radius) 10, donut의 이름(name 필드)를 "자바 도넛", 반지름(radius)을 2로 설정하시오.
- 객체의 면적을 계산하는 메소드를 설정하고, 최종적으로 화면에 출력하시오.
<1-1> Circle 라이브러리 클래스
public class Circle {
int radius; //원의 반지름 필드
String name; //원의 이름 필드
public double getArea() { //멤버 메소드
return 3.14*radius*radius;
}
}
<1-2> CircleEx 실행 클래스
public class CircleEx {
public static void main(String[] args) {
Circle pizza; //레퍼런스 변수 pizza 선언
pizza = new Circle(); //Circle 객체 생성
pizza.radius = 10;
pizza.name = "자바피자";
double area = pizza.getArea();
System.out.println(pizza.name + "의 면적: " + area);
Circle donut = new Circle(); //Circle 객체 생성
donut.radius = 2;
donut.name = "자바도넛";
area = donut.getArea();
System.out.println(donut.name + "의 면적: " + area);
}
}
02. 두 개의 생성자를 가진 Circle 클래스 생성
- 기본 생성자와 매개 변수를 가진 생성자 작성
- 생성자를 이용하여 초기화를 진행하고 클래스의 실행 결과를 출력
<2-1> Circle 라이브러리 클래스
public class Circle {
int radius; //원의 반지름 필드
String name; //원의 이름 필드
public Circle() { //매개변수가 없는 생성자
radius = 1; //필드 초기화
name = ""; //필드 초기화
}
public Circle(int r, String n) { //매개변수를 가진 생성자
radius = r; //매개변수로 필드 초기화
name = n; //매개변수로 필드 초기화
}
public double getArea() { //멤버 메소드
return 3.14*radius*radius;
}
}
<2-2> CircleEx 실행 클래스
public class CircleEx {
public static void main(String[] args) {
Circle pizza = new Circle(10, "자바피자"); //객체 생성
double area = pizza.getArea();
System.out.println(pizza.name + "의 면적: " + area);
Circle donut = new Circle(); //Circle 객체 생성 및 초기화
donut.name = "도넛피자"; //이름 변경
area = donut.getArea();
System.out.println(donut.name + "의 면적: " + area);
}
}
03. Book 클래스 작성 및 생성자 선언
º 제목과 저자를 나타내는 title과 author 필드를 가진 Book 클래스를 작성하고, 생성자를 작성하여 필드 초기화
<3-1> Book 라이브러리 클래스
public class Book {
String title;
String author;
public Book(String t) { //생성자
title = t;
author = "작자미상";
}
public Book(String t, String a) { //생성자
title = t;
author = a;
}
}
<3-2> BookEx 실행 클래스
public class BookEx {
public static void main(String[] args) {
Book Prince = new Book("어린왕자", "생텍쥐페리");
Book loveStory = new Book("춘향전");
System.out.println(Prince.title + " " + Prince.author);
System.out.println(loveStory.title + " " + loveStory.author);
}
}
04. this( )로 다른 생성자 호출
º Book 클래스의 생성자를 this()를 이용하여 수정
<4-1> Book 라이브러리 클래스
public class Book {
String title;
String author;
void show() {
System.out.println(title + " " + author);
}
public Book() {
this("", "");
System.out.println("생성자 호출됨");
}
public Book(String t) { //생성자
title = t;
author = "작자미상";
}
public Book(String t, String a) { //생성자
title = t;
author = a;
}
}
<4-2> BookEx 실행 클래스
public class BookEx {
public static void main(String[] args) {
Book Prince = new Book("어린왕자", "생텍쥐페리");
Book loveStory = new Book("춘향전");
Book emptyBook = new Book();
loveStory.show();
}
}
05. Round 객체 배열 생성
º 반지름이 0~4인 Round 객체 5개를 가지는 배열을 생성하고, 배열에 있는 모든 Round 객체의 면적 출력
<5-1> Round 라이브러리 클래스
public class Round {
int radius;
public Round(int radius) {
this.radius = radius;
}
public double getArea() {
return 3.14*radius*radius;
}
}
<5-2> RoundEx 실행 클래스
public class RoundEx {
public static void main(String[] args) {
Round [] c; //Round 배열에 대한 레퍼런스 c 선언
c = new Round[5]; //5개이 레퍼런스 배열 생성
for(int i = 0; i<c.length; i++)
c[i] = new Round(i);
for(int i = 0; i<c.length; i++)
System.out.print((int)c[i].getArea() + " ");
}
}
06. 객체 배열 활용
º Book 클래스를 활용하여 2개짜리 Book 객체 배열을 만들고, 사용자로부터 책의 제목과 저자를 입력 받아 배열 완성
<6-1> Book 라이브러리 클래스
public class Book {
String title, author;
public Book(String title, String author) { //생성자
this.title = title;
this.author = author;
}
}
<6-2> BookArray 실행 클래스
import java.util.Scanner;
public class BookArray {
public static void main(String[] args) {
Book [] book = new Book[2];
Scanner scanner = new Scanner(System.in);
for(int i=0; i<book.length; i++) { //book.length = 2
System.out.print("제목>>");
String title = scanner.nextLine();
System.out.print("저자>>");
String author = scanner.nextLine();
book[i] = new Book(title, author); //배열 원소 객체 생성
}
for(int i=0; i<book.length; i++)
System.out.print("(" + book[i].title + "," + book[i].author + ")");
scanner.close();
}
}
07. 인자로 배열 전달하는 프로그램
- char 배열을 메소드의 인자로 전달하여 배열 속의 공백(' ')문자를 ' , '로 대치하는 프로그램
- char c[] = {'T', 'h', 'i', 's', 'a', 'p', 'e', 'n', 'c', 'i', 'l', '.'};
<7-1> ArrayParameter 라이브러리 클래스
public class ArrayParameter {
//메소드
static void replaceSpace(char a[]) {
for(int i=0; i<a.length; i++)
if(a[i] == ' ')
a[i] = ','; //공백 문자를 ',(comma)'로 변경
}
static void printCharArray(char a[]) {
for(int i=0; i<a.length; i++)
System.out.print(a[i]); //배열 원소 문자 출력
System.out.println(); //배열 원소 모두 출력 후 줄바꿈
}
}
<7-2> ArrayParameterEx 실행 클래스
public class ArrayParameterEx {
public static void main(String[] args) {
char c[] = {'T','h','i','s',' ','i','s',' ','a',' ','p','e','n','c','i','l','.'};
ArrayParameter.printCharArray(c); //원의 배열 원소 출력
ArrayParameter.replaceSpace(c); //공백 문자 바꾸기
ArrayParameter.printCharArray(c); //수정된 배열 원소 출력
}
}
'JAVA > 자바 실습' 카테고리의 다른 글
Java | Lab8 (0) | 2021.06.08 |
---|---|
Java | Lab6 (0) | 2021.04.16 |
Java | Lab4_Ex (0) | 2021.04.12 |
Java | Lab4 (0) | 2021.04.12 |
Java | Lab3 (0) | 2021.04.12 |