독도 광고 모금 캠페인

특별한딸기이야기

블로그 이미지
딸기다운 사이버 공간을 만들고 싶어하는 특별한 딸기의 블로그입니다.
by 특별한녀석
  • 9,675Total hit
  • 4Today hit
  • 25Yesterday hit

최근에 달린 레몬펜 쪽지

Statistics Graph

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

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

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

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' 카테고리의 다른 글

scjp 1.4 지료  (0) 2008/07/03
자바 코드(병합 프로그램_선택 정렬 추가)  (0) 2008/05/24
자바 코드(병합 프로그램)  (0) 2008/05/22
자바 코드(숫자 바꾸기)  (0) 2008/05/22
TRACKBACK 1 AND COMMENT 0

ARTICLE CATEGORY

딸기 이야기 (254)
딸기의 혼잣말 (55)
딸기 호감 사이트 (2)
딸기의 사진 (16)
딸기 리뷰 (15)
딸기 공부방 (113)
딸기 자료실 (52)

CALENDAR

«   2008/10   »
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

ARCHIVE