1. 삼각형을 나타내는 Triangle 클래스를 작성하라. 삼각형의 속성으로는 실숫값의 밑변과 높이를, 동작으로는 넓이 구하기와 접근자가 있고 생성자도 포함한다. 작성한 클래스를 다음 코드를 사용해 테스트하라.
public class Triangle {
private double width;
private double height;
public Triangle(double width, double height) { //생성자
this.width=width;
this.height=height;
}
double getWidth() {//접근자
return width;
}
double getHeight() {//접근자
return height;
}
public double findArea() {
return (width*height)/2;
}
}
public class TriangleTest {
public static void main(String[] args) {
Triangle t = new Triangle(10.0, 5.0);
System.out.println(t.findArea());
}
}
2. 1에서 작성한 Triangle 클래스에 2개의 삼각형 넓이가 동일한지 비교하는 isSameArea()메서드를 추가하라. 그리고 다음 코드를 사용해 테스트하라.
public class Triangle {
private double width;
private double height;
private double area;
public Triangle(double width, double height) { //생성자
this.width=width;
this.height=height;
}
double getWidth() {
return width;
}
double getHeight() {
return height;
}
public double findArea() {
return width*height;
}
public boolean isSameArea(Triangle t) {
area=findArea();
if( area == t.findArea())
return true;
else
return false;
}
}
public class TriangleTest {
public static void main(String[] args) {
Triangle t1 = new Triangle(10.0, 5.0);
Triangle t2 = new Triangle(5.0, 10.0);
Triangle t3 = new Triangle(8.0, 8.0);
System.out.println(t1.isSameArea(t2));
System.out.println(t1.isSameArea(t3));
}
}
3. 회원을 관리하려고 회원을 모델링한 Member 클래스를 작성하라. 회원 정보로는 이름, 아이디, 암호, 나이가 있다. 외부 객체는 이와 같은 회원 정보에 접근할 수 없고 접근자와 설정자로만 접근할 수 있다. 그리고 모든 회원 정보를 사용해 객체를 생성할 수 있는 생성자도 있다.
public class Member {
private String name;
private String ID;
private String code;
private int age;
public Member(String name, String ID, String code, int age) {
this.name=name;
this.ID=ID;
this.code=code;
this.age=age;
}
public void setName(String name) {
this.name= name;
}
public String getName() {
return name;
}
public void setID(String ID) {
this.ID = ID;
}
public String getID() {
return ID;
}
public void setCode(String code) {
this.code=code;
}
public String getCode() {
return code;
}
public void setAge(int age) {
this.age=age;
}
public int getAge() {
return age;
}
}
4. 생산된 모든 자동차와 빨간색 자동차의 개수를 출력하는 Car 클래스를 작성하라. 그리고 다음 코드를 사용해 테스트하라.
public class Car {
String color;
static int numOfCar=0;
static int numOfRedCar=0;
public Car (String color) {
this.color=color;
numOfCar++;
if (color == "RED" || color== "red")
numOfRedCar++;
}
public static int getNumOfCar() {
return numOfCar;
}
public static int getNumOfRedCar() {
return numOfRedCar;
}
}
public class CarTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car c1 = new Car("red");
Car c2 = new Car("blue");
Car c3 = new Car("RED");
System.out.printf("자동차 수: %d, 빨간색 자동차 수: %d",
Car.getNumOfCar(), Car.getNumOfRedCar());
}
}
5. 길이 속성만 가진 직선을 모델링한 Line 클래스를 작성하고, 다음 프로그램을 테스트하라.
public class Line {
int length;
public Line(int length) {
this.length=length;
}
public boolean isSameLine(Line l) {
if(this.length== l.length)
return true;
else
return false;
}
}
public class LineTest {
public static void main(String[] args) {
Line a = new Line(1);
Line b = new Line(1);
System.out.println(a.isSameLine(b));
System.out.println(a== b);
}
}
6. 복소수를 모델링한 Complex 클래스를 작성하고, 다음 프로그램으로 테스트하라.
public class Complex {
double x, y;
public Complex(double x) {
this.x=x;
y=0.0;
}
public Complex(double x, double y) {
this.x=x;
this.y=y;
}
public void print() {
System.out.printf("%.1f + %.1fi", x, y);
System.out.println();
}
}
public class ComplexTest {
public static void main(String[] args) {
Complex c1 = new Complex(2.0);
c1.print();
Complex c2 = new Complex(1.5, 2.5);
c2.print();
}
}
7. 골프채를 모델링한 GolfClub 클래스를 작성하고, 다음 프로그램으로 테스트하라.
public class GolfClub {
String name;
int num;
public GolfClub() {
name="아이언";
num=7;
}
public GolfClub(int num) {
this.num=num;
name="아이언";
}
public GolfClub(String name) {
this.name=name;
num=0;
}
public void print() {
if(num> 0)
System.out.println(num+"번"+" "+name+"입니다.");
else
System.out.println(name+"입니다.");
}
}
public class GolfClubTest {
public static void main(String[] args) {
GolfClub g1 = new GolfClub();
g1.print();
GolfClub g2 = new GolfClub(8);
g2.print();
GolfClub g3 = new GolfClub("퍼터");
g3.print();
}
}
8. 주사위를 나타내는 Dice 클래스를 작성하고, 다음 코드를 사용해 테스트하라.
*Dice 클래스에서는 6개의 면(face)이라는 속성과 굴리기(roll)라는 동작이 있다. Math.random() 메서드는 0.0 이상 1.0 미만 double 타입의 무작위 실수를 반환한다.
public class Dice {
int face;
public int roll() {
face=(int)(Math.random()*6+1);
return face;
}
}
public class DiceTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Dice d = new Dice();
System.out.println("주사위의 숫자:"+ d.roll());
}
}
Math.random 참고: https://finger-ineedyourhelp.tistory.com/6
'자바 프로그래밍 > 쉽게 배우는 자바 프로그래밍' 카테고리의 다른 글
1장 자바 시작하기 (0) | 2022.09.01 |
---|---|
3장 프로그래밍 문제 (0) | 2022.09.01 |
5장 프로그래밍 문제 (0) | 2022.08.25 |
3장 제어문과 메서드 ~ 4장 객체 지향 (0) | 2022.08.09 |