/*
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();
}
}
*/
/*
class Circle {
int radius;
public Circle(int radius) {
this.radius=radius;
}
public double getArea() {
return 3.14*radius*radius;
}
}
public class Main {
public static void main(String[] args) {
Circle[] 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.print((int) c[i].getArea()+" ");
}
}
}
*/
/*
public class Main {
static void replaceSpace(char a[]) {
for (int i=0;i<a.length;i++)
if (a[i]==' ')
a[i]=',';
}
static void printCharArray(char a[]) {
for (int i=0;i<a.length;i++)
System.out.print(a[i]);
System.out.println();
}
public static void main(String args[]) {
char c[]= {'T','h','i','s',' ','i','s',' ','a',' ','p','e','n','c','i','l'};
printCharArray(c);
replaceSpace(c);
printCharArray(c);
}
}
*/
/* #4장 실습문제1
class Song {
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.title);
System.out.println("너의 노래는 "+yourSong.title);
}
}
*/
/*
import java.util.*;
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[] arr = new Phone[50];
arr[0] = new Phone("aa","aa");
System.out.println(arr[0].getName());
for(int i=0; i<arr.length;i++) {
arr[i] = new Phone("aa","aa");
}
Scanner sc=new Scanner(System.in);
System.out.print("이름과 전화번호 입력 >>");
Phone first=new Phone(sc.next(), sc.next());
System.out.print("이름과 전화번호 입력 >>");
Phone second=new Phone(sc.next(), sc.next());
System.out.println(first.getName()+" "+first.getTel());
System.out.println(second.getName()+" "+second.getTel());
}
}
*/
/*
import java.util.*;
class Rect {
private int width, height;
public Rect(int width,int height) {
this.width=width;
this.height=height;
}
public int getArea() {
return width*height;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Rect [] c=new Rect[4];
int sum=0;
for (int i=0;i<4;i++) {
System.out.print((i+1) +" 너비와 높이 >>");
c[i]=new Rect(sc.nextInt(),sc.nextInt());
sum+=c[i].getArea();
}
System.out.println("저장하였습니다...");
System.out.println("사각형의 전체 합은 "+sum);
}
}
*/
/*
import java.util.*;
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;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("인원수>>");
int a=sc.nextInt();
Phone [] c=new Phone[a];
for (int i=0;i<a;i++) {
System.out.println("이름과 전화번호(번호는 연속적으로 출력)>>");
c[i]=new Phone(sc.next(),sc.next());
}
System.out.println("저장되었습니다...");
while (true) {
System.out.println("검색할 이름>>");
String searchName=sc.next();
if (searchName.equals("exit")) {
System.out.println("프로그램을 종료합니다...");
break;
}
boolean isFind=false;
for (int i=0;i<a;i++) {
if (searchName.equals(c[i].getName())) {
System.out.println(c[i].getName()+"의 번호는 "+c[i].getTel()+"입니다.");
isFind=true;
break;
}
}
if(isFind==false) {
System.out.println(searchName+" 이(가) 없습니다.");
}
}
}
}
private 클래스 내에서만 접근 가능
public 누구나 접근 가능
*/
/*
class Phone {
final int AGE=10;
static int num; // 해당 클래스로 만들어진 객체들이 공유하는 필드
private String name, tel;
public Phone(String name,String tel) {
this.name=name;
this.tel=tel;
}
static void addNum() {
num++;
//name="ddd";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public String getTel() {
return tel;
}
}
class Main{
public static void main(String[] args) {
Phone.num=10;
Phone.addNum();
//Phone p = new Phone("aaa","111-222");
//Phone p1 = new Phone("ccc","333-222");
//System.out.println(p.name); (x)
//System.out.println(p.getName());
//p.name="bbb"; (x)
//p.setName("bbb");
//p.num p1.num
}
}
*/
/*
class Calc {
public static int abs(int a) {return a>0?a:-a;}
public static int max(int a, int b) {return (a>b)?a:b;}
public static int min(int a, int b) {return (a>b)?b:a;}
}
public class Main {
public static void main(String[] args) {
System.out.println(Calc.abs(-5));
System.out.println(Calc.max(10, 8));
System.out.println(Calc.min(-3, -8));
}
}
*/
/*
class Circle {
private int radius;
public Circle(int radius) {this.radius=radius; }
public int getRadius() {return this.radius; }
public void setRadius(int radius) {this.radius=radius; }
}
class CircleManager {
static void copy(Circle src, Circle dest) {
dest.setRadius(src.getRadius());
}
static boolean equals(Circle a, Circle b) {
if(a.getRadius()==b.getRadius()) {
return true;
}
else {
return false;
}
}
}
public class Main {
public static void main(String[] args) {
Circle pizza=new Circle(5);
Circle waffle=new Circle(1);
boolean res=CircleManager.equals(pizza, waffle); //result의 약자
if (res==true) {
System.out.println("pizza와 waffle 크기 같음");
}
else {
System.out.println("pizza와 wafle 크기 다름");
}
CircleManager.copy(pizza, waffle);
res=CircleManager.equals(pizza, waffle);
if (res==true) {
System.out.println("pizza와 waffle 크기 같음");
}
else {
System.out.println("pizza와 waffle 크기 다름");
}
}
}
*/
import java.util.*;
class Player {
private String name;
public Player(String name) {this.name=name; }
public String getName() {return name; }
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Player [] p=new Player[3];
for() {
System.out.println("선수 이름 입력 >>");
p[i]=new Player(sc.next());
}
int n=0;
while (true) {
System.out.print(p[n].getName()+" <Enter 외 아무키나 치세요>");
sc.next();
int [] val=new int [3];
for (int i=0;i<val.length;i++) {
val[i]=(int)(Math.random*3);
System.out.print(val[i]+"\t");
}
System.out.println();
if ()
}
}
}