´ÙÀ½ÀÇ ÇÁ·Î±×·¥À» Á÷Á¢ ¼öÇà½ÃÄÑ º¸°í ¿©·¯ °¡Áö¸¦ ¼öÁ¤ÇÏ¸é¼ Å×½ºÆ® Çغ¸½Ã¿À. - Circle
- Rectangle
- Circle
- Book
- Book
- CircleArray
- BookArray
- ArrayPassing
- GarbageEx
- AccessEx
- CalcEx
- Circle
- Circle
- Circle
- CallByValue
- ReferencePassing
- ArrayParameter
- Samp
½Ç½À¹®Á¦ | ¾Æ·¡ÀÇ ¹®Á¦¸¦ Ç®¾î Á¦ÃâÇϽÿÀ. | 1 | ¾Æ·¡ ½ÇÇà °á°ú¿Í °°ÀÌ Ãâ·ÂÇÏ´Â ´ÙÀ½ main()À» °¡Áø Song Ŭ·¡½º¸¦ ÀÛ¼ºÇ϶ó. Song Ŭ·¡½º´Â ³ë·¡ Á¦¸ñ title Çʵå, »ý¼ºÀÚ, getTitle() ¸Þ¼Òµå·Î ±¸¼ºµÈ´Ù. public static void main(String[] arg) { Song mySong = new Song("Let it go"); Song yourSong = new Song("°³²½ºÅ¸ÀÏ"); System.out.println("³» ³ë·¡´Â " + mySong.getTitle()); System.out.println("³Ê ³ë·¡´Â "+ yourSong.getTitle()); } |
| 2 | ´ÙÀ½Àº À̸§(name Çʵå)°ú ÀüȹøÈ£(tel Çʵå)¸¦ °¡Áø Phone Ŭ·¡½ºÀÌ´Ù. À̸§°ú ÀüȹøÈ£¸¦ ÀÔ·Â¹Þ¾Æ 2°³ÀÇ Phone °´Ã¼¸¦ »ý¼ºÇÏ°í, Ãâ·ÂÇÏ´Â main() ¸Þ¼Òµå¸¦ ÀÛ¼ºÇ϶ó. public class Phone { private String name, tel; public Phone(String name, String tel) { this.name = name; this.tel = tel; } public String getName() { return name; } public String getTel() { return tel; } } |
| 3 | »ç°¢ÇüÀ» Ç¥ÇöÇÏ´Â ´ÙÀ½ Rect Ŭ·¡½º¸¦ È°¿ëÇÏ¿©, Rect °´Ã¼ ¹è¿À» »ý¼ºÇÏ°í, »ç¿ëÀڷκÎÅÍ 4°³ÀÇ »ç°¢ÇüÀ» ÀÔ·Â¹Þ¾Æ ¹è¿¿¡ ÀúÀåÇÑ µÚ, ¹è¿À» °Ë»öÇÏ¿© »ç°¢Çü ¸éÀûÀÇ ÇÕÀ» Ãâ·ÂÇÏ´Â main() ¸Þ¼Òµå¸¦ °¡Áø RectArray Ŭ·¡½º¸¦ ÀÛ¼ºÇ϶ó. class Rect { private int width, height; public Rect(int width, int height) { this.width = width; this.height = height; } public int getArea() { return width*height; } } |
| 4 | À̸§(name)°ú ÀüȹøÈ£(tel) Çʵå, »ý¼ºÀÚ ¹× ÇÊ¿äÇÑ ¸Þ¼Òµå¸¦ °¡Áø Phone Ŭ·¡½º¸¦ ÀÛ¼ºÇÏ°í, ´ÙÀ½ ½ÇÇà »ç·Ê¿Í °°ÀÌ ÀÛµ¿µÇµµ·Ï main()À» °¡Áø PhoneManager Ŭ·¡½º¸¦ ÀÛ¼ºÇ϶ó. ÇÑ »ç¶÷ÀÇ ÀüȹøÈ£´Â ÇϳªÀÇ Phone °´Ã¼·Î ´Ù·é´Ù. | 5 | CircleManager´Â staitc ¸Þ¼Òµå¸¦ °¡Áø Ŭ·¡½ºÀÌ´Ù StaticTest Ŭ·¡½º´Â static ¸Þ¼Òµå¸¦ È°¿ëÇÏ´Â »ç·Ê¸¦ º¸¿©ÁØ´Ù. ½ÇÇà °á°ú¸¦ Âü°íÇÏ¿© Äڵ带 ¿Ï¼ºÇ϶ó. class Circle { private int radius; public Circle(int radius) { this.radius = radius; } public int getRadius() { return this.radius; } public void setRadius(int radius) { this.radius = radius; } } class CircleManager { // static ¸Þ¼Òµå¸¸ °¡Áü void copy(Circle src, Circle dest) { // src¸¦ dest¿¡ º¹»ç dest.setRadius(src.getRadius()); // srcÀÇ ¹ÝÁö¸§À» dest¿¡ º¹»ç } boolean equals(Circle a, Circle b) { // a¿Í bÀÇ ¹ÝÁö¸§ÀÌ °°À¸¸é true ¸®ÅÏ if else } } public class StaticTest { public static void main(String[] args) { Circle pizza = Circle waffle = boolean res = // pizza¿Í waffle ºñ±³ if (res == true) System.out.println("pizza¿Í waffle Å©±â °°À½"); else System.out.println("pizza¿Í waffle Å©±â ´Ù¸§"); CircleManager.copy(pizza, waffle); // pizza¸¦ waffle¿¡ º¹»ç res = // pizza¿Í waffle ºñ±³ if (res == true) System.out.println("pizza¿Í waffle Å©±â °°À½"); else System.out.println("pizza¿Í waffle Å©±â ´Ù¸§"); } } |
| 6 | ´ÙÀ½Àº °¡·Î ¼¼·Î·Î ±¸¼ºµÇ´Â ¹Ú½º¸¦ Ç¥ÇöÇÏ´Â Box Ŭ·¡½º¿Í À̸¦ ÀÌ¿ëÇÏ´Â ÄÚµåÀÌ´Ù. BoxÀÇ draw()´Â fill Çʵ忡 ÁöÁ¤µÈ ¹®ÀÚ·Î ÀÚ½ÅÀ» ±×¸°´Ù. ½ÇÇà °á°ú¸¦ º¸¸é¼, Äڵ带 ¿Ï¼ºÇ϶ó. public class Box { private int width, height; // ¹Ú½ºÀÇ ³Êºñ¿Í ³ôÀÌ private char fillChar; // ¹Ú½º¸¦ ±×¸®´Â µ¥ »ç¿ëÇÏ´Â ¹®ÀÚ public Box() { // ¸Å°³ º¯¼ö ¾ø´Â »ý¼ºÀÚ // this() ÀÌ¿ë } public Box(int width, int height) { // ³Êºñ¿Ü ³ôÀÌÀÇ 2 ¸Å°³ º¯¼ö¸¦ °¡Áø »ý¼ºÀÚ } public void draw() { // ¹Ú½º ±×¸®´Â ¸Þ¼Òµå for(int i=0; i } } public void fill(char c) { // ¹Ú½º¸¦ ±×¸®´Âµ¥ »ç¿ëÇÏ´Â ¹®ÀÚ ¼³Á¤ } public static void main(String[] args) { Box a = new Box(); // 10x1 »ç°¢Çü Box b = new Box(20,3); // 20x3 »ç°¢Çü a.fill('*'); // box¸¦ ±×¸± ¶§ »ç¿ëÇÏ´Â ¹®ÀÚ '*' b.fill('%'); // box¸¦ ±×¸± ¶§ »ç¿ëÇÏ´Â ¹®ÀÚ '%' a.draw(); // ¹Ú½º ±×¸®±â b.draw(); // ¹Ú½º ±×¸®±â } } |
| Bonus | µÎ »ç¶÷ÀÌ ¹ø°¥¾Æ ÇÏ´Â °·ºí¸µ °ÔÀÓÀ» ¸¸µé¾î º¸ÀÚ. 0¿¡¼ 2 »çÀÌÀÇ Á¤¼ö 3°³¸¦ ·£´ýÇÏ°Ô ¹ß»ý½ÃÄÑ ¸ðµÎ °°À¸¸é ½Â¸®ÇÑ´Ù. ¼±¼ö´Â Player Ŭ·¡½º·Î Ç¥ÇöÇÑ´Ù. ½ÇÇà °á°ú¸¦ Âü°íÇÏ¿© ´ÙÀ½ Äڵ带 ¿Ï¼ºÇ϶ó. import java.util.Scanner; class Player { // ¼±¼ö¸¦ Ç¥ÇöÇϴ Ŭ·¡½º // ¼±¼ö À̸§ // »ý¼ºÀÚ String getName() { return name; } } public class GamblingGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Player [] p = // ¼±¼ö ¹è¿ ¼±¾ð for( ){ // µÎ ¼±¼öÀÇ À̸§À» ÀԷ¹޴´Ù. System.out.print("¼±¼ö À̸§ ÀÔ·Â >>"); // ÇÁ·ÒÇÁÆ® Ãâ·Â p[i] = new Player(scanner.next()); // °¢ ¼±¼ö °´Ã¼ »ý¼º } int n=0; // µÎ ¼±¼ö°¡ ¹ø°¥¾Æ °ÔÀÓÇÒ ¶§, nÀº ¹è¿ pÀÇ À妽º while(true) { System.out.print(p[n].getName()+" <Enter ¿Ü ¾Æ¹«Å°³ª Ä¡¼¼¿ä>"); scanner.next(); // Y Å°¸¦ ÀÐ°í ¹ö¸² int [] val = new int [3]; // 3°³ÀÇ ·£´ý ¼ö¸¦ ÀúÀåÇϱâ À§ÇÑ Á¤¼ö ¹è¿ »ý¼º for(int i=0; i<val.length; i++) { // 3 °³ÀÇ ·£´ý Á¤¼ö »ý¼º val[i] = (int)(Math.random()*3); // ·£´ý Á¤¼ö °ªÀ» 0~2 »çÀÌ·Î ¹ß»ý System.out.print(val[i] + "t"); // 3°³ÀÇ Á¤¼ö Ãâ·Â } System.out.println(); // ÇÑ ÁÙ ¶ç±â if( ){ // 3 Á¤¼ö°¡ ¸ðµÎ °°À¸¸é // ½Â¸® ¸Þ½ÃÁö Ãâ·Â // while ¹þ¾î³² } n++; // ´ÙÀ½ ¼±¼ö À妽º n = n%2; // À妽º°¡ 2°¡ µÇ¸é ´Ù½Ã 0À¸·Î µ¹¸®±â À§ÇØ } } } |
|
Open Challenge | ³¡¸»Àձ⠰ÔÀÓ ¸¸µé±â | n ¸íÀÌ Âü°¡ÇÏ´Â ³¡¸»Àձ⠰ÔÀÓÀ» ÇÁ·Î±×·¥À¸·Î ¸¸µé¾îº¸ÀÚ. ¿©·¯ °³ÀÇ °´Ã¼¿Í ¹è¿À» »ç¿ëÇÏ´Â ¿¬½ÀÀ» À§ÇÑ °ÍÀÌ´Ù. óÀ½ ´Ü¾î´Â "¾Æ¹öÁö"ÀÌ´Ù. n ¸íÀÇ Âü°¡ÀÚµéÀº ¼ø¼´ë·Î ÀÚ½ÅÀÇ ´Ü¾î¸¦ ÀÔ·ÂÇÏ¸é µÈ´Ù. ³¡¸»Àձ⠿¡¼ ³¡¸»ÀÌ Æ²¸° °æ¿ì °ÔÀÓÀ» ³¡³»°í °ÔÀÓ¿¡¼ Áø Âü°¡ÀÚ¸¦ ȸ鿡 Ãâ·ÂÇÑ´Ù. ÇÁ·Î±×·¥¿¡¼´Â ½Ã°£ Áö¿¬À» ±¸ÇöÇÏÁö ¾Ê¾Æµµ µÈ´Ù. ±×·¸Áö¸¸ Âü°¡ÀÚµéÀÌ ½º½º·Î ½Ã°£À» Àç¾îº¸´Â °Íµµ ÁÁ°Ú´Ù. ÀÌŬ¸³½º¿¡¼ ÇÁ·Î±×·¥À» ½ÇÇàÇϸé ÇÑ±Û ÀÔ·ÂÀÌ Á¦´ë·Î µÇÁö ¾Ê´Â ¹®Á¦Á¡ÀÌ ÀÖÀ½À» ²À ±â¾ïÇ϶ó. ¾Æ¸¶µµ ÇÑ±Û ÀÔ·ÂÀ» ¿øÈ°È÷ ¸øÇÏ´Â ¹ö±×ÀÎ µíÇÏ´Ù. ±×·¡¼ ÀúÀÚµµ ¾Æ·¡¿Í °°ÀÌ ¸í·Éâ¿¡¼ ÇÁ·Î±×·¥À» ½ÇÇàÇÏ¿´´Ù. |
|