728x90
Chapter 4 클래스, 객체, 메소드
Mini Project - p147
class Dice {
int value;
public Dice() {
value =0;
}
void roll() {
value = (int)(Math.random()*10)%6 +1;
}
public void setValue(int value) {this.value = value;}
public int getValue() {return value;}
}
public class mini_project {
public static void main(String[] args) {
Dice d1 = new Dice();
Dice d2 = new Dice();
int cnt = 0;
while(true) {
System.out.printf("주사위1=%d 주사위2=%d\n",d1.getValue(),d2.getValue());
if(d1.getValue()+d2.getValue() == 2) {
System.out.printf("(%d,%d)이 나오는데 걸린 횟수=%d",d1.getValue(),d2.getValue(),cnt);
break;
}else {
d1.roll(); d2.roll();
cnt++;
}
}
}
}
Exercise
1.
NumberBox box = new NumberBox();
box.ivalue = 10;
box.fvalue = 1.2345;
2.
변수 | 설명 | |
상태(속성) | Year | 년도 |
Month | 월 | |
Day | 일 |
메소드 이름 | 설명 | |
동작(행동) | setDate() | 날짜를 설정 |
getDate() | 날짜를 출력 |
3.
- 클래스 참조변수만 설정하고 객체를 설정하지 않음
Rectangle myRect => Rectangle myRect = new Rectangle();
4.
int sum(int x, int y, int z){
return x+y+z;
}
int sum(int x, int y){
return x+y;
}
Programming Exercise
1.
class Student{
String name;
int rollno;
int age;
void setStuent(String name, int rollno, int age){
this.name = name;
this.rollno = rollno;
this.age = age;
}
}
public class ch4_1 {
public static void main(String[] args) {
Student s1 = new Student();
s1.setStuent("Kim", 201921232, 23);
System.out.println("학생의 이름: "+ s1.name);
System.out.println("학생의 학번: "+ s1.rollno);
System.out.println("학생의 나이: "+ s1.age);
}
}
2.
class Dog {
String breed;
int age;
String color;
Dog() {
breed = "york";
age = 1;
color = "orange";
}
void barking() {}
void hungry() {}
void sleeping() {}
}
3.
class Date {
int year;
int month;
String s_month;
int day;
Date(){
year = 2021;
month = 12;
s_month = "December";
day = 20;
}
void print1() {
System.out.println(year+"."+month+"."+day);
}
void print2() {
System.out.println(s_month+" "+day+", "+ year);
}
}
public class ch4_3 {
public static void main(String[] args) {
Date d = new Date();
d.print1();
d.print2();
}
}
4.
class ComplexNumber {
int real;
int imag;
void print() {
System.out.println(real+"+"+imag+"i");
}
}
public class ch4_4 {
public static void main(String[] args) {
ComplexNumber n = new ComplexNumber();
n.real = 10;
n.imag = 20;
n.print();
}
}
5.
class Account {
int balance;
void withdraw(int amount) {
balance -= amount;
System.out.println(amount+"원 출금");
}
void deposit(int amount) {
balance += amount;
System.out.println(amount+"원 저축");
}
int getAccount() {
return balance;
}
}
public class ch4_5 {
public static void main(String[] args) {
Account a1 = new Account();
System.out.println("새로운 계좌가 만들어졌습니다.");
a1.deposit(50000);
Account a2 = new Account();
System.out.println("새로운 계좌가 만들어졌습니다.");
a2.deposit(100000);
System.out.println("고객 #1 계좌 잔고="+a1.getAccount());
System.out.println("고객 #2 계좌 잔고="+a2.getAccount());
}
}
728x90
'Major > Java' 카테고리의 다른 글
[Power Java Compact] 6장 연습문제 (0) | 2021.12.29 |
---|---|
[Power Java Compact] 5장 연습문제 (0) | 2021.12.22 |
[Java] 문자열 안에서 단어찾기 (0) | 2021.12.20 |
[Power Java Compact] 3장 연습문제 (0) | 2021.12.15 |
[Power Java Compact] 2장 연습문제 (2) | 2021.12.13 |