Advertisement
PeanutbutterMcnasty

Text files

Apr 8th, 2020
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. import java.util.Random;
  2. public class Coin {
  3.     private String sideUp; /*A String named sideUp */
  4.     int coinToss;
  5.     String fileName="myoutput.txt";
  6.    
  7.     public void toss(){
  8.         Random random = new Random(); /* Creates an instance of a random number*/
  9.        
  10.         coinToss=random.nextInt(2); /*0 to 1*/
  11.        
  12.         if (coinToss==1) {sideUp="Heads";
  13.             } /*If the random number is 0, then it sets the sideUp field to "heads".*/
  14.         else if (coinToss==0){sideUp="Tails";
  15.              } /*If the random number is 1, then it sets the sideUp field to "tails".*/
  16.     }
  17.    
  18.     public Coin(){/* no-argument constructor that calls the toss method*/
  19.         toss();
  20.     }
  21.     public String getSideUp( ) {return sideUp;
  22.     } /* A method named getSideUp that returns the value of the sideUp field*/
  23.    
  24.     public String showPastRun() {return fileName; /*A method named showPastRun that opens the file named myoutput.txt*/
  25.     }
  26. }
  27.  
  28. package Coin;
  29.  
  30. import java.util.Scanner;
  31.  
  32. import java.io.*;
  33.  
  34. /* Author: Soren Fuerst
  35. Date 4/8/2020
  36. Assignment: # 5
  37. Instructor: Sergio Pisano*/
  38.  
  39.  
  40.  
  41. public class Driver {
  42.     public static void main(String[] args) throws IOException{
  43.         int NumFacingHeads=0;
  44.         int NumFacingTails=0;
  45.         int totalToss=20;
  46.         String answer;
  47.        
  48.         Coin thisCoin=new Coin(); /* Creates a new instance of the class "Coin"*/
  49.         var in=new Scanner(System.in);
  50.        
  51.        
  52.         PrintWriter outputFile = new PrintWriter(); /*reads data from the file and displays it*/
  53.        
  54.        
  55.         outputFile.close();
  56.        
  57.         System.out.println("Does the past run need to be displayed ?");
  58.         answer=in.next();
  59.        
  60.         if (answer.equals("no")) {System.out.println("Okay");}
  61.         else if (answer.equals("yes")) {System.out.println("The first line in the file is:" + thisCoin.showPastRun() );}
  62.        
  63.        
  64.        
  65.        
  66.         System.out.println("New Coin-Toss Simulator Run:");
  67.        
  68.         System.out.println("The initial side facing up is " + thisCoin.getSideUp()); /* Displays side that is initially facing up*/
  69.         for (int myToss=0; myToss<20; myToss++) { /* The loops 20 times*/
  70.             thisCoin.toss(); /* calls on the method "toss" to toss the coin*/
  71.             if (thisCoin.getSideUp()=="Heads") {NumFacingHeads= NumFacingHeads +1; /* Keeps count of the number of Heads and the number of Tails*/
  72.             }
  73.             else if (thisCoin.getSideUp()=="Tails") {NumFacingTails= NumFacingTails +1;
  74.             }
  75.             System.out.println("Toss " + myToss + ":" + thisCoin.getSideUp()); /* displays the current side each time it is tossed*/
  76.            
  77.         }
  78.        
  79.         System.out.println("After " + totalToss+ " tosses, the number of Heads tossed is " + NumFacingHeads + " and the number of Tails tossed is " + NumFacingTails +".");
  80.        
  81.    
  82.         FileOutputStream fos=new FileOutputStream(thisCoin.showPastRun());  /*stores data from output in the myoutput.txt*/
  83.         PrintStream ps= new PrintStream(fos);
  84.         System.setOut(ps);
  85.  
  86.        
  87.     }
  88.        
  89.    
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement