1. 다음 중 자바 식별자가 아닌 것을 고르시오.KNU** Source_ 981int 007code Label booleanKun001 $dollar (007)TTL %_0012 Speed⇒ KNU** , Source_ , 981int, 007code, boolean, (007)TTL , %)00122. 다음 중 예약어, 식별자로 가능한 이름, 식별자로 가능하지 않은 이름을 분류하시오.new private Bill programThisIsALongOne Y=ZProg#2 ‘MaxScores’Sue’s Rate Strat extendsstatic XYZ_123 123_XYZ⇒ 예약어 ? new, private, static, extends식별자로 가능한 이름 ? Bill, program, ThisIsALongOne, Rate, Strat XYZ_123식별자로 가능하지 않은 이름 ? Y=Z, Prog#2, ‘MaxScores’, Sue’s 123_XYZ3. 실습 4.8과 다음 부분 프로그램을 참조하여 enum Month {January, February, March, April, May, June, July, August, September, October, December}를 선언하고 달을 나타내는 변수를 스트링으로 입력받아 열거형 변수로 할당한 후 입력받은 달이 봄(March, April, May)인지를 확인하는 프로그램을 작성하시오.Scanner sc = new Scanner(System.in);String Str1;System.out.println(“! 과일을 입력하시오:orange, apple, pear, mellon, bananna !”);str1=sc.next();if (str1.equals(“orange”)) aFruit = Fruit.orange;⇒ import java.util.*;public class EnumerMonth {enum Month {January, February, March, April, May, June, July,August, September, Ovtober, December};public static void main(String[] args) {Month aMonth, bMonth, cMonth, dMonth ;aMonth = Month.March;bMonth = Month.April;cMonth = Month.May;Month eMonth = Month.March;Scanner sc = new Scanner(System.in);String Str1;System.out.println("! 달을 입력하시오:January, February, March, April, May, June, July, August, September, Ovtober, December !");Str1=sc.next();if (Str1.equals("Month")) aMonth = Month.March;if (aMonth.equals(Month.March)) System.out.println(" March : Yes, "+ aMonth);if (bMonth.equals(Month.April)) System.out.println(" April : Yes, "+ bMonth);if (cMonth.equals(Month.May)) System.out.println(" May : Yes, "+ cMonth);if (eMonth == aMonth) System.out.println(" The Spring !!! ");}}4. 다음 문자의 출력을 확인하시오.(int) (‘A’)’101’(int) (‘a)’⇒ public class java {public static void main(String args[]) {System.out.println("(int) ('A')n'101'n(int) ('a)'");}}
1.객체의 선언과 생성을 구분하여 설명하시오.1)객체의 선언⇒ Rectangle arect; 이와 같이 변수를 선언한다. 여기서 변수 arect는 Rectangle 클래스의 어떤 객체를 나타내는 참조 변수이다. 사용자가 정의한 클래스를 나타내는 참조변수는 해당 클래스의 객체를 가리키는 포인터 역할을 한다. 또한 위와 같은 선언만으로 자바에서는 Rectangle 클래스의 객체를 참조하는 변수를 생성할 뿐이지 Rectangle 객체를 생성하는 것은 아니다.2)객체의 생성⇒클래스 선언이나 객체의 선언이 해당 객체를 생성하는 것은 아니다. 객체 변수(참조 변수)를 선언한 후 이 변수(객체)를 사용하기 위해서는 객체를 생성하여 이 변수가 생성된 객체를 가리키도록 하여야 한다. 객체를 생성하는 방법은 new와 생성자를 이용하여 객체를 생성하는데 이를 초기화라 한다. 여기서 생성자는 객체를 생성하고 초기화 하는데 만 사용되는 특별한 종류의 메소드이다. 생성자 메소드는 클래스와 같은 이름을 가진다.2.main() 메소드가 왜 static이어야 하는지 설명하시오.⇒메소드를 static으로 선언하게 되면 해당 메소드는 인스턴스 생성없이 접근(호출)이 가능한데 메소드를 static로 선언하지 않으면 메소드를 호출하기 전에 인스턴스를 생성하여야 하는 문제가 생기므로 main() 메소드는 static으로 선언 하여야 한다.4.실습 3.3의 CircleAp1.java에서 모든 생성자를 다 사용하는 프로그램을 작성하시오.⇒import java.io.*;import java.util.*;import java.lang.*;class Circle {final static double pi = 3.14;private int x;private int y;private double radius;public Circle( ) {this.x = 0;this.y = 0;radius = 1.0;}public Circle(int x, int y) {this.x = x;this.y = y;radius = 1.0;}public Circle(int x, int y, double r) {this.x = x;this.y = y;radius = r;}public Circle(int x, int y, int a, int b) {this.x = x;this.y = y;radius = Math.sqrt((a-this.x)*(a-this.x) +(b-this.y)*(b-this.y));}public double getArea() {double result;result = pi * radius * radius;return result;}public double getCircum() {double result;result = 2.0 * pi * radius;return result;}}public class CircleAp1 {public static void main(String args[]) {int x, y, a, b;double r;Scanner sc = new Scanner(System.in);System.out.println();System.out.println("! Circle !");System.out.println();System.out.println("원 면적 구하기");System.out.println();System.out.println("원 중심의 x 좌표를 입력하시오: "); System.out.flush();x = sc.nextInt();System.out.println("원 중심의 y 좌표를 입력하시오: "); System.out.flush();y = sc.nextInt();System.out.println("원의 반지름 r 을 입력하시오: "); System.out.flush();r = sc.nextDouble();System.out.println("원 중심의 a 값을 입력하시오: "); System.out.flush();a = sc.nextInt();System.out.println("원 중심의 b 값을 입력하시오: "); System.out.flush();b = sc.nextInt();Circle acir = new Circle(x, y, a, b);System.out.println("원의 넓이: " + acir.getArea());System.out.println("원의 둘레: " + acir.getCircum());}}5.static 메소드에서의 this를 이용한 참조를 시도할 경우 에러가 발생하는 이유를 설명하시오.⇒this는 인스턴스가 생성 되었을 때 해당 인스턴스를 기리키는 참조 변수인데static은 모든 인스턴스에서 공유하기?때문에 각?인스턴스별로 구분할 수 없기 때문에 에러가 발생한다.12.실습 3.1에서 사각형의 높이(세로)와 너비(가로)를 서로 바꾸는 프로그램을 call-by reference를 사용하여 작성하시오(매개 변수로 객체를 사용할 것.)⇒import java.io.*;import java.util.*;class Rectangle {int width;int height;public Rectangle(int a, int b) {width = a;height = b;}public void hwswap(Rectangle rect) {int temp;temp = rect.height;rect.height= rect.width;rect.width = temp;}public int getArea() {int result;result = width * height;return result;}}public class Rect50 {public static void main(String args[]) {int height1, width1;Scanner sc;sc = new Scanner(System.in);System.out.println();System.out.println("높이를 입력하시오: "); System.out.flush();height1 = sc.nextInt();System.out.println("너비를 입력하시오: "); System.out.flush();width1 = sc.nextInt();Rectangle arect = new Rectangle(width1, height1);System.out.println(" 변경 전 높이: " + arect.height + ", 너비: " + arect.width);arect.hwswap(arect);System.out.println(" 변경 후 높이: " + arect.height + ", 너비: " + arect.width);}}