"κ°μ ν: λͺ
ν JAVAESSENTIAL(ν©κΈ°ν)"κ΅μ¬μ μλ‘λ λ¬Έμ λ€μ νμ΄μ μ¬λ¦½λλ€. λ¬Έμ μ λ²νΈλ§ νκΈ°νκ³ λ΅μλ§ μ λ ννλ‘ μ λ‘λ νκ³ μμ΅λλ€. κ°μΈ νμ΄μ΄λ―λ‘ μ€λ΅μ΄ μμ μ μμΌλ©° μ€λ΅ λ°κ²¬ μ λκΈ λ¨κ²¨μ£Όμλ©΄ κ°μ¬νκ² μ΅λλ€π - #μ΄ νμλ 건 μμ§ νμ§ λͺ»ν λ¬Έμ or ν·κ°λ¦¬λ λ¬Έμ μ λλ€! μΆνμ λ€μ νμ΄μ μ¬λ¦΄ μμ μ΄μμ! -ujeyhx- |
πμ€μ΅λ¬Έμ
1)
class Circle {
private int radius;
public Circle(int radius) {this.radius = radius;}
public int getRadius() { return radius;}
}
class NamedCircle extends Circle {
String name;
public NamedCircle(int radius, String name) {
super(radius);
this.name = name;
}
public void show() {
System.out.print(name + " , λ°μ§λ¦ = " + getRadius() );
}
}
public class TEST{
public static void main (String[] args) {
NamedCircle w = new NamedCircle(5, "waffle");
w.show();
}
}
2)
interface AdderInterface{
int add(int x, int y); //public abstract μλ΅μν
int add(int n);
}
class MyAdder implements AdderInterface{ //μΈν°νμ΄μ€κ΅¬ν
@Override
public int add(int x, int y) {
return x+y;
}
@Override
public int add(int n) {
int sum=0;
for (int i=1; i<=n; i++) {
sum += i;
}
return sum;
}
}
public class TEST{
public static void main (String[] args) {
MyAdder adder = new MyAdder();
System.out.println(adder.add(5,10));
System.out.println(adder.add(10));
}
}
3)
import java.util.Scanner;
abstract class Calculator{
protected int a,b;
abstract protected int calc();
protected void input() {
Scanner sc = new Scanner(System.in);
System.out.print("μ μ 2κ°λ₯Ό μ
λ ₯νμΈμ >>");
a = sc.nextInt();
b = sc.nextInt();
}
public void run() {
input();
int res = calc();
System.out.println("κ³μ°λ κ°μ " + res);
}
}
class Adder extends Calculator{
public int calc() {
return a+b;
}
}
class Subtracter extends Calculator{ //μΆμν΄λμ€ κ΅¬ννλ©΄ μΌλ°ν΄λμ€κ° λ¨
public int calc() {
return a-b;
}
}
public class TEST {
public static void main (String[] args) {
Adder adder = new Adder();
Subtracter sub = new Subtracter();
adder.run();
sub.run();
}
}
4)
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{
String color;
public ColorPoint(int x, int y, String color) {
super(x, y);
this.color = color;
}
public void setPoint(int x, int y) {
move(x,y);
}
public void setColor(String color) {
this.color = color;
}
public void show() {
System.out.print(color+"μμΌλ‘("+ getX() + "," + getY() +")");
}
}
public class TEST {
public static void main (String[] args) {
ColorPoint cp = new ColorPoint(5,5,"YELLOW");
cp.setPoint(10,20);
cp.setColor("GREEN");
cp.show();
}
}
# 5)
import java.util.Scanner;
import java.util.Stack;
interface StackInterface {
int length();
String pop();
boolean push(String ob);
}
class StringStack implements StackInterface{
int length;
Stack<String> stack = new Stack<>();
@Override
public int length() {
return this.length;
}
@Override
public String pop() {
for (int i =0; i<length(); i++) {
System.out.print(stack.pop());
}
return stack.pop();
}
@Override
public boolean push(String ob) {
if (stack.empty()) {
stack.push(ob);
}
return stack.empty();
}
}
public class TEST {
public static void main (String[] args) {
StringStack ss = new StringStack();
Scanner sc = new Scanner(System.in);
System.out.print(">>");
for (int i =0; i<5; i++) {
ss.push(sc.next());
}
}
}
import java.util.Scanner;
interface StackInterface {
int length();
String pop();
boolean push(String ob);
}
class StringStack implements StackInterface {
private String[] element;
private int index;
public StringStack(int capacity) {
element = new String[capacity];
index = 0; // μμΌλ‘ λ€μ΄κ° λ°°μ΄μ μΈλ±μ€
}
@Override
public int length() {
return index;
}
@Override
public String pop() {
if(index == 0) // μ€νμ΄ λΉμμ
return null;
index--; // μ€ν ν¬μΈν° κ°μ
return element[index];
}
@Override
public boolean push(String ob) {
if(index == element.length)
return false; // μ€νμ΄ λ€ μ°Όμ
element[index++] = ob; // μμ μ€νμ μ μ₯ ν μ€ν ν¬μΈν° μ¦κ°
return true;
}
}
public class TEST {
public static void main (String[] args) {
StringStack ss = new StringStack(10);
Scanner sc = new Scanner(System.in);
System.out.print(">>");
for (int i=0; i<5; i++) {
String text = sc.next();
ss.push(text);
}
int cnt = ss.length();
for (int i=0;i<cnt; i++)
System.out.print(ss.pop()+" ");
sc.close();
}
}
#ꡬν λ무 볡μ‘ν΄μ Έμ λ€μ μμλ³΄κ³ νμ΄μΌκ² μ
6)
import java.util.Scanner;
abstract class Shape {
public abstract void draw();
}
class Line extends Shape {
@Override
public void draw() {
System.out.print("Line");
}
}
class Rect extends Shape {
@Override
public void draw() {
System.out.print("Rect");
}
}
class Circle extends Shape {
@Override
public void draw() {
System.out.print("Circle");
}
}
public class TEST {
static void paint(Shape p) {
p.draw();
}
public static void main (String[] args) {
Line l = new Line();
int cnt = 0;
Shape[] s = new Shape[cnt];
Scanner sc = new Scanner(System.in);
while(true) {
System.out.print("μ½μ
(1),μμ (2), λͺ¨λ보기(3), μ’
λ£(4)>>");
int num = sc.nextInt();
switch(num){
case 1:
cnt++;
while(true) {
System.out.print("λν μ’
λ₯ Line(1), Rect(2), Circle(3)>>");
int num2 = sc.nextInt();
switch(num2){
case 1:
s[cnt] = l;
break;
case 2:
s[cnt] = new Rect();
break;
case 3:
s[cnt] = new Circle();
break;
default: System.out.print("μλͺ»μ
λ ₯νμ
¨μ΅λλ€.");
}
break;
}
break;
case 2:
System.out.print("μμ ν λνμ μμΉ>>");
int pos = sc.nextInt();
if (pos<s.length) { // μμΉκ°μ΄λ κ° λ£μ κ±°λ
System.out.println("μμ ν μ μμ΅λλ€.");
System.out.println();
}
else {
//μμ μ½λ
System.out.println("μμ λμμ΅λλ€.");
}
break;
case 3:
for (int i=0; i<s.length; i++) {
paint(s[i]);
System.out.println();
}
break;
case 4:
System.out.print("νλ‘κ·Έλ¨μ μ’
λ£ν©λλ€...");
break;
default: System.out.print("μλͺ»μ
λ ₯νμ
¨μ΅λλ€.");
}
}
}
}
bouns1)
interface Shape {
final double PI = 3.14;
void draw();
double getArea();
default public void redraw() {
System.out.println("--λ€μ그립λλ€---");
draw();
}
}
class Circle implements Shape{
int radius;
public Circle(int radius) {
this.radius = radius;
}
public void draw() {
System.out.print("λ°μ§λ¦" + radius);
}
public double getArea() {
return radius* radius * PI;
}
}
public class TEST {
public static void main (String[] args) {
Shape coin = new Circle(10);
coin.redraw();
System.out.println(" μ½μΈμ λ©΄μ μ " + coin.getArea());
}
}