SlideShare a Scribd company logo
1 of 7
Download to read offline
import java.util.Scanner;
public class Assignment4 {
public static void main(String args[]) {
final int LEN = 5;
int[] hand = new int[LEN];
Scanner input = new Scanner(System.in);
//input the hand
System.out.println("Enter five numeric cards, no face cards. Use 2-9.");
for (int i = 0; i < hand.length; i++) {
hand[i] = input.nextInt();
}
//sort the collection
bubbleSortCards(hand);
//determine players hand type
//flow of evaluation -> checking complex hands first
if (containsFullHouse(hand)) {
System.out.println("Full House!");
} else if (containsStraight(hand)) {
System.out.println("Straight!");
} else if (containsFourOfaKind(hand)) {
System.out.println("Four of a Kind!");
} else if (containsThreeOfaKind(hand)) {
System.out.println("Three of a Kind!");
} else if (containsTwoPair(hand)) {
System.out.println("Two Pair!");
} else if (containsPair(hand)) {
System.out.println("Pair!");
} else
System.out.println("High Card!");
}
//cardInSets is called with hand[] and pair to check if a pair is found.
public static boolean containsPair(int hand[]) {
int pair = 2;
return cardInSets(hand, pair) != NOT_FOUND;
}
//cardInSets is called with hand[] and pair to check if a pair is found.
//Then calls back again with fcard result to check for another pair.
public static boolean containsTwoPair(int hand[]) {
int pair = 2;
int fcard = cardInSets(hand, pair);
int scard = cardInSets(hand, pair, fcard);
return fcard!=NOT_FOUND && scard!=NOT_FOUND;
}
//cardInSets is called with hand[] and threeaKind to check if three of a kind has been found.
public static boolean containsThreeOfaKind(int hand[]) {
int threeaKind = 3;
return cardInSets(hand, threeaKind) != NOT_FOUND;
}
//hasStraight will resolve to true, unless the previous value++ is not equivalent.
//The loop ends as soon as hasStaight is false or the end of the hand is reached.
public static boolean containsStraight(int hand[]) {
boolean hasStraight = true;
int i=1;
do {
hasStraight = (hand[i] == (hand[i-1] + 1));
} while(hasStraight && ++i < hand.length);
return hasStraight;
}
//cardInSets is called with hand[] and threeKinds to check if three of a kind is found.
//Then calls back again with pair and tcard result to check for a pair of another card type.
public static boolean containsFullHouse(int hand[]) {
int threeKinds = 3;
int pair = 2;
int tcard = cardInSets(hand, threeKinds);
int pcard = cardInSets(hand, pair, tcard);
return tcard!=NOT_FOUND && pcard!=NOT_FOUND;
}
//cardInSets is called with hand[] and fouraKind to check if four of a kind is found.
public static boolean containsFourOfaKind(int hand[]) {
int fouraKind = 4;
return cardInSets(hand, fouraKind) != NOT_FOUND;
}
//When ignoreCard isn't needed, it overloads with a sentinel 0.
public static int cardInSets(int hand[], int numKinds) {
return cardInSets(hand, numKinds, 0);
}
//The method cardInSets is a generic evaluation that returns if the parameter specified
numKinds(like 2 for pair) has been found in the hand.
//The ignoreCard defaults to 0 through overloading, but is required for the fullhouse evalation.
//After the three of a kind is found, it passes that card value as ignoreCard to ensure the pair
evaluation will not be of the same card type.
public static int cardInSets(int hand[], int numKinds, int ignoreCard) {
numKinds--;
int last = hand[0];
int card=NOT_FOUND;
int numMatch = 0;
for (int i = 1; i < hand.length; i++) {
if (last == hand[i] && ignoreCard!=last) {
numMatch++;
//hasNumKinds = hasMatch==numKinds;
if (numMatch==numKinds) {
card=last;
break;
}
} else numMatch = NOT_FOUND;
last = hand[i];
}
return card;
}
//For ease of evaluation, the hand is sorted after input by bubble sort.
public static int[] bubbleSortCards(int hand[]) {
int temp;
for (int i=0; i < hand.length-1; i++) {
for (int j=1; j < hand.length-i; j++) {
if (hand[j-1] > hand[j]) {
temp = hand[j-1];
hand[j-1] = hand[j];
hand[j] = temp;
}
}
}
return hand;
}
}
Solution
import java.util.Scanner;
public class Assignment4 {
public static void main(String args[]) {
final int LEN = 5;
int[] hand = new int[LEN];
Scanner input = new Scanner(System.in);
//input the hand
System.out.println("Enter five numeric cards, no face cards. Use 2-9.");
for (int i = 0; i < hand.length; i++) {
hand[i] = input.nextInt();
}
//sort the collection
bubbleSortCards(hand);
//determine players hand type
//flow of evaluation -> checking complex hands first
if (containsFullHouse(hand)) {
System.out.println("Full House!");
} else if (containsStraight(hand)) {
System.out.println("Straight!");
} else if (containsFourOfaKind(hand)) {
System.out.println("Four of a Kind!");
} else if (containsThreeOfaKind(hand)) {
System.out.println("Three of a Kind!");
} else if (containsTwoPair(hand)) {
System.out.println("Two Pair!");
} else if (containsPair(hand)) {
System.out.println("Pair!");
} else
System.out.println("High Card!");
}
//cardInSets is called with hand[] and pair to check if a pair is found.
public static boolean containsPair(int hand[]) {
int pair = 2;
return cardInSets(hand, pair) != NOT_FOUND;
}
//cardInSets is called with hand[] and pair to check if a pair is found.
//Then calls back again with fcard result to check for another pair.
public static boolean containsTwoPair(int hand[]) {
int pair = 2;
int fcard = cardInSets(hand, pair);
int scard = cardInSets(hand, pair, fcard);
return fcard!=NOT_FOUND && scard!=NOT_FOUND;
}
//cardInSets is called with hand[] and threeaKind to check if three of a kind has been found.
public static boolean containsThreeOfaKind(int hand[]) {
int threeaKind = 3;
return cardInSets(hand, threeaKind) != NOT_FOUND;
}
//hasStraight will resolve to true, unless the previous value++ is not equivalent.
//The loop ends as soon as hasStaight is false or the end of the hand is reached.
public static boolean containsStraight(int hand[]) {
boolean hasStraight = true;
int i=1;
do {
hasStraight = (hand[i] == (hand[i-1] + 1));
} while(hasStraight && ++i < hand.length);
return hasStraight;
}
//cardInSets is called with hand[] and threeKinds to check if three of a kind is found.
//Then calls back again with pair and tcard result to check for a pair of another card type.
public static boolean containsFullHouse(int hand[]) {
int threeKinds = 3;
int pair = 2;
int tcard = cardInSets(hand, threeKinds);
int pcard = cardInSets(hand, pair, tcard);
return tcard!=NOT_FOUND && pcard!=NOT_FOUND;
}
//cardInSets is called with hand[] and fouraKind to check if four of a kind is found.
public static boolean containsFourOfaKind(int hand[]) {
int fouraKind = 4;
return cardInSets(hand, fouraKind) != NOT_FOUND;
}
//When ignoreCard isn't needed, it overloads with a sentinel 0.
public static int cardInSets(int hand[], int numKinds) {
return cardInSets(hand, numKinds, 0);
}
//The method cardInSets is a generic evaluation that returns if the parameter specified
numKinds(like 2 for pair) has been found in the hand.
//The ignoreCard defaults to 0 through overloading, but is required for the fullhouse evalation.
//After the three of a kind is found, it passes that card value as ignoreCard to ensure the pair
evaluation will not be of the same card type.
public static int cardInSets(int hand[], int numKinds, int ignoreCard) {
numKinds--;
int last = hand[0];
int card=NOT_FOUND;
int numMatch = 0;
for (int i = 1; i < hand.length; i++) {
if (last == hand[i] && ignoreCard!=last) {
numMatch++;
//hasNumKinds = hasMatch==numKinds;
if (numMatch==numKinds) {
card=last;
break;
}
} else numMatch = NOT_FOUND;
last = hand[i];
}
return card;
}
//For ease of evaluation, the hand is sorted after input by bubble sort.
public static int[] bubbleSortCards(int hand[]) {
int temp;
for (int i=0; i < hand.length-1; i++) {
for (int j=1; j < hand.length-i; j++) {
if (hand[j-1] > hand[j]) {
temp = hand[j-1];
hand[j-1] = hand[j];
hand[j] = temp;
}
}
}
return hand;
}
}

More Related Content

Similar to import java.util.Scanner;public class Assignment4 {    public st.pdf

import java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdfimport java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdf
apexelectronices01
 
import java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdfimport java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdf
stopgolook
 
Please observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdfPlease observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdf
apexjaipur
 
I need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docxI need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docx
Phil4IDBrownh
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
gordienaysmythe
 
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdfDigits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
annapurnnatextailes
 
Please follow the code and comments for description a)CODE p.pdf
Please follow the code and comments for description a)CODE p.pdfPlease follow the code and comments for description a)CODE p.pdf
Please follow the code and comments for description a)CODE p.pdf
LAMJM
 
Im having an issue with the simulateOPT() methodthis is the p.pdf
Im having an issue with the simulateOPT() methodthis is the p.pdfIm having an issue with the simulateOPT() methodthis is the p.pdf
Im having an issue with the simulateOPT() methodthis is the p.pdf
stopgolook
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
anujmkt
 

Similar to import java.util.Scanner;public class Assignment4 {    public st.pdf (14)

import java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdfimport java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdf
 
import java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdfimport java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdf
 
Please observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdfPlease observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdf
 
#include fstream#include iostream#include cstdlib#includ.docx
#include fstream#include iostream#include cstdlib#includ.docx#include fstream#include iostream#include cstdlib#includ.docx
#include fstream#include iostream#include cstdlib#includ.docx
 
Write a program in C that generates a list of random numbers greater.pdf
Write a program in C that generates a list of random numbers greater.pdfWrite a program in C that generates a list of random numbers greater.pdf
Write a program in C that generates a list of random numbers greater.pdf
 
DS Code (CWH).docx
DS Code (CWH).docxDS Code (CWH).docx
DS Code (CWH).docx
 
I need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docxI need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docx
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
 
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdfDigits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
 
Hw3
Hw3Hw3
Hw3
 
Complier File
Complier FileComplier File
Complier File
 
Please follow the code and comments for description a)CODE p.pdf
Please follow the code and comments for description a)CODE p.pdfPlease follow the code and comments for description a)CODE p.pdf
Please follow the code and comments for description a)CODE p.pdf
 
Im having an issue with the simulateOPT() methodthis is the p.pdf
Im having an issue with the simulateOPT() methodthis is the p.pdfIm having an issue with the simulateOPT() methodthis is the p.pdf
Im having an issue with the simulateOPT() methodthis is the p.pdf
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 

More from asif1401

#includeiostream#includestdlib.husing namespace std;class .pdf
#includeiostream#includestdlib.husing namespace std;class .pdf#includeiostream#includestdlib.husing namespace std;class .pdf
#includeiostream#includestdlib.husing namespace std;class .pdf
asif1401
 
(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf
(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf
(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf
asif1401
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
asif1401
 
QuestionAnswer1TrueThe corpus callosum , otherwise called th.pdf
QuestionAnswer1TrueThe corpus callosum , otherwise called th.pdfQuestionAnswer1TrueThe corpus callosum , otherwise called th.pdf
QuestionAnswer1TrueThe corpus callosum , otherwise called th.pdf
asif1401
 

More from asif1401 (20)

the reaction of A to B since its Ea is the highes.pdf
                     the reaction of A to B since its Ea is the highes.pdf                     the reaction of A to B since its Ea is the highes.pdf
the reaction of A to B since its Ea is the highes.pdf
 
the one thing that comes to mind for me is about .pdf
                     the one thing that comes to mind for me is about .pdf                     the one thing that comes to mind for me is about .pdf
the one thing that comes to mind for me is about .pdf
 
#includeiostream#includestdlib.husing namespace std;class .pdf
#includeiostream#includestdlib.husing namespace std;class .pdf#includeiostream#includestdlib.husing namespace std;class .pdf
#includeiostream#includestdlib.husing namespace std;class .pdf
 
(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf
(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf
(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf
 
Iodine will replace both Cl and Br It will be SN2.pdf
                     Iodine will replace both Cl and Br It will be SN2.pdf                     Iodine will replace both Cl and Br It will be SN2.pdf
Iodine will replace both Cl and Br It will be SN2.pdf
 
The number of outcomes in the event A is 4 Yes event A is a s.pdf
 The number of outcomes in the event A is 4 Yes event A is a s.pdf The number of outcomes in the event A is 4 Yes event A is a s.pdf
The number of outcomes in the event A is 4 Yes event A is a s.pdf
 
There were very few planetary leftovers in this r.pdf
                     There were very few planetary leftovers in this r.pdf                     There were very few planetary leftovers in this r.pdf
There were very few planetary leftovers in this r.pdf
 
i have no idea. im just seeing if i get karma p.pdf
                     i have no idea. im just seeing if i get karma p.pdf                     i have no idea. im just seeing if i get karma p.pdf
i have no idea. im just seeing if i get karma p.pdf
 
Fe+3 solution is more acidic because it produces .pdf
                     Fe+3 solution is more acidic because it produces .pdf                     Fe+3 solution is more acidic because it produces .pdf
Fe+3 solution is more acidic because it produces .pdf
 
Copper chloride. Copper ions will form precipitat.pdf
                     Copper chloride. Copper ions will form precipitat.pdf                     Copper chloride. Copper ions will form precipitat.pdf
Copper chloride. Copper ions will form precipitat.pdf
 
   CCl4 molecule can give IR and Raman spectroscopy.   Selection r.pdf
   CCl4 molecule can give IR and Raman spectroscopy.   Selection r.pdf   CCl4 molecule can give IR and Raman spectroscopy.   Selection r.pdf
   CCl4 molecule can give IR and Raman spectroscopy.   Selection r.pdf
 
User interface design processNavigation DesignInput DesignOu.pdf
User interface design processNavigation DesignInput DesignOu.pdfUser interface design processNavigation DesignInput DesignOu.pdf
User interface design processNavigation DesignInput DesignOu.pdf
 
True.it  is an example of an independent-measures designSolution.pdf
True.it  is an example of an independent-measures designSolution.pdfTrue.it  is an example of an independent-measures designSolution.pdf
True.it  is an example of an independent-measures designSolution.pdf
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
 
the inner electronsSolutionthe inner electrons.pdf
the inner electronsSolutionthe inner electrons.pdfthe inner electronsSolutionthe inner electrons.pdf
the inner electronsSolutionthe inner electrons.pdf
 
SolutionThe investees net income should be recorded as an increa.pdf
SolutionThe investees net income should be recorded as an increa.pdfSolutionThe investees net income should be recorded as an increa.pdf
SolutionThe investees net income should be recorded as an increa.pdf
 
QuestionAnswer1TrueThe corpus callosum , otherwise called th.pdf
QuestionAnswer1TrueThe corpus callosum , otherwise called th.pdfQuestionAnswer1TrueThe corpus callosum , otherwise called th.pdf
QuestionAnswer1TrueThe corpus callosum , otherwise called th.pdf
 
No. of Moles = Given MassMolecular Mass=3.0 x 101398= 3.06101.pdf
No. of Moles = Given MassMolecular Mass=3.0 x 101398= 3.06101.pdfNo. of Moles = Given MassMolecular Mass=3.0 x 101398= 3.06101.pdf
No. of Moles = Given MassMolecular Mass=3.0 x 101398= 3.06101.pdf
 
Let the sample size be n ,Mean of the sample = 27 (same as pop.pdf
Let the sample size be n ,Mean of the sample = 27 (same as pop.pdfLet the sample size be n ,Mean of the sample = 27 (same as pop.pdf
Let the sample size be n ,Mean of the sample = 27 (same as pop.pdf
 
JeffSolutionJeff.pdf
JeffSolutionJeff.pdfJeffSolutionJeff.pdf
JeffSolutionJeff.pdf
 

Recently uploaded

Recently uploaded (20)

Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS Publications
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscaping
 

import java.util.Scanner;public class Assignment4 {    public st.pdf

  • 1. import java.util.Scanner; public class Assignment4 { public static void main(String args[]) { final int LEN = 5; int[] hand = new int[LEN]; Scanner input = new Scanner(System.in); //input the hand System.out.println("Enter five numeric cards, no face cards. Use 2-9."); for (int i = 0; i < hand.length; i++) { hand[i] = input.nextInt(); } //sort the collection bubbleSortCards(hand); //determine players hand type //flow of evaluation -> checking complex hands first if (containsFullHouse(hand)) { System.out.println("Full House!"); } else if (containsStraight(hand)) { System.out.println("Straight!"); } else if (containsFourOfaKind(hand)) { System.out.println("Four of a Kind!"); } else if (containsThreeOfaKind(hand)) { System.out.println("Three of a Kind!"); } else if (containsTwoPair(hand)) { System.out.println("Two Pair!"); } else if (containsPair(hand)) { System.out.println("Pair!"); } else System.out.println("High Card!"); } //cardInSets is called with hand[] and pair to check if a pair is found. public static boolean containsPair(int hand[]) { int pair = 2;
  • 2. return cardInSets(hand, pair) != NOT_FOUND; } //cardInSets is called with hand[] and pair to check if a pair is found. //Then calls back again with fcard result to check for another pair. public static boolean containsTwoPair(int hand[]) { int pair = 2; int fcard = cardInSets(hand, pair); int scard = cardInSets(hand, pair, fcard); return fcard!=NOT_FOUND && scard!=NOT_FOUND; } //cardInSets is called with hand[] and threeaKind to check if three of a kind has been found. public static boolean containsThreeOfaKind(int hand[]) { int threeaKind = 3; return cardInSets(hand, threeaKind) != NOT_FOUND; } //hasStraight will resolve to true, unless the previous value++ is not equivalent. //The loop ends as soon as hasStaight is false or the end of the hand is reached. public static boolean containsStraight(int hand[]) { boolean hasStraight = true; int i=1; do { hasStraight = (hand[i] == (hand[i-1] + 1)); } while(hasStraight && ++i < hand.length); return hasStraight; } //cardInSets is called with hand[] and threeKinds to check if three of a kind is found. //Then calls back again with pair and tcard result to check for a pair of another card type. public static boolean containsFullHouse(int hand[]) { int threeKinds = 3; int pair = 2; int tcard = cardInSets(hand, threeKinds); int pcard = cardInSets(hand, pair, tcard); return tcard!=NOT_FOUND && pcard!=NOT_FOUND; } //cardInSets is called with hand[] and fouraKind to check if four of a kind is found. public static boolean containsFourOfaKind(int hand[]) {
  • 3. int fouraKind = 4; return cardInSets(hand, fouraKind) != NOT_FOUND; } //When ignoreCard isn't needed, it overloads with a sentinel 0. public static int cardInSets(int hand[], int numKinds) { return cardInSets(hand, numKinds, 0); } //The method cardInSets is a generic evaluation that returns if the parameter specified numKinds(like 2 for pair) has been found in the hand. //The ignoreCard defaults to 0 through overloading, but is required for the fullhouse evalation. //After the three of a kind is found, it passes that card value as ignoreCard to ensure the pair evaluation will not be of the same card type. public static int cardInSets(int hand[], int numKinds, int ignoreCard) { numKinds--; int last = hand[0]; int card=NOT_FOUND; int numMatch = 0; for (int i = 1; i < hand.length; i++) { if (last == hand[i] && ignoreCard!=last) { numMatch++; //hasNumKinds = hasMatch==numKinds; if (numMatch==numKinds) { card=last; break; } } else numMatch = NOT_FOUND; last = hand[i]; } return card; } //For ease of evaluation, the hand is sorted after input by bubble sort. public static int[] bubbleSortCards(int hand[]) { int temp; for (int i=0; i < hand.length-1; i++) {
  • 4. for (int j=1; j < hand.length-i; j++) { if (hand[j-1] > hand[j]) { temp = hand[j-1]; hand[j-1] = hand[j]; hand[j] = temp; } } } return hand; } } Solution import java.util.Scanner; public class Assignment4 { public static void main(String args[]) { final int LEN = 5; int[] hand = new int[LEN]; Scanner input = new Scanner(System.in); //input the hand System.out.println("Enter five numeric cards, no face cards. Use 2-9."); for (int i = 0; i < hand.length; i++) { hand[i] = input.nextInt(); } //sort the collection bubbleSortCards(hand); //determine players hand type //flow of evaluation -> checking complex hands first if (containsFullHouse(hand)) { System.out.println("Full House!"); } else if (containsStraight(hand)) { System.out.println("Straight!"); } else if (containsFourOfaKind(hand)) { System.out.println("Four of a Kind!");
  • 5. } else if (containsThreeOfaKind(hand)) { System.out.println("Three of a Kind!"); } else if (containsTwoPair(hand)) { System.out.println("Two Pair!"); } else if (containsPair(hand)) { System.out.println("Pair!"); } else System.out.println("High Card!"); } //cardInSets is called with hand[] and pair to check if a pair is found. public static boolean containsPair(int hand[]) { int pair = 2; return cardInSets(hand, pair) != NOT_FOUND; } //cardInSets is called with hand[] and pair to check if a pair is found. //Then calls back again with fcard result to check for another pair. public static boolean containsTwoPair(int hand[]) { int pair = 2; int fcard = cardInSets(hand, pair); int scard = cardInSets(hand, pair, fcard); return fcard!=NOT_FOUND && scard!=NOT_FOUND; } //cardInSets is called with hand[] and threeaKind to check if three of a kind has been found. public static boolean containsThreeOfaKind(int hand[]) { int threeaKind = 3; return cardInSets(hand, threeaKind) != NOT_FOUND; } //hasStraight will resolve to true, unless the previous value++ is not equivalent. //The loop ends as soon as hasStaight is false or the end of the hand is reached. public static boolean containsStraight(int hand[]) { boolean hasStraight = true; int i=1; do { hasStraight = (hand[i] == (hand[i-1] + 1)); } while(hasStraight && ++i < hand.length);
  • 6. return hasStraight; } //cardInSets is called with hand[] and threeKinds to check if three of a kind is found. //Then calls back again with pair and tcard result to check for a pair of another card type. public static boolean containsFullHouse(int hand[]) { int threeKinds = 3; int pair = 2; int tcard = cardInSets(hand, threeKinds); int pcard = cardInSets(hand, pair, tcard); return tcard!=NOT_FOUND && pcard!=NOT_FOUND; } //cardInSets is called with hand[] and fouraKind to check if four of a kind is found. public static boolean containsFourOfaKind(int hand[]) { int fouraKind = 4; return cardInSets(hand, fouraKind) != NOT_FOUND; } //When ignoreCard isn't needed, it overloads with a sentinel 0. public static int cardInSets(int hand[], int numKinds) { return cardInSets(hand, numKinds, 0); } //The method cardInSets is a generic evaluation that returns if the parameter specified numKinds(like 2 for pair) has been found in the hand. //The ignoreCard defaults to 0 through overloading, but is required for the fullhouse evalation. //After the three of a kind is found, it passes that card value as ignoreCard to ensure the pair evaluation will not be of the same card type. public static int cardInSets(int hand[], int numKinds, int ignoreCard) { numKinds--; int last = hand[0]; int card=NOT_FOUND; int numMatch = 0; for (int i = 1; i < hand.length; i++) { if (last == hand[i] && ignoreCard!=last) { numMatch++; //hasNumKinds = hasMatch==numKinds;
  • 7. if (numMatch==numKinds) { card=last; break; } } else numMatch = NOT_FOUND; last = hand[i]; } return card; } //For ease of evaluation, the hand is sorted after input by bubble sort. public static int[] bubbleSortCards(int hand[]) { int temp; for (int i=0; i < hand.length-1; i++) { for (int j=1; j < hand.length-i; j++) { if (hand[j-1] > hand[j]) { temp = hand[j-1]; hand[j-1] = hand[j]; hand[j] = temp; } } } return hand; } }