본문 바로가기

프로그래밍/코드분석

(25)
자바의 정석 연습문제 7-22 아래는 도형을 정의한 Shape클래스이다. 이 클래스를 조상으로 하는 Circle클래스와 Rectangle클래스를 작성하시오. 이 때, 생성자도 각 클래스에 맞게 적절히 추가해야 한다.(1) 클래스명 : Circle조상클래스 : Shape멤버변수 : double r - 반지름(2) 클래스명 : Rectangle조상클래스 : Shape멤버변수 : double width - 폭, double height - 높이메서드 :1. 메서드명 : isSquare기 능 : 정사각형인지 아닌지를 알려준다.반환타입 : boolean매개변수 : 없음 [ 문제 ] abstract class Shape { Point p; Shape() { this(new Point(0,0)); } Shape(Point p) { this.p =..
자바의 정석 연습문제 6-23 [ 문 제 ] max 메서드 작성기능 : 주어진 int형 배열의 값 중 가장 큰 값을 반환.만일 주어진 배열이 null이거나 크기가 0이면, -999999를 반환.반환타입 : int 매개변수 : int[] arr - 최대값을 구할 배열 class Exercise6_23 { /* (1) max메서드를 작성하시오. */ public static void main(String[] args) { int[] data = {3,2,9,4,7}; System.out.println(java.util.Arrays.toString(data)); System.out.println("최대값:"+max(data)); System.out.println("최대값:"+max(null)); System.out.println("최대값:..
자바의 정석 연습문제 6-4 [ 문 제 ]class exercise { public static void main(String args[]) { Student s = new Student(); s.name = "홍길동"; s.ban = 1; s.no = 1; s.kor = 100; s.eng = 60; s.math = 76; System.out.println("이름 : " + s.name); System.out.println("총점 : " + s.getTotal()); System.out.println("평균 : " + s.getAverage()); } } class Student{ String name; int ban; int no; int kor; int eng; int math; } 출력값 이름 : 홍길동총점 : 236평균 :..
자바의 정석 연습문제 6-2 [ 문 제 ]class Exercise6_2 { public static void main(String args[]) { SutdaCard card1 = new SutdaCard(3, false); SutdaCard card2 = new SutdaCard(); System.out.println(card1.info()); System.out.println(card2.info()); } } class SutdaCard { /* (1) 알맞은 코드를 넣어 완성하시오. */ } 출력값 31K [ 풀 이 ] class exercise { public static void main(String args[]) { SutdaCard card1 = new SutdaCard(3, false); SutdaCard card2..
자바의 정석 연습문제 9-4 [문제]class Exercise9_4 { static void printGraph(int[] dataArr, char ch) { /* (1) printGraph메서드를 작성하시오. */ } public static void main(String[] args) { printGraph(new int[]{3,7,1,4},'*'); } } [정답] class exercise { static void printGraph(int[] dataArr, char ch) { for( int i = 0; i < dataArr.length; i++) { for (int j = 0 ; j
자바의 정석 연습문제 9-3 [문제] class Exercise9_3 { public static void main(String[] args) { String fullPath = "c:\\jdk1.5\\work\\PathSeparateTest.java"; String path = ""; String fileName = ""; /* (1) 알맞은 코드를 넣어 완성하시오. */ System.out.println("fullPath:"+fullPath); System.out.println("path:"+path); System.out.println("fileName:"+fileName); } } [나의 코드]int index = fullPath.indexOf("work"); path = fullPath.substring(0, index+4..
자바의 정석 연습문제 9-1 class SutdaCard { int num; boolean isKwang; SutdaCard() { this(1, true); } SutdaCard(int num, boolean isKwang) { this.num = num; this.isKwang = isKwang; } public boolean equals(Object obj) { /* (1) 매개변수로 넘겨진 객체의 num, isKwang과 멤버변수 num, isKwang을 비교하도록 오버라이딩 하시오. */ } public String toString() { return num + ( isKwang ? "K":""); } } [실행결과] c1 = 3Kc2 = 3Kc1.equals(c2) : true 위 처럼 나와야 함. class SutdaC..
자바의 정석 9-20 코드분석 public class StringReplace { private String source =""; private int length; private StringBuffer buffer; public StringReplace(String source) { this.source = source; // 전달받는 문자열로 초기화 length = source.length(); // source의 길이로 초기화 buffer = new StringBuffer(length + 100); // 버퍼(저장공간)의 크기는 length+100으로 } public int length() { return length; } public String replace(String old, String nw) { return repla..