본문 바로가기

전체 글

(23)
명품 자바 프로그래밍 6장 실습 문제 1. 다음 main() 실행 시 예시와 같이 출력되도록 MyPoint클래스를 작성하라. class MyPoint { // 지역변수 int x; int y; // 생성자 public MyPoint(int x,int y) { this.x = x; this.y = y; } // toString() public String toString() { return "Point(" + x + "," + y +")"; } } public class Main { public static void main(String[] args) { MyPoint p = new MyPoint(3, 50); MyPoint q = new MyPoint(4, 50); System.out.println(p); if (p.equals(q)) Sy..
명품 자바 프로그래밍 6장 오픈챌린지 영문자 히스토그램 만들기 import java.util.*; class Alphabet { char c; int count; // 어느 객체가 생성자 매개변수로 알파벳 'A~Z'를 받으면, // 그 객체는 입력받은 알파벳의 갯수를 count할 예정 public Alphabet(char c) { this.c = c; this.count = 0; } // AlphaCount(str)메소드를 사용하면 str문자열을 뒤져보면서 // 자신이 할당받았던 알파벳과 일치할 경우 count++ void AlphaCount(String str) { for (int i = 0; i
명품 자바 프로그래밍 5장 실습문제 1. 다음 main()메소드와 실행 결과를 참고하여 TV를 상속받은 ColorTV클래스를 작성하라. package ch5; class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { int colors; public ColorTV(int size, int colors) { super(size);// 입력받은 매개변수 중 size는 슈퍼클래스의 생성자에 대입됨. this.colors = colors;// colors는 ColorTV에서 새로 만들어진 멤버colors에 대입. } void printProperty() ..
명품 자바 프로그래밍 4장 실습문제 1. 자바 클래스 작성하는 연습을 해보자. 다음 main() 메소드를 실행했을 때 예시와 같이 출력되도록 TV 클래스를 작성하라. import java.util.*; class TV { String brand; int year, inches; public TV(String brand, int year, int inches) { this.brand = brand; this.year = year; this.inches = inches; } public void show() { System.out.print(brand + "에서 만든 " + year + "년형 " + inches + "인치 TV"); } } public class CH4 { Scanner s = new Scanner(System.in); pu..
명품 자바 프로그래밍 4장 오픈챌린지 끝말잇기 게임 만들기 import java.util.*; public class WordGameApp { public static Scanner s = new Scanner(System.in); // 생성자 WordGameApp() public WordGameApp() {} public static void run() { System.out.print("게임에 참가하는 인원은 몇명입니까 >> "); int players = s.nextInt(); // players수만큼 객체배열 생성 및 객체마다 이름 입력해서 할당해줌. Player p [] = new Player[players]; for (int i = 0; i true를 리턴, 아니라면 => false 리턴 if (lastChar == firstChar) return t..
명품자바프로그래밍 3장 실습문제 1. 다음 프로그램에 대해 물음에 답하라. int sum=0; i=0; while ( i < 100 ) { sum = sum + i; i += 2; } System.out.println(sum); (1) 무엇을 계산하는 코드이며 실행 결과 출력되는 내용은? 0 ~ 99의 숫자들 중 짝수들만 더해서 sum에 값을 저장함. (2) 위의 코드를 main()메서드로 만들고 WhileTest클래스로 완성하라. public class WhileTest { public static void main(String[] args) { int sum=0; i=0; while ( i < 100 ) { sum = sum + i; i += 2; } System.out.println(sum); } } (3) for문을 이용하여 동..
명품 자바 프로그래밍 159P 3장 오픈챌린지 카드 번호 맞추기 게임 import java.util.Scanner; import java.util.Random; public class Hello { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 정답을 맞춰서 내부의 중첩 while문에서 빠져나온 뒤 n을 입력하면 이 while문이 종료됨. while (true) { int count = 1; int lower = 0; int upper = 99; Random r = new Random(); int RandNum = r.nextInt(100);// 0~99 랜덤정수 생성 : RandNum System.out.println("수를 결정하였습니다. 맞추어 보세요."); w..
명품 자바 프로그래밍 2장 실습문제 1. 원화 입력받아 달러로 바꾸어 출력해보기. 1달러는 1100원으로 가정. import java.util.Scanner; public class Hello { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("원화를 입력하세요(단위 원)>> "); int won = scanner.nextInt(); double dollar = won/1100; System.out.print(won + "원은 $" + dollar + "입니다."); scanner.close(); } } 2. 2자리 정수를 입력받고, 십의자리와 1의 자리가 같은지 판별하여 출력하는 프로그램 작성. import..