본문 바로가기

JAVA16

클래스와 객체 활용 예제 끝말잇기 게임N명이 참가하는 끝말잇기 게임을 만들어보자. 처음 단어는 "아버지"이다. N명의 참가자들은 순서대로 자신의 단어를 입력하면 된다. 끝말잇기에서 끝말이 틀린 경우 게임을 끝내고 게임에서 진 참가자를 화면에 출력한다. 프로그램에서는 시간 지연을 구현하지 않아도 된다. 그렇지만 참가자들이 스스로 시간을 재어 보는것도 좋겠다. 이 문제의 핵심은 여러 개의 객체와 배열 사용을 연습하기 위한 것으로, main()을 포함하는 WordGameApp 클래스와 각 선수를 나타내는 Player 클래스를 작성하고, 실행 중에는 WordGameApp 객체 하나와 선수 숫자만큼의 Player 객체를 생성하는데 있다.class Player { Player(int num){ } public void getWordFr.. 2024. 8. 16.
@Override 개념메소드 오버라이딩(method overriding), 즉 오버라이딩은 슈퍼클래스와 서브 클래스의 메소드 사이에서 같은 이름, 같은 반환형, 같은 매개변수 리스트를 갖는 메소드를 서브클래스에서 재작성하는 것이다. 예를 들어 슈퍼클래스에서의 draw() 함수와 서브클래스에서의 draw()함수가 존재할때, 서브클래스 객체로 선언되어진 객체에서 draw()함수를 호출하게되면 반드시 서브클래스의 draw()함수가 실행된다는 것이다.즉, 메소드 오버라이딩은 '슈퍼 클래스 메소드 무시하기 혹은 덮어쓰기'로 이해할 수 있다. 이는 슈퍼 클래스의 메소드를 무시하고 서브 클래스에서 오버라이딩된 메소드가 무조건 실행되도록 한다는 것인데, 이런 처리를 동적 바인딩이라고 부르며, 메소드 오버라이딩은 동적 바인딩을 유발시킨다.. 2024. 8. 15.
상속 class Point{ private int x,y; public void set(int x, int y) { this.x=x; this.y=y; } public void showPoint() { System.out.println("(" + x + "," + y + ")"); }}class ColorPoint extends Point{ private String color; public void setColor(String color) { this.color=color; } public void showColorPoint() { System.out.print(color); showPoint(); }}public class ColorPointEx { public static void main(S.. 2024. 8. 14.
Chapter 03 실습문제 1번1) 4252)public class ex3_1 { public static void main(String[] args) { int sum=0,i=1; while(true) { if(i>50) { break; } sum+=i; i+=3; } System.out.print(sum); }} 3) public class ex3_1 { public static void main(String[] args) { int sum=0,i=1; for(i=1;i4)public class ex3_1 { public static void main(String[] args) { int sum=0,i=1; do { sum+=i; i+=3; }while(i2번1) 20 72 2562).. 2024. 8. 13.
비정형 배열 public class SkewedArray2 { public static void main(String[] args) { int Array[][]=new int[4][]; Array[0]=new int[4]; Array[1]=new int[1]; Array[2]=new int[1]; Array[3]=new int[4]; int k=0; for(int i=0;i 2024. 8. 13.
생성자 public class Book { String title; String author; void show() { System.out.println(title+" "+ author); } public Book() { this("",""); System.out.println("생성자 호출됨"); } public Book(String title) { this(title,"작자 미상"); } public Book(String title, String author) { this.title=title; this.author=author; } public static void main(String[] args) { Book littlePrince=new Book("어린 왕자","생택쥐페리"); .. 2024. 8. 13.