codesign --force --deep --sign - /Applications/Eclipse.app/Contents/MacOS/eclipse
eclipse > settings > java > editor > templates > new
package array;
public class ArrayTest1 {
public static void main(String[] args) {
// int[] n = new int[3];
// n[0] = 100;
// n[1] = 200;
// n[2] = 300;
int[] n = {100, 200, 300};
for(int i=0; i<n.length; i++) {
System.out.println(n[i]);
}
}
}
package array;
public class ArrayTest2 {
public static void main(String[] args) {
int[][] score = {
{100, 90, 80, 70},
{90, 80, 70, 60},
{80, 70, 60, 50}
};
System.out.println("국어\t영어\t수학\t철학\t총점\t평균");
// for(int row=0; row < score.length; row++) {
// int sum = 0;
// int avg = 0;
// for(int col=0; col < score[row].length; col++) {
// System.out.print(score[row][col]+"\t");
// sum += score[row][col];
// }
// avg = sum / score[row].length;
// System.out.print(sum+"\t");
// System.out.print(avg);
//
// System.out.print("\n");
// }
for(int[] row:score) {
int sum = 0;
int avg = 0;
for(int col:row) {
System.out.print(col+"\t");
sum += col;
}
avg = sum / row.length;
System.out.print(sum+"\t");
System.out.print(avg);
System.out.print("\n");
}
System.out.println("종료");
}
}
package array;
import java.util.Arrays;
public class MethodsOfString_1 {
public static void main(String[] args) {
// 문자열 길이
String str1 = "Hello Java!";
String str2 = "안녕하세요! 반갑습니다.";
System.out.println(str1.length());
System.out.println(str2.length());
System.out.println();
// 문자열 검색
// @charAt()
System.out.println(str1.charAt(1));
System.out.println(str2.charAt(1));
System.out.println();
// @indexOf(), lastIndexOf()
System.out.println(str1.indexOf('a'));
System.out.println(str1.lastIndexOf('a'));
System.out.println(str1.indexOf('a', 8));
System.out.println(str1.lastIndexOf('a', 8));
System.out.println(str1.indexOf("Java"));
System.out.println(str1.lastIndexOf("Java"));
System.out.println(str1.indexOf("하세"));
System.out.println(str1.lastIndexOf("하세"));
System.out.println(str1.indexOf("Bye"));
System.out.println(str1.lastIndexOf("고맙습니다."));
System.out.println();
// 문자열 변환 및 연결
// @String.valueOf(기본 자료형): 기본 자료형 -> 문자열 변환
String str3 = String.valueOf(2.3);
String str4 = String.valueOf(false);
System.out.println(str3);
System.out.println(str4);
// @concat(): 문자열 연결
String str5 = str3.concat(str4);
System.out.println(str5);
// String.valueOf() + concat()
String str6 = "안녕" + 3;
String str7 = "안녕".concat(String.valueOf(3));
// 문자열 byte[] 또는 char[]로 변
String str8 = "Hello Java!";
String str9 = "안녕하세요";
// @getBytes(): 문자열 -> byte[] 변
byte[] array1 = str8.getBytes();
byte[] array2 = str9.getBytes();
System.out.println(Arrays.toString(array1));
System.out.println(Arrays.toString(array2));
// @ toCharArray(): 문자열 -> char[] 변환
char[] array3 = str8.toCharArray();
char[] array4 = str9.toCharArray();
System.out.println(Arrays.toString(array3));
System.out.println(Arrays.toString(array4));
}
}
package array;
public class PlusOperationOfString {
public static void main(String[] args) {
String str1 = "안녕" + "하세요" + "!";
System.out.println(str1);
String str2 = "반갑";
str2 += "습니다";
str2 += "!";
System.out.println(str2);
System.out.println();
// 문자열 + 기본 자료형 또는 기본 자료형 + 문자열
String str3 = "안녕" + 1;
String str4 = "안녕" + String.valueOf(1);
String str5 = "안녕" + "1";
System.out.println(str3);
System.out.println(str4);
System.out.println(str5);
System.out.println();
// 문자열과 기본 자료형 혼용
System.out.println(1 + "안녕");
System.out.println(1 + "안녕" + 2);
System.out.println("안녕" + 1 + 2);
System.out.println(1 + 2 + "안녕");
}
}
package array;
public class StringTest1 {
public static void main(String[] args) {
String str = "안녕하세요";
System.out.println(str);
StringBuffer sb = new StringBuffer("실수란 ");
System.out.println(sb.length() + ":" + sb.capacity());
sb.append("신을 용서하는 ");
System.out.println(sb.length() + ":" + sb.capacity());
sb.append("인간의 행위이다");
System.out.println(sb.length() + ":" + sb.capacity());
sb.append("~~");
System.out.println(sb.length() + ":" + sb.capacity());
}
}
public static int sum(int a, int b){
// 메서드 내용
}
package method;
public class MethodTest1 {
public static void main(String[] args) {
print();
print();
welcome("김주력");
welcome("송원선");
System.out.println("종료");
}
private static void welcome(String name) {
System.out.println("------------------------");
System.out.println("안녕하세요~ " + name + "님~");
System.out.println("------------------------");
return;
}
private static void print() {
System.out.println("----------");
System.out.println("최정윤");
System.out.println("----------");
return;
}
}
package method;
import java.util.Scanner;
public class MethodTest2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("숫자 2개를 입력 > ");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + "+" + b + "=" + add(a, b));
System.out.println("주사위 값은" + dice());
}
private static int dice() {
return (int)(Math.random() * 6)+1;
}
private static int add(int a, int b) {
return a + b;
}
}
package method;
public class MethodTest3 {
public static void main(String[] args) {
print(1);
print(2,3);
print(4,5,6);
}
private static void print(int... i) { // since jdk 5
// for(int j=0; j<i.length; j++) {
// System.out.println(i[j] + "\t");
// }
// System.out.println();
for(int j : i) {
System.out.print(j + "\t");
}
System.out.println();
}
// private static void print(int i, int j, int k) {
// System.out.println(i + "," + j + "," + k);
// }
//
// private static void print(int i, int j) {
// System.out.println(i + "," + j);
// }
//
// private static void print(int i) {
// System.out.println(i);
// }
}
package method;
public class MethodTest4 {
public static void main(String[] args) {
for(int i = 0; i < args.length; i++) {
System.out.println("args[" + i + "] : " + args[i]);
}
System.out.println("종료");
}
}
package oop1;
public class PointTest {
public static void main(String[] args) {
Point3D pt = new Point3D();
pt.setX(100);
pt.setY(200);
pt.setZ(300);
pt.print();
// System.out.println("x = " + pt.getX());
// System.out.println("y = " + pt.getY());
// System.out.println("z = " + pt.getZ());
}
}
package oop1;
public class Point2D {
private int x;
private int y;
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
public void print() {
System.out.println("x = " + this.getX());
System.out.println("y = " + this.getY());
// System.out.println("z = " + this.getZ());
}
}
package oop1;
public class Point3D extends Point2D {
private int z;
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
// 재정의 과
@Override
public void print() {
super.print();
System.out.println("z = " + this.getZ());
}
}
package oop2;
public class PointTest {
public static void main(String[] args) {
Point2D pt = new Point3D(); // upcasting : 자동형변환(묵시적형변환)
pt.x = 100;
pt.y = 200;
// pt.z = 300;
Point3D pt2 = (Point3D) pt; // downcasting : 강제형변환(명시적형변환)
System.out.println(pt2.x);
System.out.println(pt2.y);
System.out.println(pt2.z);
}
}
package oop2;
public class Point2D {
int x;
int y;
}
package oop2;
public class Point3D extends Point2D {
int z;
}
package oop3;
public class AnimalTest {
public static void main(String[] args) {
Animal a = new Condor();
System.out.println(a);
Bird b = (Bird) a;
System.out.println(b);
Condor c = null;
if(b instanceof Condor) {
c = (Condor) b;
System.out.println(c);
} else {
System.out.println("downcasting 불가");
}
// 원래 실제보다 다운캐스팅하면 안됨
if (c instanceof BlackVulture) {
BlackVulture bv = (BlackVulture) c;
System.out.println(bv);
} else {
System.out.println("downcasting 불가");
}
// Cat c = new Cat();
// System.out.println(c);
//
// Animal a = c;
// System.out.println(a);
//
// if (a instanceof Bird) {
// Bird b = (Bird)a;
// System.out.println(b);
// } else {
// System.out.println("downcasting 불가");
// }
}
}
package oop3;
public class Animal {
public String toString() {
return "Animal 입니다.";
}
}
package oop3;
public class Cat extends Animal{
public String toString() {
return " Cat 입니다";
}
}
package oop3;
public class Bird extends Animal {
@Override
public String toString() {
return " Bird 입니다";
}
}
package oop3;
public class Condor extends Bird {
public String toString() {
return " Condor 입니다";
}
}
package oop3;
public class BlackVulture extends Condor{
public String toString() {
return " BlackVulture 입니다";
}
}
package oop4;
public class ShapeTest {
public static void main(String[] args) {
// Circle c = new Circle();
// printArea(c);
//
// Rectangle r = new Rectangle();
// printArea(r);
//
// Triangle t = new Triangle();
// printArea(t);
printArea(new Circle());
printArea(new Rectangle());
printArea(new Triangle());
}
private static void printArea(Shape s) {
s.area();
if(s instanceof Circle) {
Circle c = (Circle)s;
System.out.println("반지름이 " + c.r + "인 원의 넓이는 " + c.res + "입니다.");
} else if (s instanceof Rectangle) {
Rectangle r = (Rectangle)s;
System.out.println("너비가 " + r.w + "이고 " + "높이가 " + r.h + "인 사각형의 넓이는 "+ r.res + "입니다.");
} else if (s instanceof Triangle) {
Triangle t = (Triangle)s;
System.out.println("너비가 " + t.w + "이고 " + "높이가 " + t.h + "인 사각형의 넓이는 "+ t.res + "입니다.");
}
}
// private static void printArea(Triangle t) {
// t.area();
// System.out.println(t.res);
// }
//
// private static void printArea(Rectangle r) {
// r.area();
// System.out.println(r.res);
// }
//
// private static void printArea(Circle c) {
// c.area();
// System.out.println(c.res);
// }
}
package oop4;
public class Shape {
double res;
void area() {
}
}
package oop4;
public class Circle extends Shape{
int r = 10;
public void area() {
res = r * r * 3.14;
}
}
package oop4;
public class Rectangle extends Shape{
int w = 10;
int h = 5;
public void area() {
res = w * h;
}
}
package oop4;
public class Triangle extends Shape{
int w = 10;
int h = 5;
public void area() {
res = w * h / 2;
}
}
[참고링크]