특별한딸기이야기

자바 코드(병합 프로그램) 본문

딸기 공부방/JAVA

자바 코드(병합 프로그램)

특별한녀석 2008. 5. 22. 23:35

두 개의 배열을 합하는 프로그램

사용자로부터 두 개의 배열을 입력받아 하나의 배열로 만든다.

모든 배열을 정렬되어야 한다.

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;
 }
}

'딸기 공부방 > JAVA' 카테고리의 다른 글

양력을 음력으로 바꾸는 소스  (0) 2009.04.28
scjp 1.4 지료  (0) 2008.07.03
자바 코드(병합 프로그램_선택 정렬 추가)  (0) 2008.05.24
자바 코드(숫자 바꾸기)  (0) 2008.05.22