패스트캠퍼스 데브캠프 : 남궁성의 백엔드 개발 3기

자바의 정석 챕터6, 7 연습문제 회고 - 패스트캠퍼스 백엔드 부트캠프 3기

Tech_JINI 2025. 1. 10. 14:58

피드백 내용

문제 6-19 메모리 실행 과정 그림 그려보기

문제 7-11 배열 대신 변수 활용해보기

문제 7-21 인터페이스 개념에 대해 복습하기

+ 못 풀었던 문제 다시 풀기


피드백 후 복습한 내용

[내가 작성한 답]

ABC123

ABC123456

 

[메모리 실행 과정 그려보기]

 

 

실행과정에 따른 메모리구조 변화를 그려봤습니다.

틀린 부분이 있다면 댓글달아주세요. 수정하겠습니다!!

 

[다시 작성한 답]

ABC123

ABC123

 


[내가 작성한 답]

super(), 조상 클래스 멤버의 초기화 작업이 수행되어야 하기 때문에 자손 클래스의 생성자에서 조상 클래스의 생성자가 호출되어야 한다. 

 

[강사님께서 추가로 말씀해주신 내용]

자손이 조상 iv(인스턴스 변수)를 사용할 수 없기 때문에 조상 iv를 먼저 초기화시키는 것이다. 

 


배열 대신 변수 활용해서 다시 풀어보라는 피드백을 받았다!

 

[기존의 내가 작성한 답(코드)]

class MyTv2 {
	
	private boolean isPowerOn;
	private int channel;
	private int volume;
	
	final int MAX_VOLUME = 100;
	final int MIN_VOLUME = 0;
	final int MAX_CHANNEL = 100;
	final int MIN_CHANNEL = 1;
	
	public int getChannel() {return channel;}
	public void setChannel(int channel) {
		prev_channel[channel_num]=channel;
		
		this.channel=channel;
		channel_num++;
	}
	public int getVolume() {return volume;}
	public void setVolume(int volume) { this.volume=volume;}
	
	private int[] prev_channel = new int[3];
	int prev_num;
	int channel_num;
	
	
	
	void gotoPrevChannel() {
		channel = prev_channel[prev_num];
		prev_num++;
	}
	
}

public class Exercise7_11 {

	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.gotoPrevChannel();
		System.out.println("CH: "+t.getChannel());   
		t.gotoPrevChannel();
		System.out.println("CH: "+t.getChannel());   

		
	}

}

 

 

 

[배열 대신 변수를 활용해서 다시 작성한 답(코드)]

class MyTv2 {
	
	private boolean isPowerOn;
	private int channel;
	private int volume;
	
	final int MAX_VOLUME = 100;
	final int MIN_VOLUME = 0;
	final int MAX_CHANNEL = 100;
	final int MIN_CHANNEL = 1;
	
	public int getChannel() {return channel;}
	public void setChannel(int channel) {
		
		if(this.channel!=0)prev_channel=this.channel;
		this.channel=channel;
		
	}
//	public int getVolume() {return volume;}
//	public void setVolume(int volume) { this.volume=volume;}
	
	private int prev_channel;
	
	int channel_num;
	
	
	void gotoPrevChannel() {
		// 이 메서드가 호출되면 채널의 이전의 값 호출.. 
		// 근데 채널은 int 이기때문에 이전의값을 저장하지 못한다. 
		// gotoprev는 반환타입이 없고 아래 main을 보면 get채널을 이용해서 값을 가져온다.
		// 20번으로 채널을 바꿀 때 10을 저장해야한다.
		// 10을 다시 호출할 때 20을 호출해야한다. 10이 호출되었기 때문에 20은 과거 값이다.
		
		setChannel(prev_channel);
		
	}
	
}

public class Exercise7_11 {

	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.gotoPrevChannel();
		System.out.println("CH: "+t.getChannel());   
		t.gotoPrevChannel();
		System.out.println("CH: "+t.getChannel());   
		
	}

}

 

확실히 코드가 깔끔해졌다.

하지만, 로직을 생각해내는데 꽤나 애먹었다..

// 이 메서드가 호출되면 채널의 이전의 값 호출.. 
// 근데 채널은 int 이기때문에 이전의값을 저장하지 못한다. 
// gotoprev는 반환타입이 없고 아래 main을 보면 get채널을 이용해서 값을 가져온다.
// 20번으로 채널을 바꿀 때 10을 저장해야한다.
// 10을 다시 호출할 때 20을 호출해야한다. 10이 호출되었기 때문에 20은 과거 값이다.

코드 블럭 내에서도 있지만 나의 생각 흐름은 이러했다.

 

그림까지 그려가며 1시간 정도 고민했는데 setChannel(prev_channel) 한 방에 풀리는 문제였다......

 


[답] Movable 객체, null

--> 인터페이스 내용 정리에 대한 글을 따로 작성하였다.

 


[내가 작성한 코드 내용]

//7-22
abstract class Shape {
	Point p;

	Shape(){
		this(new Point(0,0));
	}
	Shape(Point p){
		this.p = p;
	}

	abstract double calcArea();

	Point getPosition() {
		return p;
	}

	void setPosition(Point p) {
		this.p = p;
	}
}


class Point {
	int x;
	int y;

	Point(){
		this(0,0);
	}
	Point(int x, int y){
		this.x=x;
		this.y=y;
	}

	public String toString() {
		return "["+x+","+y+"]";
	}
}


class Circle extends Shape {

	double r;

	
	Circle(int r) {
		this.r=Math.abs(r);
	}

	double calcArea() {
		return r*r*Math.PI;
	}

}

class Rectangle extends Shape {
	double width;
	double height;


	Rectangle(double width, double height) {
		this.width =  width;
		this.height=  height;

	}
	double calcArea() {
		return width*height;
	}
	boolean isSquare() {
		if (width==height) return true;
		return false;
	}
}



public class Exercise7_22{

	public static void main(String[] args) {

		Circle c = new Circle(3);
		System.out.println(c.calcArea());
		
		Rectangle r = new Rectangle(3, 4);
		System.out.println(r.calcArea());
		System.out.println(r.isSquare());
		Rectangle t = new Rectangle(3, 3);
		System.out.println(t.calcArea());
		System.out.println(t.isSquare());


	}
}

 


[내가 작성한 코드 내용]

abstract class Shape {
	Point p;

	Shape(){
		this(new Point(0,0));
	}
	Shape(Point p){
		this.p = p;
	}

	abstract double calcArea();

	Point getPosition() {
		return p;
	}

	void setPosition(Point p) {
		this.p = p;
	}
}


class Point {
	int x;
	int y;

	Point(){
		this(0,0);
	}
	Point(int x, int y){
		this.x=x;
		this.y=y;
	}

	public String toString() {
		return "["+x+","+y+"]";
	}
}


class Circle extends Shape {

	double r;


	Circle(int r) {
		this.r=Math.abs(r);
	}
	Circle(double r) {
		this.r=Math.abs(r);
	}

	double calcArea() {
		return r*r*Math.PI;
	}

}

class Rectangle extends Shape {
	double width;
	double height;


	Rectangle(double width, double height) {
		this.width =  width;
		this.height=  height;

	}
	double calcArea() {
		return width*height;
	}
	boolean isSquare() {
		if (width==height) return true;
		return false;
	}
}



public class Exercise7_23 {

	static double sumArea(Shape[] arr) {
		double sum = 0.0;
		for(int i=0; i<arr.length; i++) {
			sum+=arr[i].calcArea();
		}
		return sum;
	}

	public static void main(String[] args) {

		Shape[] arr = {new Circle(5.0), new Rectangle(3, 4), new Circle(1)};
		System.out.println(sumArea(arr));


	}
}

 

Circle 생성자의 매개변수 타입을 int형과 double형으로 각각 만들었더니 에러가 수정되었다.