EXAMPLES - PowerPoint PPT Presentation

About This Presentation
Title:

EXAMPLES

Description:

Title: Slide 1 Author: Martin Drewe Last modified by: CGD Created Date: 12/19/2005 2:34:39 PM Document presentation format: On-screen Show Company – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 20
Provided by: Martin972
Category:
Tags: examples | shuffle

less

Transcript and Presenter's Notes

Title: EXAMPLES


1
EXAMPLES
2
Example 1
  • Write a Java method that performs addition on two
    binary numbers. Each binary number is kept in an
    integer array.
  • 11 1010 0101
  • 1 0111 0001
  • ---------------
  • 101 0001 0110

3
  • private static int binaryAddition(int A, int
    B)
  • int C new int max(A.length,B.length)
    1
  • int curA A.length - 1
  • int curB B.length - 1
  • int carry 0
  • for (int i C.length - 1 i gt 0 i--)
  • int sum 0
  • if (curA gt 0)
  • sum AcurA
  • curA--
  • if (curB gt 0)
  • sum BcurB
  • curB--
  • Ci (carry sum) 2
  • carry (carry sum) / 2

4
  • private static int max(int A, int B)
  • if (A gt B)
  • return A
  • else
  • return B

5
Example 2
  • Write a Java method that combines two sorted
    integer arrays into a third sorted integer array

6
  • private static int combineArrays(int A,
    int B)
  • int C new intA.length B.length
  • int curA, curB, curC
  • curA curB curC 0
  • while (curA lt A.length curB lt B.length)
  • if (AcurA lt BcurB)
  • CcurC AcurA
  • else
  • CcurC BcurB
  • if (curA lt A.length)
  • while (curA lt A.length)
  • CcurC AcurA
  • else
  • while (curB lt B.length)
  • CcurC BcurB

7
Example 3
  • Write a program that takes the first and last
    names from the user and outputs two fancy
    greetings for this name.
  • For example, if the input is "Cigdem" and
    "Gunduz-Demir", then the greetings should look
    like
  • Hello!
  • Cigdem
  • Gunduz-Demir
  • H o g G u e
  • e l ! i d m u d z D m r
  • l C e n - i
  • The latter should be formed by concatenating the
    strings "Hello!", "Cigdem", and "Gunduz-Demir"
    and outputting it in a wave pattern

8
  • import java.util.
  • public class Greetings
  • public static void main(String args)
  • Scanner scan new Scanner(System.in)
  • System.out.print("Enter your first name
    ")
  • String firstName scan.next()
  • System.out.print("Enter your last name ")
  • String lastName scan.next()
  • displayGreeting("Hello!",firstName,lastName)
  • System.out.println()
  • displayGreetingInWaveForm("Hello!",firstName
    ,lastName)

9
  • private static void displayGreeting(String s1,
    String s2,
  • String
    s3)
  • int lineLen maxLengthOfThree(s1,s2,s3)
    4
  • writeHeader(lineLen)
  • writeStringOnSingleLine(s1,lineLen)
  • writeStringOnSingleLine(s2,lineLen)
  • writeStringOnSingleLine(s3,lineLen)
  • writeHeader(lineLen)

10
  • private static int maxLengthOfThree(String s1,
    String s2,
  • String
    s3)
  • int maxLen s1.length()
  • if (maxLen lt s2.length())
  • maxLen s2.length()
  • if (maxLen lt s3.length())
  • maxLen s3.length()
  • return maxLen

11
  • private static void writeHeader(int lineLen)
  • for (int i 0 i lt lineLen i)
  • System.out.print("")
  • System.out.println()

12
  • private static void writeStringOnSingleLine(Str
    ing S,

  • int lineLen)
  • int spaceNo lineLen - 4 - S.length()
  • int leftSpaceNo spaceNo / 2
  • int rightSpaceNo spaceNo - leftSpaceNo
  • System.out.print(" ")
  • for (int i 0 i lt leftSpaceNo i)
  • System.out.print(" ")
  • System.out.print(S)
  • for (int i 0 i lt rightSpaceNo i)
  • System.out.print(" ")
  • System.out.println(" ")

13
  • private static void displayGreetingInWaveForm(S
    tring s1,
  • String
    s2, String s3)
  • String all s1 s2 s3
  • char lines new char3all.length()
  • for (int i 0 i lt lines.length i)
  • for (int j 0 j lt lines0.length
    j)
  • linesij ' '
  • for (int i 0 i lt all.length() i 4)
  • lines0i all.charAt(i)
  • for (int i 1 i lt all.length() i 2)
  • lines1i all.charAt(i)
  • for (int i 2 i lt all.length() i 4)
  • lines2i all.charAt(i)
  • writeHeader(all.length() 4)
  • for (int i 0 i lt lines.length i)
  • System.out.print(" ")
  • for (int j 0 j lt lines0.length
    j)

14
Example 4
  • Write a card shuffling and dealing simulation
    program. This program shuffles a deck of 52
    playing cards and then deal these cards to 4
    players.
  • Suits Hearts, Diamonds, Clubs, Spades
  • Faces 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A

15
  • import java.util.
  • public class CardDealing
  • public static void main(String args)
  • String faces "2","3","4","5","6","7","8
    ",
  • "9","10","J","Q","K","A"
  • String suits "Hearts","Diamonds",
  • "Clubs","Spades"
  • String deck new String52
  • String hands
  • for (int i 0 i lt deck.length i)
  • decki facesi 13 " " suitsi
    / 13
  • printDeck(deck) // sorted deck
  • shuffle(deck)
  • printDeck(deck) // shuffled deck
  • hands deal(deck,4)
  • printHands(hands)

16
  • private static void printDeck(String deck)
  • for (int i 0 i lt deck.length i)
  • System.out.println(decki)

17
  • private static void shuffle(String deck)
  • Random rand new Random()
  • for (int i 0 i lt deck.length i)
  • int rno rand.nextInt( 52 )
  • String temp decki
  • decki deckrno
  • deckrno temp

18
  • private static String deal(String deck,
  • int playerNo)
  • String hands
  • new String playerNodeck.length /
    playerNo
  • int cnt 0
  • for (int i 0 i lt hands0.length i)
  • for (int j 0 j lt hands.length j)
  • handsji deckcnt
  • cnt
  • return hands

19
  • private static void printHands(String
    hands)
  • for (int i 0 i lt hands.length i)
  • System.out.print("Player" i "\t\t")
  • System.out.println()
  • for (int i 0 i lt hands0.length i)
  • for (int j 0 j lt hands.length j)
  • System.out.print(handsji "
    \t")
  • System.out.println()
Write a Comment
User Comments (0)
About PowerShow.com