📌 클래스와 객체 Exam1
package chap6;
/*
* 가로(width),세로(height)의 멤버변수를 가지고 있는 직사각형(Rectangle)클래스 구현하기
* 멤버메서드는 void area(), void length() 를 가진다.
*/
class Rectangle {
int width;
int height;
void area() {
System.out.println("면적:"+ (width*height));
}
void length() {
System.out.println("둘레:"+ (width+height)*2);
}
}
public class Exam1 {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
r1.width = 10;
r1.height = 5;
r1.area(); // 면적출력
r1.length();// 둘레출력
Rectangle r2 = new Rectangle();
r2.width = 100;
r2.height = 20;
r2.area(); // 면적출력
r2.length();// 둘레출력
}
}
📌 클래스와 객체 Exam2
package chap6;
/*
* 가로(width),세로(height)의 멤버변수, 사각형 번호 sno, 클래스변수 cnt를 가지고 있는
* 직사각형(Rectangle2)클래스 구현하기
* sno변수 : 사각형번호.
* cnt변수 : 사각형번호를 생성하기 위한 클래스변수
* 멤버메서드는 int area(), int length(),String toString() 를 가진다.
*
* toString()
* 1번사각형 : 가로(10),세로(20),면적(200),둘레(60) 형태로 출력되도록 구현
*/
class Rectangle2 {
int width,height,sno;
static int cnt;
int area() {
return width*height;
}
int length() {
return 2 * (width+height);
}
public String toString() {
return sno+"번사각형 : " + "가로("+width+"), 세로("+height+
"), 면적(" + area() + "), 둘레(" + length() + ")";
}
}
public class Exam2 {
public static void main(String[] args) {
Rectangle2 r1 = new Rectangle2();
r1.width = 10;
r1.height = 20;
r1.sno = ++Rectangle2.cnt;
// 1번사각형 : 가로(10),세로(20),면적(200),둘레(60)
System.out.println(r1);
Rectangle2 r2 = new Rectangle2();
r2.width = 5;
r2.height = 5;
r2.sno = ++Rectangle2.cnt;
//2번사각형 : 가로(5), 세로(5), 면적(25), 둘레(20)
System.out.println(r2);
}
}
'수업(국비지원) > Java' 카테고리의 다른 글
| [Java] chap6 : 클래스와 객체 - 메서드 구성 (0) | 2023.04.15 |
|---|---|
| [Java] chap6 : 클래스와 객체 - 변수의 종류, 지역변수, 클래스 변수 사용 (0) | 2023.04.15 |
| [Java] chap6 : 클래스와 객체 - 클래스와 객체 (0) | 2023.04.15 |
| [Java] chap5 : 배열 Array - Exam1-5 (0) | 2023.04.14 |
| [Java] chap5 : 배열 Array - 배열(가변배열) (0) | 2023.04.14 |