STUDY/JAVA

Java 6일차

Anne of Green Galbes 2019. 1. 24. 19:57

1. 버블정렬

package com.day06;


public class Test1 {

//Bubble Sort

public static void main(String[] args) {

int num[] = {30, 27, 20, 15, 7};

int temp;

System.out.print("Source Data:");

for(int su:num){

System.out.printf("%4d", su);

}

System.out.println();

for(int i=1;i<num.length;i++){

for(int j=0;j<num.length-i;j++){

if(num[j]>num[j+1]){

temp = num[j];

num[j] = num[j+1];

num[j+1] = temp;

}

}

}

System.out.print("Sorted Data:");

for(int i=0; i<num.length;i++){

System.out.printf("%4d", num[i]);

}

}

}

결과

Source Data:  30  27  20  15    7

Sorted Data:   7   15  20  27  30



2. 주민번호 판독

substring

str.substring(m,n) 

▶ str의 m번쨰부터 n-1번째까지


String sub = "seoul korea";

sub.substring(0, 3)    ▶seo

sub.substring(6, 8)    ko

sub.substring(6)        korea

package com.day06;


import java.util.Scanner;


public class Test2 {

public static void main(String[] args) {


//jumin : 201112-1234567

//check : 234567 892345

//sum = (8*2)+(0*3)+ +(6*5)

//n=11-sum%11

//마지막번호=n%10

Scanner sc = new Scanner(System.in);

String jumin;

int chk[] = {2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5};

int i, tot, su;

System.out.print("주민번호를 입렵해주세요[xxxxxx-xxxxxxx]:");

jumin = sc.next();

if(jumin.length()!=14){

System.out.println("입력오류!");

//System.exit(0);

sc.close();

return;

}

tot = 0;

for(i=0;i<12;i++){

if(i>=6){

tot += chk[i] * Integer.parseInt(jumin.substring((i+1),(i+2)));

}

else

tot += chk[i] * Integer.parseInt(jumin.substring((i),(i+1)));

}

su = 11 - tot%11;

su = su%10;

if(su == Integer.parseInt(jumin.substring(13))){

System.out.println("정확한 주민번호");

}

else

System.out.println("틀린 주민번호");


sc.close();

}

}

결과

201112-1234567

틀린 주민번호



3. 이차원 배열

package com.day06;


public class Test3 {

public static void main(String[] args) {

int arr[][] = new int[6][6];

int i, j;

int n=0;

for(i=0;i<5;i++){

for(j=0;j<5;j++){

n++;

arr[i][j] = n;

arr[i][5] += n;

arr[5][j] += arr[i][j];

arr[5][5] += n;

}

}

for(i=0;i<arr.length;i++){

for(j=0;j<arr.length;j++){

System.out.printf("%4d",arr[i][j]);

}

System.out.println();

}

}

}

결과

    1     2     3     4     5    15

    6     7     8     9   10    40

  11   12   13   14   15    65

  16   17   18   19   20    90

  21   22   23   24   25  115

  55   60   65   70   75  325


4. 이차원 배열

package com.day06;


public class Test4 {

public static void main(String[] args) {

int a[][] = {{3, 6, 9}, {1, 2, 3}, {7, 8, 9}};

int b[][] = {{4, 5, 7}, {1, 3, 3}, {8, 7, 8}};

int sum[][] = new int[3][3];

int i, j;

for(i=0;i<a.length;i++){

for(j=0;j<b.length;j++){

sum[i][j] = a[i][j] + b[i][j];

System.out.printf("%4d", sum[i][j]);

}

System.out.println();

}

}

}

결과

   7  11  16

   2   5   6

  15  15  17


5

① 접근지정자

public > protected > default > private 

 접근 제한

적용 대상 

접근할 수 없는 클래스 

public 

  클래스, 필드, 생성자, 메소드

 없음 

protected

  필드, 생성자, 메소드

 자식 클래스가 아닌 다른 패키지에 소속된 클래스 

default

  클래스, 필드, 생성자, 메소드

 다른 패키지에 소속된 클래스 

private

  필드, 생성자, 메소드 

 모든 외부 클래스 


② 지역변수, 전역변수

○ 전역변수(instance 변수) : 클래스내 모두 사용 가능

○ 지역변수 : 생성자 혹은 메소드 안에서만 사용 가능


Rect 클래스 

 input : 가로와 세로의 길이를 입력 받음

 area : 넓이를 구하는 메소드

 length : 둘레를 구하는 메소드

 print : 가로, 세로, 넓이, 둘레를 출력해 주는 메소드

package com.day06;


import java.util.Scanner;

public class Rect {

int w,h; //instance변수 : 클래스내 모두 사용가능

public void input(){

Scanner sc  = new Scanner(System.in);

System.out.print("가로의 길이:");

w = sc.nextInt();

System.out.print("세로의 길이:");

h = sc.nextInt();


}

public int area(){

return w*h;

}

public int length(){

return 2*(w+h);

}

public void print(int a, int l){

System.out.println("가로:" +w);

System.out.println("세로:" +h);

System.out.println("넓이:" +a);

System.out.println("둘레:" +l);

}

}

RECT 선언하여 사용

package com.day06;


public class Test5 {


public static void main(String[] args) {

Rect r1 = new Rect();

r1.input();

int a = r1.area();

int l = r1.length();

r1.print(a, l);

r1.input();

a = r1.area();

l = r1.length();

r1.print(a, l);

Rect r2 = new Rect();

r2.input();

int area = r2.area();

int length = r2.length();

r2.print(area, length);

}

}

결과

가로의 길이:10

세로의 길이:20

가로:10

세로:20

넓이:200

둘레:60


가로의 길이:20

세로의 길이:30

가로:20

세로:30

넓이:600

둘레:100


가로의 길이:30

세로의 길이:40

가로:30

세로:40

넓이:1200

둘레:140


6. 1부터 사용자가 입력하는 수까지의 합

Hap 클래스 

 ① input 메소드 : 정수값을 입력 받는 메소드

 ② cnt 메소드 : 1부터 입력받은 정수까지의 합을 돌려주는 메소드

 ③ print 메소드 : 합을 출력해 주는 메소드

Test6 클래스

  Hap 클래스를 선언 → input()실행 → 정수 입력 → sum에다가 cnt( )값 받음 → sum값 print( )를 이용하여 출력

package com.day06;


import java.util.Scanner;


class Hap{

int su, sum;

public void input(){

Scanner sc = new Scanner(System.in);

System.out.print("정수를 입력하세요:");

su = sc.nextInt();

sc.close();

}

public int cnt(){

for(int i=1;i<=su;i++)

sum+=i;

return sum;

}

public void print(int sum){

System.out.println("1~" +su+ "까지의 합:" +sum);

}

}


public class Test6 {


public static void main(String[] args) {

Hap h = new Hap();

h.input();

int sum = h.cnt();

h.print(sum);

}

}

결과

정수를 입력하세요:5

1~5까지의 합:15


7. 구구단 가로 출력

package com.day06;


public class Q2 {

public static void main(String[] args) {

int i, j;

for(int k=2;k<=9;k+=4){

for(i=1;i<=9;i++){

for(j=0;j<4;j++){

System.out.printf("%d x %d = %d \t", (k+j), i, ((k+j)*i));

}

System.out.println();

}

System.out.println();

}

}

}

결과

2 x 1 = 2         3 x 1 = 3         4 x 1 = 4         5 x 1 = 5

2 x 2 = 4         3 x 2 = 6         4 x 2 = 8         5 x 2 = 10

2 x 3 = 6         3 x 3 = 9         4 x 3 = 12 5 x 3 = 15

2 x 4 = 8         3 x 4 = 12 4 x 4 = 16 5 x 4 = 20

2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25

2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30

2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35

2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40

2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45


6 x 1 = 6         7 x 1 = 7         8 x 1 = 8         9 x 1 = 9

6 x 2 = 12 7 x 2 = 14 8 x 2 = 16 9 x 2 = 18

6 x 3 = 18 7 x 3 = 21 8 x 3 = 24 9 x 3 = 27

6 x 4 = 24 7 x 4 = 28 8 x 4 = 32 9 x 4 = 36

6 x 5 = 30 7 x 5 = 35 8 x 5 = 40 9 x 5 = 45

6 x 6 = 36 7 x 6 = 42 8 x 6 = 48 9 x 6 = 54

6 x 7 = 42 7 x 7 = 49 8 x 7 = 56 9 x 7 = 63

6 x 8 = 48 7 x 8 = 56 8 x 8 = 64 9 x 8 = 72

6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81