hpc & ot lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 ·...

24
HPC & OT Lab. 1

Upload: others

Post on 13-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

HPC & OT Lab. 1

Page 2: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

HPC & OT Lab. 2

Page 3: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

실습 7주차실습 7 주차

Jin-Ho, JangM.S. Hanyang Univ.M.S. Hanyang Univ.

HPC&OT Lab.ji h @ [email protected]

3HPC & OT Lab.

Page 4: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

Component StructureComponent Structure

객체(object)생성개념을이해한다객체(object) 생성개념을이해한다.외부클래스에대한접근방법을이해한다.접근제어자( bli & i t )를이해한다접근제어자(public & private)를이해한다.

조건문과제어문을이해한다.

Page 5: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

기초실습기초실습 1• CalMain class에서외부에있는 Calculation class의객체를생성하여사칙연산을수행하는프로그램을작성한다.– 실행결과

15 + 3 1815 + 3 = 1815 - 3 = 1215 * 3 = 4515 / 3 = 5

#6 - 5

Page 6: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

• CalMain.java

public class CalMain{{

public static void main(String[] args){

int a=15;int b=3;;Calculation cc = new Calculation(a, b);System.out.println(a + " + " + b + " = " + cc.sum());System.out.println(a + " - " + b + " = " + cc.sub());System.out.println(a + " * " + b + " = " + cc.multi());y p ( ())System.out.println(a + " / " + b + " = "

+ cc.PrivateAccess());}

}}

#6 - 6

Page 7: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

• Calculation java• Calculation.java

bli l C l l i bli i b()public class Calculation{

private int a;private int b;

bli C l l i (i i )

public int sub(){

return a-b;}

bli i l i()public Calculation(int x, int y){

a=x;b=y;

}

public int multi(){

return a*b;}

i i di id ()}public int sum(){

return a+b;}

private int divide(){

return a/b;}

bli i P i A ()} public int PrivateAccess(){

return divide();}

}}

#6 - 7

Page 8: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

기초실습 2기초실습 2• 다음은 DisplayMain class에서외부에있는 DisplayClass

class의객체를생성하여, DisplayClass class를사용하는프로그램을작성한다.– 실행결과

+----------------+|Have a nice day!||Have a nice day!|

+------------+|I Love Java!||I Love Java!||Have a nice day!|

|Have a nice day!||Have a nice day!||Have a nice day!|+ +

|I Love Java!||I Love Java!||I Love Java!||I Love Java!|+------------++----------------+ +------------+

#6 - 8

Page 9: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

• DisplayMain.java

public class DisplayMain{

public static void main (String args[]){DisplayClass d1 = new DisplayClass("Have a nice day!");DisplayClass d2 = new DisplayClass("I Love Java!");d1.display();d2.display();

}}

#6 - 9

Page 10: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

• DisplayClass.javapublic class DisplayClass{

System.out.println("+");}

String string;int length;public DisplayClass(String string)

public void print(){

System.out.println{

this.string=string;this.length=string.length();

}

("|"+string+"|");}public void close(){}

public void open(){

System out print("+");

{open();

}public void display()System.out.print("+");

for(int i=0; i<length;i++){System.out.print("-");

}

public void display(){

open();for(int i=0;i<5;i++){} for(int i=0;i<5;i++){

print();}close();

#6 - 10

close();}

}

Page 11: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

응용실습응용실습 1

• 외부클래스를사용하여 2부터입력한수까지의소수를구하는프로그램을작성하시오 또입력한수까지를구하는프로그램을작성하시오. 또입력한수까지의소수가모두몇개인지나타내시오.

– 실행결과

#6 - 11

Page 12: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

응용실습응용실습 2외부클래스를사용하여다음과같이두수를입력하• 외부클래스를사용하여다음과같이두수를입력하여최대공약수와최소공배수를구하는프로그램을

작성하시오작성하시오.– 실행결과

#6 - 12

Page 13: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

응용실습응용실습 3

• 다음과같이입력한숫자의 2의배수의개수, 3의배수의개수 2와 3의배수의개수를구하여라의개수, 2와 3의배수의개수를구하여라.

실행결과– 실행결과

#6 - 13

Page 14: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

Control Structure(1)

if-else문을이해하고활용한다.을이해하 활용한다for문을이해하고활용한다.

Page 15: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

기초실습기초실습 1• 숫자세개를입력받아, 그중에가장큰수를출력해주는프로그램을작성한다.– 실행결과

#7 - 15

Page 16: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

• Compare.java

import javax.swing.*;public class Compare{public static void main(String args[]){public static void main(String args[]){

int max;int a = Integer.parseInt(JOptionPane.showInputDialog("첫번째수"));int b = Integer.parseInt(JOptionPane.showInputDialog("두번째수"));int c = Integer parseInt(JOptionPane showInputDialog("세번째수"));int c Integer.parseInt(JOptionPane.showInputDialog( 세번째수 ));

if(a < b){max = b;

}}else{

max = a;}

#7 - 16

Page 17: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

• Compare javaCompare.java

if(max < c){max = c;

}else{

max = max;}JOptionPane.showMessageDialog(null,"가장큰수는 : " + max);

}}

#7 - 17

Page 18: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

기초실습기초실습 2• 1에서부터입력한정수까지홀수들의합을구하는프로그램을작성하시오.

– 실행결과

#7 - 18

Page 19: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

• OddSum.java

import javax.swing.*;public class OddSum{

public static void main(String args[]){int value = 0;int input = Integer.parseInt(JOptionPane.showInputDialog(

"정수를입력하세요."));for(int i = 0; i<= input; i = i + 1){

if(i % 2 != 0){value=value+i;

}}JOptionPane.showMessageDialog(null, input +

"까지홀수의합은" + value);System.exit(0);

#7 - 19

Page 20: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

응용실습응용실습 1

• 정수세개를입력으로받아, 가장큰수의순서로출력해주는프로그램을작성하시오력해주는프로그램을작성하시오.– 실행결과

#7 - 20

Page 21: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

응용실습응용실습 2

• 입력된값 (2~9)에따라해당값의구구단을출력하는프로그램을완성하시오프로그램을완성하시오.– 단, ‘q’를입력할때까지위의과정을반복한다.실행결과– 실행결과

#7 - 21

Page 22: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

응용실습응용실습 3

• 다음과같이 00부터 10까지를 loop문을사용하여나타내어라사용하여나타내어라.

– 실행결과

#7 - 22

Page 23: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

응용실습응용실습 4• 다이얼로그를통해서한숫자를입력받은후그숫자• 다이얼로그를통해서한숫자를입력받은후그숫자가짝수이면그숫자의구구단을출력하고그숫자가홀수이면그수의승만큼을출력해준다.홀수이면 수의승만큼을출력해준다.– 실행결과

#7 - 23

Page 24: HPC & OT Lab. 1cfs6.tistory.com/upload_control/download.blog?fhandle=... · 2015-01-22 · Component StructureComponent Structure 객체(object) 생성개념을이해한다. 외부클래스에대한접근방법을이해한다

응용실습응용실습 5

• 아래와같은조건을만족하는프로그램을작성하시오. JOptionPane의입력다이얼로그를통해서 1에서 5– JOptionPane의입력다이얼로그를통해서 1에서 5까지의정수를입력받는다

– 1:white 2:black 3:red 4:blue 5:green에해당되는1:white, 2:black, 3:red, 4:blue, 5:green 에해당되는프레임의백그라운드를채운다.

– 실행결과실행결과

#7 - 24