Question
Dice.java // This class simulates a fair, standard die that is used in many games. import java.util.Random; // to use random number generator public class
Dice.java
// This class simulates a fair, standard die that is used in many games.
import java.util.Random; // to use random number generator
public class Dice { private int spots; // the number of spots up on the die private static Random generator;// a random number generator used in // simulating rolling a dice, shared by all dice // so that it will be as random as possible.
//Constructor creates a single die, initially with no spots public Dice() { generator = new Random(); //creates an instance of the random spots = 0; }
//simulates rolling the die and stores the number rolled public void roll() { spots = generator.nextInt(6) + 1; //returns 1,2,3,4,5,or 6 }
//returns the value of the die public int getSpots() { return spots; } }
DiceSimulation.java
// This class simulates rolling a pair of dice 10,000 times and // counts the number of times doubles of are rolled for each different // pair of doubles.
public class DiceSimulation { public static void main(String[] args) { final int NUMBER = 10000; //the number of times to roll the dice
Dice die1 = new Dice(); // the first die Dice die2 = new Dice(); // the second die int die1Value; // number of spots on the first die int die2Value; // number of spots on the second die int count = 0; // number of times the dice were rolled int snakeEyes = 0; // number of times snake eyes is rolled int twos = 0; // number of times double two is rolled int threes = 0; // number of times double three is rolled int fours = 0; // number of times double four is rolled int fives = 0; // number of times double five is rolled int sixes = 0; // number of times double six is rolled
//ENTER YOUR CODE FOR THE ALGORITHM HERE
System.out.println ("You rolled snake eyes " + snakeEyes + " out of " + count + " rolls."); System.out.println ("You rolled double twos " + twos + " out of " + count + " rolls."); System.out.println ("You rolled double threes " + threes + " out of " + count + " rolls."); System.out.println ("You rolled double fours " + fours + " out of " + count + " rolls."); System.out.println ("You rolled double fives " + fives + " out of " + count + " rolls."); System.out.println ("You rolled double sixes " + sixes + " out of " + count + " rolls."); } }
FileStats.java
// To calculate the statistics on a file of numbers
public class FileStats { private double mean; //the arithmetic average private double stdDev; //the standard deviation
//Constructor calls calculateMean and calculateStdDev methods //and store the results in the respective instance variables public FileStats(String filename) { mean = calculateMean(filename); stdDev = calculateStdDev(filename); }
//returns the mean public double getMean() { return mean; }
//returns the standard deviation public double getStdDev() { return stdDev; }
//returns the calculated arithmetic average public double calculateMean(String filename) { //ADD LINES FOR TASK 4 return 0; }
//returns the calculated standard deviation public double calculateStdDev(String filename) { //ADD LINES FOR TASK 5 return 0; } }
numbers.txt
87.5517 72.14015 88.24248 60.524 65.38684 94.48039 84.73287 84.74978 73.78996 73.43895 88.87511 102.14769 69.58979 67.28837 86.2944 82.68129 88.12874 88.25387 66.99109 74.20329 75.20197 67.44766 54.10776 80.44553 68.16631 84.39538 67.81337 75.03716 60.58003 92.52126 86.4458 79.34192 66.26862 76.81226 86.69747 73.23652 85.20477 69.32324 75.90384 64.52881 92.67207 77.68945 94.40487 84.61558 91.14814 76.60865 80.32715 59.5361 68.06308 101.0122 77.05959 78.53418 72.97878 69.59589 76.22583 77.37863 80.31115 72.04924 61.23763 92.23593 85.58184 70.29112 80.44833 88.47428 85.27953 88.48561 73.9378 100.60825 59.43866 95.63389 88.03861 75.43641 100.30752 71.94514 75.63399 52.73916 77.02126 85.45881 73.65227 57.60761 72.68636 88.25708 65.64637 64.44595 95.05124 69.06917 73.7882 73.38605 76.63733 74.02492 68.56522 91.23228 65.43385 91.52846 97.97465 69.50513 80.97369 76.70801 72.54716 63.96352 92.94309 80.43712 74.55941 70.93657 69.48795 82.32683 65.41493 72.70078 86.15723 78.17032 68.84073 88.1814 83.88829 77.46975 65.51234 76.33697 65.86152 75.86517 85.74803 66.14806 73.99188 85.78606 76.7588 90.02813 101.65673 74.09176 73.91821 81.38637 71.37843 75.43858 74.64254 84.10398 114.38159 78.18766 86.2572 94.53111 76.38961 80.4253 64.12608 85.31756 74.40215 97.38476 70.87722 76.4422 69.86757 92.41648 67.60469 88.81843 87.43865 85.58257 81.11809 65.22183 101.44213 78.42945 77.66831 84.52499 92.88413 81.02256 67.69401 57.24612 68.51544 79.67629 73.78531 65.63023 78.14929 51.54538 94.24651 81.91614 82.3721 103.39007 92.89474 79.94447 81.0693 81.57511 72.61689 81.00382 90.33603 79.04321 80.7864 74.13121 69.58506 64.496 70.29208 73.33788 72.81388 79.10752 87.84578 90.25121 87.53426 72.553 75.9346 60.64172 81.63648 69.49212 76.96347 61.99534 86.42832 76.95493 79.26333 89.12718 99.74034 77.8477 69.8862 85.25492 79.06482 93.40826 84.5583 69.95399 76.85548 80.91593 77.26504 68.46829 66.35421 81.71502 73.42009 83.90602 96.3103 75.63198 83.59876 78.69415 95.9398 96.58408 88.66255 73.57865 80.43553 74.05519 72.64541 95.59291 90.56399 73.3472 75.41113 78.17261 57.84296 82.26169 83.19847 75.36786 69.30778 75.05681 71.9977 66.38932 91.56929 93.46911 81.77321 74.69226 56.82533 83.61781 72.17707 89.91236 100.68164 69.07241 62.78266 63.37612 81.05872 71.7273 74.83659 75.0551 71.49785 72.16406 63.85455 80.12693 74.26601 89.64922 83.05272 86.76446 75.59279 81.95292 84.97659 73.41007 81.09152 76.5587 72.61876 83.91188 69.17833 77.07083 99.88031 77.87578 99.61758 95.39488 83.14628 83.63299 71.92619 90.95537 77.43279 82.86112 68.86039 75.80596 96.04804 65.69435 83.72006 82.61719 68.27025 69.27987 72.08785 60.18323 88.30673 76.45574 91.40705 73.63181 78.07249 89.8367 66.85215 72.93442 69.1339 67.73282 68.69734 78.87906 71.45822 82.34976 71.67506 79.29054 76.91899 86.95784 91.06281 81.97375 68.82657 81.14504 79.20632 78.64948 82.19453 81.20853 95.34467 104.56158 83.52156 94.07847 74.05733 80.20349 74.55424 65.09192 72.06974 69.99781 75.78107 78.10377 75.42934 79.5421 86.62532 58.95268 76.42016 72.85267 82.33916 88.78188 94.36632 74.64514 70.5574 73.32353 78.41411 83.77247 85.13666 72.21294 83.64432 87.69978 67.53804 66.42723 54.00055 62.14697 82.07149 77.8067 75.8406 69.52774 94.02145 89.30705 85.92263 78.28182 80.48545 68.58899 67.4721 90.43034 84.00413 85.0175 44.71618 88.12366 95.03469 82.67003 62.07251 84.87225 77.53349 83.76287 76.42027 60.2768 86.92689 63.44531 61.54058 68.77069 77.35217 83.38997 70.3601 79.92947 89.80893 78.87269 82.39104 87.7386 88.2649 65.4076 73.33734 93.76012 70.30768 75.81361 75.10605 78.96974 68.94816 72.18088 79.43778 77.31248 70.12531 70.62408 77.72661 91.88511 67.64608 72.63825 79.47922 64.63572 71.48359 69.91007 69.17051 78.99053 67.64923 87.73377 85.60784 92.80034 86.3011 63.84468 86.17702 81.92014 55.63154 81.89322 85.91154 86.33087 68.07467 68.63603 78.89047 74.27233 87.36377 85.98682 75.37642 74.47025 88.92111 78.80473 67.15876 83.37338 78.77868 91.65696 83.38635 74.54059 82.14471 86.00333 55.67753 83.86297 79.05625 96.75644 85.0393 71.38097 84.87369 68.32439 69.36899 80.59316 62.86841 89.36822 69.49796 47.6238 80.93911 73.81582 57.11009 92.11345 80.81128 63.79579 72.05084 78.95413 92.6166 66.89008 59.55594 72.79631 72.82051 87.69878 86.92689 85.43215 47.22046 72.69373 73.58304 85.28244 74.80413 69.55453 77.70305 63.67533 89.73054 75.31116 81.48995 64.16906 58.10234 65.69463 71.98867 83.58436 85.33454 70.04077 85.79984 71.66328 78.08033 70.30881 76.47101 94.63828 68.75525 76.87674 74.22978 84.29078 69.56829 55.87965 70.06556 80.0197 83.36456 85.00499 79.79979 64.6724 94.67481 77.62 104.17462 76.56814 85.11861 78.61995 66.76692 54.70858 76.62502 66.05544 93.61503 90.90752 78.43702 78.50822 85.42764 79.04288 85.35606 81.86842 79.22298 92.03489 67.23125 74.94177 65.41809 86.39753 84.94775 80.80982 74.84131 73.42201 71.95065 72.46564 69.40997 65.84071 67.03539 91.64547 82.74834 72.407 73.76084 79.49976 93.08785 99.16845 64.78642 77.68263 69.29093 72.85402 81.48026 82.24329 75.86003 50.07202 90.95609 77.86649 83.70484 78.28241 90.80515 96.67001 74.71155 81.19311 86.18529 85.19409 94.77288 68.70846 77.99023 71.09352 72.01224 93.88858 66.39731 108.16772 89.11154 90.49782 73.45777 78.05073 75.41266 102.78278 70.44176 72.76786 67.77646 91.90371 81.1168 68.24555 95.85365 87.3994 76.18831 74.28549 58.40058 70.74346 89.21252 73.14 85.26236 73.72171 55.65867 68.13267 62.53153 97.1035 68.26307 64.90585 82.59003 76.11455 84.7724 87.1102 84.50413 71.00085 84.30132 66.11939 81.55091 90.2962 83.37557 81.42657 81.02477 63.63357 83.86796 88.10934 94.07655 83.25519 74.66213 81.36303 73.29894 84.35661 74.40992 55.93404 82.0424 91.09862 81.55538 90.61225 76.99226 62.71857 70.31378 77.79526 65.43094 93.00764 74.02198 83.45183 92.06534 84.66298 70.28505 71.00747 83.34534 68.75469 62.55317 95.45285 64.42049 68.96137 84.23874 69.19829 78.13228 67.05108 78.53336 77.18261 62.4269 64.84816 82.87273 76.10655 83.6609 66.32081 81.03482 78.51663 88.79446 75.39731 93.50918 77.44056 74.53417 81.31862 74.77859 80.88832 79.14863 85.71134 67.24864 87.80289 71.02678 74.10971 72.65117 92.15472 69.73331 72.54897 76.30901 81.2087 62.58771 71.06274 90.10096 90.49937 79.33953 92.88483 79.33784 64.11756 62.71725 68.92862 91.80586 80.04256 92.89169 76.92555 70.89619 73.05649 88.79214 79.26786 81.14043 72.16213 81.70249 82.52719 59.68572 74.8431 75.41958 85.40738 67.96581 72.75867 72.76078 71.10687 81.93236 60.86003 95.40893 59.01815 65.78431 82.10035 79.45528 69.51953 89.02178 76.43021 74.18797 94.96259 83.63341 74.62827 66.26081 85.73128 74.98954 96.56393 74.76603 77.07909 74.5225 81.00303 94.104 76.26313 84.72333 91.74209 71.57884 62.49655 55.36445 75.25479 79.0894 81.56684 82.59395 84.44273 81.85679 56.89617 77.14106 64.89257 86.10429 74.45051 71.79143 68.09839 85.50318 73.09867 79.2719 61.2204 80.23852 85.39248 77.8346 92.35623 88.17469 76.03963 75.22819 72.1128 89.75153 88.48878 74.483 63.60158 80.96112 77.41708 77.52862 69.61437 70.83596 101.27058 54.62198 86.86363 75.55546 70.28591 74.47699 75.42748 86.47153 77.50398 92.15863 81.45264 75.79767 77.70988 77.04747 82.2304 102.50114 71.40915 86.91291 51.58468 95.04895 69.71296 74.78601 80.36455 77.89165 92.90732 65.55564 79.75681 79.52761 80.02519 91.52732 88.22623 76.9676 53.37983 90.69627 80.37364 65.41354 76.57261 74.14232 88.5079 83.34644 57.22147 91.38161 58.58738 71.50948 88.6317 68.6086 81.81283 66.86435 76.16675 66.84146 77.87635 71.69464 55.17959 106.02085 67.20885 87.4162 77.29979 100.03413 85.30442 77.00892 74.85296 77.26145 82.10246 75.89888 91.0946 74.20703 97.16579 70.97175 79.0962 79.82704 72.79812 92.80312 82.96146 75.1204 76.07238 67.08509 79.56175 67.50765 53.32561 89.86578 68.13133 66.96257 75.62011 74.59297 90.55384 81.11784 81.67835 81.12704 84.69442 91.58952 87.25312 84.51619 85.50882 68.24079 72.81798 73.60998 84.553 76.09972 88.68947 80.73149 81.12175 76.72117 71.79116 83.04685 62.83925 77.24642 80.97798 73.63677 62.10111 72.89574 70.48059 77.69268 84.31595 94.50601 76.73663 70.01291 67.32829 93.16113 72.62911 77.03782 93.81534 86.54442 57.06529 75.27489 70.65611 82.76782 86.97033 74.41734 76.70717 94.6768 72.76343 104.51675 76.72325 86.33595 68.47243 72.67889 75.82451 79.6325 75.07563 73.20489 65.76886 60.5179 78.78835 76.08582 82.25869 94.84087 83.66436 60.93015 66.78569 84.02248 92.28458 62.1047 89.06031 67.53559 72.46335 96.81629 68.95666 71.28671 71.76765 75.07554 76.51966 78.84945 83.23548 78.37175 70.71386 70.14238 72.03713 77.17427 76.94761 90.5625 59.60918 72.09315 65.11261 69.76326 85.01179 87.80602 85.01315 61.25843 86.34914 70.01398 60.53063 89.60063 75.19235 80.72417 98.89949 71.10524 79.61011 77.27004 67.90437 80.52359 75.32259 83.42956 70.55176 81.48076 74.46681 73.83649 81.42178 90.51921 60.13337 63.50138 63.24661 75.02934 78.31694 82.2752 76.91705 88.5679 72.52321 77.68759 72.02995 63.22963 74.44467 78.5192 79.24452 82.13974 71.3815 85.55526 84.33526 77.94066 76.77014 78.08522 80.95285 72.32361 74.64712 86.23888 78.59892 87.23297 73.29069 76.87515 84.31809 80.45177 80.73499 80.18719 82.36517 87.87335 91.19618 78.61116 91.57345 90.95272 85.13843 77.58738 73.45501 63.91337 75.47032 66.50846 84.06341 75.29289 72.7699 77.35314 67.52649 54.30485 80.4313 97.3771 90.73978 72.31282 69.68987 85.5218 95.07595 75.73264 89.11485 86.8044 94.05266 71.07354 87.98383 83.69341 71.62435 86.50924 60.79498 93.89001 91.44885 80.06758 70.87183 68.79365 67.37485 66.85618 69.84459 81.60107 79.52939 78.94811 83.89742 66.76571 62.51248 91.41008 85.66299 91.85483 81.25548 81.23217 60.55608 70.12068 77.10656 85.30817 67.73176 81.94856 77.87556 79.58542 67.46597 78.10539 67.65488 87.55176 80.89476 63.18069 89.42723 70.90595 79.52877 75.20933 77.15914 68.00399 74.50924 79.863 73.90679 72.49342 65.24536 77.38876 65.40271 82.80653 81.07618 64.05383 76.47835 72.31269 72.55358 65.50772 69.93418 83.83459 70.67977 76.78484 80.61589 84.95045 73.88115 78.07585 74.95301 81.39512 90.92415 70.2662 84.50418 56.12627 90.83744 56.33198 55.65303 86.1408 77.76714 66.01274 93.65414 78.62558 63.4806 102.85264 89.67878 77.8708 72.59261 82.39275 72.6641 80.97637 80.96668 86.82793 79.39367 76.33777 69.61465 79.99578 74.41428 61.74447 101.92494 87.3626 81.53741 89.67458 75.75711 76.2776 71.94232 75.5781 84.89828 97.41488 91.39156 84.95621 88.86885 81.76318 79.55394 91.46951 80.52128 70.0674 77.55598 78.50798 93.6977 86.26837 91.04288 77.44093 64.21304 64.61628 82.17105 73.30521 81.55434 86.62379 98.23051 79.24374 84.49018 87.08791 59.23518 70.64158 78.59835 74.31415 65.11137 85.48066 89.52913 66.99514 81.9238 78.37156 76.29078 78.62542 82.19709 95.17667 84.17411 84.92686 70.61576 71.00588 70.282 73.67213 86.75329 82.29944 83.27174 90.0928 95.80382 77.13536 73.48602 69.35307 73.1504 80.13593 77.12132 80.73709 71.38892 72.35886 57.66515 67.64501 94.58148 69.9491 74.15477 84.03821 63.18038 73.29531 62.88269 62.70046 98.03653 77.40759 73.9882 78.10108 67.75776 77.73323 89.65433 94.3124 76.509 84.10045 71.18225 74.83351 89.65418 72.02244 69.31568 91.84013 71.547 83.16068 77.9907 71.90254 69.49828 88.21252 94.79328 72.59327 73.40059 78.24902 85.08828 85.75646 86.36055 70.45611 64.44989 89.22995 61.78284 79.00266 65.93448 72.63816 65.56731 61.16426 80.05777 68.403 86.67345 85.67802 72.13204 69.65935 78.06922 86.31211 77.92229 84.12742 95.01593 85.79737 84.72764 82.85776 72.54761 71.31667 74.26795 69.78701 79.86632 84.64638 80.06189 79.00369 71.56246 62.85036 77.24162 76.26584 88.26437 54.6891 89.2083 65.89823 64.11439 84.37365 75.69169 74.73818 76.32141 60.57304 82.76461 93.26318 88.95439 90.89721 90.96262 80.72261 75.79292 71.64523 72.62403 64.01729 55.17475 75.08391 83.07756 89.16827 80.40366 62.0584 73.27787 76.86528 52.63006 63.72886 77.45107 73.85847 79.17935 84.27775 89.33206 84.69207 87.98743 91.0306 76.34612 50.005 80.3425 66.55016 92.55184 78.6308 62.89835 92.89865 73.12208 70.25888 78.20564 84.92975 60.24296 68.34725 80.97193 82.0999 93.63584 83.1594 76.80643 61.85692 65.01718 66.92663 73.89072 93.58374 74.61919 69.25879 80.19341 89.28439 74.91575 62.19095 82.63517 75.9662 75.55715 72.12601 95.70854 78.22071 84.65793 82.27919 79.23137 81.95512 72.64029 64.85908 74.36251 75.93575 79.02261 91.18194 71.6328 78.80828 73.72022 60.62256 88.84339 78.23454 91.5436 92.0605 59.80725 73.91109 76.80218 75.74708 93.29324 81.05699 72.41994 61.12427 68.94054 83.59305 60.62298 76.71793 72.38314 84.53024 88.1672 86.25456 74.38036 67.05879 77.62426 92.05224 69.12863 73.90614 72.95976 83.15389 93.23235 76.04581 68.35319 78.73714 95.26654 77.65402 76.85666 68.94662 71.32969 68.83322 68.7203 86.11246 71.18457 95.13971 58.60664 77.13415 85.1445 68.40967 71.90266 71.08416 82.55547 72.93714 97.63465 82.89071 71.79693 78.48863 79.12301 80.77995 65.18414 69.53478 71.85316 84.02319 68.27006 69.70579 63.29578 67.22721 88.27539 80.68251 57.81294 82.23838 71.86818 84.1308 66.62139 88.6034 71.43951 84.01766 74.92186 94.62991 86.6664 78.68829 76.70759 89.51023 79.13245 85.53641 79.40075 80.97254 60.70303 67.84134 79.35888 96.52985 81.64811 63.97546 93.0766 64.3103 63.89099 60.4567 71.29032 65.22241 61.03633 80.52162 87.26259 69.22054 77.56612 62.52617 90.82867 75.17704 83.24836 87.44989 77.18961 80.73307 61.36981 73.20214 65.60051 70.3341 81.13301 73.98139 84.84819 72.74833 69.33431 79.39233 97.26758 71.50671 66.35525 70.82195 63.4789 86.8369 84.71827 75.0634 63.37901 82.69659 75.059 69.48986 66.99944 101.46429 79.03554 53.13544 78.75328 79.30266 81.36466 64.30267 62.11677 63.64082 77.29173 77.86965 55.00324 81.7236 70.04848 88.80027 80.38401 77.06068 78.85449 85.08577 88.66831 86.44669 83.16241 80.62866 97.94318 70.54929 90.62189 75.29753 78.89969 74.1739 76.99361 74.0119 77.19365 73.05837 71.53182 75.30515 83.12169 78.24815 69.55854 76.75972 73.26779 90.69361 83.99505 77.52811 77.9704 73.09404 68.79002 82.88062 87.51853 90.28885 87.08096 74.55743 78.97351 57.8813 89.93696 93.52619 81.64376 58.57664 87.81355 77.42358 72.76965 69.70348 66.28002 87.71327 83.17485 73.61218 81.52852 77.96741 91.44127 87.43363 64.85005 76.40631 66.79116 59.98242 55.28449 69.83314 71.91849 77.02058 85.56898 51.51544 102.47277 55.46754 72.64793 85.78865 95.85045 86.84998 71.81106 59.68313 62.54821 71.35158 96.90637 80.68485 57.68944 91.81627 93.00116 72.584 94.52368 65.24657 79.57093 79.98905 67.97266 79.88812 77.49205 73.63753 75.76553 92.07279 86.31972 70.14921 68.43497 73.2138 87.26513 59.65546 63.18837 60.17008 70.35747 81.69604 70.98093 78.71066 77.57499 75.89509 67.96861 75.54139 83.35108 69.18376 75.63429 88.91833 82.07375 72.60395 87.2036 66.15319 72.75515 67.18855 89.3786 75.10462 64.14664 85.10051 89.81193 104.22581 68.99024 56.54902 65.72735 71.91815 57.72726 71.51099 76.241 69.53789 70.94183 81.31445 85.18315 80.02679 83.37214 78.9718 60.79864 67.47375 67.94049 78.93767 64.87602 88.04097 75.87608 71.75657 62.77978 77.69776 63.6911 84.0756 57.8851 83.34901 80.77912 92.70461 77.95839 71.67517 70.82772 74.85194 88.22851 92.70894 67.22244 73.93199 69.68989 90.18592 81.01947 79.29065 74.12768 85.11827 60.86207 77.42993 93.5244 75.19347 63.10598 71.66532 69.02188 77.48878 79.58563 69.96657 83.22714 76.77111 80.48774 74.56851 71.05303 72.14795 95.81448 70.81617 86.19718 64.97506 74.98219 93.75892 71.88717 89.7976 79.94228 79.99061 78.99961 89.93819 75.91033 61.24504 86.6596 70.34993 84.37162 92.50575 76.24659 90.18562 73.28392 65.09008 65.94165 76.23176 74.65192 77.37783 72.83506 77.93596 75.09627 99.83449 82.61589 64.6474 74.19729 87.98207 86.97369 73.22796 81.40673 90.36871 84.39774 71.81669 86.85089 63.67915 78.02564 76.18042 71.29639 81.30513 81.15447 75.65187 57.39099 68.05442 85.1127 63.63414 82.74865 88.19911 83.57037 100.19715 84.12066 73.72735 75.58333 70.45536 83.77788 80.98573 82.50583 64.54117 77.16978 84.47345 64.74095 74.95468 93.04544 71.00031 72.97789 85.11013 76.82858 86.11074 75.33274 72.1078 69.27369 70.09559 64.03588 71.50014 84.64618 63.61386 65.27492 78.05415 76.59973 77.07185 75.16982 86.57617 77.33332 75.14309 95.2456 86.78811 75.65545 75.32207 87.39688 64.21725 72.5183 92.70402 73.22921 76.03847 73.35159 69.68823 67.1884 69.58643 74.04974 91.91449 80.97751 74.39448 83.70599 72.08734 85.13916 67.92012 86.12487 89.27484 91.91781 69.61901 71.99363 67.83156 68.26912 68.65122 73.64185 69.06619 85.65158 73.54699 77.20973 81.32311 70.04452 69.86096 73.81614 66.05185 73.32769 86.74462 70.42466 95.40001 89.38088 88.24837 73.1474 70.28131 79.85117 73.23549 85.43732 93.99461 93.27424 82.42719 73.49777 50.731 64.26937 69.80397 91.58853 85.03861 79.27964 77.41419 65.68233 103.5806 71.81575 66.82389 76.89817 79.93476 71.55509 80.88937 81.53524 91.8201 71.75466 78.55212 78.7544 62.78628 80.50475 71.0641 85.12677 66.89686 82.62896 77.30226 84.16126 77.94482 74.22983 48.54693 97.33252 68.93638 85.56201 76.39694 81.09886 69.39441 72.94515 74.6721 67.66778 76.2836 75.06008 69.03843 77.37435 59.74802 99.70785 66.82596 97.01207 87.21704 95.21274 76.60569 60.22117 82.30134 71.43596 73.17989 72.70401 81.24097 71.48668 84.22352 68.12119 79.20828 87.44619 75.13205 63.14282 86.62223 63.18539 82.099 77.3923 97.78007 74.19579 85.81576 66.47251 97.90209 93.16432 75.42338 74.60319 71.19334 63.38432 81.32799 58.55908 65.33194 70.16438 64.61792 71.30604 59.54436 83.77759 69.53628 62.10133 66.97737 88.10323 83.40343 88.00685 70.54115 62.88813 78.83322 72.85465 101.11176 68.77166 75.05464 79.36636 76.55622 85.70121 71.93375 64.20207 67.45855 65.91901 66.38352 50.39633 84.75471 72.47579 93.11742 80.98176 70.68854 75.13279 88.30081 67.36909 71.84834 85.82212 77.40334 74.53495 76.98956 65.31407 69.99211 66.88967 58.87796 69.28372 74.96961 87.17889 81.91579 69.13166 95.59312 75.81318 66.81428 95.31337 70.86038 76.27653 46.15056 64.74466 88.61223 88.2354 85.02894 75.75618 79.15912 79.71804 87.97143 70.60301 70.75886 68.85943 60.46628 74.86774 74.66303 78.14973 83.94448 66.62921 64.03663 77.60795 55.78799 96.26119 88.04583 61.33679 81.15492 73.16168 75.51413 80.57335 67.95203
Objectives
Be able to convert an algorithm using control structures into Java
Be able to write a while loop
Be able to write an do-while loop
Be able to write a for loop
Be able to use the Random class to generate random numbers
Be able to use file streams for I/O
Be able to write a loop that reads until end of file
Be able to implement an accumulator and a counter
Introduction
This is a simulation of rolling dice. Actual results approach theory only when the sample size is large. So we will need to repeat rolling the dice a large number of times (we will use 10,000). The theoretical probability of rolling doubles of a specific number is 1 out of 36 or approximately 278 out of 10,000 times that you roll the pair of dice. Since this is a simulation, the numbers will vary a little each time you run it.
We will continue to use control structures that we have already learned, while exploring control structures used for repetition. We shall also continue our work with algorithms, translating a given algorithm to java in order to complete our program. We will start with a while loop, then use the same program, changing the while loop to a do-while loop, and then a for loop. We will be introduced to file input and output. We will read a file, line by line, converting each line into a number. We will then use the numbers to calculate the mean and standard deviation.
First we will learn how to use file output to get results printed to a file. Next we will use file input to read the numbers from a file and calculate the mean. Finally, we will see that when the file is closed, and then reopened, we will start reading from the top of the file again so that we can calculate the standard deviation.
Task #1 While loop
1. In lab this lab, for code listing in lab for Dice.java and DiceSimulation.java,make sure to place them both in the same directory. You can compile both programs.
Dice.java is complete and will not be modified in this lab, but DiceSimulation.java is incomplete. Since there is a large part of the program missing, the output will be incorrect if you run DiceSimulation.java.
2. You will be modifying the DiceSimulation class only. I have declared all the variables. You need to add what the method does. Convert the algorithm below to Java and place it in the main method after the variable declarations, but before the output statements. You will be using several control structures: a
while loop and an if-else-if statement nested inside another if statement. Use the indenting of the algorithm to help you decide what is included in the loop, what is included in the if statement, and what is included in the nested if-else-if statement.
Repeat while the number of dice rolls are less than the number of times the dice should be rolled.
Roll the first die
Get the value of the first die
Roll the second die
Get the value of the second die
If the value of the first die is the same as the value of the second die
If value of first die is 1
Increment the number of times snake eyes were rolled
Else if value of the first die is 2
Increment the number of times twos were rolled
Else if value of the first die is 3
Increment the number of times threes were rolled
Else if value of the first die is 4
Increment the number of times fours were rolled
Else if value of the first die is 5
Increment the number of times fives were rolled
Else if value of the first die is 6
Increment the number of times sixes were rolled
Increment the number of times the dice were rolled
3. Compile and run. You should get numbers that are somewhat close to 278 for each of the different pairs of doubles. Run it several times. You should get different results than the first time, but again it should be somewhat close to 278.
Task #2 Using Other Types of Loops
1. Change the while loop to a do-while loop. Compile and run. You should get the same results.
2. Change the do loop to a for loop. Compile and run. You should get the same results.
Task #3 Reading and Writing Using Files
1. Refer to Lab 5 in lab, FileStats.java below, you can compile FileStats.java. It will compile without errors so that you can use it to test out the StatsDemo class you will be creating.
2. Create a class called StatsDemo which consists of a main method to do the following:
a) Create a DecimalFormat object so that we can format our numbers for output with 3 decimal places (Dont forget the needed import statement).
b) Create a Scanner object to get the file name input from the user (Dont forget the needed import statement).
c) Prompt the user and read in the file name (Remember to declare any needed variables).
d) Create a FileStats object passing it the file name.
e) Create a PrintWriter object passing it the filename Results.txt (Dont forget the needed import statement).
f) Since you are using a PrintWriter object, add a throws clause to the main method header.
g) Print the mean and standard deviation to the output file using a three decimal format, labeling each.
h) Close the output file.
3. Compile, debug, and run. You should get no output to the console, but running the program will create a file called Results.txt with your output. The output you should get at this point is: mean = 0.000, standard deviation = 0.000. This is not the correct mean or standard deviation for the data, but we will fix this in the next tasks.
Task #4 The calculateMean Method
1. Open FileStats.java for editing. You will notice that the calculateMean and calculateStdDev methods do not do any calculations yet. They simply return a 0 to the constructor to initialize the instance variables. We need to add lines to each of these methods to have them return the correct value. Lets work with the calculateMean method first.
2. Create a File object passing it the filename (Dont forget the needed import statement).
3. Create a Scanner object passing it the File object.
4. Since you are using a Scanner object to open a file, add a throws clause to the calculateMean method header as well as the constructor method header (since it calls the calculateMean method).
5. Declare local variables for an accumulator of type double, a counter of type integer, and line of type String. Initialize all number variables to 0.
6. Write a loop that reads values from the file until you are at the end of the file.
7. The body of the loop will
a) read a double from the file and add the value to the accumulator
b) increment the counter
8. When the program exits the loop close the input file.
9. Calculate and return the mean instead of 0. The mean is calculated by dividing the accumulator by the counter.
10. Compile, debug, and run. You should now get a mean of 77.444, but the standard deviation will still be 0.000.
Task #5 The calculateStdDev Method
1. Do steps 2-6 as above in the calculateMean method but add another local variable called difference of type double.
2. The body of the loop will
a) read a double value from the file, subtract the mean from that value, and
store the result in difference
b) add the square of the difference to the accumulator
c) increment the counter
3. When the program exits the loop close the input file.
4. The variance is calculated by dividing the accumulator (sum of the squares of the difference) by the counter. Calculate the standard deviation by taking the square root of the variance (Use Math.sqrt ( ) to take the square root).
5. Compile, debug, and run. You should get a mean of 77.444 and standard deviation
of 10.021. Please do not alter or make changes the Number.txt file.
NOTE: The four files I provided for you are: Dice.java, DiceSimulation.java, Filestats.java and Number.txt(soft copy)
Upon completion of this lab 5, you should have these four files, DiceSimulationforLoop.java, Result.txt, and StatsDemo.java, with all the proper .class files.
NO Homework Programming Exercise assigned. Homework directory is empty.
Task #6 Please take a screen shot of the output of the DiceSimulation.class and paste here below:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started