top of page

게시판 게시물

minhyuk0426
2023년 6월 17일
In 소스 코드 제출
/* import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { private JButton [] Ibtn = new JButton [3]; private JLabel la = new JLabel(); private JLabel la1 = new JLabel(); private JLabel la2 = new JLabel(); private JLabel la3 = new JLabel(); private JLabel la4 = new JLabel(); private ImageIcon [] image = { new ImageIcon("images/scissors.png"), new ImageIcon("images/motivation.png"), new ImageIcon("images/palm.png") }; public Main() { setTitle("가위 바위 보 게임"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JPanel p = new JPanel(); c.setBackground(Color.yellow); p.setBackground(Color.GRAY); c.add(p); ButtonGroup g = new ButtonGroup(); for(int i=0; i<Ibtn.length; i++) { Ibtn[i] = new JButton(image[i]); g.add(Ibtn[i]); p.add(Ibtn[i]); Ibtn[i].addActionListener(new MyActionListener()); } c.add(la); c.add(la1); c.add(la2); c.add(la3); c.add(la4); setSize(350, 300); setVisible(true); la.setVisible(true); la.setLocation(200,150); la1.setVisible(true); la1.setLocation(200,150); la2.setVisible(true); la2.setLocation(200,150); la3.setVisible(true); la3.setLocation(200,150); la4.setVisible(true); la4.setLocation(200,150); } class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { int x = (int) (Math.random()*3); JButton pressed_Button = (JButton)e.getSource(); if(pressed_Button.equals(Ibtn[0])) { //me : 가위 la.setIcon(image[0]); if(x==0) la4.setText("Same!!!"); else if(x==1) la4.setText("Computer!!!"); else la4.setText("Me!!!"); } else if(pressed_Button.equals(Ibtn[1])) { //me : 바위 la.setIcon(image[1]); if(x==1) la4.setText("Same!!!"); else if(x==2) la4.setText("Computer!!!"); else la4.setText("Me!!!"); } else { //me : 보 la.setIcon(image[2]); if(x==2) la4.setText("Same!!!"); else if(x==0) la4.setText("Computer!!!"); else la4.setText("Me!!!"); } la1.setText("me"); la2.setIcon(image[x]); la3.setText("com"); la4.setForeground(Color.red); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { int goal = 5; private JLabel [] la = new JLabel[goal]; private JLabel jla = new JLabel(); private JButton y = new JButton("y"); private JButton n = new JButton("n"); int num=0; Container c = getContentPane(); public void reset() { jla.setVisible(false); y.setVisible(false); n.setVisible(false); for(int i=0; i<la.length; i++) { int x = (int)(Math.random()*300); int y = (int)(Math.random()*300); la[i].setLocation(x, y); la[i].setVisible(true); } } public Main() { setTitle("Ten 레이블 클릭"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(null); y.setVisible(false); n.setVisible(false); for(int i=0; i<la.length; i++) { la[i] = new JLabel(Integer.toString(i)); la[i].addMouseListener(new MyMouseListener()); la[i].setSize(10,10); int x = (int)(Math.random()*300); int y = (int)(Math.random()*300); la[i].setLocation(x, y); c.add(la[i]); } jla.setLocation(120,100); jla.setSize(200,100); jla.setVisible(true); c.add(jla); c.add(y); c.add(n); y.setSize(44, 44); y.setLocation(110, 170); y.setVisible(false); n.setSize(44,44); n.setLocation(220,170); n.setVisible(false); y.addActionListener(new MyActionListener()); n.addActionListener(new MyActionListener2()); setSize(400, 400); setVisible(true); } class MyMouseListener extends MouseAdapter{ public void mousePressed(MouseEvent e) { //int result = JOptionPane.showConfirmDialog(null, "게임을 다시 하시겠습니까?","Confirm", JOptionPane.YES_NO_OPTION); JLabel pl = (JLabel)e.getSource(); int plnum = Integer.parseInt(pl.getText()); if(plnum==num) { num++; pl.setVisible(false); } if(num==goal) { jla.setVisible(true); jla.setText("게임을 다시 하시겠습니까?"); y.setVisible(true); n.setVisible(true); num=0; } } } class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { reset(); } } class MyActionListener2 implements ActionListener{ public void actionPerformed(ActionEvent e) { System.exit(0); } } public static void main(String[] args) { new Main(); } } */
0
0
1
minhyuk0426
2023년 5월 13일
In 소스 코드 제출
/* import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { private JButton [] Ibtn = new JButton [3]; private JLabel la = new JLabel(); private JLabel la1 = new JLabel(); private JLabel la2 = new JLabel(); private JLabel la3 = new JLabel(); private JLabel la4 = new JLabel(); private ImageIcon [] image = { new ImageIcon("images/scissors.png"), new ImageIcon("images/motivation.png"), new ImageIcon("images/palm.png") }; public Main() { setTitle("가위 바위 보 게임"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JPanel p = new JPanel(); c.setBackground(Color.yellow); p.setBackground(Color.GRAY); c.add(p); ButtonGroup g = new ButtonGroup(); for(int i=0; i<Ibtn.length; i++) { Ibtn[i] = new JButton(image[i]); g.add(Ibtn[i]); p.add(Ibtn[i]); Ibtn[i].addActionListener(new MyActionListener()); } c.add(la); c.add(la1); c.add(la2); c.add(la3); c.add(la4); setSize(350, 300); setVisible(true); la.setVisible(true); la.setLocation(200,150); la1.setVisible(true); la1.setLocation(200,150); la2.setVisible(true); la2.setLocation(200,150); la3.setVisible(true); la3.setLocation(200,150); la4.setVisible(true); la4.setLocation(200,150); } class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { int x = (int) (Math.random()*3); JButton pressed_Button = (JButton)e.getSource(); if(pressed_Button.equals(Ibtn[0])) { //me : 가위 la.setIcon(image[0]); if(x==0) la4.setText("Same!!!"); else if(x==1) la4.setText("Computer!!!"); else la4.setText("Me!!!"); } else if(pressed_Button.equals(Ibtn[1])) { //me : 바위 la.setIcon(image[1]); if(x==1) la4.setText("Same!!!"); else if(x==2) la4.setText("Computer!!!"); else la4.setText("Me!!!"); } else { //me : 보 la.setIcon(image[2]); if(x==2) la4.setText("Same!!!"); else if(x==0) la4.setText("Computer!!!"); else la4.setText("Me!!!"); } la1.setText("me"); la2.setIcon(image[x]); la3.setText("com"); la4.setForeground(Color.red); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { int goal = 5; private JLabel [] la = new JLabel[goal]; private JLabel jla = new JLabel(); private JButton y = new JButton("예"); private JButton n = new JButton("아니오"); int num=0; public Main() { setTitle("Ten 레이블 클릭"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); for(int i=0; i<la.length; i++) { la[i] = new JLabel(Integer.toString(i)); la[i].addMouseListener(new MyMouseListener()); la[i].setSize(10,10); //la[i].setOpaque(true); //la[i].setBackground(Color.cyan); int x = (int)(Math.random()*300); int y = (int)(Math.random()*300); la[i].setLocation(x, y); c.add(la[i]); } jla.setLocation(120,100); jla.setSize(200,100); jla.setVisible(true); y.setLocation(110,110); y.setVisible(true); n.setLocation(120,110); n.setVisible(true); c.add(y); c.add(n); c.add(jla); setSize(400, 400); setVisible(true); } class MyMouseListener extends MouseAdapter{ public void mousePressed(MouseEvent e) { //int result = JOptionPane.showConfirmDialog(null, "게임을 다시 하시겠습니까?","Confirm", JOptionPane.YES_NO_OPTION); JLabel pl = (JLabel)e.getSource(); int plnum = Integer.parseInt(pl.getText()); if(plnum==num) { num++; pl.setVisible(false); } if(num==goal) { jla.setText("게임을 다시 하시겠습니까?"); } } } public static void main(String[] args) { new Main(); } } */
0
0
2
minhyuk0426
2023년 4월 22일
In 소스 코드 제출
/* import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { private JButton [] Ibtn = new JButton [3]; private JLabel la = new JLabel(); private JLabel la1 = new JLabel(); private JLabel la2 = new JLabel(); private JLabel la3 = new JLabel(); private JLabel la4 = new JLabel(); private ImageIcon [] image = { new ImageIcon("images/scissors.png"), new ImageIcon("images/motivation.png"), new ImageIcon("images/palm.png") }; public Main() { setTitle("가위 바위 보 게임"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JPanel p = new JPanel(); c.setBackground(Color.yellow); p.setBackground(Color.GRAY); c.add(p); ButtonGroup g = new ButtonGroup(); for(int i=0; i<Ibtn.length; i++) { Ibtn[i] = new JButton(image[i]); g.add(Ibtn[i]); p.add(Ibtn[i]); Ibtn[i].addActionListener(new MyActionListener()); } c.add(la); c.add(la1); c.add(la2); c.add(la3); c.add(la4); setSize(350, 300); setVisible(true); la.setVisible(true); la.setLocation(200,150); la1.setVisible(true); la1.setLocation(200,150); la2.setVisible(true); la2.setLocation(200,150); la3.setVisible(true); la3.setLocation(200,150); la4.setVisible(true); la4.setLocation(200,150); } class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { int x = (int) (Math.random()*3); JButton pressed_Button = (JButton)e.getSource(); if(pressed_Button.equals(Ibtn[0])) { //me : 가위 la.setIcon(image[0]); if(x==0) la4.setText("Same!!!"); else if(x==1) la4.setText("Computer!!!"); else la4.setText("Me!!!"); } else if(pressed_Button.equals(Ibtn[1])) { //me : 바위 la.setIcon(image[1]); if(x==1) la4.setText("Same!!!"); else if(x==2) la4.setText("Computer!!!"); else la4.setText("Me!!!"); } else { //me : 보 la.setIcon(image[2]); if(x==2) la4.setText("Same!!!"); else if(x==0) la4.setText("Computer!!!"); else la4.setText("Me!!!"); } la1.setText("me"); la2.setIcon(image[x]); la3.setText("com"); la4.setForeground(Color.red); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { private JLabel [] la = new JLabel[10]; public Main() { setTitle("Ten 레이블 클릭"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); for(int i=0; i<la.length; i++) { la[i] = new JLabel(Integer.toString(i)); la[i].addMouseListener(new MyMouseListener()); la[i].setSize(100,100); int x = (int)(Math.random()*300); int y = (int)(Math.random()*300); la[i].setLocation(x, y); c.add(la[i]); } setSize(400, 400); setVisible(true); } class MyMouseListener extends MouseAdapter{ public void mousePressed(MouseEvent e) { } } public static void main(String[] args) { new Main(); } } */
0
0
4
minhyuk0426
2023년 4월 15일
In 소스 코드 제출
/* import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { private JTextArea ta = new JTextArea(5, 30); JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 0); Container c = getContentPane(); public Main() { setTitle("TextArea Practice Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new FlowLayout()); ta.addKeyListener(new MyKeyListener()); slider.addChangeListener(new MyChangeListener()); slider.setPaintLabels(true); slider.setPaintTicks(true); slider.setPaintTrack(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); c.add(ta); c.add(slider); setSize(350, 200); setVisible(true); ta.setFocusable(true); ta.requestFocus(); } class MyChangeListener implements ChangeListener { public void stateChanged(ChangeEvent e) { String str = ta.getText(); int len = slider.getValue(); if(str.length()>len) str = str.substring(0, len); ta.setText(str); } } class MyKeyListener extends KeyAdapter { public void keyTyped(KeyEvent e) { int len = ta.getText().length(); slider.setValue(len); if(len == 100) { ta.setFocusable(false); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { private JButton [] Ibtn = new JButton [3]; private JLabel la = new JLabel(); private ImageIcon [] image = { new ImageIcon("images/scissors.png"), new ImageIcon("images/fist (1).png"), new ImageIcon("images/open-hands.png") }; public Main() { setTitle("가위 바위 보 게임"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JPanel p = new JPanel(); c.setBackground(Color.yellow); p.setBackground(Color.GRAY); c.add(p); ButtonGroup g = new ButtonGroup(); for(int i=0; i<Ibtn.length; i++) { Ibtn[i] = new JButton(image[i]); g.add(Ibtn[i]); p.add(Ibtn[i]); Ibtn[i].addItemListener(new MyItemListener()); } c.add(la); setSize(400, 300); setVisible(true); } class MyItemListener implements ItemListener { public void itemStateChanged(ItemEvent e) { if(Ibtn[0].isSelected()) { la.setText("fjjtfjtft"); } } } public static void main(String[] args) { new Main(); } } */
0
0
6
minhyuk0426
2023년 4월 01일
In 소스 코드 제출
/* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i,j; int[][] arr = new int[n][n]; for(i=0; i<n; i++) { arr[i][0] = sc.nextInt(); } for(i=1; i<n; i++) { for(j=1; j<=i; j++) { arr[i][j] = arr[i][j-1] - arr[i-1][j-1]; } } for(i=0; i<n; i++) { for(j=0; j<=i; j++) { System.out.print(arr[i][j]+" "); } System.out.println(" "); } } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,sum=0; for(i=a; i<=b; i++) { if(i%2==0) { sum -= i; } else { sum += i; } } System.out.println(sum); } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,j,k,sum=0,dec=0; for(k=a; k<=b; k++) { for(i=1; i<=9; i++) { System.out.println(k+"*"+i+"="+(k*i)); } } } } 20220918 클래스 : 틀 ( 필드 + 생성자 + 메소드) 객체 : 틀로 찍어낸 물체 import java.util.*; class Person{ //field 필드 int age; String name; //method 메소드 (함수) void view() { System.out.println("나이는 "+ age+"이고, 이름은 "+ name+"입니다."); } //constructor 생성자 - 객체를 생성할때 필드 값의 초기값을 정해주는 용도 Person(){ age=5000; name="noname"; } Person(int a){ age=a; name="noname"; } Person(int a, String n){ age=a; name=n; } } public class Main { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); Person s; s = new Person(); Person p = new Person(100,"tom"); Person t = new Person(50); t.view(); p.view(); s.view(); p.age=10; p.name="hello"; s.age=900; s.name="hi"; p.view(); s.view(); //System.out.println("나이는"+ p.age+"이고, 이름은 "+ p.name+"입니다."); //System.out.println("나이는"+ s.age+"이고, 이름은 "+ s.name+"입니다."); } } */ /* class Circle { int radius; String name; public double getArea() { return 3.14*radius*radius; } Circle(int r, String n){ radius=r; name=n; } } class Main{ public static void main(String[] args) { // Circle pizza; // pizza = new Circle(); // pizza.radius = 10; // pizza.name = "자바피자"; // double area = pizza.getArea(); // System.out.println(pizza.name+"의 면적은"+area); Circle donut = new Circle(2,"자바도넛"); double area = donut.getArea(); System.out.println(donut.name+"의 면적은"+area); } } */ /* import java.util.*; class Circle { int radius; String name; public Circle() { radius = 1; name = ""; } public Circle(int r, String n) { radius = r; name = n; } public double getArea() { return 3.14*radius*radius; } } class Main{ public static void main(String[]args) { Circle pizza = new Circle(10, "자바피자"); double area = pizza.getArea(); System.out.println(pizza.name + "의 면적은 " + area); Circle donut = new Circle(); donut.name = "도넛피자"; area = donut.getArea(); System.out.println(donut.name + "의 면적은 " + area); } } private 다른 클래스에서 접근 할 수 없는 필드, 메소드가 된다 */ /* class Song { private String title; public Song(String title) { this.title = title; } public String getTitle() { return title; } } class Main { public static void main(String[] args) { Song mySong = new Song("Nessun Dorma"); Song yourSong = new Song("공주는 잠 못 이루고"); System.out.println("내 노래는 " + mySong.getTitle()); System.out.println("너 노래는 " + yourSong.getTitle()); } } */ /* 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; } } class Main { public static void main(String[] args) { Phone } } */ /* import java.util.*; class TV { private String name; int year; int size; public TV(String name,int year,int size) { this.name = name; this.year = year; this.size = size; } public void show() { System.out.println(name+"에서 만든 "+year+"년형 "+size+"인치 TV"); } } class Main{ public static void main(String [] args) { TV myTV = new TV("LG", 2017, 32); myTV.show(); } } */ /* import java.util.*; class Grade { private int math; private int science; private int english; public Grade(int math,int science,int english) { this.math = math; this.science = science; this.english = english; } public int average() { return (math+science+english)/3; } } class Main { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); System.out.println("수학, 과학, 영어 순으로 3개의 정수 입력>>"); Grade me = new Grade(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()); System.out.println("평균은 " + me.average()); } } */ /* import java.util.*; class Song { private String title; private String country; private String artist; private int year; public Song(int year) { this(year,"미정","미정","미정"); } public Song(int year,String title,String country,String artist) { this.title = title; this.country = country; this.artist = artist; this.year = year; } public void show() { System.out.println(year+"년 "+country+"국적의 "+artist+"가 부른 "+title); } } class Main { public static void main(String [] args) { Song mySong = new Song(1978,"Dancing Queen","스웨덴","ABBA"); Song yourSong = new Song(1945); mySong.show(); } } */ /* import java.util.*; 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; } } class Main { public static void main(String [] args) { Book littlePrince = new Book("어린왕자","생텍쥐페리"); Book loveStory = new Book("춘향전"); Book emptyBook = new Book(); loveStory.show(); } } */ /* class Samp { int id; public Samp(int x) { this.id = x; } public Samp() { this(0); System.out.println("생성자 호출"); } } class ConstructorExample{ //필드 int x; //메소드 public void setx(int x) { this.x = x; } public int getx() { return x; } //생성자 public ConstructorExample() { } public static void main(String [] args) { ConstructorExample a = new ConstructorExample(); int n = a.getx(); } } 객체 배열을 생성할때는 1. 레퍼런스 배열의 레퍼런스 생성 2. 레퍼런스 배열 생성 3. 객체 생성해서 레퍼런스에 연결 class Person{ String name; int age; } class Main{ public static void main(String[] args) { // Person p ;//레퍼런스변수 생성 // p = new Person(); //객체 생성 Person[] arr = new Person[10]; for(int i=0;i<arr.length;i++) { arr[i] = new Person(); } arr[0].name="민혁"; } } * */ /* import java.util.*; class Circle { int radius; public Circle(int radius) { this.radius = radius; } public double getArea() { return 3.14*radius*radius; } } class Main { public static void main(String[] args) { Circle [] c; c = new Circle[5]; for(int i=0; i<c.length; i++) c[i] = new Circle(i); for(int i=0; i<c.length; i++) System.out.println((int)(c[i].getArea()) + " "); } } */ /* import java.util.Scanner; class Book { String title,author; public Book(String title,String author) { this.title = title; this.author = author; } } class Main{ public static void main(String[] args) { Book [] book = new Book[2]; Scanner scanner = new Scanner(System.in); for(int i=0; i<book.length; i++) { System.out.print("제목>>"); String title = scanner.nextLine(); System.out.print("저자>>"); String author = scanner.nextLine(); book[i] = new Book(title, author); } for(int i=0; i<book.length; i++) System.out.print("(" + book[i].title + ", " + book[i].author + ")"); } } */ /* import java.util.*; class Circle { private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Circle c[] = new Circle[3]; for (int i = 0; i < c.length; i++) { System.out.print("x, y, radius >>"); double x = scanner.nextDouble(); double y = scanner.nextDouble(); int radius = scanner.nextInt(); c[i] = new Circle(x, y, radius); } for(int i=0; i<c.length; i++) c[i].show(); scanner.close(); } } import java.util.Scanner; class Dictionary { private static String [] kor = { "사랑","아기","돈","미래","희망" }; private static String [] eng = { "love","baby","monry","future", "hope"}; public static String kor2Eng(String word) { for(int i=0;i<kor.length;i++) { if(kor[i].equals(word)) return eng[i]; } return " 저의 사전에 없습니다."; } } class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("한영 단어 검색 프로그램입니다."); while(true) { System.out.print("한글 단어?"); String name = sc.next(); if(name.equals("그만")) { return ; } else { System.out.println(name+"는 " +Dictionary.kor2Eng(name)); } } } } 부모클래스 - 자식클래스 슈퍼클래스 - 서브클래스 class Person{ int age; String name; } class Student extends Person{ String school; char grade; void speak() { System.out.println("안녕하세요, 저는 학생입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class Teacher extends Person{ String school; String major; void speak() { System.out.println("안녕하세요, 저는 선생님입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class ComTeacher extends Teacher{ } class Main{ public static void main(String[] args) { Student s = new Student(); Teacher t = new Teacher(); s.speak(); } } */ /* import java.util.*; 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; void setColor(String color) { this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main{ public static void main(String [] args) { Point p = new Point(); p.set(1,2); p.showPoint(); ColorPoint cp = new ColorPoint (); cp.set(3,4); cp.setColor("red"); cp.showColorPoint(); } } import java.util.*; class Pen { protected int amount; public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } class SharpPencil extends Pen { private int width; } class BallPen extends Pen { private String color; public String getColor() { return color; } public void setColor(String color ) { this.color = color; } } class FountainPen extends BallPen { public void refill(int n) { amount = n; } } 상속-생성자 class A{ int n; public A() { System.out.println("A의 기본 생성자"); } public A(int n) {this.n = n;} } class B extends A{ int m; public B() { System.out.println("B의 기본 생성자"); } public B(int m) { super(20); this.m = m; System.out.println("B의 1번 생성자"); } } class Main{ public static void main(String[] args) { //A x = new A(10); B y = new B(10); } } */ /* import java.util.*; class Point { private int x,y; public Point() { this.x = this.y = 0; } public Point(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 ColorPoint(int x, int y, String color) { super (x, y); this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main { public static void main(String[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showColorPoint (); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } public void printProperty() { System.out.print(getSize()+"인치 "+Color+"컬러"); } } class Main { public static void main(String [] args) { ColorTV myTV = new ColorTV(32,1024); myTV.printProperty(); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } protected int getColor() { return Color; } } class IPTV extends ColorTV { private String IP; public IPTV(String IP,int size,int Color) { super(size,Color); this.IP = IP; } public void printProperty() { System.out.print("나의 IPTV는 "+IP+" 주소의 "+getSize()+"인치 "+getColor()+"컬러"); } } class Main { public static void main(String[]args) { IPTV iptv = new IPTV("192.1.1.2", 32, 2048); iptv.printProperty(); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint(int x,int y,String Color) { super(x,y); this.Color = Color; } public void setXY(int x,int y) { move (x,y); } public void setColor(String Color) { this.Color = Color; } public String toString () { return Color+"색의 "+"("+ getX() +","+ getY() +")"+"의 점"; } } class Main { public static void main(String[]args) { ColorPoint cp = new ColorPoint(5, 5, "YELLOW"); cp.setXY(10, 20); cp.setColor("RED"); String str = cp.toString(); System.out.println(str + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint() { super(0,0); this.Color = "BLACK"; } public ColorPoint(int x,int y) { super(x,y); this.Color = "BLACK"; } public void setXY(int x,int y) { move(x,y); } public void setColor(String color) { this.Color = color; } public String toString () { return Color + "색의 " + "("+ getX() + ","+ getY() + ")" + "의 점"; } } class Main { public static void main(String[] args) { ColorPoint zeroPoint = new ColorPoint(); System.out.println(zeroPoint.toString() + "입니다."); ColorPoint cp = new ColorPoint(10,10); cp.setXY(5, 5); cp.setColor("RED"); System.out.println(cp.toString() + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class Point3D extends Point { private int z; public Point3D(int x,int y,int z) { super(x,y); this.z = z; } public void moveUp() { z = z+1; } public void moveDown() { z--; } public void move(int x, int y,int z) { super.move(x, y); this.z = z; } public String toString() { return "("+ getX() + "," + getY() + "," + z +")" + "의 점"; } } class Main { public static void main(String[] args) { Point3D p = new Point3D(1,2,3); System.out.println(p.toString() + "입니다."); p.moveUp(); System.out.println(p.toString() + "입니다."); p.moveDown(); p.move(10, 10); System.out.println(p.toString() + "입니다."); p.move(100, 200, 300); System.out.println(p.toString() + "입니다."); } } 클래스 1. 필드 (변수 ,속성) 2. 메소드 (기능, 행동) 3. 생성자(객체 생성시 필드값 초기화) 추상클래스 : 일부만 작성 == 일부는 작성 x */ /* abstract class Person{ int age; String name; void introduce() { //일반 메소드 System.out.println("I'm "+age+"years old. My name is "+name+"."); } abstract void speak(); //추상메소드 ( 이 클래스를 상속받은 모든 클래스는 이 메소드를 구현) } class Student extends Person{ void speak() { System.out.println("I'm Student."); } } class Teacher extends Person{ void speak() { System.out.println("I'm Teacher"); } } class Main{ public static void main(String[] args) { Student s = new Student(); } } */ /* abstract class Calculator { public abstract int add(int a,int b); public abstract int subtract(int a,int b); public abstract double average (int[] a); } class GoodCalc extends Calculator { public int add(int a,int b) { return a + b; } public int subtract(int a,int b) { return a-b; } public double average(int[] a) { double sum = 0; for(int i=0; i<a.length; i++) sum += a[i]; return sum/a.length; } } class Main { public static void main(String[] args) { GoodCalc c = new GoodCalc(); System.out.println(c.add(2,3)); System.out.println(c.subtract(2,3)); System.out.println(c.average(new int [] { 2, 3, 4 })); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Won2Dollar extends Converter { public Won2Dollar(int i) { } @Override protected double convert(double src) { return src/1200; } @Override protected String getSrcString() { return "원"; } @Override protected String getDestString() { return "달러"; } } class Main{ public static void main(String[] args) { Won2Dollar toDollar = new Won2Dollar(1200); toDollar.run(); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Km2Mile extends Converter { public Km2Mile(double i) { ratio=i; } protected double convert(double src) { return src/ratio; } protected String getSrcString() { return "Km"; } protected String getDestString() { return "mile"; } } class Main { public static void main(String[] args) { Km2Mile toMile = new Km2Mile(1.6); toMile.run(); } } */ /* import java.util.*; abstract class PairMap { protected String keyArray []; protected String ValueArray []; abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key); abstract int length(); } class Dictionary extends PairMap { public Dictionary(int i) { } String get(String key) { return ""; } void put(String key, String value) { } String delete(String key) { return null; } int length() { return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(10); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* import java.util.*; interface PairMap { abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key, String value); abstract int length(); } class Dictionary implements PairMap { @Override public String get(String key) { // TODO Auto-generated method stub return null; } @Override public void put(String key, String value) { // TODO Auto-generated method stub } @Override public String delete(String key, String value) { // TODO Auto-generated method stub return null; } @Override public int length() { // TODO Auto-generated method stub return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* interface Shape { final double PI = 3.14; void draw(); double getArea(); default public void redraw() { System.out.print("--- 다시 그립니다. "); draw(); } } class Circle implements Shape{ int r; public Circle(int r) { this.r = r; } public void draw() { System.out.println("반지름이 " + r + "인 원입니다."); } public double getArea() { return r*r*PI; } } class Main { public static void main(String [] args) { Shape donut = new Circle(10); donut.redraw(); System.out.println("면적은 " + donut.getArea()); } } interface MouseDriver { final int BUTTONS = 3; //int VERSION; void move(); public int click(); int out(); void drag(); default void drop() { System.out.println("drop"); } } GUI : 그림으로 보여지는것 컨테이너 container : 종이 판 (다른 것들을 포함한다) 컴포넌트 component : 스티커 (컨테이너 위에 올라간다) import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("example1"); // 창 제목 정하기 (필수아님) setSize(500, 300); //가로500, 세로 300으로 크기 셋팅 (필수) Container c = getContentPane(); // c = 현재 배경 c.setLayout(new FlowLayout()); //레이아웃 설정 (나중에) JButton b = new JButton("버튼"); //버튼 생성 c.add(b); //버튼 추가 JLabel la = new JLabel("레이블컴포넌트입니다"); c.add(la); setVisible(true); // 프레임이 보이게 설정 (필수) } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("ContentPane과 JFrame"); setSize(500,300); Container c = getContentPane(); //c.setLayout(new FlowLayout()); c.setLayout(new BorderLayout(5,10)); c.setBackground(Color.black); //배경색 설정 JButton b = new JButton("OK"); b.setBackground(Color.white); b.setForeground(Color.black); c.add(b,BorderLayout.NORTH); // JButton b1 = new JButton("Cancel"); // b1.setBackground(Color.white); // b1.setForeground(Color.black); //전경색 (글자색) 설정 // c.add(b1, BorderLayout.CENTER); JButton b2 = new JButton("Ignore"); b2.setForeground(Color.black); c.add(b2, BorderLayout.CENTER); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.setBackground(Color.black); c.add(p,BorderLayout.EAST); JButton b3 = new JButton("Cancel"); p.add(new JButton("Cancel")); p.add(b3); b3.setForeground(Color.blue); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("Let's study Java"); setSize(400,200); Container c = getContentPane(); c.setLayout(new BorderLayout(5,7)); JButton b = new JButton("Center"); c.add(b, BorderLayout.CENTER); JButton b1 = new JButton("East"); c.add(b1, BorderLayout.EAST); JButton b2 = new JButton("West"); c.add(b2, BorderLayout.WEST); JButton b3 = new JButton("North"); c.add(b3, BorderLayout.NORTH); JButton b4 = new JButton("South"); c.add(b4, BorderLayout.SOUTH); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("GridLayour Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 2); grid.setVgap(5); Container c = getContentPane(); c.setLayout(grid); c.add(new JLabel(" 이름")); c.add(new JTextField("")); c.add(new JLabel(" 학번")); c.add(new JTextField("")); c.add(new JLabel(" 학과")); c.add(new JTextField("")); c.add(new JLabel(" 과목")); c.add(new JTextField("")); setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Null Container Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); JLabel la = new JLabel("Hello, Press Buttons!"); la.setLocation(130, 50); la.setSize(200, 20); c.add(la); for(int i=1; i<=9; i++) { JButton b = new JButton(Integer.toString(i)); //*********************** //random // //************************ int x = (int) Math.random()*200; int y = (int) Math.random()*300; b.setLocation(x, y); b.setSize(50,20); c.add(b); } setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Ten Color Buttons Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(1, 10); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red, Color.orange, Color.yellow, Color.green, Color.cyan, Color.blue, Color.magenta, Color.darkGray, Color.pink, Color.gray }; for(int i=0; i<10; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("4x4 Color Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 4); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue,Color.magenta,Color.darkGray,Color.pink,Color.gray,Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue}; for(int i=0; i<16; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Random Labels"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); for(int i=0; i<20; i++) { JLabel la = new JLabel(Integer.toString(i)); int x = (int)(Math.random()*200) + 50; int y = (int)(Math.random()*200) + 50; la.setLocation(x,y); la.setSize(15, 15); la.setBackground(Color.blue); la.setForeground(Color.white); la.setOpaque(true); c.add(la); } } public static void main(String[] args) { new Main(); } } event : 모든 변화 ex) 마우스 클릭, 버튼 클릭, 버튼에 마우스 올려놓기, 키보드 a 누르기 ..... action event key event mouse evet change event ... event listener : 이벤트를 어떻게 처리할지 적어놓는 곳 import javax.swing.*; import java.awt.*; import java.awt.event.*; // event를 처리하는 부분 public class Main extends JFrame { Container c = getContentPane(); JPanel nor = new JPanel(); JPanel cen = new JPanel(); JButton b = new JButton("set black"); JButton b1 = new JButton("set red"); JButton b2 = new JButton("set pink"); public Main() { setTitle("Open Challenge 9"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new BorderLayout(3,3)); nor.setLayout(new FlowLayout()); nor.setBackground(Color.LIGHT_GRAY); cen.setLayout(null); //액션리스너 붙여주기 b.addActionListener(new MyActionListener()); b1.addActionListener(new MyActionListener()); b2.addActionListener(new MyActionListener()); nor.add(b); nor.add(b1); nor.add(b2); JLabel l = new JLabel("Hello"); l.setLocation(50,150); l.setSize(30, 30); cen.add(l); JLabel l1 = new JLabel("Java"); l1.setLocation(250,80); l1.setSize(30, 30); cen.add(l1); JLabel l2 = new JLabel("Love"); l2.setLocation(70,50); l2.setSize(30, 30); cen.add(l2); c.add(cen, BorderLayout.CENTER); c.add(nor, BorderLayout.NORTH); setSize(300,300); setVisible(true); } //액션리스너 클래스 작성하기 class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { JButton bb = (JButton)e.getSource(); // 이벤트가 일어난 버튼을 bb if(bb==b) cen.setBackground(Color.black); else if(bb==b1) cen.setBackground(Color.red); else cen.setBackground(Color.pink); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { public Main() { setTitle("Action 이벤트 리스너 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JButton btn = new JButton("Action"); btn.addActionListener(new MyActionListener()); c.add(btn); setSize(350,150); setVisible(true); } public static void main(String[] args) { new Main(); } } class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { JButton b = (JButton)e.getSource(); if(b.getText().equals("Action")) b.setText("액션"); else b.setText("Action"); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { private JLabel la = new JLabel("Hello"); public Main() { setTitle("Mouse 이벤트 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.addMouseListener(new MyMouseListener()); c.setLayout(null); la.setSize(50, 20); la.setLocation(30, 30); c.add(la); setSize(250, 250); setVisible(true); } //implements MouseListener class MyMouseListener extends MouseAdapter { public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } // public void mousePressed(MouseEvent e) { // int x = e.getX(); // int y = e.getY(); // la.setLocation(x, y); // } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("마우스 올리기 내리기"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); la.addMouseListener(new MyMouseListener()); c.setLayout(null); la.setSize(100, 20); la.setLocation(140, 30); c.add(la); setSize(350, 250); setVisible(true); }//implements MouseListener class MyMouseListener extends MouseAdapter { public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub la.setText("Love Java"); } public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub la.setText("사랑해"); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { Container c = getContentPane(); public Main() { setTitle("드래깅동안 YELLOW"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.addMouseMotionListener(new MyMouseMotionListener()); setSize(350, 250); setVisible(true); } class MyMouseMotionListener extends MouseAdapter{ public void mouseDragged(MouseEvent e) { c.setBackground(Color.YELLOW); } public void mouseMoved(MouseEvent e) { c.setBackground(Color.GREEN); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel [] keyMessage; public Main() { setTitle("keyListener 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.addKeyListener(new MyKeyListener()); keyMessage = new JLabel [3]; keyMessage[0] = new JLabel(" getKeyCode() "); keyMessage[1] = new JLabel(" getKeyCode() "); keyMessage[2] = new JLabel(" getKeyCode() "); for(int i=0; i<keyMessage.length; i++) { c.add(keyMessage[i]); keyMessage[i].setOpaque(true); keyMessage[i].setBackground(Color.CYAN); } setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); char keyChar = e.getKeyChar(); keyMessage[0].setText(Integer.toString(keyCode)); keyMessage[1].setText(Character.toString(keyChar)); keyMessage[2].setText(KeyEvent.getKeyText(keyCode)); System.out.println("KeyPressed"); } public void keyReleased(KeyEvent e) { System.out.println("KeyReleased"); } public void keyTyped(KeyEvent e) { System.out.println("KeyTyped"); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel keyMessage; Container c = getContentPane(); public Main() { setTitle("Key Code 예제 : F1키:초록색, %키:노랑색"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new FlowLayout()); c.addKeyListener(new MyKeyListener()); keyMessage = new JLabel("키가 입력되었음"); c.add(keyMessage); setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); char keyChar = e.getKeyChar(); keyMessage.setText(Character.toString(keyChar)+"키가 입력되었음"); if(e.getKeyChar()=='%') { c.setBackground(Color.yellow); } if(e.getKeyCode()==KeyEvent.VK_F1) { c.setBackground(Color.green); } System.out.println("KeyPressed"); } public void keyReleased(KeyEvent e) { System.out.println("KeyReleased"); } public void keyTyped(KeyEvent e) { System.out.println("KeyTyped"); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("HELLO"); public Main() { setTitle("상,하,좌,우 키를 이용하여 텍스트 움직이기"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); la.setLocation(50, 50); la.setSize(50,50); c.add(la); setSize(300, 300); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int x = la.getX(); int y = la.getY(); if(e.getKeyCode()==KeyEvent.VK_UP) { la.setLocation(x,y-10); } else if(e.getKeyCode()==KeyEvent.VK_DOWN) { la.setLocation(x,y+10); } else if(e.getKeyCode()==KeyEvent.VK_LEFT) { la.setLocation(x-10,y); } else if(e.getKeyCode()==KeyEvent.VK_RIGHT) { la.setLocation(x+10,y); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Click and DoubleClick 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.addMouseListener(new MyMouseListener()); setSize(300,200); setVisible(true); } class MyMouseListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { if(e.getClickCount() == 2) { int r = (int)(Math.random()*256); int g = (int)(Math.random()*256); int b = (int)(Math.random()*256); Component c = (Component)e.getSource(); c.setBackground(new Color(r, b, g)); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("Left 키로 문자열 교체"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); c.add(la); la.setSize(100,100); la.setLocation(110,0); setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_LEFT) { String latext = la.getText(); if(latext.equals("Love Java")) { la.setText("avaJ evoL"); } else { la.setText("Love Java"); } } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("Left 키로 문자열 이동"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); c.add(la); la.setSize(100,100); la.setLocation(110,0); setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_LEFT) { String text = la.getText(); String aftertext = text.substring(1) + text.substring(0,1); la.setText(aftertext); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("+,- 키로 폰트 크기 조절"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); c.add(la); la.setSize(400,100); la.setLocation(110,0); la.setFont(new Font("Arial", Font.PLAIN, 10)); setSize(400, 300); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { Font f = la.getFont(); int size = f.getSize(); if(e.getKeyChar() == '+'&&size+5<100) { la.setFont(new Font("Arial", Font.PLAIN, size+5)); } else if(e.getKeyChar() == '-'&&size-5>5) { la.setFont(new Font("Arial", Font.PLAIN, size-5)); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("C"); public Main() { setTitle("클릭 연습 용 응용프로그램"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); la.addMouseListener(new MyMouseListener()); la.setSize(100,100); la.setLocation(100, 100); c.add(la); setSize(300, 250); setVisible(true); } class MyMouseListener extends MouseAdapter{ public void mousePressed(MouseEvent e) { int x = (int)(Math.random()*250); int y = (int)(Math.random()*200); la.setLocation(x, y); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("마우스 휠을 굴려 폰트 크기 조절"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); la.addMouseWheelListener(new MyMouseWheelListener()); c.add(la); la.setSize(500,300); la.setLocation(210, -50); c.setLayout(null); la.setFont(new Font("Arial", Font.PLAIN, 10)); setSize(500, 250); setVisible(true); } class MyMouseWheelListener extends MouseAdapter{ public void mouseWheelMoved(MouseWheelEvent e) { Font f = la.getFont(); int size = f.getSize(); int n = e.getWheelRotation(); if(n<0&&size-5>5) { la.setFont(new Font("Arial", Font.PLAIN, size-5)); } else if(n>0&&size+5<100) { la.setFont(new Font("Arial", Font.PLAIN, size+5)); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("레이블 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel textLabel = new JLabel("사랑합니다."); ImageIcon heart = new ImageIcon("images/heart.png"); JLabel imageLabel = new JLabel(heart); ImageIcon phone = new ImageIcon("images/phone.png"); JLabel label = new JLabel("보고싶으면 전화하세요", phone, SwingConstants.CENTER); c.add(textLabel); c.add(imageLabel); c.add(label); setSize(400,200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("이미지 버튼 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); ImageIcon normalIcon = new ImageIcon("images/phone.png"); ImageIcon rolloverIcon = new ImageIcon("images/incoming-call.png"); ImageIcon pressedIcon = new ImageIcon("images/conversation.png"); JButton btn = new JButton("call~~", normalIcon); btn.setPressedIcon(pressedIcon); btn.setRolloverIcon(rolloverIcon); c.add(btn); setSize(250,150); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("체크박스 만들기 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); ImageIcon cherryIcon = new ImageIcon("images/cherries.png"); ImageIcon selectedCherryIcon = new ImageIcon("images/berry.png"); JCheckBox apple = new JCheckBox("사과"); JCheckBox pear = new JCheckBox("배"); JCheckBox cherry = new JCheckBox("체리", cherryIcon); c.add(apple); c.add(pear); c.add(cherry); cherry.setSelectedIcon(selectedCherryIcon); cherry.setBorderPainted(true); setSize(300,250); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JCheckBox [] fruits = new JCheckBox [3]; private String [] names = {"사과","배","체리"}; private JLabel sumLabel; public Main() { setTitle("첵크박스와 ItemEvent 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add(new JLabel("사과 100원, 배 500원 , 체리 20000원")); for(int i=0; i<fruits.length; i++) { fruits[i] = new JCheckBox(names[i]); fruits[i].addItemListener(new MyItemListener()); fruits[i].setBorderPainted(true); c.add(fruits[i]); } sumLabel = new JLabel("현재 0원 입니다."); c.add(sumLabel); setSize(250,200); setVisible(true); } class MyItemListener implements ItemListener { private int sum = 0; public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { if(e.getItem() == fruits[0]) sum += 100; else if(e.getItem() == fruits[1]) sum += 500; else sum += 20000; } else { if(e.getItem() == fruits[0]) sum -= 100; else if(e.getItem() == fruits[1]) sum -= 500; else sum -= 20000; } sumLabel.setText("현재 " + sum + "원 입니다."); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("라디오버튼 만들기 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); ImageIcon cherryIcon = new ImageIcon("images/cherries.png"); ImageIcon selectedCherryIcon = new ImageIcon("images/berry.png"); ButtonGroup g = new ButtonGroup(); JRadioButton apple = new JRadioButton("사과"); JRadioButton pear = new JRadioButton("배"); JRadioButton cherry = new JRadioButton("체리", cherryIcon); cherry.setBorderPainted(true); cherry.setSelectedIcon(selectedCherryIcon); g.add(apple); g.add(pear); g.add(cherry); c.add(apple); c.add(pear); c.add(cherry); setSize(250,150); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JRadioButton [] radio = new JRadioButton [3]; private String [] text = {"사과","배","체리"}; private ImageIcon [] image = { new ImageIcon("images/apple.png"), new ImageIcon("images/pear.png"), new ImageIcon("images/berry.png") }; private JLabel imageLabel = new JLabel(); public Main() { setTitle("라디오버튼 Item Event 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new BorderLayout()); JPanel radioPanel = new JPanel(); radioPanel.setBackground(Color.gray); ButtonGroup g = new ButtonGroup(); for(int i=0; i<radio.length; i++) { radio[i] = new JRadioButton(text[i]); g.add(radio[i]); radioPanel.add(radio[i]); radio[i].addItemListener(new MyItemListener()); } radio[2].setSelected(true); c.add(radioPanel, BorderLayout.NORTH); c.add(imageLabel, BorderLayout.CENTER); imageLabel.setHorizontalAlignment(SwingConstants.CENTER); setSize(250,150); setVisible(true); } class MyItemListener implements ItemListener { public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == e.DESELECTED) return; if(radio[0].isSelected()) imageLabel.setIcon(image[0]); else if(radio[1].isSelected()) imageLabel.setIcon(image[1]); else imageLabel.setIcon(image[2]); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("텍스트필드 만들기 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); setLayout(new FlowLayout()); c.add(new JLabel("이름 ")); c.add(new JTextField(20)); c.add(new JLabel("힉과 ")); c.add(new JTextField("컴퓨터공학과", 20)); c.add(new JLabel("주소")); c.add(new JTextField("서울시", 20)); setSize(250,150); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JTextField tf = new JTextField(20); private JTextArea ta = new JTextArea(7,20); public Main() { setTitle("텍스트 영역 만들기 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); setLayout(new FlowLayout()); // JButton b = new JButton("Delete"); // b.addActionListener(new ActionListener() { // public void actionPerformed(ActionEvent e) { // ta.setText(""); // } // }); c.add(new JLabel("입력 후 <Enter> 키를 입력하세요.")); c.add(tf); c.add(new JScrollPane(ta)); // c.add(b); tf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JTextField t = (JTextField)e.getSource(); ta.append(t.getText() + "\n"); t.setText(""); } }); setSize(300,300); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; import javax.swing.event.*; public class Main extends JFrame { private JLabel la = new JLabel(); JSlider slider = new JSlider(JSlider.HORIZONTAL, 100, 200, 100); public Main() { setTitle("JSlider Practice Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); slider.setPaintLabels(true); slider.setPaintTicks(true); slider.setPaintTrack(true); slider.setMajorTickSpacing(20); //slider.setMinorTickSpacing(10); c.add(slider); c.add(la); setSize(300, 100); setVisible(true); } class MyChangeListener implements ChangeListener { public void stateChanged(ChangeEvent e) { } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { private JTextArea ta = new JTextArea(5, 30); JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 0); Container c = getContentPane(); public Main() { setTitle("TextArea Practice Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new FlowLayout()); ta.addKeyListener(new MyKeyListener()); slider.addChangeListener(new MyChangeListener()); slider.setPaintLabels(true); slider.setPaintTicks(true); slider.setPaintTrack(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); c.add(ta); c.add(slider); setSize(350, 200); setVisible(true); ta.setFocusable(true); ta.requestFocus(); } class MyChangeListener implements ChangeListener { public void stateChanged(ChangeEvent e) { String str = ta.getText(); int len = slider.getValue(); if(str.length()>len) str = str.substring(0, len); ta.setText(str); } } class MyKeyListener extends KeyAdapter { public void keyTyped(KeyEvent e) { int len = ta.getText().length(); slider.setValue(len); if(len == 100) { ta.setFocusable(false); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { private JLabel la = new JLabel("I Love Java"); JSlider slider = new JSlider(JSlider.HORIZONTAL, 1, 100, 1); public Main() { setTitle("JSlider Practice Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new BorderLayout()); slider.addChangeListener(new MyChangeListener()); la.setFont(new Font("Arial", Font.PLAIN, 1)); slider.setPaintLabels(true); slider.setPaintTicks(true); slider.setPaintTrack(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); c.add(slider, BorderLayout.NORTH); c.add(la, BorderLayout.CENTER); setSize(400, 250); setVisible(true); } class MyChangeListener implements ChangeListener { public void stateChanged(ChangeEvent e) { la.setFont(new Font("Arial", Font.PLAIN, slider.getValue())); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; import javax.swing.event.*; public class Main extends JFrame { public Main() { setTitle("가위 바위 보 게임"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JPanel p = new JPanel(); p.setBackground(Color.GRAY); c.add(p); setSize(400, 300); setVisible(true); } public static void main(String[] args) { new Main(); } } */
0
0
6
minhyuk0426
2023년 3월 25일
In 소스 코드 제출
/* import java.util.Scanner; class Circle { static int num; private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Circle c[] = new Circle[3]; // for (int i = 0; i < c.length; i++) { // System.out.print("x, y, radius >>"); // double x = scanner.nextDouble(); // double y = scanner.nextDouble(); // int radius = scanner.nextInt(); // c[i] = new Circle(x, y, radius); // } // for(int i=0; i<c.length; i++) // c[i].show(); // Circle c = new Circle(1,1,2); c.num=0; Circle d = new Circle(2,2,3); d.num++; //System.out.println(d.num); System.out.println(Circle.num); scanner.close(); } } */ /* import java.util.Scanner; class Phone{ int num; String name; String tel; public Phone(String name,String tel) { this.name=name; this.tel=tel; } public void show() { System.out.println(); } } class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); //1. System.out.print("인원수>>"); int n = sc.nextInt(); Phone p[] = new Phone[n]; //2. scan 받아서 객체 생성 for(int i=0;i<n;i++) { System.out.print("이름과 전화번호(이름과 번호는 빈 칸없이 입력)>>"); String name = sc.next(); String tel = sc.next(); p[i] = new Phone(name,tel); } System.out.println("저장되었습니다..."); //3. 검색 while(true) { System.out.print("검색할 이름>>"); String name = sc.next(); if(name.equals("그만")) { return ; } else { boolean isFind = false; for(int i=0;i<n;i++) { if(p[i].name.equals(name)) { System.out.println(p[i].name+"의 번호는 "+p[i].tel+" 입니다."); isFind = true; break; } } if(isFind==false) System.out.println(name+" 이 없습니다."); } } } } */ /* -랜덤으로 1에서 100 까지의 숫자를 고른 뒤 플레이어가 그 숫자를 맞추게 함 -자신이 예상한 숫자를 textfield에다가 입력 -10번 안에 맞추어야 하고 그러지 못하면 게임에서 진다 -게임이 끝나면 다시 메인 화면으로 돌아간다. */ /* import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class Main extends JFrame{ ImageIcon updown= new ImageIcon("arrows.png"); ImageIcon up= new ImageIcon("up-arrow.png"); ImageIcon down= new ImageIcon("down-arrow.png"); JLabel MainImage= new JLabel(updown); JButton startB = new JButton("Game Start",updown); JLabel MainL= new JLabel("UP AND DOWN GAME"); JTextField gameT= new JTextField(10); JLabel gameL= new JLabel("1~100 숫자를 입력하시오:"); JLabel gameUD= new JLabel(); Container c= getContentPane(); String a; int t, c1=0; JLabel cL= new JLabel("남은 횟수: 15"); int flag=2; JButton finish = new JButton("메인 화면으로 돌아가기"); JLabel wl= new JLabel(); int com; public Main() { setTitle("Up and Down Game"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(null); c.setBackground(Color.YELLOW); startB.setSize(200,200); startB.setLocation(200,200); startB.addActionListener(new myActionListener()); c.add(startB); Font font= new Font("UP AND DOWN GAME",Font.BOLD,40); MainL.setFont(font); MainL.setSize(500, 100); MainL.setLocation(90,100); c.add(MainL); gameT.setLocation(246,40); gameT.setSize(200,30); gameT.setVisible(false); gameT.addActionListener(new myActionListener1()); c.add(gameT); Font font1= new Font("UP AND DOWN GAME",Font.BOLD,13); gameL.setFont(font1); gameL.setLocation(90,5); gameL.setSize(500,100); gameL.setVisible(false); c.add(gameL); Font font2= new Font("UP AND DOWN GAME",Font.BOLD,13); cL.setFont(font2); cL.setLocation(500,-35); cL.setSize(500,100); cL.setVisible(false); c.add(cL); Font font3= new Font("UP AND DOWN GAME",Font.BOLD,60); gameUD.setFont(font3); gameUD.setLocation(220,250); gameUD.setSize(500,100); c.add(gameUD); Font font4= new Font("UP AND DOWN GAME",Font.BOLD,30); wl.setFont(font4); wl.setLocation(60,230);; wl.setSize(500,100);; c.add(wl); wl.setVisible(false); finish.setLocation(350,480); finish.setSize(200,70); c.add(finish); finish.setVisible(false); finish.addActionListener(new myActionListener2()); setSize(600,600); setVisible(true); } class myActionListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { com=(int)(Math.random()*100); startB.setVisible(false); MainL.setVisible(false); gameT.setVisible(true); gameL.setVisible(true); cL.setVisible(true); System.out.println(com); } } class myActionListener1 implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { a=e.getActionCommand(); t=Integer.parseInt(a); c1++; run(); } } class myActionListener2 implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { StartScreen(); } } public void StartScreen() { finish.setVisible(false); wl.setVisible(false); wl.setText(""); c1=0; startB.setVisible(true); gameT.setText(""); MainL.setVisible(true); } public void run() { cL.setText("남은 횟수: "+Integer.toString(15-c1)); if(t>com) { gameUD.setText("DOWN"); gameUD.setForeground(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255))); } if(t<com) { gameUD.setText("UP"); gameUD.setForeground(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255))); } if(t==com&&c1<11) { flag=1; result(); } if(c1>14) { flag=0; result(); } } public void result() { gameT.setVisible(false); gameL.setVisible(false); cL.setVisible(false); gameUD.setText(""); wl.setVisible(true); finish.setVisible(true); if(flag==1) { wl.setText("축하합니다 "+Integer.toString(c1)+"번 만에 맞추셨습니다"); } if(flag==0) { wl.setText("숫자는 "+Integer.toString(com)+" 였습니다"); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class TimerThread extends Thread { private JLabel timerLabel; public TimerThread(JLabel timerLabel) { this.timerLabel = timerLabel; } public void run() { int n=0;; while(true) { timerLabel.setText(Integer.toString(n)); n++; try { Thread.sleep(1000); } catch(InterruptedException e) { return; } } } } public class Main extends JFrame{ public Main() { setTitle("Thread를 상속받은 타이머 스레드 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c= getContentPane(); c.setLayout(new FlowLayout()); JLabel timerLabel= new JLabel(); timerLabel.setFont(new Font("Gothic",Font.ITALIC,80)); c.add(timerLabel); TimerThread th = new TimerThread(timerLabel); setSize(300,170); setVisible(true); th.start(); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class TimerRunnable implements Runnable{ private JLabel timerLabel; public TimerRunnable(JLabel timerLabel) { this.timerLabel = timerLabel; } public void run() { int n=0; while(true) { timerLabel.setText(Integer.toString(n)); n++; try { Thread.sleep(1000); } catch(InterruptedException e) { return; } } } } public class Main extends JFrame{ public Main() { setTitle("Runnable을 구현한 타이머 스레드 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel timerLabel= new JLabel(); timerLabel.setFont(new Font("Gothic", Font.ITALIC,80)); c.add(timerLabel); TimerRunnable runnable = new TimerRunnable(timerLabel); Thread th = new Thread(runnable); setSize(250,150); setVisible(true); th.start(); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class FlickeringLabel extends JLabel implements Runnable{ private long delay; public FlickeringLabel(String text, long delay) { super(text); this.delay= delay; setOpaque(true); Thread th= new Thread(this); th.start(); } public void run() { int n=0; while(true) { if(n==0) setBackground(Color.YELLOW); else setBackground(Color.GREEN); if(n==0) n=1; else n=0; try { Thread.sleep(delay); } catch(InterruptedException e) { return; } } } } public class Main extends JFrame{ public Main() { setTitle("FlickeringLabelEx 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); FlickeringLabel fLabel = new FlickeringLabel("깜박",500); JLabel label = new JLabel("안깜박"); FlickeringLabel fLabel2 = new FlickeringLabel("깜박",300); c.add(fLabel); c.add(label); c.add(fLabel2); setSize(300,150); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; class TimerRunnable implements Runnable { private JLabel timerLabel; public TimerRunnable(JLabel timerLabel) { this.timerLabel = timerLabel; } public void run() { int n=0; while(true) { timerLabel.setText(Integer.toString(n)); n++; try { Thread.sleep(1000); } catch(InterruptedException e) { return; } } } } public class Main extends JFrame{ private Thread th; public Main() { setTitle("ThreadInterruptEx 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel timerLabel = new JLabel(); timerLabel.setFont(new Font("Gothic",Font.ITALIC,80)); TimerRunnable runnable = new TimerRunnable(timerLabel); th=new Thread(runnable); c.add(timerLabel); JButton btn = new JButton("kill Timer"); btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { th.interrupt(); JButton btn = (JButton)e.getSource(); btn.setEnabled(false); } }); c.add(btn); setSize(300,170); setVisible(true); th.start(); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; class RandomThread extends Thread{ private Container contentPane; private boolean flag=false; public RandomThread(Container contentPane) { this.contentPane = contentPane; } public void finish() { flag = true; } public void run() { while(true) { int x = ((int)(Math.random()*contentPane.getWidth())); int y = ((int)(Math.random()*contentPane.getHeight())); JLabel label = new JLabel("java"); label.setSize(80,30); label.setLocation(x,y); contentPane.add(label); contentPane.repaint(); try { Thread.sleep(300); if(flag==true) { contentPane.removeAll(); label = new JLabel("finish"); label.setSize(80,30); label.setLocation(100,100); label.setForeground(Color.RED); contentPane.add(label); contentPane.repaint(); return; } } catch(InterruptedException e) {return;} } } } public class Main extends JFrame{ private RandomThread th; public Main() { setTitle("ThreadFinsihFlagEx 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { th.finish(); } }); setSize(300,200); setVisible(true); th = new RandomThread(c); th.start(); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import java.util.Calendar; import javax.swing.*; class clock extends Thread{ JLabel clockL= new JLabel(); public void run() { String clockText; int hour,min,second; while(true) { Calendar c= Calendar.getInstance(); hour = c.get(Calendar.HOUR_OF_DAY); min = c.get(Calendar.MINUTE); second = c.get(Calendar.SECOND); clockText = Integer.toString(hour-12); clockText = clockText.concat("시 "); clockText = clockText.concat(Integer.toString(min)); clockText = clockText.concat("분 "); clockText = clockText.concat(Integer.toString(second)); clockText = clockText.concat("초 "); clockL.setText(clockText); try { Thread.sleep(1000); } catch(InterruptedException e) { return; } } } } public class Main extends JFrame{ public Main(){ setTitle("Digital clock"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c= getContentPane(); c.setLayout(new FlowLayout()); clock cl = new clock(); c.add(cl.clockL); setSize(300,300); setVisible(true); cl.start(); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; class Main extends JFrame{ MyPanel panel = new MyPanel(); public Main(){ setTitle("Random Circle"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setContentPane(panel); setSize(500,500); setVisible(true); } class MyPanel extends JPanel implements Runnable{ int x=0,y=0; public MyPanel() { addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e) { run(); } }); } public void run() { while(true) { x=(int)(Math.random()*300); y=(int)(Math.random()*300); try { Thread.sleep(400); }catch(InterruptedException e) { return; } repaint(); } } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawOval(x, y, 50, 50); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; import java.awt.event.*; class Main extends JFrame{ private JCheckBox [] buttons = new JCheckBox [2]; private String [] names = {"버튼 비활성화","버튼 감추기"}; JButton b = new JButton("test button"); public Main(){ setTitle("CheckBox Practice"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); MyItemListener listener = new MyItemListener(); for(int i=0; i<buttons.length; i++) { buttons[i] = new JCheckBox(names[i]); c.add(buttons[i]); buttons[i].addItemListener(listener); } c.add(b); setSize(300,200); setVisible(true); } class MyItemListener implements ItemListener { public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { if(e.getItem() == buttons[0]) { b.setEnabled(false); } else if(e.getItem() == buttons[1]) { b.setVisible(false); } } else { b.setEnabled(true); b.setVisible(true); } } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; import java.awt.event.*; class Main extends JFrame{ public Main(){ JButton b = new JButton("계산"); setTitle("Money Changer"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.setBackground(Color.PINK); JTextField mtf = new JTextField(20); c.add(new JLabel("금액 ")); c.add(mtf); c.add(b); JLabel[] la = new JLabel[8]; String[] lastr = {"오만원 ","만원 ","천원 ","500원 ","100원 ","50원 ","10원 ","1원 "}; JTextField[] tf = new JTextField[8]; for(int i=0;i<la.length;i++) { la[i] = new JLabel(lastr[i]); tf[i] = new JTextField(20); c.add(la[i]); c.add(tf[i]); } b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int money = Integer.valueOf(mtf.getText()); //System.out.println(money); tf[0].setText(Integer.toString(money/50000)); money=money%50000; tf[1].setText(Integer.toString(money/10000)); money=money%10000; tf[2].setText(Integer.toString(money/1000)); money=money%1000; tf[3].setText(Integer.toString(money/500)); money=money%500; tf[4].setText(Integer.toString(money/100)); money=money%100; tf[5].setText(Integer.toString(money/50)); money=money%50; tf[6].setText(Integer.toString(money/10)); money=money%10; tf[7].setText(Integer.toString(money/1)); money=money%1; } }); setSize(350,300); setVisible(true); } class MyItemListener implements ItemListener { int sum; public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { } } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; import java.awt.event.*; class Main extends JFrame{ JCheckBox[] jc = new JCheckBox[7]; JTextField[] tf = new JTextField[8]; public Main(){ JButton b = new JButton("계산"); setTitle("Money Changer with CheckBox"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.setBackground(Color.PINK); JTextField mtf = new JTextField(20); c.add(new JLabel("금액 ")); c.add(mtf); c.add(b); JLabel[] la = new JLabel[8]; String[] lastr = {"오만원 ","만원 ","천원 ","500원 ","100원 ","50원 ","10원 ","1원 "}; for(int i=0;i<la.length;i++) { la[i] = new JLabel(lastr[i]); tf[i] = new JTextField(20); if(i != 7) { jc[i] = new JCheckBox(); } c.add(la[i]); c.add(tf[i]); if(i != 7) { c.add(jc[i]); } } b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int money = Integer.valueOf(mtf.getText()); //System.out.println(money); if(jc[0].isSelected()==true) { tf[0].setText(Integer.toString(money/50000)); money=money%50000; } if(jc[1].isSelected()==true) { tf[1].setText(Integer.toString(money/10000)); money=money%10000; } if(jc[2].isSelected()==true) { tf[2].setText(Integer.toString(money/1000)); money=money%1000; } if(jc[3].isSelected()==true) { tf[3].setText(Integer.toString(money/500)); money=money%500; } if(jc[4].isSelected()==true) { tf[4].setText(Integer.toString(money/100)); money=money%100; } if(jc[5].isSelected()==true) { tf[5].setText(Integer.toString(money/50)); money=money%50; } if(jc[6].isSelected()==true) { tf[6].setText(Integer.toString(money/10)); money=money%10; } tf[7].setText(Integer.toString(money)); } }); setSize(350,300); setVisible(true); } public static void main(String[] args) { new Main(); } } import java.awt.*; import javax.swing.*; import java.awt.event.*; class Main extends JFrame{ private JRadioButton [] radiob = new JRadioButton [2]; private String [] names = {"left","right"}; private ImageIcon [] images = { new ImageIcon("river.png"), new ImageIcon("aaa.png"), new ImageIcon("bridge.png")}; private int d=0; private JLabel imageLabel = new JLabel(); public Main(){ setTitle("Image Gallery Practice Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new BorderLayout()); JPanel up = new JPanel(); JPanel down = new JPanel(); c.add(up,BorderLayout.NORTH); c.add(down,BorderLayout.CENTER); up.setLayout(new FlowLayout()); MyItemListener listener = new MyItemListener(); ButtonGroup g = new ButtonGroup(); for(int i=0; i<radiob.length; i++) { radiob[i] = new JRadioButton(names[i]); up.add(radiob[i]); g.add(radiob[i]); radiob[i].addItemListener(listener); } down.add(imageLabel); setSize(400,300); setVisible(true); } class MyItemListener implements ItemListener { public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { if(radiob[0].isSelected()) { if(d==0) d=2; else d--; } else { if(d==2) d=0; else d++; } imageLabel.setIcon(images[d]); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("슬라이더 만들기 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 200, 100); slider.setPaintLabels(true); slider.setPaintTicks(true); slider.setPaintTrack(true); slider.setMajorTickSpacing(50); slider.setMinorTickSpacing(10); c.add(slider); setSize(300, 100); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; import javax.swing.event.*; public class Main extends JFrame { private JLabel colorLabel; private JSlider [] sl = new JSlider [3]; public Main() { setTitle("슬라이더와 ChangeEvent 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); colorLabel = new JLabel(" SLIDER EXAMPLE "); for(int i=0; i<sl.length; i++) { sl[i] = new JSlider(JSlider.HORIZONTAL, 0, 255, 128); sl[i].setPaintLabels(true); sl[i].setPaintTicks(true); sl[i].setPaintTrack(true); sl[i].setMajorTickSpacing(50); sl[i].setMinorTickSpacing(10); sl[i].addChangeListener(new MyChangeListener()); c.add(sl[i]); } sl[0].setForeground(Color.RED); sl[1].setForeground(Color.GREEN); sl[2].setForeground(Color.BLUE); int r = sl[0].getValue(); int g = sl[1].getValue(); int b = sl[2].getValue(); colorLabel.setOpaque(true); colorLabel.setBackground(new Color(r,g,b)); c.add(colorLabel); setSize(300, 230); setVisible(true); } class MyChangeListener implements ChangeListener { public void stateChanged(ChangeEvent e) { int r = sl[0].getValue(); int g = sl[1].getValue(); int b = sl[2].getValue(); colorLabel.setBackground(new Color(r,g,b)); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; import javax.swing.event.*; public class Main extends JFrame { private JLabel la = new JLabel(); JSlider slider = new JSlider(JSlider.HORIZONTAL, 100, 200, 100); public Main() { setTitle("JSlider Practice Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); slider.setPaintLabels(true); slider.setPaintTicks(true); slider.setPaintTrack(true); slider.setMajorTickSpacing(20); //slider.setMinorTickSpacing(10); c.add(slider); c.add(la); setSize(300, 100); setVisible(true); } class MyChangeListener implements ChangeListener { public void stateChanged(ChangeEvent e) { String str = slider.getValue() la.setText(str); } } public static void main(String[] args) { new Main(); } } */
0
0
3
minhyuk0426
2023년 3월 18일
In 소스 코드 제출
/* import java.util.Scanner; class Circle { static int num; private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Circle c[] = new Circle[3]; // for (int i = 0; i < c.length; i++) { // System.out.print("x, y, radius >>"); // double x = scanner.nextDouble(); // double y = scanner.nextDouble(); // int radius = scanner.nextInt(); // c[i] = new Circle(x, y, radius); // } // for(int i=0; i<c.length; i++) // c[i].show(); // Circle c = new Circle(1,1,2); c.num=0; Circle d = new Circle(2,2,3); d.num++; //System.out.println(d.num); System.out.println(Circle.num); scanner.close(); } } */ /* import java.util.Scanner; class Phone{ int num; String name; String tel; public Phone(String name,String tel) { this.name=name; this.tel=tel; } public void show() { System.out.println(); } } class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); //1. System.out.print("인원수>>"); int n = sc.nextInt(); Phone p[] = new Phone[n]; //2. scan 받아서 객체 생성 for(int i=0;i<n;i++) { System.out.print("이름과 전화번호(이름과 번호는 빈 칸없이 입력)>>"); String name = sc.next(); String tel = sc.next(); p[i] = new Phone(name,tel); } System.out.println("저장되었습니다..."); //3. 검색 while(true) { System.out.print("검색할 이름>>"); String name = sc.next(); if(name.equals("그만")) { return ; } else { boolean isFind = false; for(int i=0;i<n;i++) { if(p[i].name.equals(name)) { System.out.println(p[i].name+"의 번호는 "+p[i].tel+" 입니다."); isFind = true; break; } } if(isFind==false) System.out.println(name+" 이 없습니다."); } } } } */ /* -랜덤으로 1에서 100 까지의 숫자를 고른 뒤 플레이어가 그 숫자를 맞추게 함 -자신이 예상한 숫자를 textfield에다가 입력 -10번 안에 맞추어야 하고 그러지 못하면 게임에서 진다 -게임이 끝나면 다시 메인 화면으로 돌아간다. */ /* import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class Main extends JFrame{ ImageIcon updown= new ImageIcon("arrows.png"); ImageIcon up= new ImageIcon("up-arrow.png"); ImageIcon down= new ImageIcon("down-arrow.png"); JLabel MainImage= new JLabel(updown); JButton startB = new JButton("Game Start",updown); JLabel MainL= new JLabel("UP AND DOWN GAME"); JTextField gameT= new JTextField(10); JLabel gameL= new JLabel("1~100 숫자를 입력하시오:"); JLabel gameUD= new JLabel(); Container c= getContentPane(); String a; int t, c1=0; JLabel cL= new JLabel("남은 횟수: 15"); int flag=2; JButton finish = new JButton("메인 화면으로 돌아가기"); JLabel wl= new JLabel(); int com; public Main() { setTitle("Up and Down Game"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(null); c.setBackground(Color.YELLOW); startB.setSize(200,200); startB.setLocation(200,200); startB.addActionListener(new myActionListener()); c.add(startB); Font font= new Font("UP AND DOWN GAME",Font.BOLD,40); MainL.setFont(font); MainL.setSize(500, 100); MainL.setLocation(90,100); c.add(MainL); gameT.setLocation(246,40); gameT.setSize(200,30); gameT.setVisible(false); gameT.addActionListener(new myActionListener1()); c.add(gameT); Font font1= new Font("UP AND DOWN GAME",Font.BOLD,13); gameL.setFont(font1); gameL.setLocation(90,5); gameL.setSize(500,100); gameL.setVisible(false); c.add(gameL); Font font2= new Font("UP AND DOWN GAME",Font.BOLD,13); cL.setFont(font2); cL.setLocation(500,-35); cL.setSize(500,100); cL.setVisible(false); c.add(cL); Font font3= new Font("UP AND DOWN GAME",Font.BOLD,60); gameUD.setFont(font3); gameUD.setLocation(220,250); gameUD.setSize(500,100); c.add(gameUD); Font font4= new Font("UP AND DOWN GAME",Font.BOLD,30); wl.setFont(font4); wl.setLocation(60,230);; wl.setSize(500,100);; c.add(wl); wl.setVisible(false); finish.setLocation(350,480); finish.setSize(200,70); c.add(finish); finish.setVisible(false); finish.addActionListener(new myActionListener2()); setSize(600,600); setVisible(true); } class myActionListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { com=(int)(Math.random()*100); startB.setVisible(false); MainL.setVisible(false); gameT.setVisible(true); gameL.setVisible(true); cL.setVisible(true); System.out.println(com); } } class myActionListener1 implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { a=e.getActionCommand(); t=Integer.parseInt(a); c1++; run(); } } class myActionListener2 implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { StartScreen(); } } public void StartScreen() { finish.setVisible(false); wl.setVisible(false); wl.setText(""); c1=0; startB.setVisible(true); gameT.setText(""); MainL.setVisible(true); } public void run() { cL.setText("남은 횟수: "+Integer.toString(15-c1)); if(t>com) { gameUD.setText("DOWN"); gameUD.setForeground(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255))); } if(t<com) { gameUD.setText("UP"); gameUD.setForeground(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255))); } if(t==com&&c1<11) { flag=1; result(); } if(c1>14) { flag=0; result(); } } public void result() { gameT.setVisible(false); gameL.setVisible(false); cL.setVisible(false); gameUD.setText(""); wl.setVisible(true); finish.setVisible(true); if(flag==1) { wl.setText("축하합니다 "+Integer.toString(c1)+"번 만에 맞추셨습니다"); } if(flag==0) { wl.setText("숫자는 "+Integer.toString(com)+" 였습니다"); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class TimerThread extends Thread { private JLabel timerLabel; public TimerThread(JLabel timerLabel) { this.timerLabel = timerLabel; } public void run() { int n=0;; while(true) { timerLabel.setText(Integer.toString(n)); n++; try { Thread.sleep(1000); } catch(InterruptedException e) { return; } } } } public class Main extends JFrame{ public Main() { setTitle("Thread를 상속받은 타이머 스레드 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c= getContentPane(); c.setLayout(new FlowLayout()); JLabel timerLabel= new JLabel(); timerLabel.setFont(new Font("Gothic",Font.ITALIC,80)); c.add(timerLabel); TimerThread th = new TimerThread(timerLabel); setSize(300,170); setVisible(true); th.start(); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class TimerRunnable implements Runnable{ private JLabel timerLabel; public TimerRunnable(JLabel timerLabel) { this.timerLabel = timerLabel; } public void run() { int n=0; while(true) { timerLabel.setText(Integer.toString(n)); n++; try { Thread.sleep(1000); } catch(InterruptedException e) { return; } } } } public class Main extends JFrame{ public Main() { setTitle("Runnable을 구현한 타이머 스레드 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel timerLabel= new JLabel(); timerLabel.setFont(new Font("Gothic", Font.ITALIC,80)); c.add(timerLabel); TimerRunnable runnable = new TimerRunnable(timerLabel); Thread th = new Thread(runnable); setSize(250,150); setVisible(true); th.start(); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class FlickeringLabel extends JLabel implements Runnable{ private long delay; public FlickeringLabel(String text, long delay) { super(text); this.delay= delay; setOpaque(true); Thread th= new Thread(this); th.start(); } public void run() { int n=0; while(true) { if(n==0) setBackground(Color.YELLOW); else setBackground(Color.GREEN); if(n==0) n=1; else n=0; try { Thread.sleep(delay); } catch(InterruptedException e) { return; } } } } public class Main extends JFrame{ public Main() { setTitle("FlickeringLabelEx 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); FlickeringLabel fLabel = new FlickeringLabel("깜박",500); JLabel label = new JLabel("안깜박"); FlickeringLabel fLabel2 = new FlickeringLabel("깜박",300); c.add(fLabel); c.add(label); c.add(fLabel2); setSize(300,150); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; class TimerRunnable implements Runnable { private JLabel timerLabel; public TimerRunnable(JLabel timerLabel) { this.timerLabel = timerLabel; } public void run() { int n=0; while(true) { timerLabel.setText(Integer.toString(n)); n++; try { Thread.sleep(1000); } catch(InterruptedException e) { return; } } } } public class Main extends JFrame{ private Thread th; public Main() { setTitle("ThreadInterruptEx 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel timerLabel = new JLabel(); timerLabel.setFont(new Font("Gothic",Font.ITALIC,80)); TimerRunnable runnable = new TimerRunnable(timerLabel); th=new Thread(runnable); c.add(timerLabel); JButton btn = new JButton("kill Timer"); btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { th.interrupt(); JButton btn = (JButton)e.getSource(); btn.setEnabled(false); } }); c.add(btn); setSize(300,170); setVisible(true); th.start(); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; class RandomThread extends Thread{ private Container contentPane; private boolean flag=false; public RandomThread(Container contentPane) { this.contentPane = contentPane; } public void finish() { flag = true; } public void run() { while(true) { int x = ((int)(Math.random()*contentPane.getWidth())); int y = ((int)(Math.random()*contentPane.getHeight())); JLabel label = new JLabel("java"); label.setSize(80,30); label.setLocation(x,y); contentPane.add(label); contentPane.repaint(); try { Thread.sleep(300); if(flag==true) { contentPane.removeAll(); label = new JLabel("finish"); label.setSize(80,30); label.setLocation(100,100); label.setForeground(Color.RED); contentPane.add(label); contentPane.repaint(); return; } } catch(InterruptedException e) {return;} } } } public class Main extends JFrame{ private RandomThread th; public Main() { setTitle("ThreadFinsihFlagEx 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { th.finish(); } }); setSize(300,200); setVisible(true); th = new RandomThread(c); th.start(); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import java.util.Calendar; import javax.swing.*; class clock extends Thread{ JLabel clockL= new JLabel(); public void run() { String clockText; int hour,min,second; while(true) { Calendar c= Calendar.getInstance(); hour = c.get(Calendar.HOUR_OF_DAY); min = c.get(Calendar.MINUTE); second = c.get(Calendar.SECOND); clockText = Integer.toString(hour-12); clockText = clockText.concat("시 "); clockText = clockText.concat(Integer.toString(min)); clockText = clockText.concat("분 "); clockText = clockText.concat(Integer.toString(second)); clockText = clockText.concat("초 "); clockL.setText(clockText); try { Thread.sleep(1000); } catch(InterruptedException e) { return; } } } } public class Main extends JFrame{ public Main(){ setTitle("Digital clock"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c= getContentPane(); c.setLayout(new FlowLayout()); clock cl = new clock(); c.add(cl.clockL); setSize(300,300); setVisible(true); cl.start(); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; class Main extends JFrame{ MyPanel panel = new MyPanel(); public Main(){ setTitle("Random Circle"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setContentPane(panel); setSize(500,500); setVisible(true); } class MyPanel extends JPanel implements Runnable{ int x=0,y=0; public MyPanel() { addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e) { run(); } }); } public void run() { while(true) { x=(int)(Math.random()*300); y=(int)(Math.random()*300); try { Thread.sleep(400); }catch(InterruptedException e) { return; } repaint(); } } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawOval(x, y, 50, 50); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; import java.awt.event.*; class Main extends JFrame{ private JCheckBox [] buttons = new JCheckBox [2]; private String [] names = {"버튼 비활성화","버튼 감추기"}; JButton b = new JButton("test button"); public Main(){ setTitle("CheckBox Practice"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); MyItemListener listener = new MyItemListener(); for(int i=0; i<buttons.length; i++) { buttons[i] = new JCheckBox(names[i]); c.add(buttons[i]); buttons[i].addItemListener(listener); } c.add(b); setSize(300,200); setVisible(true); } class MyItemListener implements ItemListener { public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { if(e.getItem() == buttons[0]) { b.setEnabled(false); } else if(e.getItem() == buttons[1]) { b.setVisible(false); } } else { b.setEnabled(true); b.setVisible(true); } } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; import java.awt.event.*; class Main extends JFrame{ public Main(){ JButton b = new JButton("계산"); setTitle("Money Changer"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.setBackground(Color.PINK); JTextField mtf = new JTextField(20); c.add(new JLabel("금액 ")); c.add(mtf); c.add(b); JLabel[] la = new JLabel[8]; String[] lastr = {"오만원 ","만원 ","천원 ","500원 ","100원 ","50원 ","10원 ","1원 "}; JTextField[] tf = new JTextField[8]; for(int i=0;i<la.length;i++) { la[i] = new JLabel(lastr[i]); tf[i] = new JTextField(20); c.add(la[i]); c.add(tf[i]); } b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int money = Integer.valueOf(mtf.getText()); //System.out.println(money); tf[0].setText(Integer.toString(money/50000)); money=money%50000; tf[1].setText(Integer.toString(money/10000)); money=money%10000; tf[2].setText(Integer.toString(money/1000)); money=money%1000; tf[3].setText(Integer.toString(money/500)); money=money%500; tf[4].setText(Integer.toString(money/100)); money=money%100; tf[5].setText(Integer.toString(money/50)); money=money%50; tf[6].setText(Integer.toString(money/10)); money=money%10; tf[7].setText(Integer.toString(money/1)); money=money%1; } }); setSize(350,300); setVisible(true); } class MyItemListener implements ItemListener { int sum; public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { } } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; import java.awt.event.*; class Main extends JFrame{ JCheckBox[] jc = new JCheckBox[7]; JTextField[] tf = new JTextField[8]; public Main(){ JButton b = new JButton("계산"); setTitle("Money Changer with CheckBox"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.setBackground(Color.PINK); JTextField mtf = new JTextField(20); c.add(new JLabel("금액 ")); c.add(mtf); c.add(b); JLabel[] la = new JLabel[8]; String[] lastr = {"오만원 ","만원 ","천원 ","500원 ","100원 ","50원 ","10원 ","1원 "}; for(int i=0;i<la.length;i++) { la[i] = new JLabel(lastr[i]); tf[i] = new JTextField(20); if(i != 7) { jc[i] = new JCheckBox(); } c.add(la[i]); c.add(tf[i]); if(i != 7) { c.add(jc[i]); } } b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int money = Integer.valueOf(mtf.getText()); //System.out.println(money); if(jc[0].isSelected()==true) { tf[0].setText(Integer.toString(money/50000)); money=money%50000; } if(jc[1].isSelected()==true) { tf[1].setText(Integer.toString(money/10000)); money=money%10000; } if(jc[2].isSelected()==true) { tf[2].setText(Integer.toString(money/1000)); money=money%1000; } if(jc[3].isSelected()==true) { tf[3].setText(Integer.toString(money/500)); money=money%500; } if(jc[4].isSelected()==true) { tf[4].setText(Integer.toString(money/100)); money=money%100; } if(jc[5].isSelected()==true) { tf[5].setText(Integer.toString(money/50)); money=money%50; } if(jc[6].isSelected()==true) { tf[6].setText(Integer.toString(money/10)); money=money%10; } tf[7].setText(Integer.toString(money)); } }); setSize(350,300); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; import java.awt.event.*; class Main extends JFrame{ private JRadioButton [] radiob = new JRadioButton [2]; private String [] names = {"left","right"}; private ImageIcon [] image = { new ImageIcon("river.png"), new ImageIcon("river (1).png"), new ImageIcon("bridge.png"), }; public Main(){ setTitle("Image Gallery Practice Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); MyMouseListener listener = new MyMouseListener(); for(int i=0; i<radiob.length; i++) { radiob[i] = new JRadioButton(names[i]); c.add(radiob[i]); radiob[i].addMouseListener(listener); } setSize(400,300); setVisible(true); } class MyMouseListener implements MouseListener { public void MouseStateChanged(ItemEvent e) { } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } public static void main(String[] args) { new Main(); } } */
0
0
1
minhyuk0426
2023년 3월 11일
In 소스 코드 제출
/* import java.awt.*; import javax.swing.*; import java.awt.event.*; class Main extends JFrame{ private JCheckBox [] buttons = new JCheckBox [2]; private String [] names = {"버튼 비활성화","버튼 감추기"}; JButton b = new JButton("test button"); public Main(){ setTitle("CheckBox Practice"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); MyItemListener listener = new MyItemListener(); for(int i=0; i<buttons.length; i++) { buttons[i] = new JCheckBox(names[i]); c.add(buttons[i]); buttons[i].addItemListener(listener); } c.add(b); setSize(300,200); setVisible(true); } class MyItemListener implements ItemListener { public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { if(e.getItem() == buttons[0]) { b.setEnabled(false); } else if(e.getItem() == buttons[1]) { b.setVisible(false); } } else { b.setEnabled(true); b.setVisible(true); } } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; import java.awt.event.*; class Main extends JFrame{ public Main(){ JButton b = new JButton("계산"); setTitle("Money Changer"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.setBackground(Color.PINK); JTextField mtf = new JTextField(20); c.add(new JLabel("금액 ")); c.add(mtf); c.add(b); JLabel[] la = new JLabel[8]; String[] lastr = {"오만원 ","만원 ","천원 ","500원 ","100원 ","50원 ","10원 ","1원 "}; JTextField[] tf = new JTextField[8]; for(int i=0;i<la.length;i++) { la[i] = new JLabel(lastr[i]); tf[i] = new JTextField(20); c.add(la[i]); c.add(tf[i]); } b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int money = Integer.valueOf(mtf.getText()); //System.out.println(money); tf[0].setText(Integer.toString(money/50000)); money=money%50000; tf[1].setText(Integer.toString(money/10000)); money=money%10000; tf[2].setText(Integer.toString(money/1000)); money=money%1000; tf[3].setText(Integer.toString(money/500)); money=money%500; tf[4].setText(Integer.toString(money/100)); money=money%100; tf[5].setText(Integer.toString(money/50)); money=money%50; tf[6].setText(Integer.toString(money/10)); money=money%10; tf[7].setText(Integer.toString(money/1)); money=money%1; } }); setSize(350,300); setVisible(true); } class MyItemListener implements ItemListener { int sum; public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { } } } public static void main(String[] args) { new Main(); } } */
0
0
1
minhyuk0426
2023년 3월 04일
In 소스 코드 제출
/* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i,j; int[][] arr = new int[n][n]; for(i=0; i<n; i++) { arr[i][0] = sc.nextInt(); } for(i=1; i<n; i++) { for(j=1; j<=i; j++) { arr[i][j] = arr[i][j-1] - arr[i-1][j-1]; } } for(i=0; i<n; i++) { for(j=0; j<=i; j++) { System.out.print(arr[i][j]+" "); } System.out.println(" "); } } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,sum=0; for(i=a; i<=b; i++) { if(i%2==0) { sum -= i; } else { sum += i; } } System.out.println(sum); } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,j,k,sum=0,dec=0; for(k=a; k<=b; k++) { for(i=1; i<=9; i++) { System.out.println(k+"*"+i+"="+(k*i)); } } } } 20220918 클래스 : 틀 ( 필드 + 생성자 + 메소드) 객체 : 틀로 찍어낸 물체 import java.util.*; class Person{ //field 필드 int age; String name; //method 메소드 (함수) void view() { System.out.println("나이는 "+ age+"이고, 이름은 "+ name+"입니다."); } //constructor 생성자 - 객체를 생성할때 필드 값의 초기값을 정해주는 용도 Person(){ age=5000; name="noname"; } Person(int a){ age=a; name="noname"; } Person(int a, String n){ age=a; name=n; } } public class Main { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); Person s; s = new Person(); Person p = new Person(100,"tom"); Person t = new Person(50); t.view(); p.view(); s.view(); p.age=10; p.name="hello"; s.age=900; s.name="hi"; p.view(); s.view(); //System.out.println("나이는"+ p.age+"이고, 이름은 "+ p.name+"입니다."); //System.out.println("나이는"+ s.age+"이고, 이름은 "+ s.name+"입니다."); } } */ /* class Circle { int radius; String name; public double getArea() { return 3.14*radius*radius; } Circle(int r, String n){ radius=r; name=n; } } class Main{ public static void main(String[] args) { // Circle pizza; // pizza = new Circle(); // pizza.radius = 10; // pizza.name = "자바피자"; // double area = pizza.getArea(); // System.out.println(pizza.name+"의 면적은"+area); Circle donut = new Circle(2,"자바도넛"); double area = donut.getArea(); System.out.println(donut.name+"의 면적은"+area); } } */ /* import java.util.*; class Circle { int radius; String name; public Circle() { radius = 1; name = ""; } public Circle(int r, String n) { radius = r; name = n; } public double getArea() { return 3.14*radius*radius; } } class Main{ public static void main(String[]args) { Circle pizza = new Circle(10, "자바피자"); double area = pizza.getArea(); System.out.println(pizza.name + "의 면적은 " + area); Circle donut = new Circle(); donut.name = "도넛피자"; area = donut.getArea(); System.out.println(donut.name + "의 면적은 " + area); } } private 다른 클래스에서 접근 할 수 없는 필드, 메소드가 된다 */ /* class Song { private String title; public Song(String title) { this.title = title; } public String getTitle() { return title; } } class Main { public static void main(String[] args) { Song mySong = new Song("Nessun Dorma"); Song yourSong = new Song("공주는 잠 못 이루고"); System.out.println("내 노래는 " + mySong.getTitle()); System.out.println("너 노래는 " + yourSong.getTitle()); } } */ /* 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; } } class Main { public static void main(String[] args) { Phone } } */ /* import java.util.*; class TV { private String name; int year; int size; public TV(String name,int year,int size) { this.name = name; this.year = year; this.size = size; } public void show() { System.out.println(name+"에서 만든 "+year+"년형 "+size+"인치 TV"); } } class Main{ public static void main(String [] args) { TV myTV = new TV("LG", 2017, 32); myTV.show(); } } */ /* import java.util.*; class Grade { private int math; private int science; private int english; public Grade(int math,int science,int english) { this.math = math; this.science = science; this.english = english; } public int average() { return (math+science+english)/3; } } class Main { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); System.out.println("수학, 과학, 영어 순으로 3개의 정수 입력>>"); Grade me = new Grade(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()); System.out.println("평균은 " + me.average()); } } */ /* import java.util.*; class Song { private String title; private String country; private String artist; private int year; public Song(int year) { this(year,"미정","미정","미정"); } public Song(int year,String title,String country,String artist) { this.title = title; this.country = country; this.artist = artist; this.year = year; } public void show() { System.out.println(year+"년 "+country+"국적의 "+artist+"가 부른 "+title); } } class Main { public static void main(String [] args) { Song mySong = new Song(1978,"Dancing Queen","스웨덴","ABBA"); Song yourSong = new Song(1945); mySong.show(); } } */ /* import java.util.*; 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; } } class Main { public static void main(String [] args) { Book littlePrince = new Book("어린왕자","생텍쥐페리"); Book loveStory = new Book("춘향전"); Book emptyBook = new Book(); loveStory.show(); } } */ /* class Samp { int id; public Samp(int x) { this.id = x; } public Samp() { this(0); System.out.println("생성자 호출"); } } class ConstructorExample{ //필드 int x; //메소드 public void setx(int x) { this.x = x; } public int getx() { return x; } //생성자 public ConstructorExample() { } public static void main(String [] args) { ConstructorExample a = new ConstructorExample(); int n = a.getx(); } } 객체 배열을 생성할때는 1. 레퍼런스 배열의 레퍼런스 생성 2. 레퍼런스 배열 생성 3. 객체 생성해서 레퍼런스에 연결 class Person{ String name; int age; } class Main{ public static void main(String[] args) { // Person p ;//레퍼런스변수 생성 // p = new Person(); //객체 생성 Person[] arr = new Person[10]; for(int i=0;i<arr.length;i++) { arr[i] = new Person(); } arr[0].name="민혁"; } } * */ /* import java.util.*; class Circle { int radius; public Circle(int radius) { this.radius = radius; } public double getArea() { return 3.14*radius*radius; } } class Main { public static void main(String[] args) { Circle [] c; c = new Circle[5]; for(int i=0; i<c.length; i++) c[i] = new Circle(i); for(int i=0; i<c.length; i++) System.out.println((int)(c[i].getArea()) + " "); } } */ /* import java.util.Scanner; class Book { String title,author; public Book(String title,String author) { this.title = title; this.author = author; } } class Main{ public static void main(String[] args) { Book [] book = new Book[2]; Scanner scanner = new Scanner(System.in); for(int i=0; i<book.length; i++) { System.out.print("제목>>"); String title = scanner.nextLine(); System.out.print("저자>>"); String author = scanner.nextLine(); book[i] = new Book(title, author); } for(int i=0; i<book.length; i++) System.out.print("(" + book[i].title + ", " + book[i].author + ")"); } } */ /* import java.util.*; class Circle { private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Circle c[] = new Circle[3]; for (int i = 0; i < c.length; i++) { System.out.print("x, y, radius >>"); double x = scanner.nextDouble(); double y = scanner.nextDouble(); int radius = scanner.nextInt(); c[i] = new Circle(x, y, radius); } for(int i=0; i<c.length; i++) c[i].show(); scanner.close(); } } import java.util.Scanner; class Dictionary { private static String [] kor = { "사랑","아기","돈","미래","희망" }; private static String [] eng = { "love","baby","monry","future", "hope"}; public static String kor2Eng(String word) { for(int i=0;i<kor.length;i++) { if(kor[i].equals(word)) return eng[i]; } return " 저의 사전에 없습니다."; } } class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("한영 단어 검색 프로그램입니다."); while(true) { System.out.print("한글 단어?"); String name = sc.next(); if(name.equals("그만")) { return ; } else { System.out.println(name+"는 " +Dictionary.kor2Eng(name)); } } } } 부모클래스 - 자식클래스 슈퍼클래스 - 서브클래스 class Person{ int age; String name; } class Student extends Person{ String school; char grade; void speak() { System.out.println("안녕하세요, 저는 학생입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class Teacher extends Person{ String school; String major; void speak() { System.out.println("안녕하세요, 저는 선생님입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class ComTeacher extends Teacher{ } class Main{ public static void main(String[] args) { Student s = new Student(); Teacher t = new Teacher(); s.speak(); } } */ /* import java.util.*; 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; void setColor(String color) { this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main{ public static void main(String [] args) { Point p = new Point(); p.set(1,2); p.showPoint(); ColorPoint cp = new ColorPoint (); cp.set(3,4); cp.setColor("red"); cp.showColorPoint(); } } import java.util.*; class Pen { protected int amount; public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } class SharpPencil extends Pen { private int width; } class BallPen extends Pen { private String color; public String getColor() { return color; } public void setColor(String color ) { this.color = color; } } class FountainPen extends BallPen { public void refill(int n) { amount = n; } } 상속-생성자 class A{ int n; public A() { System.out.println("A의 기본 생성자"); } public A(int n) {this.n = n;} } class B extends A{ int m; public B() { System.out.println("B의 기본 생성자"); } public B(int m) { super(20); this.m = m; System.out.println("B의 1번 생성자"); } } class Main{ public static void main(String[] args) { //A x = new A(10); B y = new B(10); } } */ /* import java.util.*; class Point { private int x,y; public Point() { this.x = this.y = 0; } public Point(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 ColorPoint(int x, int y, String color) { super (x, y); this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main { public static void main(String[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showColorPoint (); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } public void printProperty() { System.out.print(getSize()+"인치 "+Color+"컬러"); } } class Main { public static void main(String [] args) { ColorTV myTV = new ColorTV(32,1024); myTV.printProperty(); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } protected int getColor() { return Color; } } class IPTV extends ColorTV { private String IP; public IPTV(String IP,int size,int Color) { super(size,Color); this.IP = IP; } public void printProperty() { System.out.print("나의 IPTV는 "+IP+" 주소의 "+getSize()+"인치 "+getColor()+"컬러"); } } class Main { public static void main(String[]args) { IPTV iptv = new IPTV("192.1.1.2", 32, 2048); iptv.printProperty(); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint(int x,int y,String Color) { super(x,y); this.Color = Color; } public void setXY(int x,int y) { move (x,y); } public void setColor(String Color) { this.Color = Color; } public String toString () { return Color+"색의 "+"("+ getX() +","+ getY() +")"+"의 점"; } } class Main { public static void main(String[]args) { ColorPoint cp = new ColorPoint(5, 5, "YELLOW"); cp.setXY(10, 20); cp.setColor("RED"); String str = cp.toString(); System.out.println(str + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint() { super(0,0); this.Color = "BLACK"; } public ColorPoint(int x,int y) { super(x,y); this.Color = "BLACK"; } public void setXY(int x,int y) { move(x,y); } public void setColor(String color) { this.Color = color; } public String toString () { return Color + "색의 " + "("+ getX() + ","+ getY() + ")" + "의 점"; } } class Main { public static void main(String[] args) { ColorPoint zeroPoint = new ColorPoint(); System.out.println(zeroPoint.toString() + "입니다."); ColorPoint cp = new ColorPoint(10,10); cp.setXY(5, 5); cp.setColor("RED"); System.out.println(cp.toString() + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class Point3D extends Point { private int z; public Point3D(int x,int y,int z) { super(x,y); this.z = z; } public void moveUp() { z = z+1; } public void moveDown() { z--; } public void move(int x, int y,int z) { super.move(x, y); this.z = z; } public String toString() { return "("+ getX() + "," + getY() + "," + z +")" + "의 점"; } } class Main { public static void main(String[] args) { Point3D p = new Point3D(1,2,3); System.out.println(p.toString() + "입니다."); p.moveUp(); System.out.println(p.toString() + "입니다."); p.moveDown(); p.move(10, 10); System.out.println(p.toString() + "입니다."); p.move(100, 200, 300); System.out.println(p.toString() + "입니다."); } } 클래스 1. 필드 (변수 ,속성) 2. 메소드 (기능, 행동) 3. 생성자(객체 생성시 필드값 초기화) 추상클래스 : 일부만 작성 == 일부는 작성 x */ /* abstract class Person{ int age; String name; void introduce() { //일반 메소드 System.out.println("I'm "+age+"years old. My name is "+name+"."); } abstract void speak(); //추상메소드 ( 이 클래스를 상속받은 모든 클래스는 이 메소드를 구현) } class Student extends Person{ void speak() { System.out.println("I'm Student."); } } class Teacher extends Person{ void speak() { System.out.println("I'm Teacher"); } } class Main{ public static void main(String[] args) { Student s = new Student(); } } */ /* abstract class Calculator { public abstract int add(int a,int b); public abstract int subtract(int a,int b); public abstract double average (int[] a); } class GoodCalc extends Calculator { public int add(int a,int b) { return a + b; } public int subtract(int a,int b) { return a-b; } public double average(int[] a) { double sum = 0; for(int i=0; i<a.length; i++) sum += a[i]; return sum/a.length; } } class Main { public static void main(String[] args) { GoodCalc c = new GoodCalc(); System.out.println(c.add(2,3)); System.out.println(c.subtract(2,3)); System.out.println(c.average(new int [] { 2, 3, 4 })); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Won2Dollar extends Converter { public Won2Dollar(int i) { } @Override protected double convert(double src) { return src/1200; } @Override protected String getSrcString() { return "원"; } @Override protected String getDestString() { return "달러"; } } class Main{ public static void main(String[] args) { Won2Dollar toDollar = new Won2Dollar(1200); toDollar.run(); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Km2Mile extends Converter { public Km2Mile(double i) { ratio=i; } protected double convert(double src) { return src/ratio; } protected String getSrcString() { return "Km"; } protected String getDestString() { return "mile"; } } class Main { public static void main(String[] args) { Km2Mile toMile = new Km2Mile(1.6); toMile.run(); } } */ /* import java.util.*; abstract class PairMap { protected String keyArray []; protected String ValueArray []; abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key); abstract int length(); } class Dictionary extends PairMap { public Dictionary(int i) { } String get(String key) { return ""; } void put(String key, String value) { } String delete(String key) { return null; } int length() { return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(10); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* import java.util.*; interface PairMap { abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key, String value); abstract int length(); } class Dictionary implements PairMap { @Override public String get(String key) { // TODO Auto-generated method stub return null; } @Override public void put(String key, String value) { // TODO Auto-generated method stub } @Override public String delete(String key, String value) { // TODO Auto-generated method stub return null; } @Override public int length() { // TODO Auto-generated method stub return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* interface Shape { final double PI = 3.14; void draw(); double getArea(); default public void redraw() { System.out.print("--- 다시 그립니다. "); draw(); } } class Circle implements Shape{ int r; public Circle(int r) { this.r = r; } public void draw() { System.out.println("반지름이 " + r + "인 원입니다."); } public double getArea() { return r*r*PI; } } class Main { public static void main(String [] args) { Shape donut = new Circle(10); donut.redraw(); System.out.println("면적은 " + donut.getArea()); } } interface MouseDriver { final int BUTTONS = 3; //int VERSION; void move(); public int click(); int out(); void drag(); default void drop() { System.out.println("drop"); } } GUI : 그림으로 보여지는것 컨테이너 container : 종이 판 (다른 것들을 포함한다) 컴포넌트 component : 스티커 (컨테이너 위에 올라간다) import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("example1"); // 창 제목 정하기 (필수아님) setSize(500, 300); //가로500, 세로 300으로 크기 셋팅 (필수) Container c = getContentPane(); // c = 현재 배경 c.setLayout(new FlowLayout()); //레이아웃 설정 (나중에) JButton b = new JButton("버튼"); //버튼 생성 c.add(b); //버튼 추가 JLabel la = new JLabel("레이블컴포넌트입니다"); c.add(la); setVisible(true); // 프레임이 보이게 설정 (필수) } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("ContentPane과 JFrame"); setSize(500,300); Container c = getContentPane(); //c.setLayout(new FlowLayout()); c.setLayout(new BorderLayout(5,10)); c.setBackground(Color.black); //배경색 설정 JButton b = new JButton("OK"); b.setBackground(Color.white); b.setForeground(Color.black); c.add(b,BorderLayout.NORTH); // JButton b1 = new JButton("Cancel"); // b1.setBackground(Color.white); // b1.setForeground(Color.black); //전경색 (글자색) 설정 // c.add(b1, BorderLayout.CENTER); JButton b2 = new JButton("Ignore"); b2.setForeground(Color.black); c.add(b2, BorderLayout.CENTER); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.setBackground(Color.black); c.add(p,BorderLayout.EAST); JButton b3 = new JButton("Cancel"); p.add(new JButton("Cancel")); p.add(b3); b3.setForeground(Color.blue); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("Let's study Java"); setSize(400,200); Container c = getContentPane(); c.setLayout(new BorderLayout(5,7)); JButton b = new JButton("Center"); c.add(b, BorderLayout.CENTER); JButton b1 = new JButton("East"); c.add(b1, BorderLayout.EAST); JButton b2 = new JButton("West"); c.add(b2, BorderLayout.WEST); JButton b3 = new JButton("North"); c.add(b3, BorderLayout.NORTH); JButton b4 = new JButton("South"); c.add(b4, BorderLayout.SOUTH); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("GridLayour Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 2); grid.setVgap(5); Container c = getContentPane(); c.setLayout(grid); c.add(new JLabel(" 이름")); c.add(new JTextField("")); c.add(new JLabel(" 학번")); c.add(new JTextField("")); c.add(new JLabel(" 학과")); c.add(new JTextField("")); c.add(new JLabel(" 과목")); c.add(new JTextField("")); setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Null Container Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); JLabel la = new JLabel("Hello, Press Buttons!"); la.setLocation(130, 50); la.setSize(200, 20); c.add(la); for(int i=1; i<=9; i++) { JButton b = new JButton(Integer.toString(i)); //*********************** //random // //************************ int x = (int) Math.random()*200; int y = (int) Math.random()*300; b.setLocation(x, y); b.setSize(50,20); c.add(b); } setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Ten Color Buttons Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(1, 10); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red, Color.orange, Color.yellow, Color.green, Color.cyan, Color.blue, Color.magenta, Color.darkGray, Color.pink, Color.gray }; for(int i=0; i<10; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("4x4 Color Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 4); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue,Color.magenta,Color.darkGray,Color.pink,Color.gray,Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue}; for(int i=0; i<16; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Random Labels"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); for(int i=0; i<20; i++) { JLabel la = new JLabel(Integer.toString(i)); int x = (int)(Math.random()*200) + 50; int y = (int)(Math.random()*200) + 50; la.setLocation(x,y); la.setSize(15, 15); la.setBackground(Color.blue); la.setForeground(Color.white); la.setOpaque(true); c.add(la); } } public static void main(String[] args) { new Main(); } } event : 모든 변화 ex) 마우스 클릭, 버튼 클릭, 버튼에 마우스 올려놓기, 키보드 a 누르기 ..... action event key event mouse evet change event ... event listener : 이벤트를 어떻게 처리할지 적어놓는 곳 import javax.swing.*; import java.awt.*; import java.awt.event.*; // event를 처리하는 부분 public class Main extends JFrame { Container c = getContentPane(); JPanel nor = new JPanel(); JPanel cen = new JPanel(); JButton b = new JButton("set black"); JButton b1 = new JButton("set red"); JButton b2 = new JButton("set pink"); public Main() { setTitle("Open Challenge 9"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new BorderLayout(3,3)); nor.setLayout(new FlowLayout()); nor.setBackground(Color.LIGHT_GRAY); cen.setLayout(null); //액션리스너 붙여주기 b.addActionListener(new MyActionListener()); b1.addActionListener(new MyActionListener()); b2.addActionListener(new MyActionListener()); nor.add(b); nor.add(b1); nor.add(b2); JLabel l = new JLabel("Hello"); l.setLocation(50,150); l.setSize(30, 30); cen.add(l); JLabel l1 = new JLabel("Java"); l1.setLocation(250,80); l1.setSize(30, 30); cen.add(l1); JLabel l2 = new JLabel("Love"); l2.setLocation(70,50); l2.setSize(30, 30); cen.add(l2); c.add(cen, BorderLayout.CENTER); c.add(nor, BorderLayout.NORTH); setSize(300,300); setVisible(true); } //액션리스너 클래스 작성하기 class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { JButton bb = (JButton)e.getSource(); // 이벤트가 일어난 버튼을 bb if(bb==b) cen.setBackground(Color.black); else if(bb==b1) cen.setBackground(Color.red); else cen.setBackground(Color.pink); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { public Main() { setTitle("Action 이벤트 리스너 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JButton btn = new JButton("Action"); btn.addActionListener(new MyActionListener()); c.add(btn); setSize(350,150); setVisible(true); } public static void main(String[] args) { new Main(); } } class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { JButton b = (JButton)e.getSource(); if(b.getText().equals("Action")) b.setText("액션"); else b.setText("Action"); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { private JLabel la = new JLabel("Hello"); public Main() { setTitle("Mouse 이벤트 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.addMouseListener(new MyMouseListener()); c.setLayout(null); la.setSize(50, 20); la.setLocation(30, 30); c.add(la); setSize(250, 250); setVisible(true); } //implements MouseListener class MyMouseListener extends MouseAdapter { public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } // public void mousePressed(MouseEvent e) { // int x = e.getX(); // int y = e.getY(); // la.setLocation(x, y); // } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("마우스 올리기 내리기"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); la.addMouseListener(new MyMouseListener()); c.setLayout(null); la.setSize(100, 20); la.setLocation(140, 30); c.add(la); setSize(350, 250); setVisible(true); }//implements MouseListener class MyMouseListener extends MouseAdapter { public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub la.setText("Love Java"); } public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub la.setText("사랑해"); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { Container c = getContentPane(); public Main() { setTitle("드래깅동안 YELLOW"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.addMouseMotionListener(new MyMouseMotionListener()); setSize(350, 250); setVisible(true); } class MyMouseMotionListener extends MouseAdapter{ public void mouseDragged(MouseEvent e) { c.setBackground(Color.YELLOW); } public void mouseMoved(MouseEvent e) { c.setBackground(Color.GREEN); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel [] keyMessage; public Main() { setTitle("keyListener 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.addKeyListener(new MyKeyListener()); keyMessage = new JLabel [3]; keyMessage[0] = new JLabel(" getKeyCode() "); keyMessage[1] = new JLabel(" getKeyCode() "); keyMessage[2] = new JLabel(" getKeyCode() "); for(int i=0; i<keyMessage.length; i++) { c.add(keyMessage[i]); keyMessage[i].setOpaque(true); keyMessage[i].setBackground(Color.CYAN); } setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); char keyChar = e.getKeyChar(); keyMessage[0].setText(Integer.toString(keyCode)); keyMessage[1].setText(Character.toString(keyChar)); keyMessage[2].setText(KeyEvent.getKeyText(keyCode)); System.out.println("KeyPressed"); } public void keyReleased(KeyEvent e) { System.out.println("KeyReleased"); } public void keyTyped(KeyEvent e) { System.out.println("KeyTyped"); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel keyMessage; Container c = getContentPane(); public Main() { setTitle("Key Code 예제 : F1키:초록색, %키:노랑색"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new FlowLayout()); c.addKeyListener(new MyKeyListener()); keyMessage = new JLabel("키가 입력되었음"); c.add(keyMessage); setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); char keyChar = e.getKeyChar(); keyMessage.setText(Character.toString(keyChar)+"키가 입력되었음"); if(e.getKeyChar()=='%') { c.setBackground(Color.yellow); } if(e.getKeyCode()==KeyEvent.VK_F1) { c.setBackground(Color.green); } System.out.println("KeyPressed"); } public void keyReleased(KeyEvent e) { System.out.println("KeyReleased"); } public void keyTyped(KeyEvent e) { System.out.println("KeyTyped"); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("HELLO"); public Main() { setTitle("상,하,좌,우 키를 이용하여 텍스트 움직이기"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); la.setLocation(50, 50); la.setSize(50,50); c.add(la); setSize(300, 300); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int x = la.getX(); int y = la.getY(); if(e.getKeyCode()==KeyEvent.VK_UP) { la.setLocation(x,y-10); } else if(e.getKeyCode()==KeyEvent.VK_DOWN) { la.setLocation(x,y+10); } else if(e.getKeyCode()==KeyEvent.VK_LEFT) { la.setLocation(x-10,y); } else if(e.getKeyCode()==KeyEvent.VK_RIGHT) { la.setLocation(x+10,y); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Click and DoubleClick 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.addMouseListener(new MyMouseListener()); setSize(300,200); setVisible(true); } class MyMouseListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { if(e.getClickCount() == 2) { int r = (int)(Math.random()*256); int g = (int)(Math.random()*256); int b = (int)(Math.random()*256); Component c = (Component)e.getSource(); c.setBackground(new Color(r, b, g)); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("Left 키로 문자열 교체"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); c.add(la); la.setSize(100,100); la.setLocation(110,0); setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_LEFT) { String latext = la.getText(); if(latext.equals("Love Java")) { la.setText("avaJ evoL"); } else { la.setText("Love Java"); } } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("Left 키로 문자열 이동"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); c.add(la); la.setSize(100,100); la.setLocation(110,0); setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_LEFT) { String text = la.getText(); String aftertext = text.substring(1) + text.substring(0,1); la.setText(aftertext); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("+,- 키로 폰트 크기 조절"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); c.add(la); la.setSize(400,100); la.setLocation(110,0); la.setFont(new Font("Arial", Font.PLAIN, 10)); setSize(400, 300); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { Font f = la.getFont(); int size = f.getSize(); if(e.getKeyChar() == '+'&&size+5<100) { la.setFont(new Font("Arial", Font.PLAIN, size+5)); } else if(e.getKeyChar() == '-'&&size-5>5) { la.setFont(new Font("Arial", Font.PLAIN, size-5)); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("C"); public Main() { setTitle("클릭 연습 용 응용프로그램"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); la.addMouseListener(new MyMouseListener()); la.setSize(100,100); la.setLocation(100, 100); c.add(la); setSize(300, 250); setVisible(true); } class MyMouseListener extends MouseAdapter{ public void mousePressed(MouseEvent e) { int x = (int)(Math.random()*250); int y = (int)(Math.random()*200); la.setLocation(x, y); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("마우스 휠을 굴려 폰트 크기 조절"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); la.addMouseWheelListener(new MyMouseWheelListener()); c.add(la); la.setSize(500,300); la.setLocation(210, -50); c.setLayout(null); la.setFont(new Font("Arial", Font.PLAIN, 10)); setSize(500, 250); setVisible(true); } class MyMouseWheelListener extends MouseAdapter{ public void mouseWheelMoved(MouseWheelEvent e) { Font f = la.getFont(); int size = f.getSize(); int n = e.getWheelRotation(); if(n<0&&size-5>5) { la.setFont(new Font("Arial", Font.PLAIN, size-5)); } else if(n>0&&size+5<100) { la.setFont(new Font("Arial", Font.PLAIN, size+5)); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("레이블 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel textLabel = new JLabel("사랑합니다."); ImageIcon heart = new ImageIcon("images/heart.png"); JLabel imageLabel = new JLabel(heart); ImageIcon phone = new ImageIcon("images/phone.png"); JLabel label = new JLabel("보고싶으면 전화하세요", phone, SwingConstants.CENTER); c.add(textLabel); c.add(imageLabel); c.add(label); setSize(400,200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("이미지 버튼 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); ImageIcon normalIcon = new ImageIcon("images/phone.png"); ImageIcon rolloverIcon = new ImageIcon("images/incoming-call.png"); ImageIcon pressedIcon = new ImageIcon("images/conversation.png"); JButton btn = new JButton("call~~", normalIcon); btn.setPressedIcon(pressedIcon); btn.setRolloverIcon(rolloverIcon); c.add(btn); setSize(250,150); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("체크박스 만들기 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); ImageIcon cherryIcon = new ImageIcon("images/cherries.png"); ImageIcon selectedCherryIcon = new ImageIcon("images/berry.png"); JCheckBox apple = new JCheckBox("사과"); JCheckBox pear = new JCheckBox("배"); JCheckBox cherry = new JCheckBox("체리", cherryIcon); c.add(apple); c.add(pear); c.add(cherry); cherry.setSelectedIcon(selectedCherryIcon); cherry.setBorderPainted(true); setSize(300,250); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JCheckBox [] fruits = new JCheckBox [3]; private String [] names = {"사과","배","체리"}; private JLabel sumLabel; public Main() { setTitle("첵크박스와 ItemEvent 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add(new JLabel("사과 100원, 배 500원 , 체리 20000원")); for(int i=0; i<fruits.length; i++) { fruits[i] = new JCheckBox(names[i]); fruits[i].addItemListener(new MyItemListener()); fruits[i].setBorderPainted(true); c.add(fruits[i]); } sumLabel = new JLabel("현재 0원 입니다."); c.add(sumLabel); setSize(250,200); setVisible(true); } class MyItemListener implements ItemListener { private int sum = 0; public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { if(e.getItem() == fruits[0]) sum += 100; else if(e.getItem() == fruits[1]) sum += 500; else sum += 20000; } else { if(e.getItem() == fruits[0]) sum -= 100; else if(e.getItem() == fruits[1]) sum -= 500; else sum -= 20000; } sumLabel.setText("현재 " + sum + "원 입니다."); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("라디오버튼 만들기 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); ImageIcon cherryIcon = new ImageIcon("images/cherries.png"); ImageIcon selectedCherryIcon = new ImageIcon("images/berry.png"); ButtonGroup g = new ButtonGroup(); JRadioButton apple = new JRadioButton("사과"); JRadioButton pear = new JRadioButton("배"); JRadioButton cherry = new JRadioButton("체리", cherryIcon); cherry.setBorderPainted(true); cherry.setSelectedIcon(selectedCherryIcon); g.add(apple); g.add(pear); g.add(cherry); c.add(apple); c.add(pear); c.add(cherry); setSize(250,150); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JRadioButton [] radio = new JRadioButton [3]; private String [] text = {"사과","배","체리"}; private ImageIcon [] image = { new ImageIcon("images/apple.png"), new ImageIcon("images/pear.png"), new ImageIcon("images/berry.png") }; private JLabel imageLabel = new JLabel(); public Main() { setTitle("라디오버튼 Item Event 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new BorderLayout()); JPanel radioPanel = new JPanel(); radioPanel.setBackground(Color.gray); ButtonGroup g = new ButtonGroup(); for(int i=0; i<radio.length; i++) { radio[i] = new JRadioButton(text[i]); g.add(radio[i]); radioPanel.add(radio[i]); radio[i].addItemListener(new MyItemListener()); } radio[2].setSelected(true); c.add(radioPanel, BorderLayout.NORTH); c.add(imageLabel, BorderLayout.CENTER); imageLabel.setHorizontalAlignment(SwingConstants.CENTER); setSize(250,150); setVisible(true); } class MyItemListener implements ItemListener { public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == e.DESELECTED) return; if(radio[0].isSelected()) imageLabel.setIcon(image[0]); else if(radio[1].isSelected()) imageLabel.setIcon(image[1]); else imageLabel.setIcon(image[2]); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("텍스트필드 만들기 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); setLayout(new FlowLayout()); c.add(new JLabel("이름 ")); c.add(new JTextField(20)); c.add(new JLabel("힉과 ")); c.add(new JTextField("컴퓨터공학과", 20)); c.add(new JLabel("주소")); c.add(new JTextField("서울시", 20)); setSize(250,150); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JTextField tf = new JTextField(20); private JTextArea ta = new JTextArea(7,20); public Main() { setTitle("텍스트 영역 만들기 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); setLayout(new FlowLayout()); // JButton b = new JButton("Delete"); // b.addActionListener(new ActionListener() { // public void actionPerformed(ActionEvent e) { // ta.setText(""); // } // }); c.add(new JLabel("입력 후 <Enter> 키를 입력하세요.")); c.add(tf); c.add(new JScrollPane(ta)); // c.add(b); tf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JTextField t = (JTextField)e.getSource(); ta.append(t.getText() + "\n"); t.setText(""); } }); setSize(300,300); setVisible(true); } public static void main(String[] args) { new Main(); } } */
0
0
3
minhyuk0426
2023년 2월 26일
In 소스 코드 제출
/* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i,j; int[][] arr = new int[n][n]; for(i=0; i<n; i++) { arr[i][0] = sc.nextInt(); } for(i=1; i<n; i++) { for(j=1; j<=i; j++) { arr[i][j] = arr[i][j-1] - arr[i-1][j-1]; } } for(i=0; i<n; i++) { for(j=0; j<=i; j++) { System.out.print(arr[i][j]+" "); } System.out.println(" "); } } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,sum=0; for(i=a; i<=b; i++) { if(i%2==0) { sum -= i; } else { sum += i; } } System.out.println(sum); } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,j,k,sum=0,dec=0; for(k=a; k<=b; k++) { for(i=1; i<=9; i++) { System.out.println(k+"*"+i+"="+(k*i)); } } } } 20220918 클래스 : 틀 ( 필드 + 생성자 + 메소드) 객체 : 틀로 찍어낸 물체 import java.util.*; class Person{ //field 필드 int age; String name; //method 메소드 (함수) void view() { System.out.println("나이는 "+ age+"이고, 이름은 "+ name+"입니다."); } //constructor 생성자 - 객체를 생성할때 필드 값의 초기값을 정해주는 용도 Person(){ age=5000; name="noname"; } Person(int a){ age=a; name="noname"; } Person(int a, String n){ age=a; name=n; } } public class Main { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); Person s; s = new Person(); Person p = new Person(100,"tom"); Person t = new Person(50); t.view(); p.view(); s.view(); p.age=10; p.name="hello"; s.age=900; s.name="hi"; p.view(); s.view(); //System.out.println("나이는"+ p.age+"이고, 이름은 "+ p.name+"입니다."); //System.out.println("나이는"+ s.age+"이고, 이름은 "+ s.name+"입니다."); } } */ /* class Circle { int radius; String name; public double getArea() { return 3.14*radius*radius; } Circle(int r, String n){ radius=r; name=n; } } class Main{ public static void main(String[] args) { // Circle pizza; // pizza = new Circle(); // pizza.radius = 10; // pizza.name = "자바피자"; // double area = pizza.getArea(); // System.out.println(pizza.name+"의 면적은"+area); Circle donut = new Circle(2,"자바도넛"); double area = donut.getArea(); System.out.println(donut.name+"의 면적은"+area); } } */ /* import java.util.*; class Circle { int radius; String name; public Circle() { radius = 1; name = ""; } public Circle(int r, String n) { radius = r; name = n; } public double getArea() { return 3.14*radius*radius; } } class Main{ public static void main(String[]args) { Circle pizza = new Circle(10, "자바피자"); double area = pizza.getArea(); System.out.println(pizza.name + "의 면적은 " + area); Circle donut = new Circle(); donut.name = "도넛피자"; area = donut.getArea(); System.out.println(donut.name + "의 면적은 " + area); } } private 다른 클래스에서 접근 할 수 없는 필드, 메소드가 된다 */ /* class Song { private String title; public Song(String title) { this.title = title; } public String getTitle() { return title; } } class Main { public static void main(String[] args) { Song mySong = new Song("Nessun Dorma"); Song yourSong = new Song("공주는 잠 못 이루고"); System.out.println("내 노래는 " + mySong.getTitle()); System.out.println("너 노래는 " + yourSong.getTitle()); } } */ /* 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; } } class Main { public static void main(String[] args) { Phone } } */ /* import java.util.*; class TV { private String name; int year; int size; public TV(String name,int year,int size) { this.name = name; this.year = year; this.size = size; } public void show() { System.out.println(name+"에서 만든 "+year+"년형 "+size+"인치 TV"); } } class Main{ public static void main(String [] args) { TV myTV = new TV("LG", 2017, 32); myTV.show(); } } */ /* import java.util.*; class Grade { private int math; private int science; private int english; public Grade(int math,int science,int english) { this.math = math; this.science = science; this.english = english; } public int average() { return (math+science+english)/3; } } class Main { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); System.out.println("수학, 과학, 영어 순으로 3개의 정수 입력>>"); Grade me = new Grade(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()); System.out.println("평균은 " + me.average()); } } */ /* import java.util.*; class Song { private String title; private String country; private String artist; private int year; public Song(int year) { this(year,"미정","미정","미정"); } public Song(int year,String title,String country,String artist) { this.title = title; this.country = country; this.artist = artist; this.year = year; } public void show() { System.out.println(year+"년 "+country+"국적의 "+artist+"가 부른 "+title); } } class Main { public static void main(String [] args) { Song mySong = new Song(1978,"Dancing Queen","스웨덴","ABBA"); Song yourSong = new Song(1945); mySong.show(); } } */ /* import java.util.*; 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; } } class Main { public static void main(String [] args) { Book littlePrince = new Book("어린왕자","생텍쥐페리"); Book loveStory = new Book("춘향전"); Book emptyBook = new Book(); loveStory.show(); } } */ /* class Samp { int id; public Samp(int x) { this.id = x; } public Samp() { this(0); System.out.println("생성자 호출"); } } class ConstructorExample{ //필드 int x; //메소드 public void setx(int x) { this.x = x; } public int getx() { return x; } //생성자 public ConstructorExample() { } public static void main(String [] args) { ConstructorExample a = new ConstructorExample(); int n = a.getx(); } } 객체 배열을 생성할때는 1. 레퍼런스 배열의 레퍼런스 생성 2. 레퍼런스 배열 생성 3. 객체 생성해서 레퍼런스에 연결 class Person{ String name; int age; } class Main{ public static void main(String[] args) { // Person p ;//레퍼런스변수 생성 // p = new Person(); //객체 생성 Person[] arr = new Person[10]; for(int i=0;i<arr.length;i++) { arr[i] = new Person(); } arr[0].name="민혁"; } } * */ /* import java.util.*; class Circle { int radius; public Circle(int radius) { this.radius = radius; } public double getArea() { return 3.14*radius*radius; } } class Main { public static void main(String[] args) { Circle [] c; c = new Circle[5]; for(int i=0; i<c.length; i++) c[i] = new Circle(i); for(int i=0; i<c.length; i++) System.out.println((int)(c[i].getArea()) + " "); } } */ /* import java.util.Scanner; class Book { String title,author; public Book(String title,String author) { this.title = title; this.author = author; } } class Main{ public static void main(String[] args) { Book [] book = new Book[2]; Scanner scanner = new Scanner(System.in); for(int i=0; i<book.length; i++) { System.out.print("제목>>"); String title = scanner.nextLine(); System.out.print("저자>>"); String author = scanner.nextLine(); book[i] = new Book(title, author); } for(int i=0; i<book.length; i++) System.out.print("(" + book[i].title + ", " + book[i].author + ")"); } } */ /* import java.util.*; class Circle { private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Circle c[] = new Circle[3]; for (int i = 0; i < c.length; i++) { System.out.print("x, y, radius >>"); double x = scanner.nextDouble(); double y = scanner.nextDouble(); int radius = scanner.nextInt(); c[i] = new Circle(x, y, radius); } for(int i=0; i<c.length; i++) c[i].show(); scanner.close(); } } import java.util.Scanner; class Dictionary { private static String [] kor = { "사랑","아기","돈","미래","희망" }; private static String [] eng = { "love","baby","monry","future", "hope"}; public static String kor2Eng(String word) { for(int i=0;i<kor.length;i++) { if(kor[i].equals(word)) return eng[i]; } return " 저의 사전에 없습니다."; } } class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("한영 단어 검색 프로그램입니다."); while(true) { System.out.print("한글 단어?"); String name = sc.next(); if(name.equals("그만")) { return ; } else { System.out.println(name+"는 " +Dictionary.kor2Eng(name)); } } } } 부모클래스 - 자식클래스 슈퍼클래스 - 서브클래스 class Person{ int age; String name; } class Student extends Person{ String school; char grade; void speak() { System.out.println("안녕하세요, 저는 학생입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class Teacher extends Person{ String school; String major; void speak() { System.out.println("안녕하세요, 저는 선생님입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class ComTeacher extends Teacher{ } class Main{ public static void main(String[] args) { Student s = new Student(); Teacher t = new Teacher(); s.speak(); } } */ /* import java.util.*; 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; void setColor(String color) { this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main{ public static void main(String [] args) { Point p = new Point(); p.set(1,2); p.showPoint(); ColorPoint cp = new ColorPoint (); cp.set(3,4); cp.setColor("red"); cp.showColorPoint(); } } import java.util.*; class Pen { protected int amount; public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } class SharpPencil extends Pen { private int width; } class BallPen extends Pen { private String color; public String getColor() { return color; } public void setColor(String color ) { this.color = color; } } class FountainPen extends BallPen { public void refill(int n) { amount = n; } } 상속-생성자 class A{ int n; public A() { System.out.println("A의 기본 생성자"); } public A(int n) {this.n = n;} } class B extends A{ int m; public B() { System.out.println("B의 기본 생성자"); } public B(int m) { super(20); this.m = m; System.out.println("B의 1번 생성자"); } } class Main{ public static void main(String[] args) { //A x = new A(10); B y = new B(10); } } */ /* import java.util.*; class Point { private int x,y; public Point() { this.x = this.y = 0; } public Point(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 ColorPoint(int x, int y, String color) { super (x, y); this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main { public static void main(String[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showColorPoint (); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } public void printProperty() { System.out.print(getSize()+"인치 "+Color+"컬러"); } } class Main { public static void main(String [] args) { ColorTV myTV = new ColorTV(32,1024); myTV.printProperty(); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } protected int getColor() { return Color; } } class IPTV extends ColorTV { private String IP; public IPTV(String IP,int size,int Color) { super(size,Color); this.IP = IP; } public void printProperty() { System.out.print("나의 IPTV는 "+IP+" 주소의 "+getSize()+"인치 "+getColor()+"컬러"); } } class Main { public static void main(String[]args) { IPTV iptv = new IPTV("192.1.1.2", 32, 2048); iptv.printProperty(); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint(int x,int y,String Color) { super(x,y); this.Color = Color; } public void setXY(int x,int y) { move (x,y); } public void setColor(String Color) { this.Color = Color; } public String toString () { return Color+"색의 "+"("+ getX() +","+ getY() +")"+"의 점"; } } class Main { public static void main(String[]args) { ColorPoint cp = new ColorPoint(5, 5, "YELLOW"); cp.setXY(10, 20); cp.setColor("RED"); String str = cp.toString(); System.out.println(str + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint() { super(0,0); this.Color = "BLACK"; } public ColorPoint(int x,int y) { super(x,y); this.Color = "BLACK"; } public void setXY(int x,int y) { move(x,y); } public void setColor(String color) { this.Color = color; } public String toString () { return Color + "색의 " + "("+ getX() + ","+ getY() + ")" + "의 점"; } } class Main { public static void main(String[] args) { ColorPoint zeroPoint = new ColorPoint(); System.out.println(zeroPoint.toString() + "입니다."); ColorPoint cp = new ColorPoint(10,10); cp.setXY(5, 5); cp.setColor("RED"); System.out.println(cp.toString() + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class Point3D extends Point { private int z; public Point3D(int x,int y,int z) { super(x,y); this.z = z; } public void moveUp() { z = z+1; } public void moveDown() { z--; } public void move(int x, int y,int z) { super.move(x, y); this.z = z; } public String toString() { return "("+ getX() + "," + getY() + "," + z +")" + "의 점"; } } class Main { public static void main(String[] args) { Point3D p = new Point3D(1,2,3); System.out.println(p.toString() + "입니다."); p.moveUp(); System.out.println(p.toString() + "입니다."); p.moveDown(); p.move(10, 10); System.out.println(p.toString() + "입니다."); p.move(100, 200, 300); System.out.println(p.toString() + "입니다."); } } 클래스 1. 필드 (변수 ,속성) 2. 메소드 (기능, 행동) 3. 생성자(객체 생성시 필드값 초기화) 추상클래스 : 일부만 작성 == 일부는 작성 x */ /* abstract class Person{ int age; String name; void introduce() { //일반 메소드 System.out.println("I'm "+age+"years old. My name is "+name+"."); } abstract void speak(); //추상메소드 ( 이 클래스를 상속받은 모든 클래스는 이 메소드를 구현) } class Student extends Person{ void speak() { System.out.println("I'm Student."); } } class Teacher extends Person{ void speak() { System.out.println("I'm Teacher"); } } class Main{ public static void main(String[] args) { Student s = new Student(); } } */ /* abstract class Calculator { public abstract int add(int a,int b); public abstract int subtract(int a,int b); public abstract double average (int[] a); } class GoodCalc extends Calculator { public int add(int a,int b) { return a + b; } public int subtract(int a,int b) { return a-b; } public double average(int[] a) { double sum = 0; for(int i=0; i<a.length; i++) sum += a[i]; return sum/a.length; } } class Main { public static void main(String[] args) { GoodCalc c = new GoodCalc(); System.out.println(c.add(2,3)); System.out.println(c.subtract(2,3)); System.out.println(c.average(new int [] { 2, 3, 4 })); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Won2Dollar extends Converter { public Won2Dollar(int i) { } @Override protected double convert(double src) { return src/1200; } @Override protected String getSrcString() { return "원"; } @Override protected String getDestString() { return "달러"; } } class Main{ public static void main(String[] args) { Won2Dollar toDollar = new Won2Dollar(1200); toDollar.run(); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Km2Mile extends Converter { public Km2Mile(double i) { ratio=i; } protected double convert(double src) { return src/ratio; } protected String getSrcString() { return "Km"; } protected String getDestString() { return "mile"; } } class Main { public static void main(String[] args) { Km2Mile toMile = new Km2Mile(1.6); toMile.run(); } } */ /* import java.util.*; abstract class PairMap { protected String keyArray []; protected String ValueArray []; abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key); abstract int length(); } class Dictionary extends PairMap { public Dictionary(int i) { } String get(String key) { return ""; } void put(String key, String value) { } String delete(String key) { return null; } int length() { return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(10); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* import java.util.*; interface PairMap { abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key, String value); abstract int length(); } class Dictionary implements PairMap { @Override public String get(String key) { // TODO Auto-generated method stub return null; } @Override public void put(String key, String value) { // TODO Auto-generated method stub } @Override public String delete(String key, String value) { // TODO Auto-generated method stub return null; } @Override public int length() { // TODO Auto-generated method stub return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* interface Shape { final double PI = 3.14; void draw(); double getArea(); default public void redraw() { System.out.print("--- 다시 그립니다. "); draw(); } } class Circle implements Shape{ int r; public Circle(int r) { this.r = r; } public void draw() { System.out.println("반지름이 " + r + "인 원입니다."); } public double getArea() { return r*r*PI; } } class Main { public static void main(String [] args) { Shape donut = new Circle(10); donut.redraw(); System.out.println("면적은 " + donut.getArea()); } } interface MouseDriver { final int BUTTONS = 3; //int VERSION; void move(); public int click(); int out(); void drag(); default void drop() { System.out.println("drop"); } } GUI : 그림으로 보여지는것 컨테이너 container : 종이 판 (다른 것들을 포함한다) 컴포넌트 component : 스티커 (컨테이너 위에 올라간다) import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("example1"); // 창 제목 정하기 (필수아님) setSize(500, 300); //가로500, 세로 300으로 크기 셋팅 (필수) Container c = getContentPane(); // c = 현재 배경 c.setLayout(new FlowLayout()); //레이아웃 설정 (나중에) JButton b = new JButton("버튼"); //버튼 생성 c.add(b); //버튼 추가 JLabel la = new JLabel("레이블컴포넌트입니다"); c.add(la); setVisible(true); // 프레임이 보이게 설정 (필수) } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("ContentPane과 JFrame"); setSize(500,300); Container c = getContentPane(); //c.setLayout(new FlowLayout()); c.setLayout(new BorderLayout(5,10)); c.setBackground(Color.black); //배경색 설정 JButton b = new JButton("OK"); b.setBackground(Color.white); b.setForeground(Color.black); c.add(b,BorderLayout.NORTH); // JButton b1 = new JButton("Cancel"); // b1.setBackground(Color.white); // b1.setForeground(Color.black); //전경색 (글자색) 설정 // c.add(b1, BorderLayout.CENTER); JButton b2 = new JButton("Ignore"); b2.setForeground(Color.black); c.add(b2, BorderLayout.CENTER); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.setBackground(Color.black); c.add(p,BorderLayout.EAST); JButton b3 = new JButton("Cancel"); p.add(new JButton("Cancel")); p.add(b3); b3.setForeground(Color.blue); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("Let's study Java"); setSize(400,200); Container c = getContentPane(); c.setLayout(new BorderLayout(5,7)); JButton b = new JButton("Center"); c.add(b, BorderLayout.CENTER); JButton b1 = new JButton("East"); c.add(b1, BorderLayout.EAST); JButton b2 = new JButton("West"); c.add(b2, BorderLayout.WEST); JButton b3 = new JButton("North"); c.add(b3, BorderLayout.NORTH); JButton b4 = new JButton("South"); c.add(b4, BorderLayout.SOUTH); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("GridLayour Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 2); grid.setVgap(5); Container c = getContentPane(); c.setLayout(grid); c.add(new JLabel(" 이름")); c.add(new JTextField("")); c.add(new JLabel(" 학번")); c.add(new JTextField("")); c.add(new JLabel(" 학과")); c.add(new JTextField("")); c.add(new JLabel(" 과목")); c.add(new JTextField("")); setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Null Container Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); JLabel la = new JLabel("Hello, Press Buttons!"); la.setLocation(130, 50); la.setSize(200, 20); c.add(la); for(int i=1; i<=9; i++) { JButton b = new JButton(Integer.toString(i)); //*********************** //random // //************************ int x = (int) Math.random()*200; int y = (int) Math.random()*300; b.setLocation(x, y); b.setSize(50,20); c.add(b); } setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Ten Color Buttons Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(1, 10); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red, Color.orange, Color.yellow, Color.green, Color.cyan, Color.blue, Color.magenta, Color.darkGray, Color.pink, Color.gray }; for(int i=0; i<10; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("4x4 Color Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 4); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue,Color.magenta,Color.darkGray,Color.pink,Color.gray,Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue}; for(int i=0; i<16; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Random Labels"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); for(int i=0; i<20; i++) { JLabel la = new JLabel(Integer.toString(i)); int x = (int)(Math.random()*200) + 50; int y = (int)(Math.random()*200) + 50; la.setLocation(x,y); la.setSize(15, 15); la.setBackground(Color.blue); la.setForeground(Color.white); la.setOpaque(true); c.add(la); } } public static void main(String[] args) { new Main(); } } event : 모든 변화 ex) 마우스 클릭, 버튼 클릭, 버튼에 마우스 올려놓기, 키보드 a 누르기 ..... action event key event mouse evet change event ... event listener : 이벤트를 어떻게 처리할지 적어놓는 곳 import javax.swing.*; import java.awt.*; import java.awt.event.*; // event를 처리하는 부분 public class Main extends JFrame { Container c = getContentPane(); JPanel nor = new JPanel(); JPanel cen = new JPanel(); JButton b = new JButton("set black"); JButton b1 = new JButton("set red"); JButton b2 = new JButton("set pink"); public Main() { setTitle("Open Challenge 9"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new BorderLayout(3,3)); nor.setLayout(new FlowLayout()); nor.setBackground(Color.LIGHT_GRAY); cen.setLayout(null); //액션리스너 붙여주기 b.addActionListener(new MyActionListener()); b1.addActionListener(new MyActionListener()); b2.addActionListener(new MyActionListener()); nor.add(b); nor.add(b1); nor.add(b2); JLabel l = new JLabel("Hello"); l.setLocation(50,150); l.setSize(30, 30); cen.add(l); JLabel l1 = new JLabel("Java"); l1.setLocation(250,80); l1.setSize(30, 30); cen.add(l1); JLabel l2 = new JLabel("Love"); l2.setLocation(70,50); l2.setSize(30, 30); cen.add(l2); c.add(cen, BorderLayout.CENTER); c.add(nor, BorderLayout.NORTH); setSize(300,300); setVisible(true); } //액션리스너 클래스 작성하기 class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { JButton bb = (JButton)e.getSource(); // 이벤트가 일어난 버튼을 bb if(bb==b) cen.setBackground(Color.black); else if(bb==b1) cen.setBackground(Color.red); else cen.setBackground(Color.pink); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { public Main() { setTitle("Action 이벤트 리스너 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JButton btn = new JButton("Action"); btn.addActionListener(new MyActionListener()); c.add(btn); setSize(350,150); setVisible(true); } public static void main(String[] args) { new Main(); } } class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { JButton b = (JButton)e.getSource(); if(b.getText().equals("Action")) b.setText("액션"); else b.setText("Action"); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { private JLabel la = new JLabel("Hello"); public Main() { setTitle("Mouse 이벤트 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.addMouseListener(new MyMouseListener()); c.setLayout(null); la.setSize(50, 20); la.setLocation(30, 30); c.add(la); setSize(250, 250); setVisible(true); } //implements MouseListener class MyMouseListener extends MouseAdapter { public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } // public void mousePressed(MouseEvent e) { // int x = e.getX(); // int y = e.getY(); // la.setLocation(x, y); // } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("마우스 올리기 내리기"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); la.addMouseListener(new MyMouseListener()); c.setLayout(null); la.setSize(100, 20); la.setLocation(140, 30); c.add(la); setSize(350, 250); setVisible(true); }//implements MouseListener class MyMouseListener extends MouseAdapter { public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub la.setText("Love Java"); } public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub la.setText("사랑해"); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { Container c = getContentPane(); public Main() { setTitle("드래깅동안 YELLOW"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.addMouseMotionListener(new MyMouseMotionListener()); setSize(350, 250); setVisible(true); } class MyMouseMotionListener extends MouseAdapter{ public void mouseDragged(MouseEvent e) { c.setBackground(Color.YELLOW); } public void mouseMoved(MouseEvent e) { c.setBackground(Color.GREEN); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel [] keyMessage; public Main() { setTitle("keyListener 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.addKeyListener(new MyKeyListener()); keyMessage = new JLabel [3]; keyMessage[0] = new JLabel(" getKeyCode() "); keyMessage[1] = new JLabel(" getKeyCode() "); keyMessage[2] = new JLabel(" getKeyCode() "); for(int i=0; i<keyMessage.length; i++) { c.add(keyMessage[i]); keyMessage[i].setOpaque(true); keyMessage[i].setBackground(Color.CYAN); } setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); char keyChar = e.getKeyChar(); keyMessage[0].setText(Integer.toString(keyCode)); keyMessage[1].setText(Character.toString(keyChar)); keyMessage[2].setText(KeyEvent.getKeyText(keyCode)); System.out.println("KeyPressed"); } public void keyReleased(KeyEvent e) { System.out.println("KeyReleased"); } public void keyTyped(KeyEvent e) { System.out.println("KeyTyped"); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel keyMessage; Container c = getContentPane(); public Main() { setTitle("Key Code 예제 : F1키:초록색, %키:노랑색"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new FlowLayout()); c.addKeyListener(new MyKeyListener()); keyMessage = new JLabel("키가 입력되었음"); c.add(keyMessage); setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); char keyChar = e.getKeyChar(); keyMessage.setText(Character.toString(keyChar)+"키가 입력되었음"); if(e.getKeyChar()=='%') { c.setBackground(Color.yellow); } if(e.getKeyCode()==KeyEvent.VK_F1) { c.setBackground(Color.green); } System.out.println("KeyPressed"); } public void keyReleased(KeyEvent e) { System.out.println("KeyReleased"); } public void keyTyped(KeyEvent e) { System.out.println("KeyTyped"); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("HELLO"); public Main() { setTitle("상,하,좌,우 키를 이용하여 텍스트 움직이기"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); la.setLocation(50, 50); la.setSize(50,50); c.add(la); setSize(300, 300); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int x = la.getX(); int y = la.getY(); if(e.getKeyCode()==KeyEvent.VK_UP) { la.setLocation(x,y-10); } else if(e.getKeyCode()==KeyEvent.VK_DOWN) { la.setLocation(x,y+10); } else if(e.getKeyCode()==KeyEvent.VK_LEFT) { la.setLocation(x-10,y); } else if(e.getKeyCode()==KeyEvent.VK_RIGHT) { la.setLocation(x+10,y); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Click and DoubleClick 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.addMouseListener(new MyMouseListener()); setSize(300,200); setVisible(true); } class MyMouseListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { if(e.getClickCount() == 2) { int r = (int)(Math.random()*256); int g = (int)(Math.random()*256); int b = (int)(Math.random()*256); Component c = (Component)e.getSource(); c.setBackground(new Color(r, b, g)); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("Left 키로 문자열 교체"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); c.add(la); la.setSize(100,100); la.setLocation(110,0); setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_LEFT) { String latext = la.getText(); if(latext.equals("Love Java")) { la.setText("avaJ evoL"); } else { la.setText("Love Java"); } } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("Left 키로 문자열 이동"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); c.add(la); la.setSize(100,100); la.setLocation(110,0); setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_LEFT) { String text = la.getText(); String aftertext = text.substring(1) + text.substring(0,1); la.setText(aftertext); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("+,- 키로 폰트 크기 조절"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); c.add(la); la.setSize(400,100); la.setLocation(110,0); la.setFont(new Font("Arial", Font.PLAIN, 10)); setSize(400, 300); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { Font f = la.getFont(); int size = f.getSize(); if(e.getKeyChar() == '+'&&size+5<100) { la.setFont(new Font("Arial", Font.PLAIN, size+5)); } else if(e.getKeyChar() == '-'&&size-5>5) { la.setFont(new Font("Arial", Font.PLAIN, size-5)); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("C"); public Main() { setTitle("클릭 연습 용 응용프로그램"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); la.addMouseListener(new MyMouseListener()); la.setSize(100,100); la.setLocation(100, 100); c.add(la); setSize(300, 250); setVisible(true); } class MyMouseListener extends MouseAdapter{ public void mousePressed(MouseEvent e) { int x = (int)(Math.random()*250); int y = (int)(Math.random()*200); la.setLocation(x, y); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("마우스 휠을 굴려 폰트 크기 조절"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); la.addMouseWheelListener(new MyMouseWheelListener()); c.add(la); la.setSize(500,300); la.setLocation(210, -50); c.setLayout(null); la.setFont(new Font("Arial", Font.PLAIN, 10)); setSize(500, 250); setVisible(true); } class MyMouseWheelListener extends MouseAdapter{ public void mouseWheelMoved(MouseWheelEvent e) { Font f = la.getFont(); int size = f.getSize(); int n = e.getWheelRotation(); if(n<0&&size-5>5) { la.setFont(new Font("Arial", Font.PLAIN, size-5)); } else if(n>0&&size+5<100) { la.setFont(new Font("Arial", Font.PLAIN, size+5)); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("레이블 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JLabel textLabel = new JLabel("사랑합니다."); ImageIcon heart = new ImageIcon("images/heart.png"); JLabel imageLabel = new JLabel(heart); ImageIcon phone = new ImageIcon("images/phone.png"); JLabel label = new JLabel("보고싶으면 전화하세요", phone, SwingConstants.CENTER); c.add(textLabel); c.add(imageLabel); c.add(label); setSize(400,200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("이미지 버튼 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); ImageIcon normalIcon = new ImageIcon("images/phone.png"); ImageIcon rolloverIcon = new ImageIcon("images/incoming-call.png"); ImageIcon pressedIcon = new ImageIcon("images/conversation.png"); JButton btn = new JButton("call~~", normalIcon); btn.setPressedIcon(pressedIcon); btn.setRolloverIcon(rolloverIcon); c.add(btn); setSize(250,150); setVisible(true); } public static void main(String[] args) { new Main(); } } */
0
0
2
minhyuk0426
2023년 2월 19일
In 소스 코드 제출
/* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i,j; int[][] arr = new int[n][n]; for(i=0; i<n; i++) { arr[i][0] = sc.nextInt(); } for(i=1; i<n; i++) { for(j=1; j<=i; j++) { arr[i][j] = arr[i][j-1] - arr[i-1][j-1]; } } for(i=0; i<n; i++) { for(j=0; j<=i; j++) { System.out.print(arr[i][j]+" "); } System.out.println(" "); } } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,sum=0; for(i=a; i<=b; i++) { if(i%2==0) { sum -= i; } else { sum += i; } } System.out.println(sum); } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,j,k,sum=0,dec=0; for(k=a; k<=b; k++) { for(i=1; i<=9; i++) { System.out.println(k+"*"+i+"="+(k*i)); } } } } 20220918 클래스 : 틀 ( 필드 + 생성자 + 메소드) 객체 : 틀로 찍어낸 물체 import java.util.*; class Person{ //field 필드 int age; String name; //method 메소드 (함수) void view() { System.out.println("나이는 "+ age+"이고, 이름은 "+ name+"입니다."); } //constructor 생성자 - 객체를 생성할때 필드 값의 초기값을 정해주는 용도 Person(){ age=5000; name="noname"; } Person(int a){ age=a; name="noname"; } Person(int a, String n){ age=a; name=n; } } public class Main { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); Person s; s = new Person(); Person p = new Person(100,"tom"); Person t = new Person(50); t.view(); p.view(); s.view(); p.age=10; p.name="hello"; s.age=900; s.name="hi"; p.view(); s.view(); //System.out.println("나이는"+ p.age+"이고, 이름은 "+ p.name+"입니다."); //System.out.println("나이는"+ s.age+"이고, 이름은 "+ s.name+"입니다."); } } */ /* class Circle { int radius; String name; public double getArea() { return 3.14*radius*radius; } Circle(int r, String n){ radius=r; name=n; } } class Main{ public static void main(String[] args) { // Circle pizza; // pizza = new Circle(); // pizza.radius = 10; // pizza.name = "자바피자"; // double area = pizza.getArea(); // System.out.println(pizza.name+"의 면적은"+area); Circle donut = new Circle(2,"자바도넛"); double area = donut.getArea(); System.out.println(donut.name+"의 면적은"+area); } } */ /* import java.util.*; class Circle { int radius; String name; public Circle() { radius = 1; name = ""; } public Circle(int r, String n) { radius = r; name = n; } public double getArea() { return 3.14*radius*radius; } } class Main{ public static void main(String[]args) { Circle pizza = new Circle(10, "자바피자"); double area = pizza.getArea(); System.out.println(pizza.name + "의 면적은 " + area); Circle donut = new Circle(); donut.name = "도넛피자"; area = donut.getArea(); System.out.println(donut.name + "의 면적은 " + area); } } private 다른 클래스에서 접근 할 수 없는 필드, 메소드가 된다 */ /* class Song { private String title; public Song(String title) { this.title = title; } public String getTitle() { return title; } } class Main { public static void main(String[] args) { Song mySong = new Song("Nessun Dorma"); Song yourSong = new Song("공주는 잠 못 이루고"); System.out.println("내 노래는 " + mySong.getTitle()); System.out.println("너 노래는 " + yourSong.getTitle()); } } */ /* 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; } } class Main { public static void main(String[] args) { Phone } } */ /* import java.util.*; class TV { private String name; int year; int size; public TV(String name,int year,int size) { this.name = name; this.year = year; this.size = size; } public void show() { System.out.println(name+"에서 만든 "+year+"년형 "+size+"인치 TV"); } } class Main{ public static void main(String [] args) { TV myTV = new TV("LG", 2017, 32); myTV.show(); } } */ /* import java.util.*; class Grade { private int math; private int science; private int english; public Grade(int math,int science,int english) { this.math = math; this.science = science; this.english = english; } public int average() { return (math+science+english)/3; } } class Main { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); System.out.println("수학, 과학, 영어 순으로 3개의 정수 입력>>"); Grade me = new Grade(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()); System.out.println("평균은 " + me.average()); } } */ /* import java.util.*; class Song { private String title; private String country; private String artist; private int year; public Song(int year) { this(year,"미정","미정","미정"); } public Song(int year,String title,String country,String artist) { this.title = title; this.country = country; this.artist = artist; this.year = year; } public void show() { System.out.println(year+"년 "+country+"국적의 "+artist+"가 부른 "+title); } } class Main { public static void main(String [] args) { Song mySong = new Song(1978,"Dancing Queen","스웨덴","ABBA"); Song yourSong = new Song(1945); mySong.show(); } } */ /* import java.util.*; 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; } } class Main { public static void main(String [] args) { Book littlePrince = new Book("어린왕자","생텍쥐페리"); Book loveStory = new Book("춘향전"); Book emptyBook = new Book(); loveStory.show(); } } */ /* class Samp { int id; public Samp(int x) { this.id = x; } public Samp() { this(0); System.out.println("생성자 호출"); } } class ConstructorExample{ //필드 int x; //메소드 public void setx(int x) { this.x = x; } public int getx() { return x; } //생성자 public ConstructorExample() { } public static void main(String [] args) { ConstructorExample a = new ConstructorExample(); int n = a.getx(); } } 객체 배열을 생성할때는 1. 레퍼런스 배열의 레퍼런스 생성 2. 레퍼런스 배열 생성 3. 객체 생성해서 레퍼런스에 연결 class Person{ String name; int age; } class Main{ public static void main(String[] args) { // Person p ;//레퍼런스변수 생성 // p = new Person(); //객체 생성 Person[] arr = new Person[10]; for(int i=0;i<arr.length;i++) { arr[i] = new Person(); } arr[0].name="민혁"; } } * */ /* import java.util.*; class Circle { int radius; public Circle(int radius) { this.radius = radius; } public double getArea() { return 3.14*radius*radius; } } class Main { public static void main(String[] args) { Circle [] c; c = new Circle[5]; for(int i=0; i<c.length; i++) c[i] = new Circle(i); for(int i=0; i<c.length; i++) System.out.println((int)(c[i].getArea()) + " "); } } */ /* import java.util.Scanner; class Book { String title,author; public Book(String title,String author) { this.title = title; this.author = author; } } class Main{ public static void main(String[] args) { Book [] book = new Book[2]; Scanner scanner = new Scanner(System.in); for(int i=0; i<book.length; i++) { System.out.print("제목>>"); String title = scanner.nextLine(); System.out.print("저자>>"); String author = scanner.nextLine(); book[i] = new Book(title, author); } for(int i=0; i<book.length; i++) System.out.print("(" + book[i].title + ", " + book[i].author + ")"); } } */ /* import java.util.*; class Circle { private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Circle c[] = new Circle[3]; for (int i = 0; i < c.length; i++) { System.out.print("x, y, radius >>"); double x = scanner.nextDouble(); double y = scanner.nextDouble(); int radius = scanner.nextInt(); c[i] = new Circle(x, y, radius); } for(int i=0; i<c.length; i++) c[i].show(); scanner.close(); } } import java.util.Scanner; class Dictionary { private static String [] kor = { "사랑","아기","돈","미래","희망" }; private static String [] eng = { "love","baby","monry","future", "hope"}; public static String kor2Eng(String word) { for(int i=0;i<kor.length;i++) { if(kor[i].equals(word)) return eng[i]; } return " 저의 사전에 없습니다."; } } class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("한영 단어 검색 프로그램입니다."); while(true) { System.out.print("한글 단어?"); String name = sc.next(); if(name.equals("그만")) { return ; } else { System.out.println(name+"는 " +Dictionary.kor2Eng(name)); } } } } 부모클래스 - 자식클래스 슈퍼클래스 - 서브클래스 class Person{ int age; String name; } class Student extends Person{ String school; char grade; void speak() { System.out.println("안녕하세요, 저는 학생입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class Teacher extends Person{ String school; String major; void speak() { System.out.println("안녕하세요, 저는 선생님입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class ComTeacher extends Teacher{ } class Main{ public static void main(String[] args) { Student s = new Student(); Teacher t = new Teacher(); s.speak(); } } */ /* import java.util.*; 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; void setColor(String color) { this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main{ public static void main(String [] args) { Point p = new Point(); p.set(1,2); p.showPoint(); ColorPoint cp = new ColorPoint (); cp.set(3,4); cp.setColor("red"); cp.showColorPoint(); } } import java.util.*; class Pen { protected int amount; public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } class SharpPencil extends Pen { private int width; } class BallPen extends Pen { private String color; public String getColor() { return color; } public void setColor(String color ) { this.color = color; } } class FountainPen extends BallPen { public void refill(int n) { amount = n; } } 상속-생성자 class A{ int n; public A() { System.out.println("A의 기본 생성자"); } public A(int n) {this.n = n;} } class B extends A{ int m; public B() { System.out.println("B의 기본 생성자"); } public B(int m) { super(20); this.m = m; System.out.println("B의 1번 생성자"); } } class Main{ public static void main(String[] args) { //A x = new A(10); B y = new B(10); } } */ /* import java.util.*; class Point { private int x,y; public Point() { this.x = this.y = 0; } public Point(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 ColorPoint(int x, int y, String color) { super (x, y); this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main { public static void main(String[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showColorPoint (); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } public void printProperty() { System.out.print(getSize()+"인치 "+Color+"컬러"); } } class Main { public static void main(String [] args) { ColorTV myTV = new ColorTV(32,1024); myTV.printProperty(); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } protected int getColor() { return Color; } } class IPTV extends ColorTV { private String IP; public IPTV(String IP,int size,int Color) { super(size,Color); this.IP = IP; } public void printProperty() { System.out.print("나의 IPTV는 "+IP+" 주소의 "+getSize()+"인치 "+getColor()+"컬러"); } } class Main { public static void main(String[]args) { IPTV iptv = new IPTV("192.1.1.2", 32, 2048); iptv.printProperty(); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint(int x,int y,String Color) { super(x,y); this.Color = Color; } public void setXY(int x,int y) { move (x,y); } public void setColor(String Color) { this.Color = Color; } public String toString () { return Color+"색의 "+"("+ getX() +","+ getY() +")"+"의 점"; } } class Main { public static void main(String[]args) { ColorPoint cp = new ColorPoint(5, 5, "YELLOW"); cp.setXY(10, 20); cp.setColor("RED"); String str = cp.toString(); System.out.println(str + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint() { super(0,0); this.Color = "BLACK"; } public ColorPoint(int x,int y) { super(x,y); this.Color = "BLACK"; } public void setXY(int x,int y) { move(x,y); } public void setColor(String color) { this.Color = color; } public String toString () { return Color + "색의 " + "("+ getX() + ","+ getY() + ")" + "의 점"; } } class Main { public static void main(String[] args) { ColorPoint zeroPoint = new ColorPoint(); System.out.println(zeroPoint.toString() + "입니다."); ColorPoint cp = new ColorPoint(10,10); cp.setXY(5, 5); cp.setColor("RED"); System.out.println(cp.toString() + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class Point3D extends Point { private int z; public Point3D(int x,int y,int z) { super(x,y); this.z = z; } public void moveUp() { z = z+1; } public void moveDown() { z--; } public void move(int x, int y,int z) { super.move(x, y); this.z = z; } public String toString() { return "("+ getX() + "," + getY() + "," + z +")" + "의 점"; } } class Main { public static void main(String[] args) { Point3D p = new Point3D(1,2,3); System.out.println(p.toString() + "입니다."); p.moveUp(); System.out.println(p.toString() + "입니다."); p.moveDown(); p.move(10, 10); System.out.println(p.toString() + "입니다."); p.move(100, 200, 300); System.out.println(p.toString() + "입니다."); } } 클래스 1. 필드 (변수 ,속성) 2. 메소드 (기능, 행동) 3. 생성자(객체 생성시 필드값 초기화) 추상클래스 : 일부만 작성 == 일부는 작성 x */ /* abstract class Person{ int age; String name; void introduce() { //일반 메소드 System.out.println("I'm "+age+"years old. My name is "+name+"."); } abstract void speak(); //추상메소드 ( 이 클래스를 상속받은 모든 클래스는 이 메소드를 구현) } class Student extends Person{ void speak() { System.out.println("I'm Student."); } } class Teacher extends Person{ void speak() { System.out.println("I'm Teacher"); } } class Main{ public static void main(String[] args) { Student s = new Student(); } } */ /* abstract class Calculator { public abstract int add(int a,int b); public abstract int subtract(int a,int b); public abstract double average (int[] a); } class GoodCalc extends Calculator { public int add(int a,int b) { return a + b; } public int subtract(int a,int b) { return a-b; } public double average(int[] a) { double sum = 0; for(int i=0; i<a.length; i++) sum += a[i]; return sum/a.length; } } class Main { public static void main(String[] args) { GoodCalc c = new GoodCalc(); System.out.println(c.add(2,3)); System.out.println(c.subtract(2,3)); System.out.println(c.average(new int [] { 2, 3, 4 })); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Won2Dollar extends Converter { public Won2Dollar(int i) { } @Override protected double convert(double src) { return src/1200; } @Override protected String getSrcString() { return "원"; } @Override protected String getDestString() { return "달러"; } } class Main{ public static void main(String[] args) { Won2Dollar toDollar = new Won2Dollar(1200); toDollar.run(); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Km2Mile extends Converter { public Km2Mile(double i) { ratio=i; } protected double convert(double src) { return src/ratio; } protected String getSrcString() { return "Km"; } protected String getDestString() { return "mile"; } } class Main { public static void main(String[] args) { Km2Mile toMile = new Km2Mile(1.6); toMile.run(); } } */ /* import java.util.*; abstract class PairMap { protected String keyArray []; protected String ValueArray []; abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key); abstract int length(); } class Dictionary extends PairMap { public Dictionary(int i) { } String get(String key) { return ""; } void put(String key, String value) { } String delete(String key) { return null; } int length() { return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(10); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* import java.util.*; interface PairMap { abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key, String value); abstract int length(); } class Dictionary implements PairMap { @Override public String get(String key) { // TODO Auto-generated method stub return null; } @Override public void put(String key, String value) { // TODO Auto-generated method stub } @Override public String delete(String key, String value) { // TODO Auto-generated method stub return null; } @Override public int length() { // TODO Auto-generated method stub return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* interface Shape { final double PI = 3.14; void draw(); double getArea(); default public void redraw() { System.out.print("--- 다시 그립니다. "); draw(); } } class Circle implements Shape{ int r; public Circle(int r) { this.r = r; } public void draw() { System.out.println("반지름이 " + r + "인 원입니다."); } public double getArea() { return r*r*PI; } } class Main { public static void main(String [] args) { Shape donut = new Circle(10); donut.redraw(); System.out.println("면적은 " + donut.getArea()); } } interface MouseDriver { final int BUTTONS = 3; //int VERSION; void move(); public int click(); int out(); void drag(); default void drop() { System.out.println("drop"); } } GUI : 그림으로 보여지는것 컨테이너 container : 종이 판 (다른 것들을 포함한다) 컴포넌트 component : 스티커 (컨테이너 위에 올라간다) import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("example1"); // 창 제목 정하기 (필수아님) setSize(500, 300); //가로500, 세로 300으로 크기 셋팅 (필수) Container c = getContentPane(); // c = 현재 배경 c.setLayout(new FlowLayout()); //레이아웃 설정 (나중에) JButton b = new JButton("버튼"); //버튼 생성 c.add(b); //버튼 추가 JLabel la = new JLabel("레이블컴포넌트입니다"); c.add(la); setVisible(true); // 프레임이 보이게 설정 (필수) } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("ContentPane과 JFrame"); setSize(500,300); Container c = getContentPane(); //c.setLayout(new FlowLayout()); c.setLayout(new BorderLayout(5,10)); c.setBackground(Color.black); //배경색 설정 JButton b = new JButton("OK"); b.setBackground(Color.white); b.setForeground(Color.black); c.add(b,BorderLayout.NORTH); // JButton b1 = new JButton("Cancel"); // b1.setBackground(Color.white); // b1.setForeground(Color.black); //전경색 (글자색) 설정 // c.add(b1, BorderLayout.CENTER); JButton b2 = new JButton("Ignore"); b2.setForeground(Color.black); c.add(b2, BorderLayout.CENTER); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.setBackground(Color.black); c.add(p,BorderLayout.EAST); JButton b3 = new JButton("Cancel"); p.add(new JButton("Cancel")); p.add(b3); b3.setForeground(Color.blue); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("Let's study Java"); setSize(400,200); Container c = getContentPane(); c.setLayout(new BorderLayout(5,7)); JButton b = new JButton("Center"); c.add(b, BorderLayout.CENTER); JButton b1 = new JButton("East"); c.add(b1, BorderLayout.EAST); JButton b2 = new JButton("West"); c.add(b2, BorderLayout.WEST); JButton b3 = new JButton("North"); c.add(b3, BorderLayout.NORTH); JButton b4 = new JButton("South"); c.add(b4, BorderLayout.SOUTH); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("GridLayour Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 2); grid.setVgap(5); Container c = getContentPane(); c.setLayout(grid); c.add(new JLabel(" 이름")); c.add(new JTextField("")); c.add(new JLabel(" 학번")); c.add(new JTextField("")); c.add(new JLabel(" 학과")); c.add(new JTextField("")); c.add(new JLabel(" 과목")); c.add(new JTextField("")); setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Null Container Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); JLabel la = new JLabel("Hello, Press Buttons!"); la.setLocation(130, 50); la.setSize(200, 20); c.add(la); for(int i=1; i<=9; i++) { JButton b = new JButton(Integer.toString(i)); //*********************** //random // //************************ int x = (int) Math.random()*200; int y = (int) Math.random()*300; b.setLocation(x, y); b.setSize(50,20); c.add(b); } setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Ten Color Buttons Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(1, 10); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red, Color.orange, Color.yellow, Color.green, Color.cyan, Color.blue, Color.magenta, Color.darkGray, Color.pink, Color.gray }; for(int i=0; i<10; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("4x4 Color Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 4); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue,Color.magenta,Color.darkGray,Color.pink,Color.gray,Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue}; for(int i=0; i<16; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Random Labels"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); for(int i=0; i<20; i++) { JLabel la = new JLabel(Integer.toString(i)); int x = (int)(Math.random()*200) + 50; int y = (int)(Math.random()*200) + 50; la.setLocation(x,y); la.setSize(15, 15); la.setBackground(Color.blue); la.setForeground(Color.white); la.setOpaque(true); c.add(la); } } public static void main(String[] args) { new Main(); } } event : 모든 변화 ex) 마우스 클릭, 버튼 클릭, 버튼에 마우스 올려놓기, 키보드 a 누르기 ..... action event key event mouse evet change event ... event listener : 이벤트를 어떻게 처리할지 적어놓는 곳 import javax.swing.*; import java.awt.*; import java.awt.event.*; // event를 처리하는 부분 public class Main extends JFrame { Container c = getContentPane(); JPanel nor = new JPanel(); JPanel cen = new JPanel(); JButton b = new JButton("set black"); JButton b1 = new JButton("set red"); JButton b2 = new JButton("set pink"); public Main() { setTitle("Open Challenge 9"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new BorderLayout(3,3)); nor.setLayout(new FlowLayout()); nor.setBackground(Color.LIGHT_GRAY); cen.setLayout(null); //액션리스너 붙여주기 b.addActionListener(new MyActionListener()); b1.addActionListener(new MyActionListener()); b2.addActionListener(new MyActionListener()); nor.add(b); nor.add(b1); nor.add(b2); JLabel l = new JLabel("Hello"); l.setLocation(50,150); l.setSize(30, 30); cen.add(l); JLabel l1 = new JLabel("Java"); l1.setLocation(250,80); l1.setSize(30, 30); cen.add(l1); JLabel l2 = new JLabel("Love"); l2.setLocation(70,50); l2.setSize(30, 30); cen.add(l2); c.add(cen, BorderLayout.CENTER); c.add(nor, BorderLayout.NORTH); setSize(300,300); setVisible(true); } //액션리스너 클래스 작성하기 class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { JButton bb = (JButton)e.getSource(); // 이벤트가 일어난 버튼을 bb if(bb==b) cen.setBackground(Color.black); else if(bb==b1) cen.setBackground(Color.red); else cen.setBackground(Color.pink); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { public Main() { setTitle("Action 이벤트 리스너 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JButton btn = new JButton("Action"); btn.addActionListener(new MyActionListener()); c.add(btn); setSize(350,150); setVisible(true); } public static void main(String[] args) { new Main(); } } class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { JButton b = (JButton)e.getSource(); if(b.getText().equals("Action")) b.setText("액션"); else b.setText("Action"); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { private JLabel la = new JLabel("Hello"); public Main() { setTitle("Mouse 이벤트 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.addMouseListener(new MyMouseListener()); c.setLayout(null); la.setSize(50, 20); la.setLocation(30, 30); c.add(la); setSize(250, 250); setVisible(true); } //implements MouseListener class MyMouseListener extends MouseAdapter { public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } // public void mousePressed(MouseEvent e) { // int x = e.getX(); // int y = e.getY(); // la.setLocation(x, y); // } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("마우스 올리기 내리기"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); la.addMouseListener(new MyMouseListener()); c.setLayout(null); la.setSize(100, 20); la.setLocation(140, 30); c.add(la); setSize(350, 250); setVisible(true); }//implements MouseListener class MyMouseListener extends MouseAdapter { public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub la.setText("Love Java"); } public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub la.setText("사랑해"); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { Container c = getContentPane(); public Main() { setTitle("드래깅동안 YELLOW"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.addMouseMotionListener(new MyMouseMotionListener()); setSize(350, 250); setVisible(true); } class MyMouseMotionListener extends MouseAdapter{ public void mouseDragged(MouseEvent e) { c.setBackground(Color.YELLOW); } public void mouseMoved(MouseEvent e) { c.setBackground(Color.GREEN); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel [] keyMessage; public Main() { setTitle("keyListener 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.addKeyListener(new MyKeyListener()); keyMessage = new JLabel [3]; keyMessage[0] = new JLabel(" getKeyCode() "); keyMessage[1] = new JLabel(" getKeyCode() "); keyMessage[2] = new JLabel(" getKeyCode() "); for(int i=0; i<keyMessage.length; i++) { c.add(keyMessage[i]); keyMessage[i].setOpaque(true); keyMessage[i].setBackground(Color.CYAN); } setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); char keyChar = e.getKeyChar(); keyMessage[0].setText(Integer.toString(keyCode)); keyMessage[1].setText(Character.toString(keyChar)); keyMessage[2].setText(KeyEvent.getKeyText(keyCode)); System.out.println("KeyPressed"); } public void keyReleased(KeyEvent e) { System.out.println("KeyReleased"); } public void keyTyped(KeyEvent e) { System.out.println("KeyTyped"); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel keyMessage; Container c = getContentPane(); public Main() { setTitle("Key Code 예제 : F1키:초록색, %키:노랑색"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new FlowLayout()); c.addKeyListener(new MyKeyListener()); keyMessage = new JLabel("키가 입력되었음"); c.add(keyMessage); setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); char keyChar = e.getKeyChar(); keyMessage.setText(Character.toString(keyChar)+"키가 입력되었음"); if(e.getKeyChar()=='%') { c.setBackground(Color.yellow); } if(e.getKeyCode()==KeyEvent.VK_F1) { c.setBackground(Color.green); } System.out.println("KeyPressed"); } public void keyReleased(KeyEvent e) { System.out.println("KeyReleased"); } public void keyTyped(KeyEvent e) { System.out.println("KeyTyped"); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("HELLO"); public Main() { setTitle("상,하,좌,우 키를 이용하여 텍스트 움직이기"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); la.setLocation(50, 50); la.setSize(50,50); c.add(la); setSize(300, 300); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int x = la.getX(); int y = la.getY(); if(e.getKeyCode()==KeyEvent.VK_UP) { la.setLocation(x,y-10); } else if(e.getKeyCode()==KeyEvent.VK_DOWN) { la.setLocation(x,y+10); } else if(e.getKeyCode()==KeyEvent.VK_LEFT) { la.setLocation(x-10,y); } else if(e.getKeyCode()==KeyEvent.VK_RIGHT) { la.setLocation(x+10,y); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Click and DoubleClick 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.addMouseListener(new MyMouseListener()); setSize(300,200); setVisible(true); } class MyMouseListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { if(e.getClickCount() == 2) { int r = (int)(Math.random()*256); int g = (int)(Math.random()*256); int b = (int)(Math.random()*256); Component c = (Component)e.getSource(); c.setBackground(new Color(r, b, g)); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("Left 키로 문자열 교체"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); c.add(la); la.setSize(100,100); la.setLocation(110,0); setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_LEFT) { String latext = la.getText(); if(latext.equals("Love Java")) { la.setText("avaJ evoL"); } else { la.setText("Love Java"); } } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("Left 키로 문자열 이동"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); c.add(la); la.setSize(100,100); la.setLocation(110,0); setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_LEFT) { String text = la.getText(); String aftertext = text.substring(1) + text.substring(0,1); la.setText(aftertext); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("+,- 키로 폰트 크기 조절"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); c.add(la); la.setSize(400,100); la.setLocation(110,0); la.setFont(new Font("Arial", Font.PLAIN, 10)); setSize(400, 300); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { Font f = la.getFont(); int size = f.getSize(); if(e.getKeyChar() == '+'&&size+5<100) { la.setFont(new Font("Arial", Font.PLAIN, size+5)); } else if(e.getKeyChar() == '-'&&size-5>5) { la.setFont(new Font("Arial", Font.PLAIN, size-5)); } } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("C"); public Main() { setTitle("클릭 연습 용 응용프로그램"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); la.addMouseListener(new MyMouseListener()); la.setSize(100,100); la.setLocation(100, 100); c.add(la); setSize(300, 250); setVisible(true); } class MyMouseListener extends MouseAdapter{ public void mousePressed(MouseEvent e) { int x = (int)(Math.random()*250); int y = (int)(Math.random()*200); la.setLocation(x, y); } } public static void main(String[] args) { new Main(); } } */
0
0
1
minhyuk0426
2023년 2월 12일
In 소스 코드 제출
/* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i,j; int[][] arr = new int[n][n]; for(i=0; i<n; i++) { arr[i][0] = sc.nextInt(); } for(i=1; i<n; i++) { for(j=1; j<=i; j++) { arr[i][j] = arr[i][j-1] - arr[i-1][j-1]; } } for(i=0; i<n; i++) { for(j=0; j<=i; j++) { System.out.print(arr[i][j]+" "); } System.out.println(" "); } } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,sum=0; for(i=a; i<=b; i++) { if(i%2==0) { sum -= i; } else { sum += i; } } System.out.println(sum); } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,j,k,sum=0,dec=0; for(k=a; k<=b; k++) { for(i=1; i<=9; i++) { System.out.println(k+"*"+i+"="+(k*i)); } } } } 20220918 클래스 : 틀 ( 필드 + 생성자 + 메소드) 객체 : 틀로 찍어낸 물체 import java.util.*; class Person{ //field 필드 int age; String name; //method 메소드 (함수) void view() { System.out.println("나이는 "+ age+"이고, 이름은 "+ name+"입니다."); } //constructor 생성자 - 객체를 생성할때 필드 값의 초기값을 정해주는 용도 Person(){ age=5000; name="noname"; } Person(int a){ age=a; name="noname"; } Person(int a, String n){ age=a; name=n; } } public class Main { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); Person s; s = new Person(); Person p = new Person(100,"tom"); Person t = new Person(50); t.view(); p.view(); s.view(); p.age=10; p.name="hello"; s.age=900; s.name="hi"; p.view(); s.view(); //System.out.println("나이는"+ p.age+"이고, 이름은 "+ p.name+"입니다."); //System.out.println("나이는"+ s.age+"이고, 이름은 "+ s.name+"입니다."); } } */ /* class Circle { int radius; String name; public double getArea() { return 3.14*radius*radius; } Circle(int r, String n){ radius=r; name=n; } } class Main{ public static void main(String[] args) { // Circle pizza; // pizza = new Circle(); // pizza.radius = 10; // pizza.name = "자바피자"; // double area = pizza.getArea(); // System.out.println(pizza.name+"의 면적은"+area); Circle donut = new Circle(2,"자바도넛"); double area = donut.getArea(); System.out.println(donut.name+"의 면적은"+area); } } */ /* import java.util.*; class Circle { int radius; String name; public Circle() { radius = 1; name = ""; } public Circle(int r, String n) { radius = r; name = n; } public double getArea() { return 3.14*radius*radius; } } class Main{ public static void main(String[]args) { Circle pizza = new Circle(10, "자바피자"); double area = pizza.getArea(); System.out.println(pizza.name + "의 면적은 " + area); Circle donut = new Circle(); donut.name = "도넛피자"; area = donut.getArea(); System.out.println(donut.name + "의 면적은 " + area); } } private 다른 클래스에서 접근 할 수 없는 필드, 메소드가 된다 */ /* class Song { private String title; public Song(String title) { this.title = title; } public String getTitle() { return title; } } class Main { public static void main(String[] args) { Song mySong = new Song("Nessun Dorma"); Song yourSong = new Song("공주는 잠 못 이루고"); System.out.println("내 노래는 " + mySong.getTitle()); System.out.println("너 노래는 " + yourSong.getTitle()); } } */ /* 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; } } class Main { public static void main(String[] args) { Phone } } */ /* import java.util.*; class TV { private String name; int year; int size; public TV(String name,int year,int size) { this.name = name; this.year = year; this.size = size; } public void show() { System.out.println(name+"에서 만든 "+year+"년형 "+size+"인치 TV"); } } class Main{ public static void main(String [] args) { TV myTV = new TV("LG", 2017, 32); myTV.show(); } } */ /* import java.util.*; class Grade { private int math; private int science; private int english; public Grade(int math,int science,int english) { this.math = math; this.science = science; this.english = english; } public int average() { return (math+science+english)/3; } } class Main { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); System.out.println("수학, 과학, 영어 순으로 3개의 정수 입력>>"); Grade me = new Grade(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()); System.out.println("평균은 " + me.average()); } } */ /* import java.util.*; class Song { private String title; private String country; private String artist; private int year; public Song(int year) { this(year,"미정","미정","미정"); } public Song(int year,String title,String country,String artist) { this.title = title; this.country = country; this.artist = artist; this.year = year; } public void show() { System.out.println(year+"년 "+country+"국적의 "+artist+"가 부른 "+title); } } class Main { public static void main(String [] args) { Song mySong = new Song(1978,"Dancing Queen","스웨덴","ABBA"); Song yourSong = new Song(1945); mySong.show(); } } */ /* import java.util.*; 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; } } class Main { public static void main(String [] args) { Book littlePrince = new Book("어린왕자","생텍쥐페리"); Book loveStory = new Book("춘향전"); Book emptyBook = new Book(); loveStory.show(); } } */ /* class Samp { int id; public Samp(int x) { this.id = x; } public Samp() { this(0); System.out.println("생성자 호출"); } } class ConstructorExample{ //필드 int x; //메소드 public void setx(int x) { this.x = x; } public int getx() { return x; } //생성자 public ConstructorExample() { } public static void main(String [] args) { ConstructorExample a = new ConstructorExample(); int n = a.getx(); } } 객체 배열을 생성할때는 1. 레퍼런스 배열의 레퍼런스 생성 2. 레퍼런스 배열 생성 3. 객체 생성해서 레퍼런스에 연결 class Person{ String name; int age; } class Main{ public static void main(String[] args) { // Person p ;//레퍼런스변수 생성 // p = new Person(); //객체 생성 Person[] arr = new Person[10]; for(int i=0;i<arr.length;i++) { arr[i] = new Person(); } arr[0].name="민혁"; } } * */ /* import java.util.*; class Circle { int radius; public Circle(int radius) { this.radius = radius; } public double getArea() { return 3.14*radius*radius; } } class Main { public static void main(String[] args) { Circle [] c; c = new Circle[5]; for(int i=0; i<c.length; i++) c[i] = new Circle(i); for(int i=0; i<c.length; i++) System.out.println((int)(c[i].getArea()) + " "); } } */ /* import java.util.Scanner; class Book { String title,author; public Book(String title,String author) { this.title = title; this.author = author; } } class Main{ public static void main(String[] args) { Book [] book = new Book[2]; Scanner scanner = new Scanner(System.in); for(int i=0; i<book.length; i++) { System.out.print("제목>>"); String title = scanner.nextLine(); System.out.print("저자>>"); String author = scanner.nextLine(); book[i] = new Book(title, author); } for(int i=0; i<book.length; i++) System.out.print("(" + book[i].title + ", " + book[i].author + ")"); } } */ /* import java.util.*; class Circle { private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Circle c[] = new Circle[3]; for (int i = 0; i < c.length; i++) { System.out.print("x, y, radius >>"); double x = scanner.nextDouble(); double y = scanner.nextDouble(); int radius = scanner.nextInt(); c[i] = new Circle(x, y, radius); } for(int i=0; i<c.length; i++) c[i].show(); scanner.close(); } } import java.util.Scanner; class Dictionary { private static String [] kor = { "사랑","아기","돈","미래","희망" }; private static String [] eng = { "love","baby","monry","future", "hope"}; public static String kor2Eng(String word) { for(int i=0;i<kor.length;i++) { if(kor[i].equals(word)) return eng[i]; } return " 저의 사전에 없습니다."; } } class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("한영 단어 검색 프로그램입니다."); while(true) { System.out.print("한글 단어?"); String name = sc.next(); if(name.equals("그만")) { return ; } else { System.out.println(name+"는 " +Dictionary.kor2Eng(name)); } } } } 부모클래스 - 자식클래스 슈퍼클래스 - 서브클래스 class Person{ int age; String name; } class Student extends Person{ String school; char grade; void speak() { System.out.println("안녕하세요, 저는 학생입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class Teacher extends Person{ String school; String major; void speak() { System.out.println("안녕하세요, 저는 선생님입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class ComTeacher extends Teacher{ } class Main{ public static void main(String[] args) { Student s = new Student(); Teacher t = new Teacher(); s.speak(); } } */ /* import java.util.*; 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; void setColor(String color) { this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main{ public static void main(String [] args) { Point p = new Point(); p.set(1,2); p.showPoint(); ColorPoint cp = new ColorPoint (); cp.set(3,4); cp.setColor("red"); cp.showColorPoint(); } } import java.util.*; class Pen { protected int amount; public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } class SharpPencil extends Pen { private int width; } class BallPen extends Pen { private String color; public String getColor() { return color; } public void setColor(String color ) { this.color = color; } } class FountainPen extends BallPen { public void refill(int n) { amount = n; } } 상속-생성자 class A{ int n; public A() { System.out.println("A의 기본 생성자"); } public A(int n) {this.n = n;} } class B extends A{ int m; public B() { System.out.println("B의 기본 생성자"); } public B(int m) { super(20); this.m = m; System.out.println("B의 1번 생성자"); } } class Main{ public static void main(String[] args) { //A x = new A(10); B y = new B(10); } } */ /* import java.util.*; class Point { private int x,y; public Point() { this.x = this.y = 0; } public Point(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 ColorPoint(int x, int y, String color) { super (x, y); this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main { public static void main(String[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showColorPoint (); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } public void printProperty() { System.out.print(getSize()+"인치 "+Color+"컬러"); } } class Main { public static void main(String [] args) { ColorTV myTV = new ColorTV(32,1024); myTV.printProperty(); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } protected int getColor() { return Color; } } class IPTV extends ColorTV { private String IP; public IPTV(String IP,int size,int Color) { super(size,Color); this.IP = IP; } public void printProperty() { System.out.print("나의 IPTV는 "+IP+" 주소의 "+getSize()+"인치 "+getColor()+"컬러"); } } class Main { public static void main(String[]args) { IPTV iptv = new IPTV("192.1.1.2", 32, 2048); iptv.printProperty(); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint(int x,int y,String Color) { super(x,y); this.Color = Color; } public void setXY(int x,int y) { move (x,y); } public void setColor(String Color) { this.Color = Color; } public String toString () { return Color+"색의 "+"("+ getX() +","+ getY() +")"+"의 점"; } } class Main { public static void main(String[]args) { ColorPoint cp = new ColorPoint(5, 5, "YELLOW"); cp.setXY(10, 20); cp.setColor("RED"); String str = cp.toString(); System.out.println(str + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint() { super(0,0); this.Color = "BLACK"; } public ColorPoint(int x,int y) { super(x,y); this.Color = "BLACK"; } public void setXY(int x,int y) { move(x,y); } public void setColor(String color) { this.Color = color; } public String toString () { return Color + "색의 " + "("+ getX() + ","+ getY() + ")" + "의 점"; } } class Main { public static void main(String[] args) { ColorPoint zeroPoint = new ColorPoint(); System.out.println(zeroPoint.toString() + "입니다."); ColorPoint cp = new ColorPoint(10,10); cp.setXY(5, 5); cp.setColor("RED"); System.out.println(cp.toString() + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class Point3D extends Point { private int z; public Point3D(int x,int y,int z) { super(x,y); this.z = z; } public void moveUp() { z = z+1; } public void moveDown() { z--; } public void move(int x, int y,int z) { super.move(x, y); this.z = z; } public String toString() { return "("+ getX() + "," + getY() + "," + z +")" + "의 점"; } } class Main { public static void main(String[] args) { Point3D p = new Point3D(1,2,3); System.out.println(p.toString() + "입니다."); p.moveUp(); System.out.println(p.toString() + "입니다."); p.moveDown(); p.move(10, 10); System.out.println(p.toString() + "입니다."); p.move(100, 200, 300); System.out.println(p.toString() + "입니다."); } } 클래스 1. 필드 (변수 ,속성) 2. 메소드 (기능, 행동) 3. 생성자(객체 생성시 필드값 초기화) 추상클래스 : 일부만 작성 == 일부는 작성 x */ /* abstract class Person{ int age; String name; void introduce() { //일반 메소드 System.out.println("I'm "+age+"years old. My name is "+name+"."); } abstract void speak(); //추상메소드 ( 이 클래스를 상속받은 모든 클래스는 이 메소드를 구현) } class Student extends Person{ void speak() { System.out.println("I'm Student."); } } class Teacher extends Person{ void speak() { System.out.println("I'm Teacher"); } } class Main{ public static void main(String[] args) { Student s = new Student(); } } */ /* abstract class Calculator { public abstract int add(int a,int b); public abstract int subtract(int a,int b); public abstract double average (int[] a); } class GoodCalc extends Calculator { public int add(int a,int b) { return a + b; } public int subtract(int a,int b) { return a-b; } public double average(int[] a) { double sum = 0; for(int i=0; i<a.length; i++) sum += a[i]; return sum/a.length; } } class Main { public static void main(String[] args) { GoodCalc c = new GoodCalc(); System.out.println(c.add(2,3)); System.out.println(c.subtract(2,3)); System.out.println(c.average(new int [] { 2, 3, 4 })); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Won2Dollar extends Converter { public Won2Dollar(int i) { } @Override protected double convert(double src) { return src/1200; } @Override protected String getSrcString() { return "원"; } @Override protected String getDestString() { return "달러"; } } class Main{ public static void main(String[] args) { Won2Dollar toDollar = new Won2Dollar(1200); toDollar.run(); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Km2Mile extends Converter { public Km2Mile(double i) { ratio=i; } protected double convert(double src) { return src/ratio; } protected String getSrcString() { return "Km"; } protected String getDestString() { return "mile"; } } class Main { public static void main(String[] args) { Km2Mile toMile = new Km2Mile(1.6); toMile.run(); } } */ /* import java.util.*; abstract class PairMap { protected String keyArray []; protected String ValueArray []; abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key); abstract int length(); } class Dictionary extends PairMap { public Dictionary(int i) { } String get(String key) { return ""; } void put(String key, String value) { } String delete(String key) { return null; } int length() { return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(10); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* import java.util.*; interface PairMap { abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key, String value); abstract int length(); } class Dictionary implements PairMap { @Override public String get(String key) { // TODO Auto-generated method stub return null; } @Override public void put(String key, String value) { // TODO Auto-generated method stub } @Override public String delete(String key, String value) { // TODO Auto-generated method stub return null; } @Override public int length() { // TODO Auto-generated method stub return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* interface Shape { final double PI = 3.14; void draw(); double getArea(); default public void redraw() { System.out.print("--- 다시 그립니다. "); draw(); } } class Circle implements Shape{ int r; public Circle(int r) { this.r = r; } public void draw() { System.out.println("반지름이 " + r + "인 원입니다."); } public double getArea() { return r*r*PI; } } class Main { public static void main(String [] args) { Shape donut = new Circle(10); donut.redraw(); System.out.println("면적은 " + donut.getArea()); } } interface MouseDriver { final int BUTTONS = 3; //int VERSION; void move(); public int click(); int out(); void drag(); default void drop() { System.out.println("drop"); } } GUI : 그림으로 보여지는것 컨테이너 container : 종이 판 (다른 것들을 포함한다) 컴포넌트 component : 스티커 (컨테이너 위에 올라간다) import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("example1"); // 창 제목 정하기 (필수아님) setSize(500, 300); //가로500, 세로 300으로 크기 셋팅 (필수) Container c = getContentPane(); // c = 현재 배경 c.setLayout(new FlowLayout()); //레이아웃 설정 (나중에) JButton b = new JButton("버튼"); //버튼 생성 c.add(b); //버튼 추가 JLabel la = new JLabel("레이블컴포넌트입니다"); c.add(la); setVisible(true); // 프레임이 보이게 설정 (필수) } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("ContentPane과 JFrame"); setSize(500,300); Container c = getContentPane(); //c.setLayout(new FlowLayout()); c.setLayout(new BorderLayout(5,10)); c.setBackground(Color.black); //배경색 설정 JButton b = new JButton("OK"); b.setBackground(Color.white); b.setForeground(Color.black); c.add(b,BorderLayout.NORTH); // JButton b1 = new JButton("Cancel"); // b1.setBackground(Color.white); // b1.setForeground(Color.black); //전경색 (글자색) 설정 // c.add(b1, BorderLayout.CENTER); JButton b2 = new JButton("Ignore"); b2.setForeground(Color.black); c.add(b2, BorderLayout.CENTER); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.setBackground(Color.black); c.add(p,BorderLayout.EAST); JButton b3 = new JButton("Cancel"); p.add(new JButton("Cancel")); p.add(b3); b3.setForeground(Color.blue); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("Let's study Java"); setSize(400,200); Container c = getContentPane(); c.setLayout(new BorderLayout(5,7)); JButton b = new JButton("Center"); c.add(b, BorderLayout.CENTER); JButton b1 = new JButton("East"); c.add(b1, BorderLayout.EAST); JButton b2 = new JButton("West"); c.add(b2, BorderLayout.WEST); JButton b3 = new JButton("North"); c.add(b3, BorderLayout.NORTH); JButton b4 = new JButton("South"); c.add(b4, BorderLayout.SOUTH); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("GridLayour Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 2); grid.setVgap(5); Container c = getContentPane(); c.setLayout(grid); c.add(new JLabel(" 이름")); c.add(new JTextField("")); c.add(new JLabel(" 학번")); c.add(new JTextField("")); c.add(new JLabel(" 학과")); c.add(new JTextField("")); c.add(new JLabel(" 과목")); c.add(new JTextField("")); setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Null Container Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); JLabel la = new JLabel("Hello, Press Buttons!"); la.setLocation(130, 50); la.setSize(200, 20); c.add(la); for(int i=1; i<=9; i++) { JButton b = new JButton(Integer.toString(i)); //*********************** //random // //************************ int x = (int) Math.random()*200; int y = (int) Math.random()*300; b.setLocation(x, y); b.setSize(50,20); c.add(b); } setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Ten Color Buttons Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(1, 10); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red, Color.orange, Color.yellow, Color.green, Color.cyan, Color.blue, Color.magenta, Color.darkGray, Color.pink, Color.gray }; for(int i=0; i<10; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("4x4 Color Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 4); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue,Color.magenta,Color.darkGray,Color.pink,Color.gray,Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue}; for(int i=0; i<16; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Random Labels"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); for(int i=0; i<20; i++) { JLabel la = new JLabel(Integer.toString(i)); int x = (int)(Math.random()*200) + 50; int y = (int)(Math.random()*200) + 50; la.setLocation(x,y); la.setSize(15, 15); la.setBackground(Color.blue); la.setForeground(Color.white); la.setOpaque(true); c.add(la); } } public static void main(String[] args) { new Main(); } } event : 모든 변화 ex) 마우스 클릭, 버튼 클릭, 버튼에 마우스 올려놓기, 키보드 a 누르기 ..... action event key event mouse evet change event ... event listener : 이벤트를 어떻게 처리할지 적어놓는 곳 import javax.swing.*; import java.awt.*; import java.awt.event.*; // event를 처리하는 부분 public class Main extends JFrame { Container c = getContentPane(); JPanel nor = new JPanel(); JPanel cen = new JPanel(); JButton b = new JButton("set black"); JButton b1 = new JButton("set red"); JButton b2 = new JButton("set pink"); public Main() { setTitle("Open Challenge 9"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new BorderLayout(3,3)); nor.setLayout(new FlowLayout()); nor.setBackground(Color.LIGHT_GRAY); cen.setLayout(null); //액션리스너 붙여주기 b.addActionListener(new MyActionListener()); b1.addActionListener(new MyActionListener()); b2.addActionListener(new MyActionListener()); nor.add(b); nor.add(b1); nor.add(b2); JLabel l = new JLabel("Hello"); l.setLocation(50,150); l.setSize(30, 30); cen.add(l); JLabel l1 = new JLabel("Java"); l1.setLocation(250,80); l1.setSize(30, 30); cen.add(l1); JLabel l2 = new JLabel("Love"); l2.setLocation(70,50); l2.setSize(30, 30); cen.add(l2); c.add(cen, BorderLayout.CENTER); c.add(nor, BorderLayout.NORTH); setSize(300,300); setVisible(true); } //액션리스너 클래스 작성하기 class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { JButton bb = (JButton)e.getSource(); // 이벤트가 일어난 버튼을 bb if(bb==b) cen.setBackground(Color.black); else if(bb==b1) cen.setBackground(Color.red); else cen.setBackground(Color.pink); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { public Main() { setTitle("Action 이벤트 리스너 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JButton btn = new JButton("Action"); btn.addActionListener(new MyActionListener()); c.add(btn); setSize(350,150); setVisible(true); } public static void main(String[] args) { new Main(); } } class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { JButton b = (JButton)e.getSource(); if(b.getText().equals("Action")) b.setText("액션"); else b.setText("Action"); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { private JLabel la = new JLabel("Hello"); public Main() { setTitle("Mouse 이벤트 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.addMouseListener(new MyMouseListener()); c.setLayout(null); la.setSize(50, 20); la.setLocation(30, 30); c.add(la); setSize(250, 250); setVisible(true); } //implements MouseListener class MyMouseListener extends MouseAdapter { public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } // public void mousePressed(MouseEvent e) { // int x = e.getX(); // int y = e.getY(); // la.setLocation(x, y); // } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("마우스 올리기 내리기"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); la.addMouseListener(new MyMouseListener()); c.setLayout(null); la.setSize(100, 20); la.setLocation(140, 30); c.add(la); setSize(350, 250); setVisible(true); }//implements MouseListener class MyMouseListener extends MouseAdapter { public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub la.setText("Love Java"); } public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub la.setText("사랑해"); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { Container c = getContentPane(); public Main() { setTitle("드래깅동안 YELLOW"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.addMouseMotionListener(new MyMouseMotionListener()); setSize(350, 250); setVisible(true); } class MyMouseMotionListener extends MouseAdapter{ public void mouseDragged(MouseEvent e) { c.setBackground(Color.YELLOW); } public void mouseMoved(MouseEvent e) { c.setBackground(Color.GREEN); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel [] keyMessage; public Main() { setTitle("keyListener 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.addKeyListener(new MyKeyListener()); keyMessage = new JLabel [3]; keyMessage[0] = new JLabel(" getKeyCode() "); keyMessage[1] = new JLabel(" getKeyCode() "); keyMessage[2] = new JLabel(" getKeyCode() "); for(int i=0; i<keyMessage.length; i++) { c.add(keyMessage[i]); keyMessage[i].setOpaque(true); keyMessage[i].setBackground(Color.CYAN); } setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); char keyChar = e.getKeyChar(); keyMessage[0].setText(Integer.toString(keyCode)); keyMessage[1].setText(Character.toString(keyChar)); keyMessage[2].setText(KeyEvent.getKeyText(keyCode)); System.out.println("KeyPressed"); } public void keyReleased(KeyEvent e) { System.out.println("KeyReleased"); } public void keyTyped(KeyEvent e) { System.out.println("KeyTyped"); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel keyMessage; Container c = getContentPane(); public Main() { setTitle("Key Code 예제 : F1키:초록색, %키:노랑색"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new FlowLayout()); c.addKeyListener(new MyKeyListener()); keyMessage = new JLabel("키가 입력되었음"); c.add(keyMessage); setSize(300, 150); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); char keyChar = e.getKeyChar(); keyMessage.setText(Character.toString(keyChar)+"키가 입력되었음"); if(e.getKeyChar()=='%') { c.setBackground(Color.yellow); } if(e.getKeyCode()==KeyEvent.VK_F1) { c.setBackground(Color.green); } System.out.println("KeyPressed"); } public void keyReleased(KeyEvent e) { System.out.println("KeyReleased"); } public void keyTyped(KeyEvent e) { System.out.println("KeyTyped"); } } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Main extends JFrame { private JLabel la = new JLabel("HELLO"); public Main() { setTitle("상,하,좌,우 키를 이용하여 텍스트 움직이기"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); c.addKeyListener(new MyKeyListener()); la.setLocation(50, 50); la.setSize(50,50); c.add(la); setSize(300, 300); setVisible(true); c.setFocusable(true); c.requestFocus(); } class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int x = la.getX(); int y = la.getY(); if(e.getKeyCode()==KeyEvent.VK_UP) { la.setLocation(x,y-10); } else if(e.getKeyCode()==KeyEvent.VK_DOWN) { la.setLocation(x,y+10); } else if(e.getKeyCode()==KeyEvent.VK_LEFT) { la.setLocation(x-10,y); } else if(e.getKeyCode()==KeyEvent.VK_RIGHT) { la.setLocation(x+10,y); } } } public static void main(String[] args) { new Main(); } } */
0
0
1
minhyuk0426
2023년 1월 29일
In 소스 코드 제출
/* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i,j; int[][] arr = new int[n][n]; for(i=0; i<n; i++) { arr[i][0] = sc.nextInt(); } for(i=1; i<n; i++) { for(j=1; j<=i; j++) { arr[i][j] = arr[i][j-1] - arr[i-1][j-1]; } } for(i=0; i<n; i++) { for(j=0; j<=i; j++) { System.out.print(arr[i][j]+" "); } System.out.println(" "); } } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,sum=0; for(i=a; i<=b; i++) { if(i%2==0) { sum -= i; } else { sum += i; } } System.out.println(sum); } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,j,k,sum=0,dec=0; for(k=a; k<=b; k++) { for(i=1; i<=9; i++) { System.out.println(k+"*"+i+"="+(k*i)); } } } } 20220918 클래스 : 틀 ( 필드 + 생성자 + 메소드) 객체 : 틀로 찍어낸 물체 import java.util.*; class Person{ //field 필드 int age; String name; //method 메소드 (함수) void view() { System.out.println("나이는 "+ age+"이고, 이름은 "+ name+"입니다."); } //constructor 생성자 - 객체를 생성할때 필드 값의 초기값을 정해주는 용도 Person(){ age=5000; name="noname"; } Person(int a){ age=a; name="noname"; } Person(int a, String n){ age=a; name=n; } } public class Main { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); Person s; s = new Person(); Person p = new Person(100,"tom"); Person t = new Person(50); t.view(); p.view(); s.view(); p.age=10; p.name="hello"; s.age=900; s.name="hi"; p.view(); s.view(); //System.out.println("나이는"+ p.age+"이고, 이름은 "+ p.name+"입니다."); //System.out.println("나이는"+ s.age+"이고, 이름은 "+ s.name+"입니다."); } } */ /* class Circle { int radius; String name; public double getArea() { return 3.14*radius*radius; } Circle(int r, String n){ radius=r; name=n; } } class Main{ public static void main(String[] args) { // Circle pizza; // pizza = new Circle(); // pizza.radius = 10; // pizza.name = "자바피자"; // double area = pizza.getArea(); // System.out.println(pizza.name+"의 면적은"+area); Circle donut = new Circle(2,"자바도넛"); double area = donut.getArea(); System.out.println(donut.name+"의 면적은"+area); } } */ /* import java.util.*; class Circle { int radius; String name; public Circle() { radius = 1; name = ""; } public Circle(int r, String n) { radius = r; name = n; } public double getArea() { return 3.14*radius*radius; } } class Main{ public static void main(String[]args) { Circle pizza = new Circle(10, "자바피자"); double area = pizza.getArea(); System.out.println(pizza.name + "의 면적은 " + area); Circle donut = new Circle(); donut.name = "도넛피자"; area = donut.getArea(); System.out.println(donut.name + "의 면적은 " + area); } } private 다른 클래스에서 접근 할 수 없는 필드, 메소드가 된다 */ /* class Song { private String title; public Song(String title) { this.title = title; } public String getTitle() { return title; } } class Main { public static void main(String[] args) { Song mySong = new Song("Nessun Dorma"); Song yourSong = new Song("공주는 잠 못 이루고"); System.out.println("내 노래는 " + mySong.getTitle()); System.out.println("너 노래는 " + yourSong.getTitle()); } } */ /* 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; } } class Main { public static void main(String[] args) { Phone } } */ /* import java.util.*; class TV { private String name; int year; int size; public TV(String name,int year,int size) { this.name = name; this.year = year; this.size = size; } public void show() { System.out.println(name+"에서 만든 "+year+"년형 "+size+"인치 TV"); } } class Main{ public static void main(String [] args) { TV myTV = new TV("LG", 2017, 32); myTV.show(); } } */ /* import java.util.*; class Grade { private int math; private int science; private int english; public Grade(int math,int science,int english) { this.math = math; this.science = science; this.english = english; } public int average() { return (math+science+english)/3; } } class Main { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); System.out.println("수학, 과학, 영어 순으로 3개의 정수 입력>>"); Grade me = new Grade(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()); System.out.println("평균은 " + me.average()); } } */ /* import java.util.*; class Song { private String title; private String country; private String artist; private int year; public Song(int year) { this(year,"미정","미정","미정"); } public Song(int year,String title,String country,String artist) { this.title = title; this.country = country; this.artist = artist; this.year = year; } public void show() { System.out.println(year+"년 "+country+"국적의 "+artist+"가 부른 "+title); } } class Main { public static void main(String [] args) { Song mySong = new Song(1978,"Dancing Queen","스웨덴","ABBA"); Song yourSong = new Song(1945); mySong.show(); } } */ /* import java.util.*; 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; } } class Main { public static void main(String [] args) { Book littlePrince = new Book("어린왕자","생텍쥐페리"); Book loveStory = new Book("춘향전"); Book emptyBook = new Book(); loveStory.show(); } } */ /* class Samp { int id; public Samp(int x) { this.id = x; } public Samp() { this(0); System.out.println("생성자 호출"); } } class ConstructorExample{ //필드 int x; //메소드 public void setx(int x) { this.x = x; } public int getx() { return x; } //생성자 public ConstructorExample() { } public static void main(String [] args) { ConstructorExample a = new ConstructorExample(); int n = a.getx(); } } 객체 배열을 생성할때는 1. 레퍼런스 배열의 레퍼런스 생성 2. 레퍼런스 배열 생성 3. 객체 생성해서 레퍼런스에 연결 class Person{ String name; int age; } class Main{ public static void main(String[] args) { // Person p ;//레퍼런스변수 생성 // p = new Person(); //객체 생성 Person[] arr = new Person[10]; for(int i=0;i<arr.length;i++) { arr[i] = new Person(); } arr[0].name="민혁"; } } * */ /* import java.util.*; class Circle { int radius; public Circle(int radius) { this.radius = radius; } public double getArea() { return 3.14*radius*radius; } } class Main { public static void main(String[] args) { Circle [] c; c = new Circle[5]; for(int i=0; i<c.length; i++) c[i] = new Circle(i); for(int i=0; i<c.length; i++) System.out.println((int)(c[i].getArea()) + " "); } } */ /* import java.util.Scanner; class Book { String title,author; public Book(String title,String author) { this.title = title; this.author = author; } } class Main{ public static void main(String[] args) { Book [] book = new Book[2]; Scanner scanner = new Scanner(System.in); for(int i=0; i<book.length; i++) { System.out.print("제목>>"); String title = scanner.nextLine(); System.out.print("저자>>"); String author = scanner.nextLine(); book[i] = new Book(title, author); } for(int i=0; i<book.length; i++) System.out.print("(" + book[i].title + ", " + book[i].author + ")"); } } */ /* import java.util.*; class Circle { private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Circle c[] = new Circle[3]; for (int i = 0; i < c.length; i++) { System.out.print("x, y, radius >>"); double x = scanner.nextDouble(); double y = scanner.nextDouble(); int radius = scanner.nextInt(); c[i] = new Circle(x, y, radius); } for(int i=0; i<c.length; i++) c[i].show(); scanner.close(); } } import java.util.Scanner; class Dictionary { private static String [] kor = { "사랑","아기","돈","미래","희망" }; private static String [] eng = { "love","baby","monry","future", "hope"}; public static String kor2Eng(String word) { for(int i=0;i<kor.length;i++) { if(kor[i].equals(word)) return eng[i]; } return " 저의 사전에 없습니다."; } } class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("한영 단어 검색 프로그램입니다."); while(true) { System.out.print("한글 단어?"); String name = sc.next(); if(name.equals("그만")) { return ; } else { System.out.println(name+"는 " +Dictionary.kor2Eng(name)); } } } } 부모클래스 - 자식클래스 슈퍼클래스 - 서브클래스 class Person{ int age; String name; } class Student extends Person{ String school; char grade; void speak() { System.out.println("안녕하세요, 저는 학생입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class Teacher extends Person{ String school; String major; void speak() { System.out.println("안녕하세요, 저는 선생님입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class ComTeacher extends Teacher{ } class Main{ public static void main(String[] args) { Student s = new Student(); Teacher t = new Teacher(); s.speak(); } } */ /* import java.util.*; 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; void setColor(String color) { this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main{ public static void main(String [] args) { Point p = new Point(); p.set(1,2); p.showPoint(); ColorPoint cp = new ColorPoint (); cp.set(3,4); cp.setColor("red"); cp.showColorPoint(); } } import java.util.*; class Pen { protected int amount; public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } class SharpPencil extends Pen { private int width; } class BallPen extends Pen { private String color; public String getColor() { return color; } public void setColor(String color ) { this.color = color; } } class FountainPen extends BallPen { public void refill(int n) { amount = n; } } 상속-생성자 class A{ int n; public A() { System.out.println("A의 기본 생성자"); } public A(int n) {this.n = n;} } class B extends A{ int m; public B() { System.out.println("B의 기본 생성자"); } public B(int m) { super(20); this.m = m; System.out.println("B의 1번 생성자"); } } class Main{ public static void main(String[] args) { //A x = new A(10); B y = new B(10); } } */ /* import java.util.*; class Point { private int x,y; public Point() { this.x = this.y = 0; } public Point(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 ColorPoint(int x, int y, String color) { super (x, y); this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main { public static void main(String[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showColorPoint (); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } public void printProperty() { System.out.print(getSize()+"인치 "+Color+"컬러"); } } class Main { public static void main(String [] args) { ColorTV myTV = new ColorTV(32,1024); myTV.printProperty(); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } protected int getColor() { return Color; } } class IPTV extends ColorTV { private String IP; public IPTV(String IP,int size,int Color) { super(size,Color); this.IP = IP; } public void printProperty() { System.out.print("나의 IPTV는 "+IP+" 주소의 "+getSize()+"인치 "+getColor()+"컬러"); } } class Main { public static void main(String[]args) { IPTV iptv = new IPTV("192.1.1.2", 32, 2048); iptv.printProperty(); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint(int x,int y,String Color) { super(x,y); this.Color = Color; } public void setXY(int x,int y) { move (x,y); } public void setColor(String Color) { this.Color = Color; } public String toString () { return Color+"색의 "+"("+ getX() +","+ getY() +")"+"의 점"; } } class Main { public static void main(String[]args) { ColorPoint cp = new ColorPoint(5, 5, "YELLOW"); cp.setXY(10, 20); cp.setColor("RED"); String str = cp.toString(); System.out.println(str + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint() { super(0,0); this.Color = "BLACK"; } public ColorPoint(int x,int y) { super(x,y); this.Color = "BLACK"; } public void setXY(int x,int y) { move(x,y); } public void setColor(String color) { this.Color = color; } public String toString () { return Color + "색의 " + "("+ getX() + ","+ getY() + ")" + "의 점"; } } class Main { public static void main(String[] args) { ColorPoint zeroPoint = new ColorPoint(); System.out.println(zeroPoint.toString() + "입니다."); ColorPoint cp = new ColorPoint(10,10); cp.setXY(5, 5); cp.setColor("RED"); System.out.println(cp.toString() + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class Point3D extends Point { private int z; public Point3D(int x,int y,int z) { super(x,y); this.z = z; } public void moveUp() { z = z+1; } public void moveDown() { z--; } public void move(int x, int y,int z) { super.move(x, y); this.z = z; } public String toString() { return "("+ getX() + "," + getY() + "," + z +")" + "의 점"; } } class Main { public static void main(String[] args) { Point3D p = new Point3D(1,2,3); System.out.println(p.toString() + "입니다."); p.moveUp(); System.out.println(p.toString() + "입니다."); p.moveDown(); p.move(10, 10); System.out.println(p.toString() + "입니다."); p.move(100, 200, 300); System.out.println(p.toString() + "입니다."); } } 클래스 1. 필드 (변수 ,속성) 2. 메소드 (기능, 행동) 3. 생성자(객체 생성시 필드값 초기화) 추상클래스 : 일부만 작성 == 일부는 작성 x */ /* abstract class Person{ int age; String name; void introduce() { //일반 메소드 System.out.println("I'm "+age+"years old. My name is "+name+"."); } abstract void speak(); //추상메소드 ( 이 클래스를 상속받은 모든 클래스는 이 메소드를 구현) } class Student extends Person{ void speak() { System.out.println("I'm Student."); } } class Teacher extends Person{ void speak() { System.out.println("I'm Teacher"); } } class Main{ public static void main(String[] args) { Student s = new Student(); } } */ /* abstract class Calculator { public abstract int add(int a,int b); public abstract int subtract(int a,int b); public abstract double average (int[] a); } class GoodCalc extends Calculator { public int add(int a,int b) { return a + b; } public int subtract(int a,int b) { return a-b; } public double average(int[] a) { double sum = 0; for(int i=0; i<a.length; i++) sum += a[i]; return sum/a.length; } } class Main { public static void main(String[] args) { GoodCalc c = new GoodCalc(); System.out.println(c.add(2,3)); System.out.println(c.subtract(2,3)); System.out.println(c.average(new int [] { 2, 3, 4 })); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Won2Dollar extends Converter { public Won2Dollar(int i) { } @Override protected double convert(double src) { return src/1200; } @Override protected String getSrcString() { return "원"; } @Override protected String getDestString() { return "달러"; } } class Main{ public static void main(String[] args) { Won2Dollar toDollar = new Won2Dollar(1200); toDollar.run(); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Km2Mile extends Converter { public Km2Mile(double i) { ratio=i; } protected double convert(double src) { return src/ratio; } protected String getSrcString() { return "Km"; } protected String getDestString() { return "mile"; } } class Main { public static void main(String[] args) { Km2Mile toMile = new Km2Mile(1.6); toMile.run(); } } */ /* import java.util.*; abstract class PairMap { protected String keyArray []; protected String ValueArray []; abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key); abstract int length(); } class Dictionary extends PairMap { public Dictionary(int i) { } String get(String key) { return ""; } void put(String key, String value) { } String delete(String key) { return null; } int length() { return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(10); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* import java.util.*; interface PairMap { abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key, String value); abstract int length(); } class Dictionary implements PairMap { @Override public String get(String key) { // TODO Auto-generated method stub return null; } @Override public void put(String key, String value) { // TODO Auto-generated method stub } @Override public String delete(String key, String value) { // TODO Auto-generated method stub return null; } @Override public int length() { // TODO Auto-generated method stub return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* interface Shape { final double PI = 3.14; void draw(); double getArea(); default public void redraw() { System.out.print("--- 다시 그립니다. "); draw(); } } class Circle implements Shape{ int r; public Circle(int r) { this.r = r; } public void draw() { System.out.println("반지름이 " + r + "인 원입니다."); } public double getArea() { return r*r*PI; } } class Main { public static void main(String [] args) { Shape donut = new Circle(10); donut.redraw(); System.out.println("면적은 " + donut.getArea()); } } interface MouseDriver { final int BUTTONS = 3; //int VERSION; void move(); public int click(); int out(); void drag(); default void drop() { System.out.println("drop"); } } GUI : 그림으로 보여지는것 컨테이너 container : 종이 판 (다른 것들을 포함한다) 컴포넌트 component : 스티커 (컨테이너 위에 올라간다) import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("example1"); // 창 제목 정하기 (필수아님) setSize(500, 300); //가로500, 세로 300으로 크기 셋팅 (필수) Container c = getContentPane(); // c = 현재 배경 c.setLayout(new FlowLayout()); //레이아웃 설정 (나중에) JButton b = new JButton("버튼"); //버튼 생성 c.add(b); //버튼 추가 JLabel la = new JLabel("레이블컴포넌트입니다"); c.add(la); setVisible(true); // 프레임이 보이게 설정 (필수) } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("ContentPane과 JFrame"); setSize(500,300); Container c = getContentPane(); //c.setLayout(new FlowLayout()); c.setLayout(new BorderLayout(5,10)); c.setBackground(Color.black); //배경색 설정 JButton b = new JButton("OK"); b.setBackground(Color.white); b.setForeground(Color.black); c.add(b,BorderLayout.NORTH); // JButton b1 = new JButton("Cancel"); // b1.setBackground(Color.white); // b1.setForeground(Color.black); //전경색 (글자색) 설정 // c.add(b1, BorderLayout.CENTER); JButton b2 = new JButton("Ignore"); b2.setForeground(Color.black); c.add(b2, BorderLayout.CENTER); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.setBackground(Color.black); c.add(p,BorderLayout.EAST); JButton b3 = new JButton("Cancel"); p.add(new JButton("Cancel")); p.add(b3); b3.setForeground(Color.blue); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("Let's study Java"); setSize(400,200); Container c = getContentPane(); c.setLayout(new BorderLayout(5,7)); JButton b = new JButton("Center"); c.add(b, BorderLayout.CENTER); JButton b1 = new JButton("East"); c.add(b1, BorderLayout.EAST); JButton b2 = new JButton("West"); c.add(b2, BorderLayout.WEST); JButton b3 = new JButton("North"); c.add(b3, BorderLayout.NORTH); JButton b4 = new JButton("South"); c.add(b4, BorderLayout.SOUTH); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("GridLayour Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 2); grid.setVgap(5); Container c = getContentPane(); c.setLayout(grid); c.add(new JLabel(" 이름")); c.add(new JTextField("")); c.add(new JLabel(" 학번")); c.add(new JTextField("")); c.add(new JLabel(" 학과")); c.add(new JTextField("")); c.add(new JLabel(" 과목")); c.add(new JTextField("")); setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Null Container Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); JLabel la = new JLabel("Hello, Press Buttons!"); la.setLocation(130, 50); la.setSize(200, 20); c.add(la); for(int i=1; i<=9; i++) { JButton b = new JButton(Integer.toString(i)); //*********************** //random // //************************ int x = (int) Math.random()*200; int y = (int) Math.random()*300; b.setLocation(x, y); b.setSize(50,20); c.add(b); } setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Ten Color Buttons Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(1, 10); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red, Color.orange, Color.yellow, Color.green, Color.cyan, Color.blue, Color.magenta, Color.darkGray, Color.pink, Color.gray }; for(int i=0; i<10; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("4x4 Color Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 4); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue,Color.magenta,Color.darkGray,Color.pink,Color.gray,Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue}; for(int i=0; i<16; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Random Labels"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); for(int i=0; i<20; i++) { JLabel la = new JLabel(Integer.toString(i)); int x = (int)(Math.random()*200) + 50; int y = (int)(Math.random()*200) + 50; la.setLocation(x,y); la.setSize(15, 15); la.setBackground(Color.blue); la.setForeground(Color.white); la.setOpaque(true); c.add(la); } } public static void main(String[] args) { new Main(); } } event : 모든 변화 ex) 마우스 클릭, 버튼 클릭, 버튼에 마우스 올려놓기, 키보드 a 누르기 ..... action event key event mouse evet change event ... event listener : 이벤트를 어떻게 처리할지 적어놓는 곳 import javax.swing.*; import java.awt.*; import java.awt.event.*; // event를 처리하는 부분 public class Main extends JFrame { Container c = getContentPane(); JPanel nor = new JPanel(); JPanel cen = new JPanel(); JButton b = new JButton("set black"); JButton b1 = new JButton("set red"); JButton b2 = new JButton("set pink"); public Main() { setTitle("Open Challenge 9"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new BorderLayout(3,3)); nor.setLayout(new FlowLayout()); nor.setBackground(Color.LIGHT_GRAY); cen.setLayout(null); //액션리스너 붙여주기 b.addActionListener(new MyActionListener()); b1.addActionListener(new MyActionListener()); b2.addActionListener(new MyActionListener()); nor.add(b); nor.add(b1); nor.add(b2); JLabel l = new JLabel("Hello"); l.setLocation(50,150); l.setSize(30, 30); cen.add(l); JLabel l1 = new JLabel("Java"); l1.setLocation(250,80); l1.setSize(30, 30); cen.add(l1); JLabel l2 = new JLabel("Love"); l2.setLocation(70,50); l2.setSize(30, 30); cen.add(l2); c.add(cen, BorderLayout.CENTER); c.add(nor, BorderLayout.NORTH); setSize(300,300); setVisible(true); } //액션리스너 클래스 작성하기 class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { JButton bb = (JButton)e.getSource(); // 이벤트가 일어난 버튼을 bb if(bb==b) cen.setBackground(Color.black); else if(bb==b1) cen.setBackground(Color.red); else cen.setBackground(Color.pink); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { public Main() { setTitle("Action 이벤트 리스너 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JButton btn = new JButton("Action"); btn.addActionListener(new MyActionListener()); c.add(btn); setSize(350,150); setVisible(true); } public static void main(String[] args) { new Main(); } } class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { JButton b = (JButton)e.getSource(); if(b.getText().equals("Action")) b.setText("액션"); else b.setText("Action"); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { private JLabel la = new JLabel("Hello"); public Main() { setTitle("Mouse 이벤트 예제"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.addMouseListener(new MyMouseListener()); c.setLayout(null); la.setSize(50, 20); la.setLocation(30, 30); c.add(la); setSize(250, 250); setVisible(true); } //implements MouseListener class MyMouseListener extends MouseAdapter { public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } // public void mousePressed(MouseEvent e) { // int x = e.getX(); // int y = e.getY(); // la.setLocation(x, y); // } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { private JLabel la = new JLabel("Love Java"); public Main() { setTitle("마우스 올리기 내리기"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); la.addMouseListener(new MyMouseListener()); c.setLayout(null); la.setSize(100, 20); la.setLocation(140, 30); c.add(la); setSize(350, 250); setVisible(true); }//implements MouseListener class MyMouseListener extends MouseAdapter { public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub la.setText("Love Java"); } public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub la.setText("사랑해"); } } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame { Container c = getContentPane(); public Main() { setTitle("드래깅동안 YELLOW"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.addMouseMotionListener(new MyMouseMotionListener()); setSize(350, 250); setVisible(true); } class MyMouseMotionListener extends MouseAdapter{ public void mouseDragged(MouseEvent e) { c.setBackground(Color.YELLOW); } public void mouseMoved(MouseEvent e) { c.setBackground(Color.GREEN); } } public static void main(String[] args) { new Main(); } } */
0
0
1
minhyuk0426
2023년 1월 08일
In 소스 코드 제출
/* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i,j; int[][] arr = new int[n][n]; for(i=0; i<n; i++) { arr[i][0] = sc.nextInt(); } for(i=1; i<n; i++) { for(j=1; j<=i; j++) { arr[i][j] = arr[i][j-1] - arr[i-1][j-1]; } } for(i=0; i<n; i++) { for(j=0; j<=i; j++) { System.out.print(arr[i][j]+" "); } System.out.println(" "); } } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,sum=0; for(i=a; i<=b; i++) { if(i%2==0) { sum -= i; } else { sum += i; } } System.out.println(sum); } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,j,k,sum=0,dec=0; for(k=a; k<=b; k++) { for(i=1; i<=9; i++) { System.out.println(k+"*"+i+"="+(k*i)); } } } } 20220918 클래스 : 틀 ( 필드 + 생성자 + 메소드) 객체 : 틀로 찍어낸 물체 import java.util.*; class Person{ //field 필드 int age; String name; //method 메소드 (함수) void view() { System.out.println("나이는 "+ age+"이고, 이름은 "+ name+"입니다."); } //constructor 생성자 - 객체를 생성할때 필드 값의 초기값을 정해주는 용도 Person(){ age=5000; name="noname"; } Person(int a){ age=a; name="noname"; } Person(int a, String n){ age=a; name=n; } } public class Main { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); Person s; s = new Person(); Person p = new Person(100,"tom"); Person t = new Person(50); t.view(); p.view(); s.view(); p.age=10; p.name="hello"; s.age=900; s.name="hi"; p.view(); s.view(); //System.out.println("나이는"+ p.age+"이고, 이름은 "+ p.name+"입니다."); //System.out.println("나이는"+ s.age+"이고, 이름은 "+ s.name+"입니다."); } } */ /* class Circle { int radius; String name; public double getArea() { return 3.14*radius*radius; } Circle(int r, String n){ radius=r; name=n; } } class Main{ public static void main(String[] args) { // Circle pizza; // pizza = new Circle(); // pizza.radius = 10; // pizza.name = "자바피자"; // double area = pizza.getArea(); // System.out.println(pizza.name+"의 면적은"+area); Circle donut = new Circle(2,"자바도넛"); double area = donut.getArea(); System.out.println(donut.name+"의 면적은"+area); } } */ /* import java.util.*; class Circle { int radius; String name; public Circle() { radius = 1; name = ""; } public Circle(int r, String n) { radius = r; name = n; } public double getArea() { return 3.14*radius*radius; } } class Main{ public static void main(String[]args) { Circle pizza = new Circle(10, "자바피자"); double area = pizza.getArea(); System.out.println(pizza.name + "의 면적은 " + area); Circle donut = new Circle(); donut.name = "도넛피자"; area = donut.getArea(); System.out.println(donut.name + "의 면적은 " + area); } } private 다른 클래스에서 접근 할 수 없는 필드, 메소드가 된다 */ /* class Song { private String title; public Song(String title) { this.title = title; } public String getTitle() { return title; } } class Main { public static void main(String[] args) { Song mySong = new Song("Nessun Dorma"); Song yourSong = new Song("공주는 잠 못 이루고"); System.out.println("내 노래는 " + mySong.getTitle()); System.out.println("너 노래는 " + yourSong.getTitle()); } } */ /* 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; } } class Main { public static void main(String[] args) { Phone } } */ /* import java.util.*; class TV { private String name; int year; int size; public TV(String name,int year,int size) { this.name = name; this.year = year; this.size = size; } public void show() { System.out.println(name+"에서 만든 "+year+"년형 "+size+"인치 TV"); } } class Main{ public static void main(String [] args) { TV myTV = new TV("LG", 2017, 32); myTV.show(); } } */ /* import java.util.*; class Grade { private int math; private int science; private int english; public Grade(int math,int science,int english) { this.math = math; this.science = science; this.english = english; } public int average() { return (math+science+english)/3; } } class Main { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); System.out.println("수학, 과학, 영어 순으로 3개의 정수 입력>>"); Grade me = new Grade(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()); System.out.println("평균은 " + me.average()); } } */ /* import java.util.*; class Song { private String title; private String country; private String artist; private int year; public Song(int year) { this(year,"미정","미정","미정"); } public Song(int year,String title,String country,String artist) { this.title = title; this.country = country; this.artist = artist; this.year = year; } public void show() { System.out.println(year+"년 "+country+"국적의 "+artist+"가 부른 "+title); } } class Main { public static void main(String [] args) { Song mySong = new Song(1978,"Dancing Queen","스웨덴","ABBA"); Song yourSong = new Song(1945); mySong.show(); } } */ /* import java.util.*; 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; } } class Main { public static void main(String [] args) { Book littlePrince = new Book("어린왕자","생텍쥐페리"); Book loveStory = new Book("춘향전"); Book emptyBook = new Book(); loveStory.show(); } } */ /* class Samp { int id; public Samp(int x) { this.id = x; } public Samp() { this(0); System.out.println("생성자 호출"); } } class ConstructorExample{ //필드 int x; //메소드 public void setx(int x) { this.x = x; } public int getx() { return x; } //생성자 public ConstructorExample() { } public static void main(String [] args) { ConstructorExample a = new ConstructorExample(); int n = a.getx(); } } 객체 배열을 생성할때는 1. 레퍼런스 배열의 레퍼런스 생성 2. 레퍼런스 배열 생성 3. 객체 생성해서 레퍼런스에 연결 class Person{ String name; int age; } class Main{ public static void main(String[] args) { // Person p ;//레퍼런스변수 생성 // p = new Person(); //객체 생성 Person[] arr = new Person[10]; for(int i=0;i<arr.length;i++) { arr[i] = new Person(); } arr[0].name="민혁"; } } * */ /* import java.util.*; class Circle { int radius; public Circle(int radius) { this.radius = radius; } public double getArea() { return 3.14*radius*radius; } } class Main { public static void main(String[] args) { Circle [] c; c = new Circle[5]; for(int i=0; i<c.length; i++) c[i] = new Circle(i); for(int i=0; i<c.length; i++) System.out.println((int)(c[i].getArea()) + " "); } } */ /* import java.util.Scanner; class Book { String title,author; public Book(String title,String author) { this.title = title; this.author = author; } } class Main{ public static void main(String[] args) { Book [] book = new Book[2]; Scanner scanner = new Scanner(System.in); for(int i=0; i<book.length; i++) { System.out.print("제목>>"); String title = scanner.nextLine(); System.out.print("저자>>"); String author = scanner.nextLine(); book[i] = new Book(title, author); } for(int i=0; i<book.length; i++) System.out.print("(" + book[i].title + ", " + book[i].author + ")"); } } */ /* import java.util.*; class Circle { private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Circle c[] = new Circle[3]; for (int i = 0; i < c.length; i++) { System.out.print("x, y, radius >>"); double x = scanner.nextDouble(); double y = scanner.nextDouble(); int radius = scanner.nextInt(); c[i] = new Circle(x, y, radius); } for(int i=0; i<c.length; i++) c[i].show(); scanner.close(); } } import java.util.Scanner; class Dictionary { private static String [] kor = { "사랑","아기","돈","미래","희망" }; private static String [] eng = { "love","baby","monry","future", "hope"}; public static String kor2Eng(String word) { for(int i=0;i<kor.length;i++) { if(kor[i].equals(word)) return eng[i]; } return " 저의 사전에 없습니다."; } } class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("한영 단어 검색 프로그램입니다."); while(true) { System.out.print("한글 단어?"); String name = sc.next(); if(name.equals("그만")) { return ; } else { System.out.println(name+"는 " +Dictionary.kor2Eng(name)); } } } } 부모클래스 - 자식클래스 슈퍼클래스 - 서브클래스 class Person{ int age; String name; } class Student extends Person{ String school; char grade; void speak() { System.out.println("안녕하세요, 저는 학생입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class Teacher extends Person{ String school; String major; void speak() { System.out.println("안녕하세요, 저는 선생님입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class ComTeacher extends Teacher{ } class Main{ public static void main(String[] args) { Student s = new Student(); Teacher t = new Teacher(); s.speak(); } } */ /* import java.util.*; 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; void setColor(String color) { this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main{ public static void main(String [] args) { Point p = new Point(); p.set(1,2); p.showPoint(); ColorPoint cp = new ColorPoint (); cp.set(3,4); cp.setColor("red"); cp.showColorPoint(); } } import java.util.*; class Pen { protected int amount; public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } class SharpPencil extends Pen { private int width; } class BallPen extends Pen { private String color; public String getColor() { return color; } public void setColor(String color ) { this.color = color; } } class FountainPen extends BallPen { public void refill(int n) { amount = n; } } 상속-생성자 class A{ int n; public A() { System.out.println("A의 기본 생성자"); } public A(int n) {this.n = n;} } class B extends A{ int m; public B() { System.out.println("B의 기본 생성자"); } public B(int m) { super(20); this.m = m; System.out.println("B의 1번 생성자"); } } class Main{ public static void main(String[] args) { //A x = new A(10); B y = new B(10); } } */ /* import java.util.*; class Point { private int x,y; public Point() { this.x = this.y = 0; } public Point(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 ColorPoint(int x, int y, String color) { super (x, y); this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main { public static void main(String[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showColorPoint (); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } public void printProperty() { System.out.print(getSize()+"인치 "+Color+"컬러"); } } class Main { public static void main(String [] args) { ColorTV myTV = new ColorTV(32,1024); myTV.printProperty(); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } protected int getColor() { return Color; } } class IPTV extends ColorTV { private String IP; public IPTV(String IP,int size,int Color) { super(size,Color); this.IP = IP; } public void printProperty() { System.out.print("나의 IPTV는 "+IP+" 주소의 "+getSize()+"인치 "+getColor()+"컬러"); } } class Main { public static void main(String[]args) { IPTV iptv = new IPTV("192.1.1.2", 32, 2048); iptv.printProperty(); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint(int x,int y,String Color) { super(x,y); this.Color = Color; } public void setXY(int x,int y) { move (x,y); } public void setColor(String Color) { this.Color = Color; } public String toString () { return Color+"색의 "+"("+ getX() +","+ getY() +")"+"의 점"; } } class Main { public static void main(String[]args) { ColorPoint cp = new ColorPoint(5, 5, "YELLOW"); cp.setXY(10, 20); cp.setColor("RED"); String str = cp.toString(); System.out.println(str + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint() { super(0,0); this.Color = "BLACK"; } public ColorPoint(int x,int y) { super(x,y); this.Color = "BLACK"; } public void setXY(int x,int y) { move(x,y); } public void setColor(String color) { this.Color = color; } public String toString () { return Color + "색의 " + "("+ getX() + ","+ getY() + ")" + "의 점"; } } class Main { public static void main(String[] args) { ColorPoint zeroPoint = new ColorPoint(); System.out.println(zeroPoint.toString() + "입니다."); ColorPoint cp = new ColorPoint(10,10); cp.setXY(5, 5); cp.setColor("RED"); System.out.println(cp.toString() + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class Point3D extends Point { private int z; public Point3D(int x,int y,int z) { super(x,y); this.z = z; } public void moveUp() { z = z+1; } public void moveDown() { z--; } public void move(int x, int y,int z) { super.move(x, y); this.z = z; } public String toString() { return "("+ getX() + "," + getY() + "," + z +")" + "의 점"; } } class Main { public static void main(String[] args) { Point3D p = new Point3D(1,2,3); System.out.println(p.toString() + "입니다."); p.moveUp(); System.out.println(p.toString() + "입니다."); p.moveDown(); p.move(10, 10); System.out.println(p.toString() + "입니다."); p.move(100, 200, 300); System.out.println(p.toString() + "입니다."); } } 클래스 1. 필드 (변수 ,속성) 2. 메소드 (기능, 행동) 3. 생성자(객체 생성시 필드값 초기화) 추상클래스 : 일부만 작성 == 일부는 작성 x */ /* abstract class Person{ int age; String name; void introduce() { //일반 메소드 System.out.println("I'm "+age+"years old. My name is "+name+"."); } abstract void speak(); //추상메소드 ( 이 클래스를 상속받은 모든 클래스는 이 메소드를 구현) } class Student extends Person{ void speak() { System.out.println("I'm Student."); } } class Teacher extends Person{ void speak() { System.out.println("I'm Teacher"); } } class Main{ public static void main(String[] args) { Student s = new Student(); } } */ /* abstract class Calculator { public abstract int add(int a,int b); public abstract int subtract(int a,int b); public abstract double average (int[] a); } class GoodCalc extends Calculator { public int add(int a,int b) { return a + b; } public int subtract(int a,int b) { return a-b; } public double average(int[] a) { double sum = 0; for(int i=0; i<a.length; i++) sum += a[i]; return sum/a.length; } } class Main { public static void main(String[] args) { GoodCalc c = new GoodCalc(); System.out.println(c.add(2,3)); System.out.println(c.subtract(2,3)); System.out.println(c.average(new int [] { 2, 3, 4 })); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Won2Dollar extends Converter { public Won2Dollar(int i) { } @Override protected double convert(double src) { return src/1200; } @Override protected String getSrcString() { return "원"; } @Override protected String getDestString() { return "달러"; } } class Main{ public static void main(String[] args) { Won2Dollar toDollar = new Won2Dollar(1200); toDollar.run(); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Km2Mile extends Converter { public Km2Mile(double i) { ratio=i; } protected double convert(double src) { return src/ratio; } protected String getSrcString() { return "Km"; } protected String getDestString() { return "mile"; } } class Main { public static void main(String[] args) { Km2Mile toMile = new Km2Mile(1.6); toMile.run(); } } */ /* import java.util.*; abstract class PairMap { protected String keyArray []; protected String ValueArray []; abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key); abstract int length(); } class Dictionary extends PairMap { public Dictionary(int i) { } String get(String key) { return ""; } void put(String key, String value) { } String delete(String key) { return null; } int length() { return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(10); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* import java.util.*; interface PairMap { abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key, String value); abstract int length(); } class Dictionary implements PairMap { @Override public String get(String key) { // TODO Auto-generated method stub return null; } @Override public void put(String key, String value) { // TODO Auto-generated method stub } @Override public String delete(String key, String value) { // TODO Auto-generated method stub return null; } @Override public int length() { // TODO Auto-generated method stub return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* interface Shape { final double PI = 3.14; void draw(); double getArea(); default public void redraw() { System.out.print("--- 다시 그립니다. "); draw(); } } class Circle implements Shape{ int r; public Circle(int r) { this.r = r; } public void draw() { System.out.println("반지름이 " + r + "인 원입니다."); } public double getArea() { return r*r*PI; } } class Main { public static void main(String [] args) { Shape donut = new Circle(10); donut.redraw(); System.out.println("면적은 " + donut.getArea()); } } interface MouseDriver { final int BUTTONS = 3; //int VERSION; void move(); public int click(); int out(); void drag(); default void drop() { System.out.println("drop"); } } GUI : 그림으로 보여지는것 컨테이너 container : 종이 판 (다른 것들을 포함한다) 컴포넌트 component : 스티커 (컨테이너 위에 올라간다) import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("example1"); // 창 제목 정하기 (필수아님) setSize(500, 300); //가로500, 세로 300으로 크기 셋팅 (필수) Container c = getContentPane(); // c = 현재 배경 c.setLayout(new FlowLayout()); //레이아웃 설정 (나중에) JButton b = new JButton("버튼"); //버튼 생성 c.add(b); //버튼 추가 JLabel la = new JLabel("레이블컴포넌트입니다"); c.add(la); setVisible(true); // 프레임이 보이게 설정 (필수) } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("ContentPane과 JFrame"); setSize(500,300); Container c = getContentPane(); //c.setLayout(new FlowLayout()); c.setLayout(new BorderLayout(5,10)); c.setBackground(Color.black); //배경색 설정 JButton b = new JButton("OK"); b.setBackground(Color.white); b.setForeground(Color.black); c.add(b,BorderLayout.NORTH); // JButton b1 = new JButton("Cancel"); // b1.setBackground(Color.white); // b1.setForeground(Color.black); //전경색 (글자색) 설정 // c.add(b1, BorderLayout.CENTER); JButton b2 = new JButton("Ignore"); b2.setForeground(Color.black); c.add(b2, BorderLayout.CENTER); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.setBackground(Color.black); c.add(p,BorderLayout.EAST); JButton b3 = new JButton("Cancel"); p.add(new JButton("Cancel")); p.add(b3); b3.setForeground(Color.blue); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("Let's study Java"); setSize(400,200); Container c = getContentPane(); c.setLayout(new BorderLayout(5,7)); JButton b = new JButton("Center"); c.add(b, BorderLayout.CENTER); JButton b1 = new JButton("East"); c.add(b1, BorderLayout.EAST); JButton b2 = new JButton("West"); c.add(b2, BorderLayout.WEST); JButton b3 = new JButton("North"); c.add(b3, BorderLayout.NORTH); JButton b4 = new JButton("South"); c.add(b4, BorderLayout.SOUTH); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("GridLayour Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 2); grid.setVgap(5); Container c = getContentPane(); c.setLayout(grid); c.add(new JLabel(" 이름")); c.add(new JTextField("")); c.add(new JLabel(" 학번")); c.add(new JTextField("")); c.add(new JLabel(" 학과")); c.add(new JTextField("")); c.add(new JLabel(" 과목")); c.add(new JTextField("")); setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Null Container Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); JLabel la = new JLabel("Hello, Press Buttons!"); la.setLocation(130, 50); la.setSize(200, 20); c.add(la); for(int i=1; i<=9; i++) { JButton b = new JButton(Integer.toString(i)); //*********************** //random // //************************ int x = (int) Math.random()*200; int y = (int) Math.random()*300; b.setLocation(x, y); b.setSize(50,20); c.add(b); } setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Ten Color Buttons Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(1, 10); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red, Color.orange, Color.yellow, Color.green, Color.cyan, Color.blue, Color.magenta, Color.darkGray, Color.pink, Color.gray }; for(int i=0; i<10; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("4x4 Color Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 4); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue,Color.magenta,Color.darkGray,Color.pink,Color.gray,Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue}; for(int i=0; i<16; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Random Labels"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); for(int i=0; i<20; i++) { JLabel la = new JLabel(Integer.toString(i)); int x = (int)(Math.random()*200) + 50; int y = (int)(Math.random()*200) + 50; la.setLocation(x,y); la.setSize(15, 15); la.setBackground(Color.blue); la.setForeground(Color.white); la.setOpaque(true); c.add(la); } } public static void main(String[] args) { new Main(); } } event : 모든 변화 ex) 마우스 클릭, 버튼 클릭, 버튼에 마우스 올려놓기, 키보드 a 누르기 ..... action event key event mouse evet change event ... event listener : 이벤트를 어떻게 처리할지 적어놓는 곳 import javax.swing.*; import java.awt.*; import java.awt.event.*; // event를 처리하는 부분 public class Main extends JFrame { Container c = getContentPane(); JPanel nor = new JPanel(); JPanel cen = new JPanel(); JButton b = new JButton("set black"); JButton b1 = new JButton("set red"); JButton b2 = new JButton("set pink"); public Main() { setTitle("Open Challenge 9"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c.setLayout(new BorderLayout(3,3)); nor.setLayout(new FlowLayout()); nor.setBackground(Color.LIGHT_GRAY); cen.setLayout(null); //액션리스너 붙여주기 b.addActionListener(new MyActionListener()); b1.addActionListener(new MyActionListener()); b2.addActionListener(new MyActionListener()); nor.add(b); nor.add(b1); nor.add(b2); JLabel l = new JLabel("Hello"); l.setLocation(50,150); l.setSize(30, 30); cen.add(l); JLabel l1 = new JLabel("Java"); l1.setLocation(250,80); l1.setSize(30, 30); cen.add(l1); JLabel l2 = new JLabel("Love"); l2.setLocation(70,50); l2.setSize(30, 30); cen.add(l2); c.add(cen, BorderLayout.CENTER); c.add(nor, BorderLayout.NORTH); setSize(300,300); setVisible(true); } //액션리스너 클래스 작성하기 class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { JButton bb = (JButton)e.getSource(); // 이벤트가 일어난 버튼을 bb if(bb==b) cen.setBackground(Color.black); else if(bb==b1) cen.setBackground(Color.red); else cen.setBackground(Color.pink); } } public static void main(String[] args) { new Main(); } } */
0
1
4
minhyuk0426
2022년 12월 18일
In 소스 코드 제출
/* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i,j; int[][] arr = new int[n][n]; for(i=0; i<n; i++) { arr[i][0] = sc.nextInt(); } for(i=1; i<n; i++) { for(j=1; j<=i; j++) { arr[i][j] = arr[i][j-1] - arr[i-1][j-1]; } } for(i=0; i<n; i++) { for(j=0; j<=i; j++) { System.out.print(arr[i][j]+" "); } System.out.println(" "); } } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,sum=0; for(i=a; i<=b; i++) { if(i%2==0) { sum -= i; } else { sum += i; } } System.out.println(sum); } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,j,k,sum=0,dec=0; for(k=a; k<=b; k++) { for(i=1; i<=9; i++) { System.out.println(k+"*"+i+"="+(k*i)); } } } } 20220918 클래스 : 틀 ( 필드 + 생성자 + 메소드) 객체 : 틀로 찍어낸 물체 import java.util.*; class Person{ //field 필드 int age; String name; //method 메소드 (함수) void view() { System.out.println("나이는 "+ age+"이고, 이름은 "+ name+"입니다."); } //constructor 생성자 - 객체를 생성할때 필드 값의 초기값을 정해주는 용도 Person(){ age=5000; name="noname"; } Person(int a){ age=a; name="noname"; } Person(int a, String n){ age=a; name=n; } } public class Main { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); Person s; s = new Person(); Person p = new Person(100,"tom"); Person t = new Person(50); t.view(); p.view(); s.view(); p.age=10; p.name="hello"; s.age=900; s.name="hi"; p.view(); s.view(); //System.out.println("나이는"+ p.age+"이고, 이름은 "+ p.name+"입니다."); //System.out.println("나이는"+ s.age+"이고, 이름은 "+ s.name+"입니다."); } } */ /* class Circle { int radius; String name; public double getArea() { return 3.14*radius*radius; } Circle(int r, String n){ radius=r; name=n; } } class Main{ public static void main(String[] args) { // Circle pizza; // pizza = new Circle(); // pizza.radius = 10; // pizza.name = "자바피자"; // double area = pizza.getArea(); // System.out.println(pizza.name+"의 면적은"+area); Circle donut = new Circle(2,"자바도넛"); double area = donut.getArea(); System.out.println(donut.name+"의 면적은"+area); } } */ /* import java.util.*; class Circle { int radius; String name; public Circle() { radius = 1; name = ""; } public Circle(int r, String n) { radius = r; name = n; } public double getArea() { return 3.14*radius*radius; } } class Main{ public static void main(String[]args) { Circle pizza = new Circle(10, "자바피자"); double area = pizza.getArea(); System.out.println(pizza.name + "의 면적은 " + area); Circle donut = new Circle(); donut.name = "도넛피자"; area = donut.getArea(); System.out.println(donut.name + "의 면적은 " + area); } } private 다른 클래스에서 접근 할 수 없는 필드, 메소드가 된다 */ /* class Song { private String title; public Song(String title) { this.title = title; } public String getTitle() { return title; } } class Main { public static void main(String[] args) { Song mySong = new Song("Nessun Dorma"); Song yourSong = new Song("공주는 잠 못 이루고"); System.out.println("내 노래는 " + mySong.getTitle()); System.out.println("너 노래는 " + yourSong.getTitle()); } } */ /* 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; } } class Main { public static void main(String[] args) { Phone } } */ /* import java.util.*; class TV { private String name; int year; int size; public TV(String name,int year,int size) { this.name = name; this.year = year; this.size = size; } public void show() { System.out.println(name+"에서 만든 "+year+"년형 "+size+"인치 TV"); } } class Main{ public static void main(String [] args) { TV myTV = new TV("LG", 2017, 32); myTV.show(); } } */ /* import java.util.*; class Grade { private int math; private int science; private int english; public Grade(int math,int science,int english) { this.math = math; this.science = science; this.english = english; } public int average() { return (math+science+english)/3; } } class Main { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); System.out.println("수학, 과학, 영어 순으로 3개의 정수 입력>>"); Grade me = new Grade(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()); System.out.println("평균은 " + me.average()); } } */ /* import java.util.*; class Song { private String title; private String country; private String artist; private int year; public Song(int year) { this(year,"미정","미정","미정"); } public Song(int year,String title,String country,String artist) { this.title = title; this.country = country; this.artist = artist; this.year = year; } public void show() { System.out.println(year+"년 "+country+"국적의 "+artist+"가 부른 "+title); } } class Main { public static void main(String [] args) { Song mySong = new Song(1978,"Dancing Queen","스웨덴","ABBA"); Song yourSong = new Song(1945); mySong.show(); } } */ /* import java.util.*; 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; } } class Main { public static void main(String [] args) { Book littlePrince = new Book("어린왕자","생텍쥐페리"); Book loveStory = new Book("춘향전"); Book emptyBook = new Book(); loveStory.show(); } } */ /* class Samp { int id; public Samp(int x) { this.id = x; } public Samp() { this(0); System.out.println("생성자 호출"); } } class ConstructorExample{ //필드 int x; //메소드 public void setx(int x) { this.x = x; } public int getx() { return x; } //생성자 public ConstructorExample() { } public static void main(String [] args) { ConstructorExample a = new ConstructorExample(); int n = a.getx(); } } 객체 배열을 생성할때는 1. 레퍼런스 배열의 레퍼런스 생성 2. 레퍼런스 배열 생성 3. 객체 생성해서 레퍼런스에 연결 class Person{ String name; int age; } class Main{ public static void main(String[] args) { // Person p ;//레퍼런스변수 생성 // p = new Person(); //객체 생성 Person[] arr = new Person[10]; for(int i=0;i<arr.length;i++) { arr[i] = new Person(); } arr[0].name="민혁"; } } * */ /* import java.util.*; class Circle { int radius; public Circle(int radius) { this.radius = radius; } public double getArea() { return 3.14*radius*radius; } } class Main { public static void main(String[] args) { Circle [] c; c = new Circle[5]; for(int i=0; i<c.length; i++) c[i] = new Circle(i); for(int i=0; i<c.length; i++) System.out.println((int)(c[i].getArea()) + " "); } } */ /* import java.util.Scanner; class Book { String title,author; public Book(String title,String author) { this.title = title; this.author = author; } } class Main{ public static void main(String[] args) { Book [] book = new Book[2]; Scanner scanner = new Scanner(System.in); for(int i=0; i<book.length; i++) { System.out.print("제목>>"); String title = scanner.nextLine(); System.out.print("저자>>"); String author = scanner.nextLine(); book[i] = new Book(title, author); } for(int i=0; i<book.length; i++) System.out.print("(" + book[i].title + ", " + book[i].author + ")"); } } */ /* import java.util.*; class Circle { private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Circle c[] = new Circle[3]; for (int i = 0; i < c.length; i++) { System.out.print("x, y, radius >>"); double x = scanner.nextDouble(); double y = scanner.nextDouble(); int radius = scanner.nextInt(); c[i] = new Circle(x, y, radius); } for(int i=0; i<c.length; i++) c[i].show(); scanner.close(); } } import java.util.Scanner; class Dictionary { private static String [] kor = { "사랑","아기","돈","미래","희망" }; private static String [] eng = { "love","baby","monry","future", "hope"}; public static String kor2Eng(String word) { for(int i=0;i<kor.length;i++) { if(kor[i].equals(word)) return eng[i]; } return " 저의 사전에 없습니다."; } } class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("한영 단어 검색 프로그램입니다."); while(true) { System.out.print("한글 단어?"); String name = sc.next(); if(name.equals("그만")) { return ; } else { System.out.println(name+"는 " +Dictionary.kor2Eng(name)); } } } } 부모클래스 - 자식클래스 슈퍼클래스 - 서브클래스 class Person{ int age; String name; } class Student extends Person{ String school; char grade; void speak() { System.out.println("안녕하세요, 저는 학생입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class Teacher extends Person{ String school; String major; void speak() { System.out.println("안녕하세요, 저는 선생님입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class ComTeacher extends Teacher{ } class Main{ public static void main(String[] args) { Student s = new Student(); Teacher t = new Teacher(); s.speak(); } } */ /* import java.util.*; 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; void setColor(String color) { this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main{ public static void main(String [] args) { Point p = new Point(); p.set(1,2); p.showPoint(); ColorPoint cp = new ColorPoint (); cp.set(3,4); cp.setColor("red"); cp.showColorPoint(); } } import java.util.*; class Pen { protected int amount; public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } class SharpPencil extends Pen { private int width; } class BallPen extends Pen { private String color; public String getColor() { return color; } public void setColor(String color ) { this.color = color; } } class FountainPen extends BallPen { public void refill(int n) { amount = n; } } 상속-생성자 class A{ int n; public A() { System.out.println("A의 기본 생성자"); } public A(int n) {this.n = n;} } class B extends A{ int m; public B() { System.out.println("B의 기본 생성자"); } public B(int m) { super(20); this.m = m; System.out.println("B의 1번 생성자"); } } class Main{ public static void main(String[] args) { //A x = new A(10); B y = new B(10); } } */ /* import java.util.*; class Point { private int x,y; public Point() { this.x = this.y = 0; } public Point(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 ColorPoint(int x, int y, String color) { super (x, y); this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main { public static void main(String[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showColorPoint (); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } public void printProperty() { System.out.print(getSize()+"인치 "+Color+"컬러"); } } class Main { public static void main(String [] args) { ColorTV myTV = new ColorTV(32,1024); myTV.printProperty(); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } protected int getColor() { return Color; } } class IPTV extends ColorTV { private String IP; public IPTV(String IP,int size,int Color) { super(size,Color); this.IP = IP; } public void printProperty() { System.out.print("나의 IPTV는 "+IP+" 주소의 "+getSize()+"인치 "+getColor()+"컬러"); } } class Main { public static void main(String[]args) { IPTV iptv = new IPTV("192.1.1.2", 32, 2048); iptv.printProperty(); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint(int x,int y,String Color) { super(x,y); this.Color = Color; } public void setXY(int x,int y) { move (x,y); } public void setColor(String Color) { this.Color = Color; } public String toString () { return Color+"색의 "+"("+ getX() +","+ getY() +")"+"의 점"; } } class Main { public static void main(String[]args) { ColorPoint cp = new ColorPoint(5, 5, "YELLOW"); cp.setXY(10, 20); cp.setColor("RED"); String str = cp.toString(); System.out.println(str + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint() { super(0,0); this.Color = "BLACK"; } public ColorPoint(int x,int y) { super(x,y); this.Color = "BLACK"; } public void setXY(int x,int y) { move(x,y); } public void setColor(String color) { this.Color = color; } public String toString () { return Color + "색의 " + "("+ getX() + ","+ getY() + ")" + "의 점"; } } class Main { public static void main(String[] args) { ColorPoint zeroPoint = new ColorPoint(); System.out.println(zeroPoint.toString() + "입니다."); ColorPoint cp = new ColorPoint(10,10); cp.setXY(5, 5); cp.setColor("RED"); System.out.println(cp.toString() + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class Point3D extends Point { private int z; public Point3D(int x,int y,int z) { super(x,y); this.z = z; } public void moveUp() { z = z+1; } public void moveDown() { z--; } public void move(int x, int y,int z) { super.move(x, y); this.z = z; } public String toString() { return "("+ getX() + "," + getY() + "," + z +")" + "의 점"; } } class Main { public static void main(String[] args) { Point3D p = new Point3D(1,2,3); System.out.println(p.toString() + "입니다."); p.moveUp(); System.out.println(p.toString() + "입니다."); p.moveDown(); p.move(10, 10); System.out.println(p.toString() + "입니다."); p.move(100, 200, 300); System.out.println(p.toString() + "입니다."); } } 클래스 1. 필드 (변수 ,속성) 2. 메소드 (기능, 행동) 3. 생성자(객체 생성시 필드값 초기화) 추상클래스 : 일부만 작성 == 일부는 작성 x */ /* abstract class Person{ int age; String name; void introduce() { //일반 메소드 System.out.println("I'm "+age+"years old. My name is "+name+"."); } abstract void speak(); //추상메소드 ( 이 클래스를 상속받은 모든 클래스는 이 메소드를 구현) } class Student extends Person{ void speak() { System.out.println("I'm Student."); } } class Teacher extends Person{ void speak() { System.out.println("I'm Teacher"); } } class Main{ public static void main(String[] args) { Student s = new Student(); } } */ /* abstract class Calculator { public abstract int add(int a,int b); public abstract int subtract(int a,int b); public abstract double average (int[] a); } class GoodCalc extends Calculator { public int add(int a,int b) { return a + b; } public int subtract(int a,int b) { return a-b; } public double average(int[] a) { double sum = 0; for(int i=0; i<a.length; i++) sum += a[i]; return sum/a.length; } } class Main { public static void main(String[] args) { GoodCalc c = new GoodCalc(); System.out.println(c.add(2,3)); System.out.println(c.subtract(2,3)); System.out.println(c.average(new int [] { 2, 3, 4 })); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Won2Dollar extends Converter { public Won2Dollar(int i) { } @Override protected double convert(double src) { return src/1200; } @Override protected String getSrcString() { return "원"; } @Override protected String getDestString() { return "달러"; } } class Main{ public static void main(String[] args) { Won2Dollar toDollar = new Won2Dollar(1200); toDollar.run(); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Km2Mile extends Converter { public Km2Mile(double i) { ratio=i; } protected double convert(double src) { return src/ratio; } protected String getSrcString() { return "Km"; } protected String getDestString() { return "mile"; } } class Main { public static void main(String[] args) { Km2Mile toMile = new Km2Mile(1.6); toMile.run(); } } */ /* import java.util.*; abstract class PairMap { protected String keyArray []; protected String ValueArray []; abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key); abstract int length(); } class Dictionary extends PairMap { public Dictionary(int i) { } String get(String key) { return ""; } void put(String key, String value) { } String delete(String key) { return null; } int length() { return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(10); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* import java.util.*; interface PairMap { abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key, String value); abstract int length(); } class Dictionary implements PairMap { @Override public String get(String key) { // TODO Auto-generated method stub return null; } @Override public void put(String key, String value) { // TODO Auto-generated method stub } @Override public String delete(String key, String value) { // TODO Auto-generated method stub return null; } @Override public int length() { // TODO Auto-generated method stub return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* interface Shape { final double PI = 3.14; void draw(); double getArea(); default public void redraw() { System.out.print("--- 다시 그립니다. "); draw(); } } class Circle implements Shape{ int r; public Circle(int r) { this.r = r; } public void draw() { System.out.println("반지름이 " + r + "인 원입니다."); } public double getArea() { return r*r*PI; } } class Main { public static void main(String [] args) { Shape donut = new Circle(10); donut.redraw(); System.out.println("면적은 " + donut.getArea()); } } interface MouseDriver { final int BUTTONS = 3; //int VERSION; void move(); public int click(); int out(); void drag(); default void drop() { System.out.println("drop"); } } GUI : 그림으로 보여지는것 컨테이너 container : 종이 판 (다른 것들을 포함한다) 컴포넌트 component : 스티커 (컨테이너 위에 올라간다) import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("example1"); // 창 제목 정하기 (필수아님) setSize(500, 300); //가로500, 세로 300으로 크기 셋팅 (필수) Container c = getContentPane(); // c = 현재 배경 c.setLayout(new FlowLayout()); //레이아웃 설정 (나중에) JButton b = new JButton("버튼"); //버튼 생성 c.add(b); //버튼 추가 JLabel la = new JLabel("레이블컴포넌트입니다"); c.add(la); setVisible(true); // 프레임이 보이게 설정 (필수) } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("ContentPane과 JFrame"); setSize(500,300); Container c = getContentPane(); //c.setLayout(new FlowLayout()); c.setLayout(new BorderLayout(5,10)); c.setBackground(Color.black); //배경색 설정 JButton b = new JButton("OK"); b.setBackground(Color.white); b.setForeground(Color.black); c.add(b,BorderLayout.NORTH); // JButton b1 = new JButton("Cancel"); // b1.setBackground(Color.white); // b1.setForeground(Color.black); //전경색 (글자색) 설정 // c.add(b1, BorderLayout.CENTER); JButton b2 = new JButton("Ignore"); b2.setForeground(Color.black); c.add(b2, BorderLayout.CENTER); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.setBackground(Color.black); c.add(p,BorderLayout.EAST); JButton b3 = new JButton("Cancel"); p.add(new JButton("Cancel")); p.add(b3); b3.setForeground(Color.blue); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("Let's study Java"); setSize(400,200); Container c = getContentPane(); c.setLayout(new BorderLayout(5,7)); JButton b = new JButton("Center"); c.add(b, BorderLayout.CENTER); JButton b1 = new JButton("East"); c.add(b1, BorderLayout.EAST); JButton b2 = new JButton("West"); c.add(b2, BorderLayout.WEST); JButton b3 = new JButton("North"); c.add(b3, BorderLayout.NORTH); JButton b4 = new JButton("South"); c.add(b4, BorderLayout.SOUTH); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("GridLayour Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 2); grid.setVgap(5); Container c = getContentPane(); c.setLayout(grid); c.add(new JLabel(" 이름")); c.add(new JTextField("")); c.add(new JLabel(" 학번")); c.add(new JTextField("")); c.add(new JLabel(" 학과")); c.add(new JTextField("")); c.add(new JLabel(" 과목")); c.add(new JTextField("")); setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Null Container Sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); JLabel la = new JLabel("Hello, Press Buttons!"); la.setLocation(130, 50); la.setSize(200, 20); c.add(la); for(int i=1; i<=9; i++) { JButton b = new JButton(Integer.toString(i)); //*********************** //random // //************************ int x = (int) Math.random()*200; int y = (int) Math.random()*300; b.setLocation(x, y); b.setSize(50,20); c.add(b); } setSize(300, 200); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Ten Color Buttons Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(1, 10); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red, Color.orange, Color.yellow, Color.green, Color.cyan, Color.blue, Color.magenta, Color.darkGray, Color.pink, Color.gray }; for(int i=0; i<10; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("4x4 Color Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout grid = new GridLayout(4, 4); Container c = getContentPane(); c.setLayout(grid); Color[] clr = {Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue,Color.magenta,Color.darkGray,Color.pink,Color.gray,Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue}; for(int i=0; i<16; i++) { JButton b = new JButton(Integer.toString(i)); b.setBackground(clr[i]); b.setSize(50,20); c.add(b); } setSize(600, 250); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import javax.swing.*; import java.awt.*; public class Main extends JFrame { public Main() { setTitle("Random Labels"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } */
0
0
2
minhyuk0426
2022년 12월 11일
In 소스 코드 제출
/* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i,j; int[][] arr = new int[n][n]; for(i=0; i<n; i++) { arr[i][0] = sc.nextInt(); } for(i=1; i<n; i++) { for(j=1; j<=i; j++) { arr[i][j] = arr[i][j-1] - arr[i-1][j-1]; } } for(i=0; i<n; i++) { for(j=0; j<=i; j++) { System.out.print(arr[i][j]+" "); } System.out.println(" "); } } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,sum=0; for(i=a; i<=b; i++) { if(i%2==0) { sum -= i; } else { sum += i; } } System.out.println(sum); } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,j,k,sum=0,dec=0; for(k=a; k<=b; k++) { for(i=1; i<=9; i++) { System.out.println(k+"*"+i+"="+(k*i)); } } } } 20220918 클래스 : 틀 ( 필드 + 생성자 + 메소드) 객체 : 틀로 찍어낸 물체 import java.util.*; class Person{ //field 필드 int age; String name; //method 메소드 (함수) void view() { System.out.println("나이는 "+ age+"이고, 이름은 "+ name+"입니다."); } //constructor 생성자 - 객체를 생성할때 필드 값의 초기값을 정해주는 용도 Person(){ age=5000; name="noname"; } Person(int a){ age=a; name="noname"; } Person(int a, String n){ age=a; name=n; } } public class Main { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); Person s; s = new Person(); Person p = new Person(100,"tom"); Person t = new Person(50); t.view(); p.view(); s.view(); p.age=10; p.name="hello"; s.age=900; s.name="hi"; p.view(); s.view(); //System.out.println("나이는"+ p.age+"이고, 이름은 "+ p.name+"입니다."); //System.out.println("나이는"+ s.age+"이고, 이름은 "+ s.name+"입니다."); } } */ /* class Circle { int radius; String name; public double getArea() { return 3.14*radius*radius; } Circle(int r, String n){ radius=r; name=n; } } class Main{ public static void main(String[] args) { // Circle pizza; // pizza = new Circle(); // pizza.radius = 10; // pizza.name = "자바피자"; // double area = pizza.getArea(); // System.out.println(pizza.name+"의 면적은"+area); Circle donut = new Circle(2,"자바도넛"); double area = donut.getArea(); System.out.println(donut.name+"의 면적은"+area); } } */ /* import java.util.*; class Circle { int radius; String name; public Circle() { radius = 1; name = ""; } public Circle(int r, String n) { radius = r; name = n; } public double getArea() { return 3.14*radius*radius; } } class Main{ public static void main(String[]args) { Circle pizza = new Circle(10, "자바피자"); double area = pizza.getArea(); System.out.println(pizza.name + "의 면적은 " + area); Circle donut = new Circle(); donut.name = "도넛피자"; area = donut.getArea(); System.out.println(donut.name + "의 면적은 " + area); } } private 다른 클래스에서 접근 할 수 없는 필드, 메소드가 된다 */ /* class Song { private String title; public Song(String title) { this.title = title; } public String getTitle() { return title; } } class Main { public static void main(String[] args) { Song mySong = new Song("Nessun Dorma"); Song yourSong = new Song("공주는 잠 못 이루고"); System.out.println("내 노래는 " + mySong.getTitle()); System.out.println("너 노래는 " + yourSong.getTitle()); } } */ /* 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; } } class Main { public static void main(String[] args) { Phone } } */ /* import java.util.*; class TV { private String name; int year; int size; public TV(String name,int year,int size) { this.name = name; this.year = year; this.size = size; } public void show() { System.out.println(name+"에서 만든 "+year+"년형 "+size+"인치 TV"); } } class Main{ public static void main(String [] args) { TV myTV = new TV("LG", 2017, 32); myTV.show(); } } */ /* import java.util.*; class Grade { private int math; private int science; private int english; public Grade(int math,int science,int english) { this.math = math; this.science = science; this.english = english; } public int average() { return (math+science+english)/3; } } class Main { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); System.out.println("수학, 과학, 영어 순으로 3개의 정수 입력>>"); Grade me = new Grade(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()); System.out.println("평균은 " + me.average()); } } */ /* import java.util.*; class Song { private String title; private String country; private String artist; private int year; public Song(int year) { this(year,"미정","미정","미정"); } public Song(int year,String title,String country,String artist) { this.title = title; this.country = country; this.artist = artist; this.year = year; } public void show() { System.out.println(year+"년 "+country+"국적의 "+artist+"가 부른 "+title); } } class Main { public static void main(String [] args) { Song mySong = new Song(1978,"Dancing Queen","스웨덴","ABBA"); Song yourSong = new Song(1945); mySong.show(); } } */ /* import java.util.*; 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; } } class Main { public static void main(String [] args) { Book littlePrince = new Book("어린왕자","생텍쥐페리"); Book loveStory = new Book("춘향전"); Book emptyBook = new Book(); loveStory.show(); } } */ /* class Samp { int id; public Samp(int x) { this.id = x; } public Samp() { this(0); System.out.println("생성자 호출"); } } class ConstructorExample{ //필드 int x; //메소드 public void setx(int x) { this.x = x; } public int getx() { return x; } //생성자 public ConstructorExample() { } public static void main(String [] args) { ConstructorExample a = new ConstructorExample(); int n = a.getx(); } } 객체 배열을 생성할때는 1. 레퍼런스 배열의 레퍼런스 생성 2. 레퍼런스 배열 생성 3. 객체 생성해서 레퍼런스에 연결 class Person{ String name; int age; } class Main{ public static void main(String[] args) { // Person p ;//레퍼런스변수 생성 // p = new Person(); //객체 생성 Person[] arr = new Person[10]; for(int i=0;i<arr.length;i++) { arr[i] = new Person(); } arr[0].name="민혁"; } } * */ /* import java.util.*; class Circle { int radius; public Circle(int radius) { this.radius = radius; } public double getArea() { return 3.14*radius*radius; } } class Main { public static void main(String[] args) { Circle [] c; c = new Circle[5]; for(int i=0; i<c.length; i++) c[i] = new Circle(i); for(int i=0; i<c.length; i++) System.out.println((int)(c[i].getArea()) + " "); } } */ /* import java.util.Scanner; class Book { String title,author; public Book(String title,String author) { this.title = title; this.author = author; } } class Main{ public static void main(String[] args) { Book [] book = new Book[2]; Scanner scanner = new Scanner(System.in); for(int i=0; i<book.length; i++) { System.out.print("제목>>"); String title = scanner.nextLine(); System.out.print("저자>>"); String author = scanner.nextLine(); book[i] = new Book(title, author); } for(int i=0; i<book.length; i++) System.out.print("(" + book[i].title + ", " + book[i].author + ")"); } } */ /* import java.util.*; class Circle { private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Circle c[] = new Circle[3]; for (int i = 0; i < c.length; i++) { System.out.print("x, y, radius >>"); double x = scanner.nextDouble(); double y = scanner.nextDouble(); int radius = scanner.nextInt(); c[i] = new Circle(x, y, radius); } for(int i=0; i<c.length; i++) c[i].show(); scanner.close(); } } import java.util.Scanner; class Dictionary { private static String [] kor = { "사랑","아기","돈","미래","희망" }; private static String [] eng = { "love","baby","monry","future", "hope"}; public static String kor2Eng(String word) { for(int i=0;i<kor.length;i++) { if(kor[i].equals(word)) return eng[i]; } return " 저의 사전에 없습니다."; } } class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("한영 단어 검색 프로그램입니다."); while(true) { System.out.print("한글 단어?"); String name = sc.next(); if(name.equals("그만")) { return ; } else { System.out.println(name+"는 " +Dictionary.kor2Eng(name)); } } } } 부모클래스 - 자식클래스 슈퍼클래스 - 서브클래스 class Person{ int age; String name; } class Student extends Person{ String school; char grade; void speak() { System.out.println("안녕하세요, 저는 학생입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class Teacher extends Person{ String school; String major; void speak() { System.out.println("안녕하세요, 저는 선생님입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class ComTeacher extends Teacher{ } class Main{ public static void main(String[] args) { Student s = new Student(); Teacher t = new Teacher(); s.speak(); } } */ /* import java.util.*; 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; void setColor(String color) { this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main{ public static void main(String [] args) { Point p = new Point(); p.set(1,2); p.showPoint(); ColorPoint cp = new ColorPoint (); cp.set(3,4); cp.setColor("red"); cp.showColorPoint(); } } import java.util.*; class Pen { protected int amount; public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } class SharpPencil extends Pen { private int width; } class BallPen extends Pen { private String color; public String getColor() { return color; } public void setColor(String color ) { this.color = color; } } class FountainPen extends BallPen { public void refill(int n) { amount = n; } } 상속-생성자 class A{ int n; public A() { System.out.println("A의 기본 생성자"); } public A(int n) {this.n = n;} } class B extends A{ int m; public B() { System.out.println("B의 기본 생성자"); } public B(int m) { super(20); this.m = m; System.out.println("B의 1번 생성자"); } } class Main{ public static void main(String[] args) { //A x = new A(10); B y = new B(10); } } */ /* import java.util.*; class Point { private int x,y; public Point() { this.x = this.y = 0; } public Point(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 ColorPoint(int x, int y, String color) { super (x, y); this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main { public static void main(String[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showColorPoint (); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } public void printProperty() { System.out.print(getSize()+"인치 "+Color+"컬러"); } } class Main { public static void main(String [] args) { ColorTV myTV = new ColorTV(32,1024); myTV.printProperty(); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } protected int getColor() { return Color; } } class IPTV extends ColorTV { private String IP; public IPTV(String IP,int size,int Color) { super(size,Color); this.IP = IP; } public void printProperty() { System.out.print("나의 IPTV는 "+IP+" 주소의 "+getSize()+"인치 "+getColor()+"컬러"); } } class Main { public static void main(String[]args) { IPTV iptv = new IPTV("192.1.1.2", 32, 2048); iptv.printProperty(); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint(int x,int y,String Color) { super(x,y); this.Color = Color; } public void setXY(int x,int y) { move (x,y); } public void setColor(String Color) { this.Color = Color; } public String toString () { return Color+"색의 "+"("+ getX() +","+ getY() +")"+"의 점"; } } class Main { public static void main(String[]args) { ColorPoint cp = new ColorPoint(5, 5, "YELLOW"); cp.setXY(10, 20); cp.setColor("RED"); String str = cp.toString(); System.out.println(str + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint() { super(0,0); this.Color = "BLACK"; } public ColorPoint(int x,int y) { super(x,y); this.Color = "BLACK"; } public void setXY(int x,int y) { move(x,y); } public void setColor(String color) { this.Color = color; } public String toString () { return Color + "색의 " + "("+ getX() + ","+ getY() + ")" + "의 점"; } } class Main { public static void main(String[] args) { ColorPoint zeroPoint = new ColorPoint(); System.out.println(zeroPoint.toString() + "입니다."); ColorPoint cp = new ColorPoint(10,10); cp.setXY(5, 5); cp.setColor("RED"); System.out.println(cp.toString() + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class Point3D extends Point { private int z; public Point3D(int x,int y,int z) { super(x,y); this.z = z; } public void moveUp() { z = z+1; } public void moveDown() { z--; } public void move(int x, int y,int z) { super.move(x, y); this.z = z; } public String toString() { return "("+ getX() + "," + getY() + "," + z +")" + "의 점"; } } class Main { public static void main(String[] args) { Point3D p = new Point3D(1,2,3); System.out.println(p.toString() + "입니다."); p.moveUp(); System.out.println(p.toString() + "입니다."); p.moveDown(); p.move(10, 10); System.out.println(p.toString() + "입니다."); p.move(100, 200, 300); System.out.println(p.toString() + "입니다."); } } 클래스 1. 필드 (변수 ,속성) 2. 메소드 (기능, 행동) 3. 생성자(객체 생성시 필드값 초기화) 추상클래스 : 일부만 작성 == 일부는 작성 x */ /* abstract class Person{ int age; String name; void introduce() { //일반 메소드 System.out.println("I'm "+age+"years old. My name is "+name+"."); } abstract void speak(); //추상메소드 ( 이 클래스를 상속받은 모든 클래스는 이 메소드를 구현) } class Student extends Person{ void speak() { System.out.println("I'm Student."); } } class Teacher extends Person{ void speak() { System.out.println("I'm Teacher"); } } class Main{ public static void main(String[] args) { Student s = new Student(); } } */ /* abstract class Calculator { public abstract int add(int a,int b); public abstract int subtract(int a,int b); public abstract double average (int[] a); } class GoodCalc extends Calculator { public int add(int a,int b) { return a + b; } public int subtract(int a,int b) { return a-b; } public double average(int[] a) { double sum = 0; for(int i=0; i<a.length; i++) sum += a[i]; return sum/a.length; } } class Main { public static void main(String[] args) { GoodCalc c = new GoodCalc(); System.out.println(c.add(2,3)); System.out.println(c.subtract(2,3)); System.out.println(c.average(new int [] { 2, 3, 4 })); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Won2Dollar extends Converter { public Won2Dollar(int i) { } @Override protected double convert(double src) { return src/1200; } @Override protected String getSrcString() { return "원"; } @Override protected String getDestString() { return "달러"; } } class Main{ public static void main(String[] args) { Won2Dollar toDollar = new Won2Dollar(1200); toDollar.run(); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Km2Mile extends Converter { public Km2Mile(double i) { ratio=i; } protected double convert(double src) { return src/ratio; } protected String getSrcString() { return "Km"; } protected String getDestString() { return "mile"; } } class Main { public static void main(String[] args) { Km2Mile toMile = new Km2Mile(1.6); toMile.run(); } } */ /* import java.util.*; abstract class PairMap { protected String keyArray []; protected String ValueArray []; abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key); abstract int length(); } class Dictionary extends PairMap { public Dictionary(int i) { } String get(String key) { return ""; } void put(String key, String value) { } String delete(String key) { return null; } int length() { return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(10); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* import java.util.*; interface PairMap { abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key, String value); abstract int length(); } class Dictionary implements PairMap { @Override public String get(String key) { // TODO Auto-generated method stub return null; } @Override public void put(String key, String value) { // TODO Auto-generated method stub } @Override public String delete(String key, String value) { // TODO Auto-generated method stub return null; } @Override public int length() { // TODO Auto-generated method stub return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */ /* interface Shape { final double PI = 3.14; void draw(); double getArea(); default public void redraw() { System.out.print("--- 다시 그립니다. "); draw(); } } class Circle implements Shape{ int r; public Circle(int r) { this.r = r; } public void draw() { System.out.println("반지름이 " + r + "인 원입니다."); } public double getArea() { return r*r*PI; } } class Main { public static void main(String [] args) { Shape donut = new Circle(10); donut.redraw(); System.out.println("면적은 " + donut.getArea()); } } interface MouseDriver { final int BUTTONS = 3; //int VERSION; void move(); public int click(); int out(); void drag(); default void drop() { System.out.println("drop"); } } GUI : 그림으로 보여지는것 컨테이너 container : 종이 판 (다른 것들을 포함한다) 컴포넌트 component : 스티커 (컨테이너 위에 올라간다) import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("example1"); // 창 제목 정하기 (필수아님) setSize(500, 300); //가로500, 세로 300으로 크기 셋팅 (필수) Container c = getContentPane(); // c = 현재 배경 c.setLayout(new FlowLayout()); //레이아웃 설정 (나중에) JButton b = new JButton("버튼"); //버튼 생성 c.add(b); //버튼 추가 JLabel la = new JLabel("레이블컴포넌트입니다"); c.add(la); setVisible(true); // 프레임이 보이게 설정 (필수) } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("ContentPane과 JFrame"); setSize(500,300); Container c = getContentPane(); //c.setLayout(new FlowLayout()); c.setLayout(new BorderLayout(5,10)); c.setBackground(Color.black); //배경색 설정 JButton b = new JButton("OK"); b.setBackground(Color.white); b.setForeground(Color.black); c.add(b,BorderLayout.NORTH); // JButton b1 = new JButton("Cancel"); // b1.setBackground(Color.white); // b1.setForeground(Color.black); //전경색 (글자색) 설정 // c.add(b1, BorderLayout.CENTER); JButton b2 = new JButton("Ignore"); b2.setForeground(Color.black); c.add(b2, BorderLayout.CENTER); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.setBackground(Color.black); c.add(p,BorderLayout.EAST); JButton b3 = new JButton("Cancel"); p.add(new JButton("Cancel")); p.add(b3); b3.setForeground(Color.blue); setVisible(true); } public static void main(String[] args) { new Main(); } } */ /* import java.awt.*; import javax.swing.*; class Main extends JFrame{ public Main() { setTitle("Let's study Java"); setSize(400,200); Container c = getContentPane(); c.setLayout(new BorderLayout(5,7)); JButton b = new JButton("Center"); c.add(b, BorderLayout.CENTER); JButton b1 = new JButton("East"); c.add(b1, BorderLayout.EAST); JButton b2 = new JButton("West"); c.add(b2, BorderLayout.WEST); JButton b3 = new JButton("North"); c.add(b3, BorderLayout.NORTH); JButton b4 = new JButton("South"); c.add(b4, BorderLayout.SOUTH); setVisible(true); } public static void main(String[] args) { new Main(); } } */
0
0
1
minhyuk0426
2022년 11월 27일
In 소스 코드 제출
/* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i,j; int[][] arr = new int[n][n]; for(i=0; i<n; i++) { arr[i][0] = sc.nextInt(); } for(i=1; i<n; i++) { for(j=1; j<=i; j++) { arr[i][j] = arr[i][j-1] - arr[i-1][j-1]; } } for(i=0; i<n; i++) { for(j=0; j<=i; j++) { System.out.print(arr[i][j]+" "); } System.out.println(" "); } } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,sum=0; for(i=a; i<=b; i++) { if(i%2==0) { sum -= i; } else { sum += i; } } System.out.println(sum); } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,j,k,sum=0,dec=0; for(k=a; k<=b; k++) { for(i=1; i<=9; i++) { System.out.println(k+"*"+i+"="+(k*i)); } } } } 20220918 클래스 : 틀 ( 필드 + 생성자 + 메소드) 객체 : 틀로 찍어낸 물체 import java.util.*; class Person{ //field 필드 int age; String name; //method 메소드 (함수) void view() { System.out.println("나이는 "+ age+"이고, 이름은 "+ name+"입니다."); } //constructor 생성자 - 객체를 생성할때 필드 값의 초기값을 정해주는 용도 Person(){ age=5000; name="noname"; } Person(int a){ age=a; name="noname"; } Person(int a, String n){ age=a; name=n; } } public class Main { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); Person s; s = new Person(); Person p = new Person(100,"tom"); Person t = new Person(50); t.view(); p.view(); s.view(); p.age=10; p.name="hello"; s.age=900; s.name="hi"; p.view(); s.view(); //System.out.println("나이는"+ p.age+"이고, 이름은 "+ p.name+"입니다."); //System.out.println("나이는"+ s.age+"이고, 이름은 "+ s.name+"입니다."); } } */ /* class Circle { int radius; String name; public double getArea() { return 3.14*radius*radius; } Circle(int r, String n){ radius=r; name=n; } } class Main{ public static void main(String[] args) { // Circle pizza; // pizza = new Circle(); // pizza.radius = 10; // pizza.name = "자바피자"; // double area = pizza.getArea(); // System.out.println(pizza.name+"의 면적은"+area); Circle donut = new Circle(2,"자바도넛"); double area = donut.getArea(); System.out.println(donut.name+"의 면적은"+area); } } */ /* import java.util.*; class Circle { int radius; String name; public Circle() { radius = 1; name = ""; } public Circle(int r, String n) { radius = r; name = n; } public double getArea() { return 3.14*radius*radius; } } class Main{ public static void main(String[]args) { Circle pizza = new Circle(10, "자바피자"); double area = pizza.getArea(); System.out.println(pizza.name + "의 면적은 " + area); Circle donut = new Circle(); donut.name = "도넛피자"; area = donut.getArea(); System.out.println(donut.name + "의 면적은 " + area); } } private 다른 클래스에서 접근 할 수 없는 필드, 메소드가 된다 */ /* class Song { private String title; public Song(String title) { this.title = title; } public String getTitle() { return title; } } class Main { public static void main(String[] args) { Song mySong = new Song("Nessun Dorma"); Song yourSong = new Song("공주는 잠 못 이루고"); System.out.println("내 노래는 " + mySong.getTitle()); System.out.println("너 노래는 " + yourSong.getTitle()); } } */ /* 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; } } class Main { public static void main(String[] args) { Phone } } */ /* import java.util.*; class TV { private String name; int year; int size; public TV(String name,int year,int size) { this.name = name; this.year = year; this.size = size; } public void show() { System.out.println(name+"에서 만든 "+year+"년형 "+size+"인치 TV"); } } class Main{ public static void main(String [] args) { TV myTV = new TV("LG", 2017, 32); myTV.show(); } } */ /* import java.util.*; class Grade { private int math; private int science; private int english; public Grade(int math,int science,int english) { this.math = math; this.science = science; this.english = english; } public int average() { return (math+science+english)/3; } } class Main { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); System.out.println("수학, 과학, 영어 순으로 3개의 정수 입력>>"); Grade me = new Grade(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()); System.out.println("평균은 " + me.average()); } } */ /* import java.util.*; class Song { private String title; private String country; private String artist; private int year; public Song(int year) { this(year,"미정","미정","미정"); } public Song(int year,String title,String country,String artist) { this.title = title; this.country = country; this.artist = artist; this.year = year; } public void show() { System.out.println(year+"년 "+country+"국적의 "+artist+"가 부른 "+title); } } class Main { public static void main(String [] args) { Song mySong = new Song(1978,"Dancing Queen","스웨덴","ABBA"); Song yourSong = new Song(1945); mySong.show(); } } */ /* import java.util.*; 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; } } class Main { public static void main(String [] args) { Book littlePrince = new Book("어린왕자","생텍쥐페리"); Book loveStory = new Book("춘향전"); Book emptyBook = new Book(); loveStory.show(); } } */ /* class Samp { int id; public Samp(int x) { this.id = x; } public Samp() { this(0); System.out.println("생성자 호출"); } } class ConstructorExample{ //필드 int x; //메소드 public void setx(int x) { this.x = x; } public int getx() { return x; } //생성자 public ConstructorExample() { } public static void main(String [] args) { ConstructorExample a = new ConstructorExample(); int n = a.getx(); } } 객체 배열을 생성할때는 1. 레퍼런스 배열의 레퍼런스 생성 2. 레퍼런스 배열 생성 3. 객체 생성해서 레퍼런스에 연결 class Person{ String name; int age; } class Main{ public static void main(String[] args) { // Person p ;//레퍼런스변수 생성 // p = new Person(); //객체 생성 Person[] arr = new Person[10]; for(int i=0;i<arr.length;i++) { arr[i] = new Person(); } arr[0].name="민혁"; } } * */ /* import java.util.*; class Circle { int radius; public Circle(int radius) { this.radius = radius; } public double getArea() { return 3.14*radius*radius; } } class Main { public static void main(String[] args) { Circle [] c; c = new Circle[5]; for(int i=0; i<c.length; i++) c[i] = new Circle(i); for(int i=0; i<c.length; i++) System.out.println((int)(c[i].getArea()) + " "); } } */ /* import java.util.Scanner; class Book { String title,author; public Book(String title,String author) { this.title = title; this.author = author; } } class Main{ public static void main(String[] args) { Book [] book = new Book[2]; Scanner scanner = new Scanner(System.in); for(int i=0; i<book.length; i++) { System.out.print("제목>>"); String title = scanner.nextLine(); System.out.print("저자>>"); String author = scanner.nextLine(); book[i] = new Book(title, author); } for(int i=0; i<book.length; i++) System.out.print("(" + book[i].title + ", " + book[i].author + ")"); } } */ /* import java.util.*; class Circle { private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Circle c[] = new Circle[3]; for (int i = 0; i < c.length; i++) { System.out.print("x, y, radius >>"); double x = scanner.nextDouble(); double y = scanner.nextDouble(); int radius = scanner.nextInt(); c[i] = new Circle(x, y, radius); } for(int i=0; i<c.length; i++) c[i].show(); scanner.close(); } } import java.util.Scanner; class Dictionary { private static String [] kor = { "사랑","아기","돈","미래","희망" }; private static String [] eng = { "love","baby","monry","future", "hope"}; public static String kor2Eng(String word) { for(int i=0;i<kor.length;i++) { if(kor[i].equals(word)) return eng[i]; } return " 저의 사전에 없습니다."; } } class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("한영 단어 검색 프로그램입니다."); while(true) { System.out.print("한글 단어?"); String name = sc.next(); if(name.equals("그만")) { return ; } else { System.out.println(name+"는 " +Dictionary.kor2Eng(name)); } } } } 부모클래스 - 자식클래스 슈퍼클래스 - 서브클래스 class Person{ int age; String name; } class Student extends Person{ String school; char grade; void speak() { System.out.println("안녕하세요, 저는 학생입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class Teacher extends Person{ String school; String major; void speak() { System.out.println("안녕하세요, 저는 선생님입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class ComTeacher extends Teacher{ } class Main{ public static void main(String[] args) { Student s = new Student(); Teacher t = new Teacher(); s.speak(); } } */ /* import java.util.*; 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; void setColor(String color) { this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main{ public static void main(String [] args) { Point p = new Point(); p.set(1,2); p.showPoint(); ColorPoint cp = new ColorPoint (); cp.set(3,4); cp.setColor("red"); cp.showColorPoint(); } } import java.util.*; class Pen { protected int amount; public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } class SharpPencil extends Pen { private int width; } class BallPen extends Pen { private String color; public String getColor() { return color; } public void setColor(String color ) { this.color = color; } } class FountainPen extends BallPen { public void refill(int n) { amount = n; } } 상속-생성자 class A{ int n; public A() { System.out.println("A의 기본 생성자"); } public A(int n) {this.n = n;} } class B extends A{ int m; public B() { System.out.println("B의 기본 생성자"); } public B(int m) { super(20); this.m = m; System.out.println("B의 1번 생성자"); } } class Main{ public static void main(String[] args) { //A x = new A(10); B y = new B(10); } } */ /* import java.util.*; class Point { private int x,y; public Point() { this.x = this.y = 0; } public Point(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 ColorPoint(int x, int y, String color) { super (x, y); this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main { public static void main(String[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showColorPoint (); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } public void printProperty() { System.out.print(getSize()+"인치 "+Color+"컬러"); } } class Main { public static void main(String [] args) { ColorTV myTV = new ColorTV(32,1024); myTV.printProperty(); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } protected int getColor() { return Color; } } class IPTV extends ColorTV { private String IP; public IPTV(String IP,int size,int Color) { super(size,Color); this.IP = IP; } public void printProperty() { System.out.print("나의 IPTV는 "+IP+" 주소의 "+getSize()+"인치 "+getColor()+"컬러"); } } class Main { public static void main(String[]args) { IPTV iptv = new IPTV("192.1.1.2", 32, 2048); iptv.printProperty(); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint(int x,int y,String Color) { super(x,y); this.Color = Color; } public void setXY(int x,int y) { move (x,y); } public void setColor(String Color) { this.Color = Color; } public String toString () { return Color+"색의 "+"("+ getX() +","+ getY() +")"+"의 점"; } } class Main { public static void main(String[]args) { ColorPoint cp = new ColorPoint(5, 5, "YELLOW"); cp.setXY(10, 20); cp.setColor("RED"); String str = cp.toString(); System.out.println(str + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint() { super(0,0); this.Color = "BLACK"; } public ColorPoint(int x,int y) { super(x,y); this.Color = "BLACK"; } public void setXY(int x,int y) { move(x,y); } public void setColor(String color) { this.Color = color; } public String toString () { return Color + "색의 " + "("+ getX() + ","+ getY() + ")" + "의 점"; } } class Main { public static void main(String[] args) { ColorPoint zeroPoint = new ColorPoint(); System.out.println(zeroPoint.toString() + "입니다."); ColorPoint cp = new ColorPoint(10,10); cp.setXY(5, 5); cp.setColor("RED"); System.out.println(cp.toString() + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class Point3D extends Point { private int z; public Point3D(int x,int y,int z) { super(x,y); this.z = z; } public void moveUp() { z = z+1; } public void moveDown() { z--; } public void move(int x, int y,int z) { super.move(x, y); this.z = z; } public String toString() { return "("+ getX() + "," + getY() + "," + z +")" + "의 점"; } } class Main { public static void main(String[] args) { Point3D p = new Point3D(1,2,3); System.out.println(p.toString() + "입니다."); p.moveUp(); System.out.println(p.toString() + "입니다."); p.moveDown(); p.move(10, 10); System.out.println(p.toString() + "입니다."); p.move(100, 200, 300); System.out.println(p.toString() + "입니다."); } } 클래스 1. 필드 (변수 ,속성) 2. 메소드 (기능, 행동) 3. 생성자(객체 생성시 필드값 초기화) 추상클래스 : 일부만 작성 == 일부는 작성 x */ /* abstract class Person{ int age; String name; void introduce() { //일반 메소드 System.out.println("I'm "+age+"years old. My name is "+name+"."); } abstract void speak(); //추상메소드 ( 이 클래스를 상속받은 모든 클래스는 이 메소드를 구현) } class Student extends Person{ void speak() { System.out.println("I'm Student."); } } class Teacher extends Person{ void speak() { System.out.println("I'm Teacher"); } } class Main{ public static void main(String[] args) { Student s = new Student(); } } */ /* abstract class Calculator { public abstract int add(int a,int b); public abstract int subtract(int a,int b); public abstract double average (int[] a); } class GoodCalc extends Calculator { public int add(int a,int b) { return a + b; } public int subtract(int a,int b) { return a-b; } public double average(int[] a) { double sum = 0; for(int i=0; i<a.length; i++) sum += a[i]; return sum/a.length; } } class Main { public static void main(String[] args) { GoodCalc c = new GoodCalc(); System.out.println(c.add(2,3)); System.out.println(c.subtract(2,3)); System.out.println(c.average(new int [] { 2, 3, 4 })); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Won2Dollar extends Converter { public Won2Dollar(int i) { } @Override protected double convert(double src) { return src/1200; } @Override protected String getSrcString() { return "원"; } @Override protected String getDestString() { return "달러"; } } class Main{ public static void main(String[] args) { Won2Dollar toDollar = new Won2Dollar(1200); toDollar.run(); } } */ /* import java.util.*; abstract class Converter { abstract protected double convert(double src); abstract protected String getSrcString(); abstract protected String getDestString(); protected double ratio; public void run() { Scanner scanner = new Scanner(System.in); System.out.println(getSrcString()+"을 "+getDestString()+"로 바꿉니다."); System.out.println(getSrcString()+"을 입력하세요>> "); double val = scanner.nextDouble(); double res = convert(val); System.out.println("변환 결과: "+res+getDestString()+"입니다."); } } class Km2Mile extends Converter { public Km2Mile(double i) { ratio=i; } protected double convert(double src) { return src/ratio; } protected String getSrcString() { return "Km"; } protected String getDestString() { return "mile"; } } class Main { public static void main(String[] args) { Km2Mile toMile = new Km2Mile(1.6); toMile.run(); } } */ /* import java.util.*; abstract class PairMap { protected String keyArray []; protected String ValueArray []; abstract String get(String key); abstract void put(String key, String value); abstract String delete(String key); abstract int length(); } class Dictionary extends PairMap { public Dictionary(int i) { } String get(String key) { return ""; } void put(String key, String value) { } String delete(String key) { return null; } int length() { return 0; } } class Main { public static void main(String[] args) { Dictionary dic = new Dictionary(10); dic.put("황기태", "자바"); dic.put("이재문", "파이선"); dic.put("이재문", "C++"); System.out.println("이재문의 값은 " + dic.get("이재문")); System.out.println("황기태의 값은 " + dic.get("황기태")); dic.delete("황기태"); System.out.println("황기태의 값은 " + dic.get("황기태")); } } */
0
0
1
minhyuk0426
2022년 11월 20일
In 소스 코드 제출
/* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i,j; int[][] arr = new int[n][n]; for(i=0; i<n; i++) { arr[i][0] = sc.nextInt(); } for(i=1; i<n; i++) { for(j=1; j<=i; j++) { arr[i][j] = arr[i][j-1] - arr[i-1][j-1]; } } for(i=0; i<n; i++) { for(j=0; j<=i; j++) { System.out.print(arr[i][j]+" "); } System.out.println(" "); } } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,sum=0; for(i=a; i<=b; i++) { if(i%2==0) { sum -= i; } else { sum += i; } } System.out.println(sum); } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,j,k,sum=0,dec=0; for(k=a; k<=b; k++) { for(i=1; i<=9; i++) { System.out.println(k+"*"+i+"="+(k*i)); } } } } 20220918 클래스 : 틀 ( 필드 + 생성자 + 메소드) 객체 : 틀로 찍어낸 물체 import java.util.*; class Person{ //field 필드 int age; String name; //method 메소드 (함수) void view() { System.out.println("나이는 "+ age+"이고, 이름은 "+ name+"입니다."); } //constructor 생성자 - 객체를 생성할때 필드 값의 초기값을 정해주는 용도 Person(){ age=5000; name="noname"; } Person(int a){ age=a; name="noname"; } Person(int a, String n){ age=a; name=n; } } public class Main { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); Person s; s = new Person(); Person p = new Person(100,"tom"); Person t = new Person(50); t.view(); p.view(); s.view(); p.age=10; p.name="hello"; s.age=900; s.name="hi"; p.view(); s.view(); //System.out.println("나이는"+ p.age+"이고, 이름은 "+ p.name+"입니다."); //System.out.println("나이는"+ s.age+"이고, 이름은 "+ s.name+"입니다."); } } */ /* class Circle { int radius; String name; public double getArea() { return 3.14*radius*radius; } Circle(int r, String n){ radius=r; name=n; } } class Main{ public static void main(String[] args) { // Circle pizza; // pizza = new Circle(); // pizza.radius = 10; // pizza.name = "자바피자"; // double area = pizza.getArea(); // System.out.println(pizza.name+"의 면적은"+area); Circle donut = new Circle(2,"자바도넛"); double area = donut.getArea(); System.out.println(donut.name+"의 면적은"+area); } } */ /* import java.util.*; class Circle { int radius; String name; public Circle() { radius = 1; name = ""; } public Circle(int r, String n) { radius = r; name = n; } public double getArea() { return 3.14*radius*radius; } } class Main{ public static void main(String[]args) { Circle pizza = new Circle(10, "자바피자"); double area = pizza.getArea(); System.out.println(pizza.name + "의 면적은 " + area); Circle donut = new Circle(); donut.name = "도넛피자"; area = donut.getArea(); System.out.println(donut.name + "의 면적은 " + area); } } private 다른 클래스에서 접근 할 수 없는 필드, 메소드가 된다 */ /* class Song { private String title; public Song(String title) { this.title = title; } public String getTitle() { return title; } } class Main { public static void main(String[] args) { Song mySong = new Song("Nessun Dorma"); Song yourSong = new Song("공주는 잠 못 이루고"); System.out.println("내 노래는 " + mySong.getTitle()); System.out.println("너 노래는 " + yourSong.getTitle()); } } */ /* 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; } } class Main { public static void main(String[] args) { Phone } } */ /* import java.util.*; class TV { private String name; int year; int size; public TV(String name,int year,int size) { this.name = name; this.year = year; this.size = size; } public void show() { System.out.println(name+"에서 만든 "+year+"년형 "+size+"인치 TV"); } } class Main{ public static void main(String [] args) { TV myTV = new TV("LG", 2017, 32); myTV.show(); } } */ /* import java.util.*; class Grade { private int math; private int science; private int english; public Grade(int math,int science,int english) { this.math = math; this.science = science; this.english = english; } public int average() { return (math+science+english)/3; } } class Main { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); System.out.println("수학, 과학, 영어 순으로 3개의 정수 입력>>"); Grade me = new Grade(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()); System.out.println("평균은 " + me.average()); } } */ /* import java.util.*; class Song { private String title; private String country; private String artist; private int year; public Song(int year) { this(year,"미정","미정","미정"); } public Song(int year,String title,String country,String artist) { this.title = title; this.country = country; this.artist = artist; this.year = year; } public void show() { System.out.println(year+"년 "+country+"국적의 "+artist+"가 부른 "+title); } } class Main { public static void main(String [] args) { Song mySong = new Song(1978,"Dancing Queen","스웨덴","ABBA"); Song yourSong = new Song(1945); mySong.show(); } } */ /* import java.util.*; 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; } } class Main { public static void main(String [] args) { Book littlePrince = new Book("어린왕자","생텍쥐페리"); Book loveStory = new Book("춘향전"); Book emptyBook = new Book(); loveStory.show(); } } */ /* class Samp { int id; public Samp(int x) { this.id = x; } public Samp() { this(0); System.out.println("생성자 호출"); } } class ConstructorExample{ //필드 int x; //메소드 public void setx(int x) { this.x = x; } public int getx() { return x; } //생성자 public ConstructorExample() { } public static void main(String [] args) { ConstructorExample a = new ConstructorExample(); int n = a.getx(); } } 객체 배열을 생성할때는 1. 레퍼런스 배열의 레퍼런스 생성 2. 레퍼런스 배열 생성 3. 객체 생성해서 레퍼런스에 연결 class Person{ String name; int age; } class Main{ public static void main(String[] args) { // Person p ;//레퍼런스변수 생성 // p = new Person(); //객체 생성 Person[] arr = new Person[10]; for(int i=0;i<arr.length;i++) { arr[i] = new Person(); } arr[0].name="민혁"; } } * */ /* import java.util.*; class Circle { int radius; public Circle(int radius) { this.radius = radius; } public double getArea() { return 3.14*radius*radius; } } class Main { public static void main(String[] args) { Circle [] c; c = new Circle[5]; for(int i=0; i<c.length; i++) c[i] = new Circle(i); for(int i=0; i<c.length; i++) System.out.println((int)(c[i].getArea()) + " "); } } */ /* import java.util.Scanner; class Book { String title,author; public Book(String title,String author) { this.title = title; this.author = author; } } class Main{ public static void main(String[] args) { Book [] book = new Book[2]; Scanner scanner = new Scanner(System.in); for(int i=0; i<book.length; i++) { System.out.print("제목>>"); String title = scanner.nextLine(); System.out.print("저자>>"); String author = scanner.nextLine(); book[i] = new Book(title, author); } for(int i=0; i<book.length; i++) System.out.print("(" + book[i].title + ", " + book[i].author + ")"); } } */ /* import java.util.*; class Circle { private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Circle c[] = new Circle[3]; for (int i = 0; i < c.length; i++) { System.out.print("x, y, radius >>"); double x = scanner.nextDouble(); double y = scanner.nextDouble(); int radius = scanner.nextInt(); c[i] = new Circle(x, y, radius); } for(int i=0; i<c.length; i++) c[i].show(); scanner.close(); } } import java.util.Scanner; class Dictionary { private static String [] kor = { "사랑","아기","돈","미래","희망" }; private static String [] eng = { "love","baby","monry","future", "hope"}; public static String kor2Eng(String word) { for(int i=0;i<kor.length;i++) { if(kor[i].equals(word)) return eng[i]; } return " 저의 사전에 없습니다."; } } class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("한영 단어 검색 프로그램입니다."); while(true) { System.out.print("한글 단어?"); String name = sc.next(); if(name.equals("그만")) { return ; } else { System.out.println(name+"는 " +Dictionary.kor2Eng(name)); } } } } 부모클래스 - 자식클래스 슈퍼클래스 - 서브클래스 class Person{ int age; String name; } class Student extends Person{ String school; char grade; void speak() { System.out.println("안녕하세요, 저는 학생입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class Teacher extends Person{ String school; String major; void speak() { System.out.println("안녕하세요, 저는 선생님입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class ComTeacher extends Teacher{ } class Main{ public static void main(String[] args) { Student s = new Student(); Teacher t = new Teacher(); s.speak(); } } */ /* import java.util.*; 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; void setColor(String color) { this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main{ public static void main(String [] args) { Point p = new Point(); p.set(1,2); p.showPoint(); ColorPoint cp = new ColorPoint (); cp.set(3,4); cp.setColor("red"); cp.showColorPoint(); } } import java.util.*; class Pen { protected int amount; public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } class SharpPencil extends Pen { private int width; } class BallPen extends Pen { private String color; public String getColor() { return color; } public void setColor(String color ) { this.color = color; } } class FountainPen extends BallPen { public void refill(int n) { amount = n; } } 상속-생성자 class A{ int n; public A() { System.out.println("A의 기본 생성자"); } public A(int n) {this.n = n;} } class B extends A{ int m; public B() { System.out.println("B의 기본 생성자"); } public B(int m) { super(20); this.m = m; System.out.println("B의 1번 생성자"); } } class Main{ public static void main(String[] args) { //A x = new A(10); B y = new B(10); } } */ /* import java.util.*; class Point { private int x,y; public Point() { this.x = this.y = 0; } public Point(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 ColorPoint(int x, int y, String color) { super (x, y); this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main { public static void main(String[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showColorPoint (); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } public void printProperty() { System.out.print(getSize()+"인치 "+Color+"컬러"); } } class Main { public static void main(String [] args) { ColorTV myTV = new ColorTV(32,1024); myTV.printProperty(); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } protected int getColor() { return Color; } } class IPTV extends ColorTV { private String IP; public IPTV(String IP,int size,int Color) { super(size,Color); this.IP = IP; } public void printProperty() { System.out.print("나의 IPTV는 "+IP+" 주소의 "+getSize()+"인치 "+getColor()+"컬러"); } } class Main { public static void main(String[]args) { IPTV iptv = new IPTV("192.1.1.2", 32, 2048); iptv.printProperty(); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint(int x,int y,String Color) { super(x,y); this.Color = Color; } public void setXY(int x,int y) { move (x,y); } public void setColor(String Color) { this.Color = Color; } public String toString () { return Color+"색의 "+"("+ getX() +","+ getY() +")"+"의 점"; } } class Main { public static void main(String[]args) { ColorPoint cp = new ColorPoint(5, 5, "YELLOW"); cp.setXY(10, 20); cp.setColor("RED"); String str = cp.toString(); System.out.println(str + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint() { super(0,0); this.Color = "BLACK"; } public ColorPoint(int x,int y) { super(x,y); this.Color = "BLACK"; } public void setXY(int x,int y) { move(x,y); } public void setColor(String color) { this.Color = color; } public String toString () { return Color + "색의 " + "("+ getX() + ","+ getY() + ")" + "의 점"; } } class Main { public static void main(String[] args) { ColorPoint zeroPoint = new ColorPoint(); System.out.println(zeroPoint.toString() + "입니다."); ColorPoint cp = new ColorPoint(10,10); cp.setXY(5, 5); cp.setColor("RED"); System.out.println(cp.toString() + "입니다."); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class Point3D extends Point { private int z; public Point3D(int x,int y,int z) { super(x,y); this.z = z; } public void moveUp() { z = z+1; } public void moveDown() { z--; } public void move(int x, int y,int z) { super.move(x, y); this.z = z; } public String toString() { return "("+ getX() + "," + getY() + "," + z +")" + "의 점"; } } class Main { public static void main(String[] args) { Point3D p = new Point3D(1,2,3); System.out.println(p.toString() + "입니다."); p.moveUp(); System.out.println(p.toString() + "입니다."); p.moveDown(); p.move(10, 10); System.out.println(p.toString() + "입니다."); p.move(100, 200, 300); System.out.println(p.toString() + "입니다."); } } */
0
0
2
minhyuk0426
2022년 11월 13일
In 소스 코드 제출
/* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i,j; int[][] arr = new int[n][n]; for(i=0; i<n; i++) { arr[i][0] = sc.nextInt(); } for(i=1; i<n; i++) { for(j=1; j<=i; j++) { arr[i][j] = arr[i][j-1] - arr[i-1][j-1]; } } for(i=0; i<n; i++) { for(j=0; j<=i; j++) { System.out.print(arr[i][j]+" "); } System.out.println(" "); } } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,sum=0; for(i=a; i<=b; i++) { if(i%2==0) { sum -= i; } else { sum += i; } } System.out.println(sum); } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,j,k,sum=0,dec=0; for(k=a; k<=b; k++) { for(i=1; i<=9; i++) { System.out.println(k+"*"+i+"="+(k*i)); } } } } 20220918 클래스 : 틀 ( 필드 + 생성자 + 메소드) 객체 : 틀로 찍어낸 물체 import java.util.*; class Person{ //field 필드 int age; String name; //method 메소드 (함수) void view() { System.out.println("나이는 "+ age+"이고, 이름은 "+ name+"입니다."); } //constructor 생성자 - 객체를 생성할때 필드 값의 초기값을 정해주는 용도 Person(){ age=5000; name="noname"; } Person(int a){ age=a; name="noname"; } Person(int a, String n){ age=a; name=n; } } public class Main { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); Person s; s = new Person(); Person p = new Person(100,"tom"); Person t = new Person(50); t.view(); p.view(); s.view(); p.age=10; p.name="hello"; s.age=900; s.name="hi"; p.view(); s.view(); //System.out.println("나이는"+ p.age+"이고, 이름은 "+ p.name+"입니다."); //System.out.println("나이는"+ s.age+"이고, 이름은 "+ s.name+"입니다."); } } */ /* class Circle { int radius; String name; public double getArea() { return 3.14*radius*radius; } Circle(int r, String n){ radius=r; name=n; } } class Main{ public static void main(String[] args) { // Circle pizza; // pizza = new Circle(); // pizza.radius = 10; // pizza.name = "자바피자"; // double area = pizza.getArea(); // System.out.println(pizza.name+"의 면적은"+area); Circle donut = new Circle(2,"자바도넛"); double area = donut.getArea(); System.out.println(donut.name+"의 면적은"+area); } } */ /* import java.util.*; class Circle { int radius; String name; public Circle() { radius = 1; name = ""; } public Circle(int r, String n) { radius = r; name = n; } public double getArea() { return 3.14*radius*radius; } } class Main{ public static void main(String[]args) { Circle pizza = new Circle(10, "자바피자"); double area = pizza.getArea(); System.out.println(pizza.name + "의 면적은 " + area); Circle donut = new Circle(); donut.name = "도넛피자"; area = donut.getArea(); System.out.println(donut.name + "의 면적은 " + area); } } private 다른 클래스에서 접근 할 수 없는 필드, 메소드가 된다 */ /* class Song { private String title; public Song(String title) { this.title = title; } public String getTitle() { return title; } } class Main { public static void main(String[] args) { Song mySong = new Song("Nessun Dorma"); Song yourSong = new Song("공주는 잠 못 이루고"); System.out.println("내 노래는 " + mySong.getTitle()); System.out.println("너 노래는 " + yourSong.getTitle()); } } */ /* 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; } } class Main { public static void main(String[] args) { Phone } } */ /* import java.util.*; class TV { private String name; int year; int size; public TV(String name,int year,int size) { this.name = name; this.year = year; this.size = size; } public void show() { System.out.println(name+"에서 만든 "+year+"년형 "+size+"인치 TV"); } } class Main{ public static void main(String [] args) { TV myTV = new TV("LG", 2017, 32); myTV.show(); } } */ /* import java.util.*; class Grade { private int math; private int science; private int english; public Grade(int math,int science,int english) { this.math = math; this.science = science; this.english = english; } public int average() { return (math+science+english)/3; } } class Main { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); System.out.println("수학, 과학, 영어 순으로 3개의 정수 입력>>"); Grade me = new Grade(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()); System.out.println("평균은 " + me.average()); } } */ /* import java.util.*; class Song { private String title; private String country; private String artist; private int year; public Song(int year) { this(year,"미정","미정","미정"); } public Song(int year,String title,String country,String artist) { this.title = title; this.country = country; this.artist = artist; this.year = year; } public void show() { System.out.println(year+"년 "+country+"국적의 "+artist+"가 부른 "+title); } } class Main { public static void main(String [] args) { Song mySong = new Song(1978,"Dancing Queen","스웨덴","ABBA"); Song yourSong = new Song(1945); mySong.show(); } } */ /* import java.util.*; 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; } } class Main { public static void main(String [] args) { Book littlePrince = new Book("어린왕자","생텍쥐페리"); Book loveStory = new Book("춘향전"); Book emptyBook = new Book(); loveStory.show(); } } */ /* class Samp { int id; public Samp(int x) { this.id = x; } public Samp() { this(0); System.out.println("생성자 호출"); } } class ConstructorExample{ //필드 int x; //메소드 public void setx(int x) { this.x = x; } public int getx() { return x; } //생성자 public ConstructorExample() { } public static void main(String [] args) { ConstructorExample a = new ConstructorExample(); int n = a.getx(); } } 객체 배열을 생성할때는 1. 레퍼런스 배열의 레퍼런스 생성 2. 레퍼런스 배열 생성 3. 객체 생성해서 레퍼런스에 연결 class Person{ String name; int age; } class Main{ public static void main(String[] args) { // Person p ;//레퍼런스변수 생성 // p = new Person(); //객체 생성 Person[] arr = new Person[10]; for(int i=0;i<arr.length;i++) { arr[i] = new Person(); } arr[0].name="민혁"; } } * */ /* import java.util.*; class Circle { int radius; public Circle(int radius) { this.radius = radius; } public double getArea() { return 3.14*radius*radius; } } class Main { public static void main(String[] args) { Circle [] c; c = new Circle[5]; for(int i=0; i<c.length; i++) c[i] = new Circle(i); for(int i=0; i<c.length; i++) System.out.println((int)(c[i].getArea()) + " "); } } */ /* import java.util.Scanner; class Book { String title,author; public Book(String title,String author) { this.title = title; this.author = author; } } class Main{ public static void main(String[] args) { Book [] book = new Book[2]; Scanner scanner = new Scanner(System.in); for(int i=0; i<book.length; i++) { System.out.print("제목>>"); String title = scanner.nextLine(); System.out.print("저자>>"); String author = scanner.nextLine(); book[i] = new Book(title, author); } for(int i=0; i<book.length; i++) System.out.print("(" + book[i].title + ", " + book[i].author + ")"); } } */ /* import java.util.*; class Circle { private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Circle c[] = new Circle[3]; for (int i = 0; i < c.length; i++) { System.out.print("x, y, radius >>"); double x = scanner.nextDouble(); double y = scanner.nextDouble(); int radius = scanner.nextInt(); c[i] = new Circle(x, y, radius); } for(int i=0; i<c.length; i++) c[i].show(); scanner.close(); } } import java.util.Scanner; class Dictionary { private static String [] kor = { "사랑","아기","돈","미래","희망" }; private static String [] eng = { "love","baby","monry","future", "hope"}; public static String kor2Eng(String word) { for(int i=0;i<kor.length;i++) { if(kor[i].equals(word)) return eng[i]; } return " 저의 사전에 없습니다."; } } class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("한영 단어 검색 프로그램입니다."); while(true) { System.out.print("한글 단어?"); String name = sc.next(); if(name.equals("그만")) { return ; } else { System.out.println(name+"는 " +Dictionary.kor2Eng(name)); } } } } 부모클래스 - 자식클래스 슈퍼클래스 - 서브클래스 class Person{ int age; String name; } class Student extends Person{ String school; char grade; void speak() { System.out.println("안녕하세요, 저는 학생입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class Teacher extends Person{ String school; String major; void speak() { System.out.println("안녕하세요, 저는 선생님입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class ComTeacher extends Teacher{ } class Main{ public static void main(String[] args) { Student s = new Student(); Teacher t = new Teacher(); s.speak(); } } */ /* import java.util.*; 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; void setColor(String color) { this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main{ public static void main(String [] args) { Point p = new Point(); p.set(1,2); p.showPoint(); ColorPoint cp = new ColorPoint (); cp.set(3,4); cp.setColor("red"); cp.showColorPoint(); } } import java.util.*; class Pen { protected int amount; public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } class SharpPencil extends Pen { private int width; } class BallPen extends Pen { private String color; public String getColor() { return color; } public void setColor(String color ) { this.color = color; } } class FountainPen extends BallPen { public void refill(int n) { amount = n; } } 상속-생성자 class A{ int n; public A() { System.out.println("A의 기본 생성자"); } public A(int n) {this.n = n;} } class B extends A{ int m; public B() { System.out.println("B의 기본 생성자"); } public B(int m) { super(20); this.m = m; System.out.println("B의 1번 생성자"); } } class Main{ public static void main(String[] args) { //A x = new A(10); B y = new B(10); } } */ /* import java.util.*; class Point { private int x,y; public Point() { this.x = this.y = 0; } public Point(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 ColorPoint(int x, int y, String color) { super (x, y); this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main { public static void main(String[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showColorPoint (); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } public void printProperty() { System.out.print(getSize()+"인치 "+Color+"컬러"); } } class Main { public static void main(String [] args) { ColorTV myTV = new ColorTV(32,1024); myTV.printProperty(); } } */ /* class TV { private int size; public TV(int size) { this.size = size; } protected int getSize() { return size; } } class ColorTV extends TV { private int Color; public ColorTV(int size,int Color) { super(size); this.Color = Color; } protected int getColor() { return Color; } } class IPTV extends ColorTV { private String IP; public IPTV(String IP,int size,int Color) { super(size,Color); this.IP = IP; } public void printProperty() { System.out.print("나의 IPTV는 "+IP+" 주소의 "+getSize()+"인치 "+getColor()+"컬러"); } } class Main { public static void main(String[]args) { IPTV iptv = new IPTV("192.1.1.2", 32, 2048); iptv.printProperty(); } } */ /* import java.util.*; class Point { private int x,y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } protected void move(int x, int y) { this.x = x; this.y = y; } } class ColorPoint extends Point { private String Color; public ColorPoint(int x,int y,String Color) { super(x,y); this.Color = Color; } public void setXY(int x,int y) { move (x,y); } public void setColor(String Color) { this.Color = Color; } public String toString () { return Color+"색의 "+"("+ getX() +","+ getY() +")"+"의 점"; } } class Main { public static void main(String[]args) { ColorPoint cp = new ColorPoint(5, 5, "YELLOW"); cp.setXY(10, 20); cp.setColor("RED"); String str = cp.toString(); System.out.println(str + "입니다."); } } */
0
0
2
minhyuk0426
2022년 11월 05일
In 소스 코드 제출
/* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i,j; int[][] arr = new int[n][n]; for(i=0; i<n; i++) { arr[i][0] = sc.nextInt(); } for(i=1; i<n; i++) { for(j=1; j<=i; j++) { arr[i][j] = arr[i][j-1] - arr[i-1][j-1]; } } for(i=0; i<n; i++) { for(j=0; j<=i; j++) { System.out.print(arr[i][j]+" "); } System.out.println(" "); } } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,sum=0; for(i=a; i<=b; i++) { if(i%2==0) { sum -= i; } else { sum += i; } } System.out.println(sum); } } */ /* import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int i,j,k,sum=0,dec=0; for(k=a; k<=b; k++) { for(i=1; i<=9; i++) { System.out.println(k+"*"+i+"="+(k*i)); } } } } 20220918 클래스 : 틀 ( 필드 + 생성자 + 메소드) 객체 : 틀로 찍어낸 물체 import java.util.*; class Person{ //field 필드 int age; String name; //method 메소드 (함수) void view() { System.out.println("나이는 "+ age+"이고, 이름은 "+ name+"입니다."); } //constructor 생성자 - 객체를 생성할때 필드 값의 초기값을 정해주는 용도 Person(){ age=5000; name="noname"; } Person(int a){ age=a; name="noname"; } Person(int a, String n){ age=a; name=n; } } public class Main { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); Person s; s = new Person(); Person p = new Person(100,"tom"); Person t = new Person(50); t.view(); p.view(); s.view(); p.age=10; p.name="hello"; s.age=900; s.name="hi"; p.view(); s.view(); //System.out.println("나이는"+ p.age+"이고, 이름은 "+ p.name+"입니다."); //System.out.println("나이는"+ s.age+"이고, 이름은 "+ s.name+"입니다."); } } */ /* class Circle { int radius; String name; public double getArea() { return 3.14*radius*radius; } Circle(int r, String n){ radius=r; name=n; } } class Main{ public static void main(String[] args) { // Circle pizza; // pizza = new Circle(); // pizza.radius = 10; // pizza.name = "자바피자"; // double area = pizza.getArea(); // System.out.println(pizza.name+"의 면적은"+area); Circle donut = new Circle(2,"자바도넛"); double area = donut.getArea(); System.out.println(donut.name+"의 면적은"+area); } } */ /* import java.util.*; class Circle { int radius; String name; public Circle() { radius = 1; name = ""; } public Circle(int r, String n) { radius = r; name = n; } public double getArea() { return 3.14*radius*radius; } } class Main{ public static void main(String[]args) { Circle pizza = new Circle(10, "자바피자"); double area = pizza.getArea(); System.out.println(pizza.name + "의 면적은 " + area); Circle donut = new Circle(); donut.name = "도넛피자"; area = donut.getArea(); System.out.println(donut.name + "의 면적은 " + area); } } private 다른 클래스에서 접근 할 수 없는 필드, 메소드가 된다 */ /* class Song { private String title; public Song(String title) { this.title = title; } public String getTitle() { return title; } } class Main { public static void main(String[] args) { Song mySong = new Song("Nessun Dorma"); Song yourSong = new Song("공주는 잠 못 이루고"); System.out.println("내 노래는 " + mySong.getTitle()); System.out.println("너 노래는 " + yourSong.getTitle()); } } */ /* 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; } } class Main { public static void main(String[] args) { Phone } } */ /* import java.util.*; class TV { private String name; int year; int size; public TV(String name,int year,int size) { this.name = name; this.year = year; this.size = size; } public void show() { System.out.println(name+"에서 만든 "+year+"년형 "+size+"인치 TV"); } } class Main{ public static void main(String [] args) { TV myTV = new TV("LG", 2017, 32); myTV.show(); } } */ /* import java.util.*; class Grade { private int math; private int science; private int english; public Grade(int math,int science,int english) { this.math = math; this.science = science; this.english = english; } public int average() { return (math+science+english)/3; } } class Main { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); System.out.println("수학, 과학, 영어 순으로 3개의 정수 입력>>"); Grade me = new Grade(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()); System.out.println("평균은 " + me.average()); } } */ /* import java.util.*; class Song { private String title; private String country; private String artist; private int year; public Song(int year) { this(year,"미정","미정","미정"); } public Song(int year,String title,String country,String artist) { this.title = title; this.country = country; this.artist = artist; this.year = year; } public void show() { System.out.println(year+"년 "+country+"국적의 "+artist+"가 부른 "+title); } } class Main { public static void main(String [] args) { Song mySong = new Song(1978,"Dancing Queen","스웨덴","ABBA"); Song yourSong = new Song(1945); mySong.show(); } } */ /* import java.util.*; 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; } } class Main { public static void main(String [] args) { Book littlePrince = new Book("어린왕자","생텍쥐페리"); Book loveStory = new Book("춘향전"); Book emptyBook = new Book(); loveStory.show(); } } */ /* class Samp { int id; public Samp(int x) { this.id = x; } public Samp() { this(0); System.out.println("생성자 호출"); } } class ConstructorExample{ //필드 int x; //메소드 public void setx(int x) { this.x = x; } public int getx() { return x; } //생성자 public ConstructorExample() { } public static void main(String [] args) { ConstructorExample a = new ConstructorExample(); int n = a.getx(); } } 객체 배열을 생성할때는 1. 레퍼런스 배열의 레퍼런스 생성 2. 레퍼런스 배열 생성 3. 객체 생성해서 레퍼런스에 연결 class Person{ String name; int age; } class Main{ public static void main(String[] args) { // Person p ;//레퍼런스변수 생성 // p = new Person(); //객체 생성 Person[] arr = new Person[10]; for(int i=0;i<arr.length;i++) { arr[i] = new Person(); } arr[0].name="민혁"; } } * */ /* import java.util.*; class Circle { int radius; public Circle(int radius) { this.radius = radius; } public double getArea() { return 3.14*radius*radius; } } class Main { public static void main(String[] args) { Circle [] c; c = new Circle[5]; for(int i=0; i<c.length; i++) c[i] = new Circle(i); for(int i=0; i<c.length; i++) System.out.println((int)(c[i].getArea()) + " "); } } */ /* import java.util.Scanner; class Book { String title,author; public Book(String title,String author) { this.title = title; this.author = author; } } class Main{ public static void main(String[] args) { Book [] book = new Book[2]; Scanner scanner = new Scanner(System.in); for(int i=0; i<book.length; i++) { System.out.print("제목>>"); String title = scanner.nextLine(); System.out.print("저자>>"); String author = scanner.nextLine(); book[i] = new Book(title, author); } for(int i=0; i<book.length; i++) System.out.print("(" + book[i].title + ", " + book[i].author + ")"); } } */ /* import java.util.*; class Circle { private double x, y; private int radius; public Circle(double x, double y, int radius) { this.x = x; this.y = y; this.radius = radius; } public void show() { System.out.println("("+x+", "+y+")"+radius); } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Circle c[] = new Circle[3]; for (int i = 0; i < c.length; i++) { System.out.print("x, y, radius >>"); double x = scanner.nextDouble(); double y = scanner.nextDouble(); int radius = scanner.nextInt(); c[i] = new Circle(x, y, radius); } for(int i=0; i<c.length; i++) c[i].show(); scanner.close(); } } import java.util.Scanner; class Dictionary { private static String [] kor = { "사랑","아기","돈","미래","희망" }; private static String [] eng = { "love","baby","monry","future", "hope"}; public static String kor2Eng(String word) { for(int i=0;i<kor.length;i++) { if(kor[i].equals(word)) return eng[i]; } return " 저의 사전에 없습니다."; } } class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("한영 단어 검색 프로그램입니다."); while(true) { System.out.print("한글 단어?"); String name = sc.next(); if(name.equals("그만")) { return ; } else { System.out.println(name+"는 " +Dictionary.kor2Eng(name)); } } } } 부모클래스 - 자식클래스 슈퍼클래스 - 서브클래스 class Person{ int age; String name; } class Student extends Person{ String school; char grade; void speak() { System.out.println("안녕하세요, 저는 학생입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class Teacher extends Person{ String school; String major; void speak() { System.out.println("안녕하세요, 저는 선생님입니다."); System.out.println("나이는 "+age+"살 이고, 이름은 "+name+"입니다"); } } class ComTeacher extends Teacher{ } class Main{ public static void main(String[] args) { Student s = new Student(); Teacher t = new Teacher(); s.speak(); } } */ /* import java.util.*; 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; void setColor(String color) { this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main{ public static void main(String [] args) { Point p = new Point(); p.set(1,2); p.showPoint(); ColorPoint cp = new ColorPoint (); cp.set(3,4); cp.setColor("red"); cp.showColorPoint(); } } import java.util.*; class Pen { protected int amount; public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } } class SharpPencil extends Pen { private int width; } class BallPen extends Pen { private String color; public String getColor() { return color; } public void setColor(String color ) { this.color = color; } } class FountainPen extends BallPen { public void refill(int n) { amount = n; } } 상속-생성자 class A{ int n; public A() { System.out.println("A의 기본 생성자"); } public A(int n) {this.n = n;} } class B extends A{ int m; public B() { System.out.println("B의 기본 생성자"); } public B(int m) { super(20); this.m = m; System.out.println("B의 1번 생성자"); } } class Main{ public static void main(String[] args) { //A x = new A(10); B y = new B(10); } } */ /* class Point { private int x,y; public Point() { this.x = this.y = 0; } public Point(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 ColorPoint(int x, int y, String color) { super (x, y); this.color = color; } public void showColorPoint() { System.out.print(color); showPoint(); } } class Main { public static void main(String[] args) { ColorPoint cp = new ColorPoint(5, 6, "blue"); cp.showColorPoint(); } } */
0
0
1

minhyuk0426

더보기
bottom of page