JAVA

4일차

구자룡 2021. 3. 9. 17:52

    //다차원배열에서 데이터값을 임의로 강제로 주고자할때...(동적배열)

public class Exam20 {

    public static void main(String[] ar) {

       int[][] a=new int[3][];

       a[0]=new int[4];

       a[1]=new int[6];

       a[2]=new int[2];

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

           for(int j=0;j<a[i].length;++j) {

           System.out.println("a["+i+"]"+"["+j+"]="+a[i][j]);

           }

       }

    }

 

}

결과:

a[0][0]=0
a[0][1]=0
a[0][2]=0
a[0][3]=0
a[1][0]=0
a[1][1]=0
a[1][2]=0
a[1][3]=0
a[1][4]=0
a[1][5]=0
a[2][0]=0
a[2][1]=0


//동적배열을 임의로 주어서 처리하고자 할때..

import java.io.*;

public class Exam21 {

    public static void main(String[] ar) throws IOException {

       BufferedReader in=

              new BufferedReader(new InputStreamReader(System.in));

       int num[][] =null;

       System.out.print("몇행=");

       int x=Integer.parseInt(in.readLine());

       num=new int[x][];

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

           System.out.print(i+"번째행의열=");

           int y=Integer.parseInt(in.readLine());

           num[i]=new int[y];}

          

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

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

               System.out.println("num["+i+"]["+j+"]=");

               num[i][j]=Integer.parseInt(in.readLine());

           }

       }

       System.out.println();

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

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

           System.out.println("a["+i+"]["+j+"]="+num[i][j]);

           }

       }

       }

    }

결과:

몇행=3
0번째행의열=1
1번째행의열=2
2번째행의열=3
num[0][0]=
0
num[1][0]=
0
num[1][1]=
0
num[2][0]=
0
num[2][1]=
0
num[2][2]=
0

a[0][0]=0
a[1][0]=0
a[1][1]=0
a[2][0]=0
a[2][1]=0
a[2][2]=0


//2차원배열을 학년별로 1,2학기 성적으로 저장하고 4년간 전체평균을 구하시오

public class Exam22 {

    public static void main(String[] ar) {

       double score[][]= {{3.3,3.4},

              {3.5,3.6},

              {3.7,4.0},

              {4.1,4.2}};

    double sum=0;

    for(int year=0; year<score.length; year++)

        for(int term=0; term<score[year].length; term++)

           sum += score[year][term];

   

    int n = score.length;

    int m = score[0].length;

    System.out.println("4 전체 평점 평균은" + sum/(n*m));

   

    }

    }

결과:

4년 전체 평점 평균은3.725


//예외처리를 위한 throw 프로그램내용임.

import java.io.*;

public class Exam23 {

    public static void main(String[] ar) throws Exception {//Exceprion

       //최상위의 예외처리 클래스임.

       if(ar.length<3) {

           System.out.println("Usage:java Exam23 매개1 매개2 매개3");

           System.exit(0);//0 정상종료 1,-1 비정상종료

          

       }

       System.out.println("ar[2]"+ar[2]);

      

       BufferedReader in =

              new BufferedReader(new InputStreamReader(System.in));

       float su1=0,su2=0,tot=0;

       System.out.print("su1=");

       su1=Integer.parseInt(in.readLine());

       if(su1>100) {

           System.out.println("예외발생");

           Exception ex=new Exception(">100");

           throw ex;

       }

       System.out.print("su2=");

       su2=Integer.parseInt(in.readLine());

       tot=su1/su2;

       System.out.println("tot="+tot);

    }

 

}

결과:

ar[2]c
su1=90
su2=0
tot=Infinity


//예외처리의 try~catch~finally문에 대한 프로그램예제임.

import java.io.*;

public class Exam25 {

    public static void main(String[] ar) throws IOException{

       BufferedReader in =

              new BufferedReader(new InputStreamReader(System.in));

       int su1=0,su2=0,tot=0;

       try {System.out.print("su1=");//문자가 아니면 이것도 예외가 발생함.

       

        su1=Integer.parseInt(in.readLine());

      

      

   

    }catch(NumberFormatException e) {

       System.out.println("숫자 포멧이 잘못됬군요..");

       System.exit(0);

      

    }

 

}

}

결과:

su1=dsf
숫자 포멧이 잘못됬군요..


//예외처리를 위한 다수의 case 진행예제임.

import java.io.*;

public class Exam26 {

    public static void main(String[] ar) throws IOException{

       BufferedReader in =

              new BufferedReader (new InputStreamReader(System.in));

       int su1=0,su2=0,tot=0;

       try {

           System.out.print("su1=");

           su1=Integer.parseInt(in.readLine());

           System.out.print("su2=");

       su2=Integer.parseInt(in.readLine());//NumberFormatException

           tot=su1/su2;//su2-->0:

           System.out.println("tot="+tot);

       }catch(NumberFormatException e) {

           System.out.println("숫자포멧이 잘못되었네요.");

       }catch(ArithmeticException e) {

           System.out.println("나눗셈은 0 사용할 없어요.");

       }

    }

 

}

결과:

su1=90         
su2=30
tot=3


//try~catch~finally 따른 예외처리문예제.

public class Exam27 {

    public static void aaa( ) {

       try {

           System.out.println("A");

           int tot=10/0;//예외처리 발생.

           System.out.println("tot="+tot);

           System.out.println("처리완료");

           return;

          

       }catch(ArithmeticException e) {

           System.out.println("수는 0으로 나눌수 없습니다");

           System.out.println("aaa처리불가");

           return;

       }

       finally {

           System.out.println("aaa처리완료");

       }

    }

    public static void main(String[]ar) {

       aaa();

    }

 

}

결과:

A
수는 0으로 나눌수 없습니다
aaa처리불가
aaa처리완료


public class Exam01 {//클래스는 효율적으로 관리하기위한 다른자료형들끼리도 하나의 자료형태로

    //묶어서 관리하는 내용의 개념.

    static class AA{

       int a=100,b=200,c=300;//멤버/필드..

       void aaa() {

           System.out.println(a+":"+b+":"+c);

       }

    }

    static class BB{

       int a=500,b=1000;

    }

    public static void main(String [] ar) {

       AA ap=new AA();

       BB bp=new BB();

      

       System.out.println("ap.a="+ap.a);//100

       System.out.println("ap.b="+ap.b);//200

       System.out.println("ap.c="+ap.c);//300

       System.out.println();

       System.out.println("bp.a="+bp.a);//500

       System.out.println("bp.b="+bp.b);//1000

       ap.aaa();

    }

 

}

결과:

ap.a=100
ap.b=200
ap.c=300

bp.a=500
bp.b=1000
100:200:300


//생성자로 구성된 클래스임.

public class Exam02 {

    private int x;

    private int y;

    public Exam02() {

       System.out.println("A");

       x=10;

       y=20;

    }

    public Exam02(int a) {

       System.out.println("B");

       x=a;

       y=20;

    }

    public Exam02(int a,int b) {

       System.out.println("c");

       x=a;

       y=b;

    }

   

    void disp() {

       System.out.println("출력");

       System.out.println("x="+x);

       System.out.println("y="+y);

       System.out.println();

    }

 

}


//메인으로 작용하는 클래스임.

public class Exam03 {

    public static void main(String[] ar) {

       Exam02 ex=new Exam02();

       Exam02 ex1=new Exam02(100);

       Exam02 ex2=new Exam02(100,200);

      

       ex.disp();//10,20

       ex1.disp();//100,20

       ex2.disp();//100,200

    }

 

}

결과:

A
B
c
출력
x=10
y=20

출력
x=100
y=20

출력
x=100
y=200


public class Exam04 {

    int x,y,z,w;

   

    public Exam04() {

       x=10;

       y=20;

       z=30;

       w=40;

    }

    public Exam04(int x) {

       this();

       this.x=x;

      

    }

    public Exam04(int x,int y) {

       this(x);

       this.y=y;

      

    }

    public void disp() {

       System.out.println("x="+x);

       System.out.println("y="+y);

       System.out.println("z="+z);

       System.out.println("w="+w);

       System.out.println();

      

    }

 

}

 

public class Exam05 {

    public static void main(String[] ar) {

       Exam04 ex1=new Exam04();

       Exam04 ex2=new Exam04(100);

       Exam04 ex3=new Exam04(100,200);

      

       ex1.disp();

       ex2.disp();

       ex3.disp();

    }

 

}

결과:

x=10
y=20
z=30
w=40

x=100
y=20
z=30
w=40

x=100
y=200
z=30
w=40

'JAVA' 카테고리의 다른 글

6일차  (0) 2021.03.11
5일차  (0) 2021.03.10
3일차  (0) 2021.03.08
2일차  (0) 2021.03.05
1일차  (0) 2021.03.05