the command shell
play

The Command Shell, Libraries and Clients, Formatted Printing - PowerPoint PPT Presentation

The Command Shell, Libraries and Clients, Formatted Printing Fundamentals of Computer Science Outline Graphical User Interface vs. Command Shell Starting the Command Shell Commands Compiling and Running a Java Program File


  1. The Command Shell, Libraries and Clients, Formatted Printing Fundamentals of Computer Science

  2. Outline  Graphical User Interface vs. Command Shell  Starting the Command Shell  Commands  Compiling and Running a Java Program  File Redirection and Piping  Libraries and Clients  Formatted Printing

  3. Interfacing with your Computer  GUI (Graphical User Interfaces)  Today: predominant interaction method  Windows, buttons, mouse  Advantages  Easier for novices  No commands to remember  Rich input and output capabilities 3

  4. Interfacing with your Computer  Command Line Interface (CLI)  Originally the only option  Input by typing commands  Advantages:  Can be faster for experts than a GUI  Easier to automate tasks  Easier to hook programs together 4

  5. Starting a Command Shell Windows 7 Start → type " cmd" All Programs → Accessories → Command Prompt Mac Spotlight → type "terminal" Go → Applications → Utilities → Terminal 5

  6. Getting around the Command Line Action Windows Mac OS / Unix Move into a folder cd myfolder cd myfolder Move into parent folder cd .. cd .. Move into a folder, absolute cd \Users\keith cd /Users/keith folder List files in current folder dir ls Compile program in current javac Prog.java javac Prog.java folder Run a compiled program java Prog java Prog See what is in a text file type Prog.java more Prog.java Auto-complete filenames <tab key> <tab key> Previous command <up arrow> <up arrow> 6

  7. Getting Around in the Command Shell Changing to a different drive and to different folders Looking at the contents of a folder 7

  8. File Redirection and Piping Redirecting program output to a file using > followed by the output filename. Reading input from file using < followed by the filename. Directly piping output from one program to another using pipe | 8

  9. Libraries and Clients

  10. A Foundation for Programming any program you might want to write objects build bigger programs functions and modules and reuse code graphics, sound, and image I/O http://www.flickr.com/photos/vermegrigio/5923415248/ arrays conditionals and loops Math text I/O primitive data types assignment statements 10

  11. Programs Thus Far  Problems with one big main() :  Doesn't scale to complex programs  Often find ourselves repeating similar code "Repeated public class DiceRolling code is { public static void main(String [] args) evil!" { int rolls = 0; int sum = 0; int target = ( int ) (Math.random() * 12) + 2; System. out. println("Rolling dice until I get " + target + "."); do { int dice1 = ( int ) (Math. random () * 6) + 1; int dice2 = ( int ) (Math. random () * 6) + 1; sum = dice1 + dice2; ... 11

  12. Calling our New Method  Use handy new method in DiceRolling  Add somewhere inside public class {}'s public class DiceRolling { public static int getRandomNum( int start, int end) { return ( int ) (Math. random () * (end - start + 1)) + start; } public static void main(String [] args) { int rolls = 0; int sum = 0; int target = getRandomNum(1, 12); System. out. println("Rolling dice until I get " + target + "."); do { int dice1 = getRandomNum(1, 6); int dice2 = getRandomNum(1, 6); sum = dice1 + dice2; ... 12

  13. Calling our New Method  Alternative: put method in new class  Allows us to create a class with a bunch of helper methods (just like StdIn.java, StdDraw.java ) public class RandomUtil { // Return random integer in [start, end] inclusive getRandomInt() is public static int getRandomNum( int start, int end) overloaded: { return ( int ) (Math. random () * Two methods with (end - start + 1)) + start; same name, but } different // Return random integer in [0, end] inclusive signatures (e.g. public static int getRandomNum( int end) different number of { return ( int ) (Math. random () * (end + 1)); parameters) } } 13

  14. Using our New Class  Put RandomUtil.java in same directory  Methods qualified with RandomUtil. in front public class DiceRolling { public static void main(String [] args) { int rolls = 0; int sum = 0; int target = RandomUtil . getRandomNum (2, 12); System. out. println("Rolling dice until I get " + target + "."); do { int dice1 = RandomUtil. getRandomNum (1, 6); int dice2 = RandomUtil. getRandomNum (1, 6); sum = dice1 + dice2; System. out .println(dice1 + " + " + dice2 + " = " + sum); rolls++; } while (sum != target); System. out. println("It took " + rolls + " rolls."); } } 14

  15. Terminology  Library: A module whose methods are primarily intended for use by many other programs  Client: Program that calls a library  API:  Application Programming Interface  Contract between client and implementation  https://docs.oracle.com/javase/8/docs/api/  Implementation: Program that implements the methods in an API 15

  16. Random Numbers “ The generation of random numbers is far too important to leave to chance. Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin. ” Jon von Neumann (left), ENIAC (right) 16

  17. 17

  18. "QRBG121 is a fast non-deterministic random bit (number) generator whose randomness relies on intrinsic randomness of the quantum physical process of photonic emission in semiconductors and subsequent detection by photoelectric effect. In this process photons are detected at random, one by one independently of each other. Timing information of detected photons is used to generate random binary digits - bits. The unique feature of this method is that it uses only one photon detector to produce both zeros and ones which results in a very small bias and high immunity to components variation and aging. Furthermore, detection of individual photons is made by a photomultiplier (PMT). Compared to solid state photon detectors the PMT's have drastically superior signal to noise performance and much lower probability of appearing of afterpulses which could be a source of unwanted correlations." 2700 € http://qrbg.irb.hr 18

  19. Standard Random  Standard random  Library to generate pseudo-random numbers  Application Programming Interface (API): 19

  20. Standard random, implementation public class StdRandom { // between a and b public static double uniform( double a, double b) { return a + Math. random () * (b-a); } // between 0 and N-1 public static int uniform( int N) { return ( int ) (Math. random () * N); } // true with probability p public static boolean bernoulli( double p) { return Math. random () < p; } // gaussian with mean = 0, stddev = 1 public static double gaussian() /* see Exercise 1.2.27 */ // gaussian with given mean and stddev public static double gaussian( double mean, double stddev) { return mean + (stddev * gaussian ()); } … } 20

  21. Unit Testing  Include main() method to test each library public static void main(String[] args) { int N = Integer. parseInt (args[0]); if (args.length == 2) StdRandom. setSeed (Long.parseLong(args[1])); double [] t = { .5, .3, .1, .1 }; System. out .println("seed = " + StdRandom.getSeed()); for ( int i = 0; i < N; i++) { These "random" System. out .printf("%2d " , uniform(100)); System. out .printf("%8.5f ", uniform(10.0, 99.0)); numbers are the System. out .printf("%5b " , bernoulli(.5)); same as the first System. out .printf("%7.5f ", gaussian(9.0, .2)); run! System. out .printf("%2d " , discrete(t)); System. out .println(); } % java StdRandom 5 % java StdRandom 5 % java StdRandom 5 1349544048443 seed = 1349544048443 seed = 1349544178256 seed = 1349544048443 } 72 34.23045 false 8.86067 18 23.68569 false 8.85914 72 34.23045 false 8.86067 10 47.24745 true 8.83698 71 70.97195 false 8.71287 10 47.24745 true 8.83698 65 35.25313 true 9.28941 17 93.72297 false 9.14421 65 35.25313 true 9.28941 17 34.37725 false 9.56543 24 93.54278 false 9.48963 17 34.37725 false 9.56543 52 90.80849 false 8.84883 41 52.23556 false 9.10782 52 90.80849 false 8.84883 21

  22. Using a Library public class RandomPoints { public static void main(String args[]) { int N = Integer. parseInt (args[0]); for ( int i = 0; i < N; i++) { double x = StdRandom. gaussian (0.5, 0.2); double y = StdRandom. gaussian (0.5, 0.2); StdDraw. point (x, y); } } } Use library name to invoke the method 22

  23. Modular Programming  Modular programming  Divide program into self-contained pieces.  Test each piece individually.  Combine pieces to make program.  Example: Flip N coins. How many heads?  Read arguments from user.  Flip one fair coin.  Flip N fair coins and count number of heads.  Repeat simulation, counting number of times each outcome occurs.  Plot histogram of empirical results.  Compare with theoretical predictions. 23

Recommend


More recommend