/*
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 + "입니다.");
}
}
*/