Chapter 6 science and art of java programming

Source: Internet
Author: User

You are welcome to repost it. To retain the author's results, some files can be downloaded from resources! If you do not have any credits, contact me for help!

1,

Package SixthCharter;/** File: Poker. java * --------------------------- * this is the first question in Chapter 6th * Author luoriver */import acm. program. *; import acm. util. *; public class Poker extends ConsoleProgram {public void run () {println ("this program is a random display of selected Poker"); while (true) {println (); int I = readInt (); showPoker (); if (I = 2) break;}/** display random poker cards */private void showPoker () {int num = rgen. nextInt (1, 13); // rgen. setSeed (2); switch (num) {case 1: print ("Ace"); break; case 11: print ("Jack"); break; case 12: print ("Queen"); break; case 13: print ("King"); break; default: print (num);} print (""); int color = rgen. nextInt (1, 4); switch (color) {case 1: print ("Spades"); break; case 2: print ("Diamonds"); break; case 3: print ("Clubs"); break; case 4: print ("Hearts"); break ;}} /* create an instance variable for the random number generator */private RandomGenerator rgen = RandomGenerator. getInstance ();}

2,
Package SixthCharter;/** File: TossCoin. java * --------------------------- * This is the 6th question in Chapter 1 * Author luoriver */import acm. program. *; import acm. util. *; public class TossCoin extends ConsoleProgram {public void run () {println ("this program throws a coin until three consecutive values appear, and displays the total number of times thrown: "); int toss_num = 0; // sets the checkpoint, whether the number of consecutive times int n = 0; while (n <CHECK_NUM) {tossCoin (); toss_num ++; if (tossCoin (). equals ("Head") {println ("Head"); n ++;} else {prin Tln ("Tails"); n = 0 ;}} println ("total run" + toss_num + "times. ");}/*** Here is the method for coin writing */private String tossCoin () {int n = rgen. nextInt (1, 2); return returnContent (n );} /*** here is the returned coin side * @ Head * @ Tails */private String returnContent (int num) {if (num = 1) {return "Head" ;}else {return "Tails" ;}}/* create an instance variable for the random number generator */private RandomGenerator rgen = RandomGenerator. getInstance (); private static final int CHECK_NUM = 3 ;}

3,
Package SixthCharter;/** File: ThrowDart. java * --------------------------- * This is the 6th question in Chapter 1 * Author luoriver */import acm. program. *; import acm. graphics. *; import acm. util. *; public class ThrowDart extends ConsoleProgram {public void run () {println ("this program uses the projection operator to simulate that the ratio of Square to circular area is an approximate PI value :"); double SquareArea = 4 * (R * R); double CircleArea = PI * (R * R); println ("the area ratio of the circle to the square" + CircleArea/SquareArea ); println ("+ throwDart (); // println (3.14/4); // throwDart ();} private double throwDart () {double circlenum = 0; double squarenum = 0; while (squarenum <ThrowNum) {double rx = rgen. nextDouble (-1.0, 1.0); double ry = rgen. nextDouble (-1.0, 1.0); // rgen. setSeed (2); if (rx * rx) + (ry * ry) <1) {circlenum ++; squarenum ++ ;} else {squarenum ++ ;}} println (); println ("the number of times cast in the circle is" + circlenum ); println ("the number of times cast in the square is" + squarenum); return circlenum/squarenum;}/* create an instance variable for the random number generator */private RandomGenerator rgen = RandomGenerator. getInstance (); // you can specify the number of times that a token is returned. private static final int ThrowNum = 10000; private static final double PI = 3.14; private static final double R = 1 ;}

4,
Package SixthCharter;/** File: Poker. java * --------------------------- * this is the first question in Chapter 6th * Author luoriver */import acm. program. *; import acm. util. *; public class HalfLife extends ConsoleProgram {public void run () {println ("this program is used to simulate the atomic half-life "); println ("There are" + RADIOACTIVE_ACTOM_NUM + "actom initially"); int left_num = week; int year = 0; while (left_num> 0) {int new_actom_num = Week (left_num ); left_num-= new_actom_num; year ++; println ("There are" + left_num + "actom at the end of year" + year); new_actom_num = 0 ;}} // defines the number of attenuation atoms. private int left_halflife_actom_num (int num) {int actom_num = 0; for (int I = num; I> 0; I --) {while (rgen. nextBoolean () {actom_num ++; break;} return actom_num;}/* create an instance variable for the random number generator */private RandomGenerator rgen = RandomGenerator. getInstance ();/*** defines the number of radioactive atoms * @ RADIOACTIVE_ACTOM_NUM */private static final int RADIOACTIVE_ACTOM_NUM = 10000 ;}

5,
Package SixthCharter;/** File: Poker. java * --------------------------- * This is the 6th question in Chapter 1 * Author luoriver */import acm. program. *; import java. util. upload; import acm. util. *; public class Bingo extends ConsoleProgram {public void run () {/** first, ask if you want to introduce the gambling rule. If you want to write and read the text later, it will not be replaced by a number, 1 indicates continuing. 2 indicates not playing */println (); int know_rules = readInt ("do you want to know the rules of gambling machine? 1 indicates you want to know about the rule. 2 indicates you already know the rule: "); if (know_rules = 1) {println (/*" there are too many contents. I didn't write it yet, let's take a look at the science and art pages of the Java language, "*/RULES);} println (" get started! Have a good time! "); Start_play ();} private void start_play () {int remain_money = CLIENT_INIT_MONEY; print_remain_money (remain_money); while (remain_money> 0) {remain_money --; if (remain_money = 0) {println ("");} String first_show = show_result (); // print ("111111" + first_show); String second_show = show_result (); // println ("22222" + first_show); String third_show = show_result (); // print ("333333333" + first_show);/* specify num = New partition (System. in); int n = num. nextInt (); */int n = readInt (); if (n = 2) break; // print_remain_money (remain_money); if (first_show.equals ("BAR ") & second_show.equals ("BAR") & third_show.equals ("BAR") {remain_money + = 250; println (first_show + "" + second_show + "" + third_show + "you win"); print_remain_money (remain_money);} else if (first_show.equals ("BELL ") & second_show.equals ("BELL ")&&( Third_show.equals ("BELL") | third_show.equals ("BAR") {remain_money + = 20; println (first_show + "" + second_show + "" + third_show + "you win"); print_remain_money (remain_money);} else if (first_show.equals ("PLUM ") & second_show.equals ("PLUM") & (third_show.equals ("PLUM") | third_show.equals ("BAR") {remain_money + = 14; println (first_show + "" + second_show + "" + third_show + "you win"); prin T_remain_money (remain_money);} else if (first_show.equals ("ORANGE") & second_show.equals ("ORANGE") & (third_show.equals ("ORANGE ") | third_show.equals ("BAR") {remain_money + = 10; println (first_show + "" + second_show + "" + third_show + "you win "); print_remain_money (remain_money);} else if (first_show.equals ("CHERY") & second_show.equals ("CHERY") & third_show.equals ("CHERY") {remain_money +; Println (first_show + "" + second_show + "" + third_show + "you win"); print_remain_money (remain_money);} else if (first_show.equals ("CHERY ") & second_show.equals ("CHERY") {remain_money + = 5; println (first_show + "" + second_show + "" + third_show + "you win "); print_remain_money (remain_money);} else if (first_show.equals ("CHERY") {remain_money + = 2; println (first_show + "" + second_show + "" + th Ird_show + "you win"); print_remain_money (remain_money);} else {println (first_show + "" + second_show + "" + third_show + "you loss "); print_remain_money (remain_money) ;}// print_remain_money (remain_money);} private void print_remain_money (int remain) {println ("you have $" + remain + "wowould you like to play? ");} Private String show_result () {int random_show = rgen. nextInt (1, 6); switch (random_show) {case 1: return "BAR"; case 2: return "LEMON"; case 3: return "ORANGE"; case 4: return "PLUM"; case 5: return "BELL"; default: return "CHERRY ";}} /** set the system to give the user initial money */private static final int CLIENT_INIT_MONEY = 50;/** create an instance variable for the random number generator */private RandomGenerator rgen = RandomGener Ator. getInstance (); private static final String RULES = "a typical gambling machine has three wheels and they rotate behind a narrow window. Every wheel is marked as CHERRY, LEMON, ORANGE, PLUM, BELL, and BAR. However, the window can only see one symbol of one wheel at a time. "+" The following shows a typical winning Mode and Its Related returns: "+" BAR -- pay $250 "+" BELL/BAR -- pay $20 "+" PLUM/BAR -- pay $14 "+" ORANGE-ORANGE -ORANGE/BAR -- pay $10 "+" CHERRY-CHERRY -- pay $7 "+" CHERRY--- pay $5 "+" CHERRY --- -- pay $2 ";};

6. 7 merged
Package SixthCharter;/** File: Poker. java * --------------------------- * This is the 6th question in Chapter 1 * Author luoriver */import acm. program. *; import java. util. upload; import acm. util. *; public class Calculate extends ConsoleProgram {public void run () {int first = rgen. nextInt (0, 20); int second = rgen. nextInt (0, 20); sys_question (first, second);}/** method for defining a system issue */private void sys_question (int first, int second) {for (int I = 0; I <PROBLEM_NUM; I ++) {first = rgen. nextInt (0, 20); second = rgen. nextInt (0, 20); int sys_answer = first + second; if (sys_answer <= 20) {print (first + "+" + second + "= "); answer (sys_answer);} else {I -- ;}}/** define the student's answer Method */private void answer (int sys_answer) {int student_answer = readInt (); if (student_answer = sys_answer) {println (success () ;}else {for (int I = 0; I <ANSWER_NUM; I ++) {println ("Incorrect answer, please enter it again"); student_answer = readInt () ;}println ("The correct answer is" + sys_answer );}} /** show the encouragement information after entering the correct answer */private String success () {int n = rgen. nextInt (1, 3); switch (n) {case 1: return "You got it"; case 2: return "connect"; default: return "That's the answer" ;}}/** create an instance variable for the random number generator */private RandomGenerator rgen = RandomGenerator. getInstance ();/** defines the number of questions */private static final int PROBLEM_NUM = 5; /** define the number of student error answers */private static final int ANSWER_NUM = 3 ;}

8,
Package SixthCharter;/** File: Poker. java * --------------------------- * This is the 6th question in Chapter 1 * Author luoriver */import acm. program. *; import acm. util. *; public class NewPoker extends ConsoleProgram {public void run () {println ("this program is a random poker player"); int level = level (); string color = color (); Card show = new Card (level, color); println ("show class" + show); println (); print (show. toString (level, color);} public class Card {public Card (int level, String color) {println ("level is" + level + "color is" + color);} public String toString (int level, String color) {return "toString method:" + level + "color:" + color;} public int getLevel () {return level ();} public String getColor () {return color ();}/** display random poker cards */private void showPoker () {int num = rgen. nextInt (1, 13); // rgen. setSeed (2); switch (num) {case 1: print ("Ace"); break; case 11: print ("Jack"); break; case 12: print ("Queen"); break; case 13: print ("King"); break; default: print (num) ;}} private String color () {int color = rgen. nextInt (1, 4); switch (color) {case 1: // print ("Spades"); return "Spades"; case 2: // print ("Diamonds "); return "Diamonds"; case 3: // print ("Clubs"); return "Clubs"; default: // print ("Hearts"); return "Hearts ";}} private int level () {int n = rgen. nextInt (1, 4); switch (n) {case 1: return LEVEL1; case 2: return LEVEL2; case 3: return LEVEL3; default: return LEVEL4 ;} /* private void color () {int color = rgen. nextInt (1, 4); switch (color) {case 1: print ("Spades"); case 2: print ("Diamonds"); case 3: print ("Clubs "); case 4: print ("Hearts");} */}/* create an instance variable for the random number generator */private RandomGenerator rgen = RandomGenerator. getInstance ();/** define the color constant of poker */private static final String CLUBS = "CLUBS"; private static final String DIMONDS = "DIMONDS "; private static final String HEARTS = "HEARTS"; private static final String SPADES = "SPADES";/** define the value of a level constant */private static final int LEVEL1 = 1; private static final int LEVEL2 = 11; private static final int LEVEL3 = 12; private static final int LEVEL4 = 13 ;}

9,
Package SixthCharter; public class LibaryRecord {public LibaryRecord (String book_name, String author, int catalog_number, String publisher, int public_date, boolean isCurrent) {this. book_name = book_name; this. author = author; this. catalog_number = catalog_number; this. publisher = publisher; this. public_date = public_date; this. isCurrent = isCurrent;} public LibaryRecord (String book_name, String author, int catalog_number, String publisher, int public_date) {this. book_name = book_name; this. author = author; this. catalog_number = catalog_number; this. publisher = publisher; this. public_date = public_date; isCurrent = true;} public String getBookName () {return book_name;} public String getAuthor () {return author;} public int getCatalogNum () {return catalog_number ;} public String getPublisher () {return publisher;} public int getPublic_date () {return public_date;} public boolean getisCurrent () {return isCurrent;} public void setisCurrent (boolean isCurrent) {this. isCurrent = isCurrent;} @ Overridepublic String toString () {return "Book_name" + book_name + "author:" + author + "directory number: "+ catalog_number +" publisher "+ publisher +" publication year: "+ public_date +" current circulation: "+ isCurrent;}/** define the title */private String book_name; private String author; private int catalog_number; private String publisher; private int public_date; private boolean isCurrent ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.