Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 8 printf() modular arithmetic how to read assignments

Similar presentations


Presentation on theme: "Lab 8 printf() modular arithmetic how to read assignments"— 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 = "); 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 = one decimal place f1 = 123.5 two decimal places f1 = three decimal places f1 = ten decimal places f1 = <- see floating point error!

4 Gives output: Double precision defaults to printing like float each type specifier has a different effect with f = with d = with g = e+08 with s d1 = E8 with e d1 = e+08 with a d1 = 0x1.d6f bp29 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);

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: 5/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 public class HelloWorld {
public static void main(String[] args) { System.out.println("Absolute Answers to math section of test"); System.out.println(" * 2 = " + (2 + 3 * 2) ); System.out.println(" / 4 = " + ( / 4) ); System.out.println(" \"GSU\" = " + ( "GSU") ); System.out.println(" \"CS\" = " + ("CS" ) ); System.out.println(" 5 % 12 * 3 / 2 = " + (5 % 12 * 3 / 2) ); } Absolute Answers to math section of test 2 + 3 * 2 = 8 / 4 = 4.5 "GSU" = 3GSU "CS" = 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)


Download ppt "Lab 8 printf() modular arithmetic how to read assignments"

Similar presentations


Ads by Google