본문 바로가기

프로그램언어/자바

컬렉션 프레임워크 문제

1.  arr[] = {10, 20, 30, 40, 50, 60, 70, 40, 30, 20} 배열에 중복된 데이터를 제거하고 아래의 그림과 같이 출력하는 프로그램을 작성하시오. (데이터출력시 순서는 상관없음)

 

출력예]

 

더보기

import java.util.HashSet;

import java.util.Set;

 

public class ex01 {

 

    public static void main(String[] args) {

        int[] arr = {10, 20, 30, 40, 50, 60, 70, 40, 30, 20};

        int i;

        Set<Integer> set = new HashSet<Integer>();

 

        System.out.println("데이터" + "\t" + "set삽입");

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

            System.out.println(arr[i] + "\t" + (set.add(arr[i])?"true":"중복데이터"));

        }

 

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

    } 

}

 

 

2. Set 계열의 클래스를 이용하여 로또번호를 렌덤함수를 이용하여 생성하고  List계열의 클래스를 이용하여 정렬하여 출력하는 로또구매 프로그램을 작성하시오.

 

 출력예]

 

더보기

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashSet;

import java.util.Scanner;

import java.util.Set;

 

public class LottoStore {

    Scanner scan = new Scanner(System.in);

 

    public void Store_Menu(){

        System.out.println("Lotto 프로그램");

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

        System.out.println("1. Lotto 구입");

        System.out.println("2. 프로그램 종료");

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

    }

 

    public void StoreStart(){

        while(true){

            Store_Menu();

 

            System.out.print("메뉴 선택 : ");

            int choice = scan.nextInt();

 

            if(choice == 1)

                BuyLotto();

            else if(choice == 2) {

                System.out.println("프로그램 종료");

                return;

            }

            else

                 System.out.println("번호를 잘못 입력했습니다. 1 또는2를 입력하세요.");

        }

    }

 

    public void BuyLotto(){

        System.out.print("금액 입력 : ");

        int money = scan.nextInt();

 

        if(money<1000){

            System.out.println("1000원 이상 입력해 주세요.");

            return;

        }else if(money>100000){

            System.out.println("100000원을 초과 할수 없습니다. 다시 입력해주세요.");

            return;

        }

 

        LottoNum(money);

 

        System.out.println("받은 금액은 " + money + "원이고, 거스름돈은 " + (money%1000) + "원 입니다.");

}

 

    public void LottoNum(int money){

        Set<Integer> lottoSet = new HashSet<>();

 

        int amount = money/1000;

 

        System.out.println();

        System.out.println("로또 번호 출력하기");

        for(int i=1; i<=amount; i++){

             while(lottoSet.size()<6){

                 lottoSet.add((int)(Math.random() * 45 + 1));

             }

             ArrayList<Integer> lottoList = new ArrayList<Integer>(lottoSet);

             Collections.sort(lottoList);

             System.out.println("로또번호" + i + " : " + lottoList);

             lottoSet.clear();

        }

    }

 

    public static void main(String[] args) {

        new LottoStore().StoreStart();

    }

}

 

 

 

3. 아래의 데이터는 이름과 주민번호 앞자리부분인 생년월일이다. 이름은 중복이 가능하나 주민번호 앞자리 6개는 중복이 될수 없다고 가정하고 중복된 데이터를 제외 하고 아래와 같이 출력하는 프로그램을 작성하시오. (출력데이터 순서는 상관없음)

(임꺽정, 800525, 홍길동, 891205, 일지매, 870721, 철수, 891205, 순이, 900817 )

 

출력예]

 

 

4. ArrayList를 이용하여 성적처리 프로그램을 작성하시오.

학생클래스를 작성하고, 메인함수에서 제네릭을 사용하여 학생클래스를 자료형으로 사용하시오.

 

출력 예]

'프로그램언어 > 자바' 카테고리의 다른 글

Collection 계열 클래스2  (0) 2020.11.14
Collection 계열 클래스1  (0) 2020.11.14
스윙 컴포넌트 그리기3  (0) 2020.11.05
스윙 컴포넌트 그리기2  (0) 2020.11.05
스윙 컴포넌트 그리기1  (0) 2020.11.05