250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- React
- Java
- 데이터베이스
- 소셜로그인구현
- 국비지원코딩
- 자바개발자
- 운영체제
- Codeup
- 미라클모닝
- 자바의정석
- 백엔드
- SpringBoot
- 백엔드개발자
- 개발자
- 프로세스
- db
- 프로그래밍
- 스프링부트
- 자바
- 자바스크립트
- 프로그래머
- 코딩
- 자바알고리즘
- 프로그래머스
- 리액트
- 국비지원
- 코드업
- 혼공컴운
- 알고리즘
- 개발자일기
Archives
- Today
- Total
초코딩(chocoding)
[Java] 자바의 정석 / chap7.객체지향 프로그래밍_연습문제 본문
728x90
7-1)
package example01;
class SutdaDeck {
final int CARD_NUM = 20;
SutdaCard[] cards = new SutdaCard[CARD_NUM];
SutdaDeck() {
for (int i = 0; i < cards.length; i++) {
int num = i % 10 + 1;
boolean isKwang = (i < 10) && (num == 1 || num == 3 || num == 8);
cards[i] = new SutdaCard(num, isKwang);
}
}
}
class SutdaCard {
int num;
boolean isKwang;
SutdaCard() {
this(1, true);
}
SutdaCard(int num, boolean isKwang) {
this.num = num;
this.isKwang = isKwang;
}
public String toString() {
return num + (isKwang ? "K" : "");
}
}
public class Ex05_Chapter7 {
public static void main(String[] args) {
SutdaDeck deck = new SutdaDeck();
for (int i = 0; i < deck.cards.length; i++) {
System.out.print(deck.cards[i] + ",");
}
}
}
7-2)
package example01;
class SutdaDeck {
final int CARD_NUM = 20;
SutdaCard[] cards = new SutdaCard[CARD_NUM];
SutdaDeck() {
for (int i = 0; i < cards.length; i++) {
int num = i % 10 + 1;
boolean isKwang = (i < 10) && (num == 1 || num == 3 || num == 8);
cards[i] = new SutdaCard(num, isKwang);
}
}
public void shuffle() {
for (int i = 0; i < cards.length; i++) {
int rNum = (int)(Math.random() * cards.length);
SutdaCard temp = cards[i];
cards[i] = cards[rNum];
cards[rNum] = temp;
}
}//shuffle()
public SutdaCard pick(int index) {
if (index < 0 || index >= CARD_NUM) {
return null;
}
return cards[index];
}//pick(int index)
public SutdaCard pick() {
int rNum = (int)(Math.random() * cards.length);
return cards[rNum];
//return pick(rNum); //pick(int index) 호출도 가능
}//pick()
}//Class SutdaDeck
class SutdaCard {
int num;
boolean isKwang;
SutdaCard() {
this(1, true);
}
SutdaCard(int num, boolean isKwang) {
this.num = num;
this.isKwang = isKwang;
}
public String toString() {
return num + (isKwang ? "K" : "");
}
}//Class SutdaCard
public class Ex05_Chapter7 {
public static void main(String[] args) {
SutdaDeck deck = new SutdaDeck();
System.out.println(deck.pick(0));
System.out.println(deck.pick());
deck.shuffle();
for (int i = 0; i < deck.cards.length; i++) {
System.out.print(deck.cards[i] + ",") ;
}
System.out.println();
System.out.println(deck.pick(0));
}
}
7-3)
package example01;
class Product {
int price;
int bonusPoint;
//기본 생성자
Product() {}
Product(int price) {
this.price = price;
bonusPoint = (int)(price / 10.0);
}
}//class Product
class Tv extends Product {
//기본 생성자
Tv() {}
//= Tv() {super();} => super() 호출
public String toString() {
return "Tv";
}
}//class Tv
public class Ex05_Chapter7 {
public static void main(String[] args) {
Tv t = new Tv();
}
}
7-4)
package example01;
class MyTv {
boolean isPowerOn;
int channel;
int volume;
final int MAX_VOLUME = 100;
final int MIN_VOLUME = 0;
final int MAX_CHANNEL = 100;
final int MIN_CHANNEL = 1;
public void setChannel(int channel) {
if (channel > MAX_CHANNEL || channel < MIN_CHANNEL) {
return;
}
this.channel = channel;
}
public int getChannel() {
return channel;
}
public void setVolume(int volume) {
if (volume > MAX_VOLUME || volume < MIN_VOLUME) {
return;
}
this.volume = volume;
}
public int getVolume() {
return volume;
}
}
public class Ex05_Chapter7 {
public static void main(String[] args) {
MyTv t = new MyTv();
t.setChannel(10);
System.out.println("CH: " + t.getChannel());
t.setVolume(20);
System.out.println("VOL: " + t.getVolume());
}
}
7-5)
package example01;
class MyTv2 {
private boolean isPowerOn;
private int channel;
private int volume;
private int preChannel;
final int MAX_VOLUME = 100;
final int MIN_VOLUME = 0;
final int MAX_CHANNEL = 100;
final int MIN_CHANNEL = 1;
public void setChannel(int channel) {
if (channel > MAX_CHANNEL || channel < MIN_CHANNEL) {
return;
}
preChannel = this.channel;
this.channel = channel;
}
public int getChannel() {
return channel;
}
public void setVolume(int volume) {
if (volume > MAX_VOLUME || volume < MIN_VOLUME) {
return;
}
this.volume = volume;
}
public int getVolume() {
return volume;
}
public void gotoPreChannel() {
setChannel(preChannel);
}
}
public class Ex05_Chapter7 {
public static void main(String[] args) {
MyTv2 t = new MyTv2();
t.setChannel(10);
System.out.println("CH: " + t.getChannel());
t.setChannel(20);
System.out.println("CH: " + t.getChannel());
t.gotoPreChannel();
System.out.println("CH: " + t.getChannel());
t.gotoPreChannel();
System.out.println("CH: " + t.getChannel());
}
}
7-6)
package j10_Exception;
class Outer {
class Inner {
int iv = 100;
}
}
public class Ex05_Chapter7 {
public static void main(String[] args) {
Outer o = new Outer();
Outer.Inner i = o.new Inner();
System.out.println(i.iv);
}
}
7-7)
package j10_Exception;
class Outer {
static class Inner {
int iv = 200;
}
}
public class Ex05_Chapter7 {
public static void main(String[] args) {
Outer.Inner si = new Outer.Inner();
System.out.println(si.iv);
}
}
7-8)
package j10_Exception;
class Outer {
int value = 10;
class Inner {
int value = 20;
void method1() {
int value = 30;
System.out.println(value);
System.out.println(this.value);
System.out.println(Outer.this.value);
}
}
}
public class Ex05_Chapter7 {
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.method1();
}
}
7-9)
package j10_Exception;
import java.awt.*;
import java.awt.event.*;
public class Ex05_Chapter7 {
public static void main(String[] args) {
Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().setVisible(false);
e.getWindow().dispose();
System.exit(0);
}
});
}
}
.......어제 저녁에 풀고
아침에.......... 이어서 풀었는데
왤케 어려워...?
눈물날 거 같아............................................
728x90