C언어의 printf 함수와 동일한 기능을 하는 printf 메소드 사용%d 구문을 사용하면 글자 간격을 설정할 수 있다. public class gugudan { public static void main(String[] args) {// TODO Auto-generated method stubint result; for (int i = 1; i < 10; i++) {for (int j = 2; j < 10; j++) {result = j * i;System.out.printf("%d*%d = %2d ",j,i,result);}System.out.println();}}}
2~n까지의 수 중에서 소수를 판별하고 개수를 출력한다.for1 : 1은 제외하고 2부터 n까지 반복for2 : 2부터 i까지 모듈러 연산을 통해 소수인지 판별안쪽 반복문 종료 후 break 문으로인한 종료가 아니라 정상 종료라면 소수 public class Prime { public static void main(String[] args) {// TODO Auto-generated method stub int prime = 0;int n = 1000, j; //n은 1000으로 설정 for (int i = 2; i < n; i++) { for (j = 2; j < i; j++) {if (i % j == 0) {break;}}if (i == j) {prime++;System.out.println(i);}..
윈도우 띄우고 그림 그리기 import java.awt.Graphics; import javax.swing.JFrame; public class GraphicTest extends JFrame{ public GraphicTest(String str){super(str);setSize(500,500); //창 크기setLocation(300,300); //창 위치setVisible(true); //창 보이기}public void paint(Graphics g){g.drawLine(10, 10, 190, 190); //직선g.drawRect(10,10,100,100); //사각형g.drawOval(50,50,100,100); //원g.drawArc(100, 100, 80, 80, 0, 90); //호}pub..
재귀함수 이용 import java.util.Scanner; public class FiboTest { public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("피보나치 위치 입력 : ");Scanner scan = new Scanner(System.in);long n = scan.nextInt();System.out.println("fibo[" + n + "] = " + fibo(n));} static long fibo(long n) {// TODO Auto-generated method stubif (n == 1 || n == 2)return 1;elsereturn fibo(n - 1) + ..
class Vehicle {int speed; void drive() {};void stop() {};} public class AirPlane extends Vehicle {int height; void takeOff() {};void landing() {}; public static void main(String arg[]) {AirPlane ap1 = new Vehicle(); //에러 부모 -> 자식간 인스턴스 할당 불가, 형변환 생략불가AirPlane ap2 = (AirPlane) new Vehicle(); // 강제 형변환 Vehicle v1 = new AirPlane(); //정상적인 코드, 형변환 생략가능Vehicle v2 = (Vehicle) new AirPlane(); // 강제 형변환..
package afternoon; public class Factorial { static int num = 5;public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("factorial "+num+" = "+factorial(num));} private static long factorial(int i) {// TODO Auto-generated method stubif(i==1)return 1;elsereturn i * factorial(i-1);}}
클래스 변수는 해당 클래스가 공통으로 사용하는 변수를 말하며 인스턴스 변수에 static 예약어를 사용한다. //클래스 변수를 이용한 제목을 지정하지 않은 문서의 번호 매기기package afternoon; public class DocumentTest { public static void main(String[] args) {// TODO Auto-generated method stubDocument dc = new Document();Document dd = new Document("Class Define");Document de = new Document();Document df = new Document("Serial Test");Document dg = new Document();Document..
일반상속과는 다르게 인터페이스는 다중상속이 허용된다 package afternoon; public class InterfaceInheritance { public static void main(String arg[]){C obj = new C();obj.method1();obj.method2();obj.method3();obj.method4();}}interface C1{void method1();void method2();}interface C2{void method3();}interface C3 extends C1,C2{void method4();}class C implements C3{ @Overridepublic void method1() {// TODO Auto-generated method s..