/*클래스만들기, 객체 만들기
* 필드, 메소드 만들기
import java.util.*;
class Person{
String name; //필드
int age;
void view() //메소드 (함수)
{
System.out.println(name+"는 "+age+"살이예요.");
}
}
class Main{
public static void main(String[] args) { //메인메소드
Person p; //1. 레퍼런스변수 생성
p = new Person(); //2. Person 객체 생성
Person s = new Person(); //1,2번 한번에 하기
p.age = 100;
p.name = "준영이";
p.view();
s.age = 500;
s.name = "준상이";
s.view();
}
}
import java.util.*;
class Circle{
int radius;
String name;
public Circle() { }
public double getArea()
{
return 3.14*radius*radius;
}
}
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();
donut.radius = 2;
donut.name = "자바도넛";
area = donut.getArea();
System.out.println(donut.name + "의 면적은" + area);
}
}
import java.util.Scanner;
class Rectangle {
int width, height; //필드
Rectangle(){
}
public int getArea() { //메소드
return width*height;
}
}
public class Main{
public static void main(String[] args) {
Rectangle main = new Rectangle();
Scanner scanner = new Scanner(System.in);
System.out.println(">> ");
main.width = scanner.nextInt();
main.height = scanner.nextInt();
System.out.println("사각형의 면적은 " + main.getArea());
scanner.close();
}
}
*/
//생성자 : 객체 생성시 필드의 초기값 설정
/*
import java.util.Scanner;
class Rectangle {
int width, height; //필드
Rectangle() { //기본 생성자 1
width=10;
height=10;
}
Rectangle(int w, int h){ //2번 생성자
width=w;
height=h;
}
public int getArea() { //메소드
return width*height;
}
}
public class Main{
public static void main(String[] args) {
Rectangle main = new Rectangle(4,5); //2번 생성자
System.out.println("사각형의 면적은 " + main.getArea());
Rectangle b = new Rectangle(); //1번생성자
System.out.println("사각형의 면적은 " + b.getArea());
}
}
*/
/*
import java.util.Scanner;
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;
}
}
public 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);
}
}*/
/*
import java.util.Scanner;
class TV{
String a;
int year, inch;
public TV(String LG, int year, int inch)
{
a = LG;
year = 2017;
inch = 32;
}
public void show()
{
System.out.println(a + "에서 만든" + year + "년형" + inch + "인치 TV");
}
}
public class Main{
public static void main(String[] args) {
TV myTV = new TV("LG", 2017, 32);
myTV.show();
}
}
*/
/*
import java.util.Scanner;
clas+s Main{
public static void main(String[] args) {
for(;;) {
Scanner scanner = new Scanner(System.in); //스캐너 객체 생성
System.out.print("한글단어?");
String name = scanner.next();
if(name.equals("그만"))
{
break;
}
else
{
System.out.println(Dictionary.kor2eng(name));
}
}
}
static class Dictionary
{
private static String [] kor = { "사랑", "아기", "돈", "미래", "희망"};
private static String [] eng = { "love", "babe", "money", "future", "hope"};
private static String kor2eng(String word) {
for(int i=0;i<5;i++)
{
if(word.equals(kor[i]))
return kor[i]+"는 "+eng[i];
}
return word+ "는 저의 사전에 없습니다.";
};
}
}
extend 연장
상속
부모클래스 - 자식클래스
슈퍼클래스 - 서브클래스
*/
/*
class Person{
int age;
String name;
void sleep() {};
void speak() {};
}
class Student extends Person{
char grade;
}
class Teacher extends Person{
String subject;
}
class Main{
public static void main(String[] args) {
}
}
private 그 클래스 내부에서만
protected 상속받은 자식클래스만 접근 가능
public 모든 클래스에서
*/
/*
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;
public void setColor(String color) {
this.color = color;
}
public void showColorPoint() {
System.out.print(color);
System.out.println(x);
showPoint();
}
}
public 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();
}
}
class TV {
private int size;
public TV(int size) { this.size = size; }
protected int getSize() { return size; }
}
class ColorTV extends TV{
int color;
public ColorTV(int size, int color){
super(size);
this.color = color;
}
}
class Main{
public static void main(String[] args) {
ColorTV myTV = new ColorTV(32, 1024);
myTV.printproperty();
}
}
*/
/*
class Point{
private int x, y; //필드
public Point(int x, int y) { this.x = x; this.y = y; } //생성자
public Point() {this.x = 0; this.y = 0; }
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 {
public int z;
public Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
public void moveUp()
{
z++;
}
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() + "입니다.");
}
}
import java.util.*;
abstract class Calc{
int a, b;
void setValue(int a, int b) {
this.a=a; this.b=b;
}
abstract int calculate();
}
class Add extends Calc
{
int calculate() {
return a+b;
}
}
class sub extends Calc
{
int calculate() {
return a-b;
}
}
class Mul extends Calc
{
int calculate() {
return a*b;
}
}
class Div extends Calc
{
int calculate() {
return a/b;
}
}
class Main{
public static void main(String[] args) {
System.out.println("두 정수와 연산자를 입력하시오>>");
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt();
String z = scanner.next();
Calc c;
if(z.equals("+")) c = new Add();
else if(z.equals("-")) c = new sub();
else if(z.equals("/")) c = new Mul();
else c = new Div();
c.setValue(x, y);
System.out.println(c.calculate());
}
}
*/
//GUI : Graphic User Interface
/*GUI 기본틀
import javax.swing.*;
import java.awt.*;
class Main extends JFrame{
Main(){
}
public static void main(String[] args) {
new Main();
}
}
컨테이너 JFrame, JPanel
컴포넌트 JButton, JLabel, JTextField
*/
/*
import javax.swing.*;
import java.awt.*;
class Main extends JFrame{
Main(){
setTitle("title입니다");
setSize(400,200);
setVisible(true);
Container c = getContentPane(); //JFrame을 c라고 부르겠다!
c.setLayout(new FlowLayout()); //layout
c.setBackground(Color.ORANGE);
JButton b1 = new JButton("button1");
b1.setBackground(Color.red); //배경색설정
b1.setForeground(Color.white);//글자색설정
JButton b2 = new JButton("button2");
JButton b3 = new JButton("button3");
c.add(b1);
c.add(b2);
c.add(b3);
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import javax.swing.*;
import java.awt.*;
class Main extends JFrame{
Main()
{
setTitle("BorederLayout Practice");
setSize(400,200);
setVisible(true);
setLayout(new BorderLayout(5, 7));
JButton b1 = new JButton("East");
JButton b2 = new JButton("Wast");
JButton b3 = new JButton("South");
JButton b4 = new JButton("North");
JButton b5 = new JButton("Center");
add(b1,BorderLayout.EAST);
add(b2,BorderLayout.WEST);
add(b3,BorderLayout.SOUTH);
add(b4,BorderLayout.NORTH);
add(b5,BorderLayout.CENTER
);
}
public static void main(String[] args) {
new Main();
}
}
*/
import javax.swing.*;
import java.awt.*;
class Main extends JFrame{
Main()
{
setTitle("GreidLayout Sample");
setSize(400,200);
setVisible(true);
Container c = getContentPane(); //JFrame을 c라고 부르겠다!
c.setLayout(new GridLayout(4, 2));
JLabel b1 = new JLabel("이름");
JLabel b2 = new JLabel("학년");
JTextField c2 = new JTextField("");
JLabel b3 = new JLabel("학과");
JTextField c3 = new JTextField("");
JLabel b4 = new JLabel("과목");
JTextField c4 = new JTextField("");
JTextField c1 = new JTextField("");
c.add(b1);
c.add(c1);
c.add(b2);
c.add(c2);
c.add(b3);
c.add(c3);
c.add(b4);
c.add(c4);
}
public static void main(String[] args) {
new Main();
}
}