class Circle {
// double rad = 0;
private double rad = 0; // 반지름 설정
final double PI = 3.14; // PI
public Circle(double r) { // 생성자 함수
setRad(r);
}
public void setRad(double r) { // 반지름을 설정하는 함수
if (r < 0) {
rad = 0;
return;
}
rad = r;
}
public double getArea() { // 넓이를 구하는 함수
return (rad * rad) * PI;
}
}
public class JavaPractice {
public static void main(String[] args) {
Circle c = new Circle(1.5);
System.out.println(c.getArea());
c.setRad(2.5);
System.out.println(c.getArea());
c.setRad(-3.3);
System.out.println(c.getArea());
// c.rad = -4.5;
// System.out.println(c.getArea());
}
}
example1 패키지 - A.java
package example1;
public class A {
int a; // default
// public int a;
public A(){}
private void accessTest() {
}
// void accessTest() {}
}
example2 패키지 - B.java
package example2;
import example1.A;
public class B {
int b;
void accessTest() {
A a = new A();
a.accessTest();
// 다른 패키지이므로 접근 불가
System.out.println(a.a);
// public 붙이면 Error 없음, public 떼면 default로 변경되므로 Error
}
}
example1 패키지 - C.java
package example1;
public class C {
int c;
void accessTest() {
A a = new A();
a.accessTest();
// 같은 패키지에 있어도 다른 클래스에 있으므로 private에는 접근 불가
System.out.println(a.a);
// 같은 패키지는 default에 접근 가능하다
}
}
class Baby {
private int age;
private String name;
public void initBaby(int a, String b) {
age = a;
name = b;
}
public void show() {
System.out.println(age + "살 " + name + "입니다");
}
public void setAge(int num) { // 나이를 설정하는 함수
if (num < 0) {
num = 0;
}
age = num;
}
public int getAge() { // 나이를 리턴하는 함수
return age;
}
}
class ObjectTest {
public static void main(String[] args) {
Baby baby = new Baby();
baby.initBaby(5,"아기");
baby.show();
}
}
class Rectangle {
private int width;
private int height;
// data는 private, method()는 public
public Rectangle(int width, int height) {
setWidth(width);
setHeight(height);
}
public void setWidth(int width) {
if (width < 0) {
this.width = 0;
System.out.println("잘못된 입력입니다.");
return;
}
this.width = width;
}
public void setHeight(int height) {
if (height < 0) {
this.height = 0;
System.out.println("잘못된 입력입니다.");
return;
}
this.height = height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
void show() {
}
int getArea() {
return width * height;
}
}
class ObjectTest {
public static void main(String[] args) {
Rectangle rec = new Rectangle(10, -20);
// rec.width = 10;
}
}
class Rectangle {
private int x;
private int y;
private int width;
private int height;
// data는 private, method()는 public
public Rectangle(int x, int y, int width, int height) {
// x, y는 좌표, width는 가로, height는 세로
setX(x);
setY(y);
setWidth(width);
setHeight(height);
}
public int getX() { // x좌표 얻어오기
return x;
}
public void setX(int x) { // x좌표 설정
this.x = x;
}
public int getY() { // y좌표 얻어오기
return y;
}
public void setY(int y) { // y좌표 설정
this.y = y;
}
public void setWidth(int width) { // 가로 길이 설정하는 함수
if (width < 0) {
this.width = 0;
System.out.println("잘못된 입력입니다.");
return;
}
this.width = width;
}
public void setHeight(int height) { // 세로 길이 설정하는 함수
if (height < 0) {
this.height = 0;
System.out.println("잘못된 입력입니다.");
return;
}
this.height = height;
}
public int getWidth() { // 가로 길이 리턴
return width;
}
public int getHeight() { // 세로 길이 리턴
return height;
}
public int square() { // 사각형 넓이 리턴
return width * height;
}
public void show() { // 사각형의 좌표와 넓이 출력
System.out.println("(" + x + "," + y + ")에서 크기가 " + width + "x" + height + "인 사각형");
}
public boolean contains(Rectangle rectangle) {
/*
나의 x좌표가 매개변수의 x좌표보다 작거나 같다
&& 나의 (x+width)좌표가 매개변수의 (x+width)좌표보다 크거나 같다
&& 나의 y좌표가 매개변수의 y좌표보다 작거나 같다
&& 나의 (y+height)좌표가 매개변수의 (y+height)좌표보다 크거나 같다
*/
if ((this.x <= rectangle.x) && ((this.x) + (this.width) >= (rectangle.x) + (rectangle.width))
&& (this.y <= rectangle.y) && (this.y) + (this.height) >= (rectangle.y) + (rectangle.height)) {
return true;
} else {
return false;
}
}
}
class ObjectTest {
public static void main(String[] args) {
Rectangle r = new Rectangle(2, 2, 8, 7);
Rectangle s = new Rectangle(5, 5, 6, 6);
Rectangle t = new Rectangle(1, 1, 10, 10);
Rectangle w = new Rectangle(3, 7, 2, 8);
r.show();
System.out.println("s의 면적은 " + s.square());
if (t.contains(r)) {
System.out.println("t는 r을 포함합니다.");
}
if (t.contains(s)) {
System.out.println("t는 s를 포함합니다.");
}
if (t.contains(w)) {
System.out.println("t는 w를 포함합니다.");
}
}
}
[Console]
(2,2)에서 크기가 8x7인 사각형
s의 면적은 48
t는 r을 포함합니다.
t는 s를 포함합니다.
public boolean contains(Rectangle rectangle) {
boolean flag = true;
if (this.x > rectangle.x) {
flag = false;
}
if (this.y > rectangle.y) {
flag = false;
}
if ((this.x) + (this.width) < (rectangle.x) + (rectangle.width)) {
flag = false;
}
if ((this.y) + (this.height) < (rectangle.y) + (rectangle.height)) {
flag = false;
}
return flag;
}
public boolean contains(Rectangle r) {
if(x < r.x && y < r.y) {
if((width+x) > (r.x + r.width) && (height + y) > (r.y + r.height)) {
return true;
}
}
return false;
}