241p q2
package kr.co.kihd.practice04;
import java.util.Scanner;
//241p 2번문제
public class Grade {
private int math;
private int science;
private int english;
public Grade(int math, int science, int english) {
super();
this.math = math;
this.science = science;
this.english = english;
}
public double average() {
double avg = (this.math + this.science + this.english)/3;
return avg;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("수학, 과학, 영어 순으로 3개의 점수 입력 >> ");
int math = scan.nextInt();
int science = scan.nextInt();
int english = scan.nextInt();
Grade me = new Grade(math, science, english);
System.out.println("평균은 " + me.average()); //me.average()는 평균을 계산하여 리턴
scan.close();
}
}
241p q4
package kr.co.kihd.practice04;
//241p 4번문제
public class Rectangle {
private int x;
private int y;
private int width;
private int height;
//생성자
public Rectangle(int x, int y, int width, int height) {
super();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public int square() {
return width*height;
}
public void show() {
System.out.printf("(%d,%d)에서 크기가 %dx%d인 사각형",
this.x,this.y,this.width,this.height);
System.out.println();
}
public boolean contains(Rectangle rectangle) {
if(x < rectangle.x
&& y < rectangle.y
&& x + width > rectangle.x + rectangle.width
&& y + height > rectangle.y + rectangle.height)
return true;
else
return false;
}
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);
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을 포함합니다.");
}
}