'자바'에 해당되는 글 4건
- 2008/05/24
- 2008/05/22
- 2008/05/22
-
2008/03/21
자바 예제 11.3 (1)
package com.tistory.special0strawberry;
public class Run
{
// 메인 함수
public static void main(String[] args)
{
IntArray a, b, c;
Sort s = new Sort();
Add add = new Add();
a = new IntArray();
b = new IntArray();
System.out.print("첫번째 ");
a.UserInput();
s.BubbleSort(a);
System.out.print("\n첫번째 ");
a.PrintArray();
System.out.print("\n두번째 ");
b.UserInput();
s.SelectSort(b);
System.out.print("\n두번째 ");
b.PrintArray();
c = add.ArrayAdd(a, b);
s.SelectSort(c);
System.out.print("\n세번째 ");
c.PrintArray();
}
}
package com.tistory.special0strawberry;
public class Sort
{
// 입력값 : 정렬할 IntArray 포인터
// 버블 정렬을 통해 배열을 정렬한다.
public void BubbleSort(IntArray a)
{
int temp, temp_1, temp_2;
for(temp = 0; temp < a.GetArrayLength() - 1; temp++)
{
for(temp_1 = 0; temp_1 < a.GetArrayLength() - temp - 1; temp_1++)
{
if(a.GetArrayValue(temp_1) > a.GetArrayValue(temp_1 + 1))
{
temp_2 = a.GetArrayValue(temp_1 + 1);
a.SetArrayValue(temp_1 + 1, a.GetArrayValue(temp_1));
a.SetArrayValue(temp_1, temp_2);
}
}
}
}
// 입력값 : 정렬할 IntArray 포인터
// 선택 정렬을 통해 배열을 정렬한다.
public void SelectSort(IntArray i)
{
int temp, choice, temp_1, temp_2;
for(temp = 0; temp < i.GetArrayLength(); temp++)
{
choice = temp;
for(temp_1 = temp; temp_1 < i.GetArrayLength(); temp_1++)
{
if(i.GetArrayValue(temp_1) < i.GetArrayValue(choice))
{
choice = temp_1;
}
}
temp_2 = i.GetArrayValue(choice);
i.SetArrayValue(choice, i.GetArrayValue(temp));
i.SetArrayValue(temp, temp_2);
}
}
}
| scjp 1.4 지료 (0) | 2008/07/03 |
|---|---|
| 자바 코드(병합 프로그램_선택 정렬 추가) (0) | 2008/05/24 |
| 자바 코드(병합 프로그램) (0) | 2008/05/22 |
| 자바 코드(숫자 바꾸기) (0) | 2008/05/22 |
두 개의 배열을 합하는 프로그램
사용자로부터 두 개의 배열을 입력받아 하나의 배열로 만든다.
모든 배열을 정렬되어야 한다.
public class Run
{
// 메인 함수
public static void main(String[] args)
{
IntArray a, b, c;
Sort s = new Sort();
Add add = new Add();
a = new IntArray();
b = new IntArray();
a.UserInput();
s.BubbleSort(a);
a.PrintArray();
b.UserInput();
s.BubbleSort(b);
b.PrintArray();
c = add.ArrayAdd(a, b);
s.BubbleSort(c);
c.PrintArray();
}
}
import java.util.*;
public class IntArray
{
int[] number;
Scanner scan;
// 생성자
// 키보드 입력을 위해 Scanner 객체 생성
public IntArray()
{
scan = new Scanner(System.in);
}
// 반환값 : 배열의 크기
// 배열의 크기를 반환한다.
public int GetArrayLength()
{
return number.length;
}
// 입력값 : 배열위치
// 반환값 : 배열위치에 저장된 정수값
// 지정 위치에 저장된 값을 반환한다
public int GetArrayValue(int position)
{
return number[position];
}
// 입력값 : 배열의 크기
// 배열을 입력값에 맞는 크기로 설정한다.
public void SetArrayLength(int length)
{
number = new int[length];
}
// 입력값 : 배열의 위치, 저장될 값
// 배열의 위치에 저장될 값을 저장한다.
public void SetArrayValue(int position, int value)
{
number[position] = value;
}
// 사용자로 부터 배열의 크기와 각 배열의 값을 받아온다.
public void UserInput()
{
int temp, temp_1;
System.out.print("배열의 크기를 입력하세요 : ");
temp = scan.nextInt();
this.SetArrayLength(temp);
for(temp_1 = 0; temp_1 < this.GetArrayLength(); temp_1++)
{
System.out.print(temp_1 + "번째 값을 입력하세요 : ");
temp = scan.nextInt();
this.SetArrayValue(temp_1, temp);
}
}
// 배열에 저장된 값을 출력한다
public void PrintArray()
{
int temp;
System.out.println("저장된 배열의 값");
for(temp = 0; temp < this.GetArrayLength(); temp++)
{
System.out.println((temp + 1) + "번째 값 : " + this.GetArrayValue(temp));
}
}
}
public class Sort
{
// 입력값 : 정렬할 IntArray 포인터
// 버블 정렬을 통해 배열을 정렬한다.
public void BubbleSort(IntArray a)
{
int temp, temp_1, temp_2;
for(temp = 0; temp < a.GetArrayLength() - 1; temp++)
{
for(temp_1 = 0; temp_1 < a.GetArrayLength() - temp - 1; temp_1++)
{
if(a.GetArrayValue(temp_1) > a.GetArrayValue(temp_1 + 1))
{
temp_2 = a.GetArrayValue(temp_1 + 1);
a.SetArrayValue(temp_1 + 1, a.GetArrayValue(temp_1));
a.SetArrayValue(temp_1, temp_2);
}
}
}
}
}
public class Add
{
// 입력값 : 합칠 IntArray 포인터 2개
// 반환값 : 합쳐진 IntArray 포인터
// 두개의 IntArray를 합친다.
public IntArray ArrayAdd(IntArray a, IntArray b)
{
IntArray result = new IntArray();
int temp;
result.SetArrayLength(a.GetArrayLength() + b.GetArrayLength());
for(temp = 0; temp < a.GetArrayLength(); temp++)
{
result.SetArrayValue(temp, a.GetArrayValue(temp));
}
for(temp = 0; temp < b.GetArrayLength(); temp++)
{
result.SetArrayValue(temp + a.GetArrayLength(), b.GetArrayValue(temp));
}
return result;
}
}
| scjp 1.4 지료 (0) | 2008/07/03 |
|---|---|
| 자바 코드(병합 프로그램_선택 정렬 추가) (0) | 2008/05/24 |
| 자바 코드(병합 프로그램) (0) | 2008/05/22 |
| 자바 코드(숫자 바꾸기) (0) | 2008/05/22 |
// 숫자를 입력받아 반대로 출력하는 프로그램
// 예 : 123456 -> 654321
package com.tistory.special0strawberry;
import java.util.*;
public class Integer_Print
{
private int x;
private int division;
private int[] result;
private Scanner scan;
// 생성자
// 내용 : 변수의 초기화
public Integer_Print()
{
setX(0);
setDivision(1);
scan = new Scanner(System.in);
}
// 입력값 : 배열의 크기
// 내용 : result 배열의 크기를 정한다.
public void Set_Result_Length(int s)
{
result = new int[s];
}
// 입력값 : 배열 위치의 값, 배열의 위치
// 내용 : 배열의 위치에 맞는 값을 넣는다.
public void Set_Result_Position(int n, int p)
{
result[n] = p;
}
// 반환값 : 배열의 크기
// 내용 : 배열의 크기를 반환한다.
public int Get_Result_Length()
{
return result.length;
}
// 입력값 : 배열의 위치
// 반환값 : result 배열의 위치의 값
// 내용 : 배열 중에서 위치에 맞는 값을 반환한다.
public int Get_Result_Position(int p)
{
return result[p];
}
// 입력값 : 정수형 변수 i
// 내용 : x의 값을 i로 정한다.
public void setX(int i)
{
x = i;
}
// 반환값 : 정수형 값
// 내용 : x의 값을 반환한다.
// 반환값 : x의 값
// 내용 : x의 현재값을 반환한다.
public int getX()
{
return x;
}
// 입력값 : 정수형 변수 d
// 내용 : division의 값을 d로 정한다.
// 입력값 : 정수값
// 내용 : division의 값을 정수값으로 정한다.
public void setDivision(int d)
{
division = d;
}
// 반환값 : 정수형 값
// 내용 : division의 값을 반환한다.
// 반환값 : division의 값
// 내용 : division의 값을 반환한다.
public int getDivision()
{
return division;
}
// 내용 : 정수 값을 키보드로 부터 받아온다.
public int Scan()
{
int result;
System.out.print("정수의 값을 입력하세요 : ");
result = scan.nextInt();
return result;
}
// 내용 : division의 값을 계산한다.
public void Cal_Division()
{
int temp;
int temp_1;
temp = getX();
temp_1 = getDivision();
while(true)
{
if((temp / temp_1) == 0)
{
break;
}
else
{
temp_1 *= 10;
}
}
setDivision(temp_1);
}
// 내용 : 값이 들어갈 배열의 크기를 계산한다.
public void Cal_Result_Length()
{
Set_Result_Length((int)Math.log10(getDivision()) + 1);
}
// 내용 : 값을 배열에 집어 넣는다.
public void Input_Result()
{
int temp, x_value, input_value, number, cal_number;
x_value = getX();
number = 0;
cal_number = 0;
for(temp = Get_Result_Length() - 1; temp > 0; temp--)
{
// 배열에 들어갈 값 계산
input_value = x_value % 10;
x_value /= 10;
// 정수값 계산
cal_number *= 10;
cal_number += input_value;
Set_Result_Position(number, input_value);
Set_Result_Position(this.Get_Result_Length() - 1, cal_number);
number++;
}
}
// 내용 : result배열의 값을 화면에 출력한다.
public void Print_Result()
{
int length;
System.out.println("number : " + this.getX());
System.out.println("result : " + this.Get_Result_Position(this.Get_Result_Length() -1));
for(length = 0; length < this.Get_Result_Length() - 1; length++)
{
System.out.println(length + " : " + this.Get_Result_Position(length));
}
}
// 내용 : 프로그램을 시작한다.
public void Start()
{
setX(Scan());
Cal_Division();
Cal_Result_Length();
Input_Result();
Print_Result();
}
}
| scjp 1.4 지료 (0) | 2008/07/03 |
|---|---|
| 자바 코드(병합 프로그램_선택 정렬 추가) (0) | 2008/05/24 |
| 자바 코드(병합 프로그램) (0) | 2008/05/22 |
| 자바 코드(숫자 바꾸기) (0) | 2008/05/22 |
// 실습 11.3: Taegukgi.java
import java.applet.Applet;
import java.awt.*;
public class Taegukgi2 extends Applet
{
//태극기를 그린다.
public void paint (Graphics page)
{
//태극마크를 그린다.
page.setColor(Color.red);
page.fillArc(120,60,120,120,0,180);
page.setColor(Color.blue);
page.fillArc(120,60,120,120,180,180);
page.setColor(Color.red);
page.fillArc(120,90,60,60,180,180);
page.setColor(Color.blue);
page.fillArc(180,90,60,60,0,180);
//건곤감리를 그린다.
page.setColor(Color.black);
int x1[]={54,64,99,89};
int y1[]={72,79,28,21};
page.fillPolygon(x1,y1,4);
int x2[]={67,77,112,102};
int y2[]={81,88,37,30};
page.fillPolygon(x2,y2,4);
int x3[]={80,90,125,115};
int y3[]={90,97,46,39};
page.fillPolygon(x3,y3,4);
int x4_1[]={235,245,261,251};
int y4_1[]={46,39,63,70};
page.fillPolygon(x4_1,y4_1,4);
int x4_2[]={254,264,280,270};
int y4_2[]={74,67,91,98};
page.fillPolygon(x4_2,y4_2,4);
int x5[]={248,258,293,283};
int y5[]={37,30,81,88};
page.fillPolygon(x5,y5,4);
int x6_1[]={261,271,287,277};
int y6_1[]={28,21,45,52};
page.fillPolygon(x6_1,y6_1,4);
int x6_2[]={280,290,306,296};
int y6_2[]={56,49,73,80};
page.fillPolygon(x6_2,y6_2,4);
int x7[]={54,64,99,89};
int y7[]={168,161,212,219};
page.fillPolygon(x7,y7,4);
int x8_1[]={67,77,93,83};
int y8_1[]={159,152,176,183};
page.fillPolygon(x8_1,y8_1,4);
int x8_2[]={86,96,112,102};
int y8_2[]={186,179,203,210};
page.fillPolygon(x8_2,y8_2,4);
int x9[]={80,90,125,115};
int y9[]={150,143,194,201};
page.fillPolygon(x9,y9,4);
int x10_1[]={235,245,261,251};
int y10_1[]={194,201,177,170};
page.fillPolygon(x10_1,y10_1,4);
int x10_2[]={254,264,280,270};
int y10_2[]={167,174,150,143};
page.fillPolygon(x10_2,y10_2,4);
int x11_1[]={248,258,274,264};
int y11_1[]={203,210,186,179};
page.fillPolygon(x11_1,y11_1,4);
int x11_2[]={267,277,293,283};
int y11_2[]={176,183,159,152};
page.fillPolygon(x11_2,y11_2,4);
int x12_1[]={261,271,287,277};
int y12_1[]={212,219,195,188};
page.fillPolygon(x12_1,y12_1,4);
int x12_2[]={280,290,306,296};
int y12_2[]={185,192,168,161};
page.fillPolygon(x12_2,y12_2,4);
}
}
| 자바 예제 11.3 (1) | 2008/03/21 |
|---|---|
| 소프트웨어사업대가의기준_고시전문(0711월) (0) | 2008/03/12 |
| 간단한 이진 계산 (0) | 2008/01/28 |
| 함수 이용시 변수 및 함수가 보이지 않을 때 (0) | 2008/01/28 |
| 2008년 정보처리기사 시험일정 (0) | 2008/01/21 |
| 네이트온 파일방 설정 방법 (0) | 2008/01/10 |