Chapter 9 Lab 문제정답 1. public class Circle private double radius; static final double PI=3.141592; // PI 이름으로 3.141592 로초기화된정적상수 (1) public Circle(double r) radius = r; (2) public double getradius() return radius; public void setradius(double radius) (3) this.radius = radius; private double square(double value) return value*value; (4) public double getarea() (5) return square(radius)*pi; public double getperimeter() (6) return 2.0*PI*radius; public static double getpi() return PI; (7) square() 함수는정적함수가아니라서 main() 에서호출하면오류가발생한다.
(8) getpi() 함수는정적함수이므로 main() 에서호출할수있다. (9) class Circle private double radius; static final double PI=3.141592; // PI 이름으로 3.141592 로초기화된정적상수 public Circle(double r) radius = r; public double getradius() return radius; public void setradius(double radius) this.radius = radius; private double square(double value) return value*value; public double getarea() return square(radius)*pi; public double getperimeter() return 2.0*PI*radius; public static double getpi() return PI; public class CircleTest public static void main(string args[]) Circle c = new Circle(5.0); // 객체생성, 생성자호출시반지름을 5.0 으로설정 System.out.println(c.getArea());// 원의면적계산하여출력 System.out.println(c.getPerimeter());// 원의둘레계산하여출력
Exercise 1. (1) 객체가생성될때에필드에게초기값을제공하고필요한초기화절차를실행 (2) 매개변수의자료형이나매개변수개수로구별되어호출 (3) 자기자신을참조 (4) 정적변수는하나의클래스에하나만존재하여그클래스의모든객체들에의해공유되지 만인스턴스변수는각인스턴스마다별도로생성된다. (5) 객체의참조값이전달된다. (6) 정적메소드는객체가생성되지않은상태에서호출되는메소드이므로객체안에서존재 하는인스턴스변수들은사용할수없다. 2. (1) 생성자 Point() 는값을반환하지않는다따라서 void를삭제한다. (2) 메소드의반환형이다르다고해서메소드를중복시킬수있는것은아니다. class MyMath public int getrandom1() return (int)math.random(); public double getrandom() return Math.random(); (3) 정적메소드 getstringname() 에서인스턴스메소드 getname() 을호출할수없다. class MyClass private static String getname() return "Myclass"; public static String getclassname() return getname(); 3. (1) public class Cube private double side; // 정육면체의한변 public Cube() side = 0;
public double getside() return side; public double getvolume() return side*side*side; (2) public class Cube private double side; // 정육면체의한변 public Cube() side = 0; public Cube(double side) this.side = side; public double getside() return side; public double getvolume() return side*side*side; 4. class MyMetric private static double distance; public static double kilotomile(double d) distance = d / 1.6093; return distance; public static void miletokilo(double d) distance = d * 1.6093; public class MyMetricTest public static void main(string args[]) double d = MyMetric.kiloToMile(1); System.out.println(d);
5. s_instance 가 null 일때만객체를생성하고이미객체가생성되어있으면단순히객체의참 조값을반환한다. class Single private static Single s_instance; public static Single getinstance() if (s_instance == null) s_instance = new Single(); return s_instance;
Programming 2. class Plane private int num, p_num; private String model; private static int planes; public void setnum(int n) num = n; public void setpnum(int pn) p_num = pn; public void setmodel(string m) model = m; public int getnum() return num; public int getpnum() return p_num; public String getmodel() return model; public static void setplanes(int p) planes = p; public static int getplanes() return planes; public Plane() public Plane(int n, String m, int pn) num = n; p_num = pn; model = m;
public Plane(int n, String m) num = n; model = m; public String tostring() return " 식별번호 :" + getnum() + " 모델 : " + getmodel() + " 승객수 : " + getpnum(); public class PlaneTest public static void main(string[] args) Plane plane1 = new Plane(1, "aa", 200); Plane plane2 = new Plane(2, "bb"); Plane plane3 = new Plane(); plane1.setplanes(0); plane1.getplanes(); plane3.setnum(3); plane3.setmodel("cc"); plane3.setpnum(150);
3. public class Box private int width, length, height; private boolean empty = false; public int getwidth() return width; public void setwidth(int width) this.width = width; public int getlength() return length; public void setlength(int length) this.length = length; public int getheight() return height; public void setheight(int height) this.height = height; public boolean isempty() return empty; public void setempty(boolean empty) this.empty = empty;
public Box() width = 0; length = 0; height = 0; empty = true; public Box(int w, int l, int h) width = w; length = l; height = h; empty = true; 4. public class Movie private String title; private String direction; private String company; public Movie() public Movie(String t, String d, String c) title = t; direction = d; company = c;
5. public class BankAccount private String ownername; private int accountnumber; private int balance; private double rate; public String getownername() return ownername; public void setownername(string ownername) this.ownername = ownername; public int getaccountnumber() return accountnumber; public void setaccountnumber(int accountnumber) this.accountnumber = accountnumber; public int getbalance() return balance; public void setbalance(int balance) this.balance = balance; public double getrate() return rate; public void setrate(double rate) this.rate = rate; public BankAccount() public BankAccount(String n, int a, int b, double r) ownername = n; accountnumber = a; balance = b; rate = r;