Presentation is loading. Please wait.

Presentation is loading. Please wait.

프로그래밍 개론 Ⅰ 제 3장. 클래스와 객체의 사용 ②.

Similar presentations


Presentation on theme: "프로그래밍 개론 Ⅰ 제 3장. 클래스와 객체의 사용 ②."— Presentation transcript:

1 프로그래밍 개론 Ⅰ 제 3장. 클래스와 객체의 사용 ②

2 차례 Formatting Output Playing With Cards Integer Class Nested Panels

3 Formatting Output 다음의 프로그램(Deli.java)을 완성하시오. 입력 값 출력 값
Unit Price : $4.25 Weight : 41 ounces 출력 값 ***** CS Deli ***** Unit Price : $4.25 per pound Weight : 2.56 pounds TOTAL : $10.89 import java.util.Scanner; // DecimalFormat, NumberFormat Class를 사용하기 위한 import문을 추가한다. public class Deli { public static void main (String[] args) final double OUNCES_PER_POUND = 16.0; double pricePerPound; // price per pound double weightOunces; // weight in ounces double weight; // weight in pounds double totalPrice; // total price for the item Scanner scan = new Scanner(System.in);

4 Formatting Output (cont.)
// NumberFormat형 변수 momey를 선언하고, // getCurrencyInstance메소드를 이용하여 값을 할당한다. // DecimalFormat형 변수 fmt를 선언하고, // 적어도 한개의 숫자가 소수점의 왼쪽에 출력되고, // 소수점 이하 부분이 2자리 수인 패턴으로 초기화한다. // prompt the user and read in each input System.out.println ("Welcome to the CS Deli!!\n "); System.out.print ("Enter the price per pound of your item: "); pricePerPound = scan.nextDouble(); System.out.print ("Enter the weight (ounces): "); weightOunces = scan.nextDouble(); // Convert ounces to pounds and compute the total price weight = weightOunces / OUNCES_PER_POUND; totalPrice = pricePerPound * weight; // price는 money를 사용하고, weight는 fmt를 사용하여 // 결과를 출력한다. }

5 Playing With Cards 다음과 같이 동작하는 프로그램을 작성하시오. 다음의 값을 갖는 열거타입 Rank를 정의한다.
Ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king Rank형 변수 highCard, faceCard, card1, card2를 선언한다. highCard에는 ace를 넣고, FaceCard에는 jack, queen, king중 하나를 넣고, Card1, card2에는 나머지 숫자 카드 중 하나를 넣는다. Int형 변수 card1Val, card2Val을 선언한다. Card1, card2의 실제 숫자 값을 저장한다. (ordinal메소드 사용) Ex. two → 2, three → 3 카드의 값들을 다음과 같이 출력한다. A blackjack hand : highCard값 and faceCard값 A two card hand : card1값, card2값 Hand value : card1,card2의 실제 숫자 값의 합

6 Integer Class 포장클래스인 Integer 클래스를 이용하여, 아래의 프로그램을 작성하시오.
두 수를 문자로 입력 받아, 이를 숫자로 바꾼 후, 두 수를 더해 그 값을 출력한다.

7 Nested Panels NestedPanels.java를 가지고 다음과 같이 따라해보자.
컴파일, 실행하고, 프레임의 사이즈를 조절해 반응을 살펴본다. 세번째 panel을 추가하고, 다시 실행해본다. 기존의 두 패널과 높이는 같게, 너비는 두배로 만든다. 적당한 레이블을 달고, color를 지정해준다. Primary panel의 preferred size를 320, 260으로 지정하고, 다시 실행해본다. 네번째 panel을 추가하고, 다시 실행해본다. Background color는 blue로, size는 320,20으로 한다. “My Panels”라고 레이블을 붙인다. Primary panel에 이 패널을 제일 먼저 붙인다.

8 Nested Panels (cont.) import java.awt.*; import javax.swing.*;
public class NestedPanels { // // Presents two colored panels nested within a third. public static void main (String[] args) JFrame frame = new JFrame ("Nested Panels"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // Set up first subpanel JPanel subPanel1 = new JPanel(); subPanel1.setPreferredSize (new Dimension(150, 100)); subPanel1.setBackground (Color.green); JLabel label1 = new JLabel ("One"); subPanel1.add (label1);

9 Nested Panels (cont.) // Set up second subpanel
JPanel subPanel2 = new JPanel(); subPanel2.setPreferredSize (new Dimension(150, 100)); subPanel2.setBackground (Color.red); JLabel label2 = new JLabel ("Two"); subPanel2.add (label2); // Set up primary panel JPanel primary = new JPanel(); primary.setBackground (Color.blue); primary.add (subPanel1); primary.add (subPanel2); frame.getContentPane().add(primary); frame.pack(); frame.setVisible(true); }


Download ppt "프로그래밍 개론 Ⅰ 제 3장. 클래스와 객체의 사용 ②."

Similar presentations


Ads by Google