Examples2

advertisement
EXAMPLES
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
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 >= 0; i--){
int sum = 0;
if (curA >= 0){
sum += A[curA];
curA--;
}
if (curB >= 0){
sum += B[curB];
curB--;
}
C[i] = (carry + sum) % 2;
carry = (carry + sum) / 2;
}
return C;
}
private static int max(int A, int B){
if (A > B)
return A;
else
return B;
}
Example 2:
• Write a Java method that combines two
sorted integer arrays into a third sorted
integer array
private static int [] combineArrays(int[] A, int[] B){
int[] C = new int[A.length + B.length];
int curA, curB, curC;
curA = curB = curC = 0;
while (curA < A.length && curB < B.length){
if (A[curA] < B[curB])
C[curC++] = A[curA++];
else
C[curC++] = B[curB++];
}
if (curA < A.length)
while (curA < A.length)
C[curC++] = A[curA++];
else
while (curB < B.length)
C[curC++] = B[curB++];
return C;
}
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
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);
}
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);
}
private static int maxLengthOfThree(String s1, String s2,
String s3){
int maxLen = s1.length();
if (maxLen < s2.length())
maxLen = s2.length();
if (maxLen < s3.length())
maxLen = s3.length();
return maxLen;
}
private static void writeHeader(int lineLen){
for (int i = 0; i < lineLen; i++)
System.out.print("*");
System.out.println();
}
private static void writeStringOnSingleLine(String 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 < leftSpaceNo; i++)
System.out.print(" ");
System.out.print(S);
for (int i = 0; i < rightSpaceNo; i++)
System.out.print(" ");
System.out.println(" *");
}
private static void displayGreetingInWaveForm(String s1,
String s2, String s3){
String all = s1 + s2 + s3;
char [][] lines = new char[3][all.length()];
for (int i = 0; i < lines.length; i++)
for (int j = 0; j < lines[0].length; j++)
lines[i][j] = ' ';
for (int i = 0; i < all.length(); i += 4)
lines[0][i] = all.charAt(i);
for (int i = 1; i < all.length(); i += 2)
lines[1][i] = all.charAt(i);
for (int i = 2; i < all.length(); i += 4)
lines[2][i] = all.charAt(i);
writeHeader(all.length() + 4);
for (int i = 0; i < lines.length; i++){
System.out.print("* ");
for (int j = 0; j < lines[0].length; j++)
System.out.print(lines[i][j]);
System.out.println(" *");
}
writeHeader(all.length() + 4);
}
}
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
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 String[52];
String[][] hands;
for (int i = 0; i < deck.length; i++)
deck[i] = faces[i % 13] + " " + suits[i / 13];
printDeck(deck); // sorted deck
shuffle(deck);
printDeck(deck); // shuffled deck
hands = deal(deck,4);
printHands(hands);
}
// write your helper methods here
}
private static void printDeck(String[] deck){
for (int i = 0; i < deck.length; i++)
System.out.println(deck[i]);
}
private static void shuffle(String[] deck){
Random rand = new Random();
for (int i = 0; i < deck.length; i++){
int rno = rand.nextInt( 52 );
String temp = deck[i];
deck[i] = deck[rno];
deck[rno] = temp;
}
}
private static String [][] deal(String[] deck,
int playerNo){
String[][] hands =
new String [playerNo][deck.length / playerNo];
int cnt = 0;
for (int i = 0; i < hands[0].length; i++){
for (int j = 0; j < hands.length; j++){
hands[j][i] = deck[cnt];
cnt++;
}
}
return hands;
}
private static void printHands(String[][] hands){
for (int i = 0; i < hands.length; i++){
System.out.print("Player" + i + "\t\t");
}
System.out.println();
for (int i = 0; i < hands[0].length; i++){
for (int j = 0; j < hands.length; j++){
System.out.print(hands[j][i] + " \t");
}
System.out.println();
}
}
Download