5 :
2 (method) (public) (private) (interface)
5.1 (Method), (public method) (private method) (constructor), 3
4 5.2 (client). (receiver)., System.out.println("Hello"); (client object) (receiver object) println System.out println
5, (ASCII Art),-. _ " _ \_/ 'm' >{ - (_\ /_) ( ) sahr / \ (\ /) ejm97 `-^ hjw /** printbee: */ public void printbee() { System.out.println(",-."); System.out.println(" \\_/"); System.out.println(">{ -"); System.out.println(" / \\"); System.out.println(" `-^");
6 AsciiArtWriter /** AsciiArtWriter */ public class AsciiArtWriter { /** : */ public AsciiArtWriter() // { System.out.println(); /** printbee: */ public void printbee() { System.out.println(",-."); System.out.println(" \\_/"); // : \ \\ System.out.println(">{ -"); System.out.println(" / \\"); System.out.println(" `-^ hjw"); System.out.println(); ( )
7 AsciiArtWriter /** printbutterfly: */ public void printbutterfly() { System.out.println(" _ \""); // : " \" System.out.println(" (_\\ /_)"); System.out.println(" (/ \\) ejm97"); System.out.println(); /** printladybug: */ public void printladybug() { System.out.println(" `m\'"); // : ' \' System.out.println(" ( ) sahr"); System.out.println(); ( )
8 DrawArt /** DrawArt */ public class DrawArt { public static void main(string[] args) { AsciiArtWriter writer = new AsciiArtWriter(); writer.printbee(); System.out.println( This is a test. ); writer.printbutterfly(); writer.printladybug(); printbee, printbutterfly, printladybug
9 DrawArt main println System.out println printbee printbutterfly printladybug AsciiArtWriter printbee printbutterfly printladybug println
10 5.3 (Parameter),. (argument) (actual parameter). (formal parameter)..,.,,.
11 : printbeewithname /** printbeewithname: * @param name ( ) */ public void printbeewithname(string name) { System.out.println(",-."); System.out.println(" \\_/"); System.out.println(">{ - + name + - ); System.out.println(" / \\"); System.out.println(" `-^ System.out.println(); hjw");,-. \_/ >{ -Lucy- / \ `-^ (binding) AsciiArtWriter writer = new AsciiArtWriter(); Writer.printBeeWithName( Lucy );
12 : printinverse #1 import java.text.*; public class MathOperations {... /** printinverse: 1/i, 3 * @param i */ public void printinverse(int i) { DecimalFormat formatter = new DecimalFormat( 0.000 ); double d = 1.0 / i; String s = formatter.format(d); System.out.println(s);... MathOperations calculator = new MathOperations(); calculator.printinverse(3);
13 : printinverse #2 import java.text.*; public class MathOperations {... /** printinverse: 1/i pattern * @param i * @param pattern - */ public void printinverse(int i, String pattern) { DecimalFormat formatter = new DecimalFormat(pattern); double d = 1.0 / i; String s = formatter.format(d); System.out.println(s);... MathOperations calculator = new MathOperations(); calculator.printinverse(3, "0.00000"); calculator.printinverse(5, "0.0");
14 : printinverse #3 import java.text.*; public class MathOperations {... /** printinverse: 1/i pattern * @param i * @param pattern - */ public void printinverse(int i, DecimalFormat f) { double d = 1.0 / i; String s = f.format(d); System.out.println(s);... MathOperations calculator = new MathOperations(); DecimalFormat five_places = new DecimalFormat("0.00000"); calculator.printinverse(3, five_places);
15 printinverse(int i) printinverse(int i, String pattern) printinverse(int i, DecimalFormat f) printinverse(5, "0.00000"); (overloading).
16 5.4 :. < >.writesentence("oukseh Lee");. < >.repositionsentence(50, 80);
17 (specification) class MyWriter: (constructor): MyWriter(int w, int h) (w, h) (private attributes): sentence width, height x_position, y_position (methods): writesentence(string s) s repositionsentence(int x, int y) (x, y)
18 : class MyWriter import java.awt.*; import javax.swing.*; /** MyWriter: */ public class MyWriter extends JPanel { private int width; // private int height; // private String sentence = ""; // private int x_position; // x private int y_position; // y public MyWriter(int w, int h) {... public void paintcomponent(graphics g) {... public void writesentence(string s) {... public void repositionsentence(int x, int y) {...
19 : /** MyWriter * @param w - * @param h - */ public MyWriter(int w, int h) { width = w; height = h; x_position = width / 5; y_position = height / 2; JFrame f = new JFrame(); f.getcontentpane().add(this); f.settitle("mywriter"); f.setsize(width, height); f.setvisible(true); // //
20 : /** paintcomponent: * @param g - */ public void paintcomponent(graphics g) { g.setcolor(color.red); g.drawstring(sentence, x_position, y_position);
21 : /** writesentence: s * @param s - */ public void writesentence(string s) { sentence = s; this.repaint(); /** repositionsentence: (x,y) * @param x - x * @param y - y */ public void repositionsentence(int x, int y) { x_position = x; y_position = y; writesentence(sentence);
22 : MyExample import javax.swing.*; public class MyExample { public static void main(string[] args) { int x = 50; int y = 80; MyWriter w = new MyWriter(300, 200); w.repositionsentence(x, y); String s = JOptionPane.showInputDialog ("Please type some text:"); w.writesentence(s);
5.5 (Function):., int c = new Integer(input).intValue();. : int, char, double, boolean : String, GregorianCalendar, Integer 23
24, class TemperatureConvertor { /** celsiusintofahrenheit: * @param c - (int) * @return (double) */ public double celsiusintofahrenheit(int c) { double f = ((9.0/5.0)*c) + 32; return f; return String input = JOptionPane.showInputDialog( Type an integer Celsius temperature: ); int c = new Integer(input).intValue(); TemperatureConvert convert = new TemperatureConvertor(); double f = convert.celsiusintofahrenheit(c); double f = new TemperatureConvertor().celsiusIntoFahrenheit(c);
return. return. convert.celsiusintofahrenheit(c); 25
5.6 (Private Method), 26
, ver 2.0. makeborder 27
28 : private void makeborder(graphics g) { g.setcolor(color.blue); g.fillrect(0, 0, width, height); // int border_size = 20; int center_rectangle_width = width - (2 * border_size); int center_rectangle_height = height - (2 * border_size); g.setcolor(color.white); // g.fillrect(border_size, border_size, center_rectangle_width, center_rectangle_height); public void paintcomponent(graphics g) { makeborder(g); g.setcolor(color.red); g.drawstring(sentence, x_position, y_position);
,., paintanegg,. 29
30 StackedEggsWriter import java.awt.*; import javax.swing.*; public class StackedEggsWriter extends JPanel { private int frame_width; private int frame_height; // private int egg1_size; private int egg2_size; private int egg3_size; public StackedEggsWriter (int width, int height, int size1, int size2, int size3) {... private int paintanegg(int bottom, int width, Graphics pen) {... public void paintcomponent(graphics g) {... public static void main(string[] args) {...
31 public StackedEggsWriter (int width, int height, int size1, int size2, int size3) { frame_width = width; frame_height = height; egg1_size = size1; egg2_size = size2; egg3_size = size3; JFrame my_frame = new JFrame(); my_frame.getcontentpane().add(this); my_frame.settitle("stackedeggswriter"); my_frame.setsize(frame_width, frame_height); my_frame.setbackground(color.yellow); my_frame.setvisible(true); public static void main(string[] args) { new StackedEggsWriter(300, 200, 50, 90, 140);
32 paintanegg @return bottom /** paintanegg: width*2/3 : bottom : width, 3:2 width * @param bottom - y * @param width - * @param pen - * @return y */ private int paintanegg(int bottom, int width, Graphics pen) { int height = (2 * width) / 3; int top_edge = bottom - height; int left_edge = (frame_width - width) /2; pen.setcolor(color.pink); pen.filloval(left_edge, top_edge, width, height); pen.setcolor(color.black); pen.drawoval(left_edge, top_edge, width, height); return top_edge;
public void paintcomponent(graphics g) { // int egg1_top = paintanegg(frame_height, egg1_size, g); // int egg2_top = paintanegg(egg1_top, egg2_size, g); // paintanegg(egg2_top, egg3_size, g); 33
,,,, : MyWriter, GregorianCalendar : printbee, writesentence,, : answer, left_edge FRAME_WIDTH 34
35 (Static)., Math.abs(f).. class A static int a = ; static void f() {... a...... b...
36 (static):, ( ):, new. class A static int a = ; static void f() {... a...... b... new instance of A int b = ; void f() {... a...... b...
37 vs class A static int a = ; static void f() {... a b...... f() g()... new instance of A int b = ; void g() {... a b...... f() g()... instance of A int b = ; void g() {... a b...... f() g()...
38 final: (8.7, 9 ) abstract: (body) (9 ) synchronized:
39 (method) (public), (private), (static) (parameter)