1 / 11

Lab 8

Lab 8. printf() modular arithmetic how to read assignments what is the time-space tradeoff? discuss data cleanup algorithm give midterms back, discuss how grades are calculated last day to drop classes is Tues the October 15!. System.out.printf(). Another way to print output.

jun
Download Presentation

Lab 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab 8 • printf() • modular arithmetic • how to read assignments • what is the time-space tradeoff? • discuss data cleanup algorithm • give midterms back, discuss how grades are calculated • last day to drop classes is Tues the October 15!

  2. System.out.printf() • Another way to print output. • Probably the most powerful of the print statements due to its high configurability. • Makes printing types as other types fairly easy. • like System.out.print() you must specific newlines when you want them.

  3. System.out.println("To print a float you specify %f in the printf"); System.out.println("and pass it an argument which it prints as a float"); System.out.println("Our floating point number f1 = 123.4567"); System.out.printf("System decimal places = %f \n", f1); System.out.printf(" one decimal places = %.1f \n", f1); System.out.printf(" two decimal places = %.2f \n", f1); System.out.printf(" three decimal places = %.3f \n", f1); System.out.printf(" ten decimal places = %.10f <- see floating point errror!\n", f1); • Using the correct printf syntax allows you to customize your output WITHOUT having to modify your variable. • Gives output: • To print a float you specify %f in the printf • and pass it an argument which it prints as a float • System decimal places f1 = 123.456703 • one decimal place f1 = 123.5 • two decimal places f1 = 123.46 • three decimal places f1 = 123.457 • ten decimal places f1 = 123.4567031860 <- see floating point error!

  4. System.out.println("Double precision defaults to printing like float"); System.out.println("each type specifier has a different effect"); System.out.printf("with f = %f \n", d1); System.out.printf("with d = %d \n", (int)d1); System.out.printf("with g = %g \n", d1); System.out.printf("with s d1 = %s \n", d1); System.out.printf("with e d1 = %e \n", d1); System.out.printf("with a d1 = %a \n", d1); • Gives output: • Double precision defaults to printing like float • each type specifier has a different effect • with f = 987654321.012346 • with d = 987654321 • with g = 9.87654e+08 • with s d1 = 9.876543210123457E8 • with e d1 = 9.876543e+08 • with a d1 = 0x1.d6f345881948bp29

  5. Modular Arithmetic • System of arithmetic where numbers wrap around, much the same way they do on a clock face. • Excellent resource on modular arithmetic: • http://www.math.cornell.edu/~morris/135/mod.pdf • the modulo operator is ‘%’ • and a quick example....

  6. 5 % 7 = 12 % 7 = 26 % 7 • = 5 • 15 % 7 = 8 % 7 = 22 % 7 • = 1

  7. publicclassHelloWorld { publicstaticvoidmain(String[] args) { System.out.println("Absolute Answers to math section of test"); System.out.println(" 2 + 3 * 2 = " + (2 + 3 * 2) ); System.out.println(" 2.5 + 10 / 4 = " + (2.5 + 10 / 4) ); System.out.println(" 1 + 2 + \"GSU\" = " + (1 + 2 + "GSU") ); System.out.println(" \"CS\" + 3 + 7 = " + ("CS" + 3 + 7) ); System.out.println(" 5 % 12 * 3 / 2 = " + (5 % 12 * 3 / 2) ); } } • Absolute Answers to math section of test • 2 + 3 * 2 = 8 • 2.5 + 10 / 4 = 4.5 • 1 + 2 + "GSU" = 3GSU • "CS" + 3 + 7 = CS37 • 5 % 12 * 3 / 2 = 7 • WHY? (java book section 2.7) • Precedence and Associativity

  8. How to read questions? • “Write a Java program, Mowing.java, that reads in the length (in yards) and width (in yards) of a rectangular yard and the length (in yards) and width (in yards) of a rectangular house situated in the yard. Your program should compute the time required (in minutes rounded off to second decimal place) to cut the grass at the rate of 2.3 square meters per second. A sample run of the program is shown below.” • What are we asking you to do?

  9. Unpack the question: • 1. Identify what the question is giving you. • a. length and width are in yards • b. velocity is in meters/second • c. one area is inside the other area, so you will be subtracting it out. • 2. Identify what the question is asking for. • a. output is in minutes • -this requires a conversion from yards to meters • -this requires a conversion from seconds to minutes • -this requires eliminating distance to arrive a time • b. program reads data • -this requires a means of reading input • c. program must round output correctly • You now have all the information you need to go about formulating an algorithm to solve the problem.

  10. time-space tradeoff • this was discussed in class wed/thurs • in a nutshell, sometimes you can see a speed increase by using more space. • ie: with the data cleanup algorithm discussed in class. instead of trying to do this in place with an O(n2) sorting algorithm, create an output array and use an O(n) scan algorithm instead.

  11. data cleanup algorithm • There are concise notes on this posted on the course website. (notes for 10/09/2013)

More Related