1 / 32

Self Check

Self Check. Which are the most commonly used number types in Java? Suppose x is a double. When does the cast (long) x yield a different result from the call Math.round(x) ?

harris
Télécharger la présentation

Self Check

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. Self Check • Which are the most commonly used number types in Java? • Suppose x is a double. When does the cast (long)x yield a different result from the call Math.round(x)? • How do you round the double value x to the nearest int value, assuming that you know that it is less than 2 · 109?

  2. Answers • int and double • When the fractional part of x is ≥ 0.5 • By using a cast: (int) Math.round(x)

  3. Self Check • What is the difference between the following two statements?and • What is stylistically wrong with the following statement? final double CM_PER_INCH = 2.54; public static final double CM_PER_INCH = 2.54; double circumference = 3.14 * diameter;

  4. Answers • The first definition is used inside a method, the second inside a class • (1) You should use a named constant, not the "magic number" 3.14(2) 3.14 is not an accurate representation of π

  5. Self Check • What is the meaning of the following statement? • What is the value of n after the following sequence of statements? n--;n++;n--; balance = balance + amount;

  6. Answers • The statement adds the amount value to the balance variable • One less than it was before

  7. Self Check • What is the value of 1729 / 100? Of 1729 % 100? • Why doesn't the following statement compute the average of s1, s2, and s3? • What is the value of in mathematical notation? double average = s1 + s2 + s3 / 3; Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2))

  8. Answers • 17 and 29 • Only s3 is divided by 3. To get the correct result, use parentheses. Moreover, if s1, s2, and s3 are integers, you must divide by 3.0 to avoid integer division: (s1 + s2 + s3) / 3.0

  9. Self Check • Why can't you call x.pow(y) to compute xy? • Is the call System.out.println(4) a static method call?

  10. Answers • x is a number, not an object, and you cannot invoke methods on numbers Additionally, pow is static method of the Math class, and must be invoked by Math.pow(n) 12. No–the println method is called on the object System.out

  11. Self Check • Assuming the String variable s holds the value "Agent", what is the effect of the assignment s = s + s.length()? • Assuming the String variable river holds the value "Mississippi", what is the value of river.substring(1, 2)? Of river.substring(2, river.length() - 3)?

  12. Answers • s is set to the string Agent5 • The strings "i" and "ssissi"

  13. Strings • A string is a sequence of characters • Strings are objects of the String class • String constants: • String variables: • String length: • Empty string: "Hello, World!" String message = "Hello, World!"; int n = message.length(); ""

  14. Concatenation • Use the + operator: • If one of the arguments of the + operator is a string, the other is converted to a string String name = "Dave";String message = "Hello, " + name; // message is "Hello, Dave" String a = "Agent";int n = 7;String bond = a + n; // bond is Agent7

  15. Concatenation in Print Statements • Useful to reduce the number of System.out.print instructions versus System.out.print("The total is ");System.out.println(total); System.out.println("The total is " + total);

  16. Converting between Strings and Numbers • Convert to number: • Convert to string: int n = Integer.parseInt(str);double x = Double.parseDouble(str); String str = "" + n;str = Integer.toString(n);

  17. Substrings • Supply start and “past the end” position • First position is at 0 String greeting = "Hello, World!";String sub = greeting.substring(0, 5); // sub is "Hello" Continued… Figure 3:String Positions

  18. Substrings • Substring length is “past the end” - start Figure 4:Extracting a Substring

  19. How do we find out the other methods which String objects provide?? www.java.sun.com

  20. Self Check • Why can't input be read directly from System.in? • Suppose in is a Scanner object that reads from System.in, and your program callsString name = in.next();What is the value of name if the user enters John Q. Public?

  21. Answers • The class only has a method to read a single byte. It would be very tedious to form characters, strings, and numbers from those bytes. • The value is "John". The next method reads the next word.

  22. Self Check • How can you compute the length of the string "Mississippi"? • How can you print out the uppercase version of "Hello, World!"? • Is it legal to call river.println()? Why or why not?

  23. Answers • It is not legal. The variable river has type String. The println method is not a method of the String class. river.length() or "Mississippi".length() System.out.println(greeting.toUpperCase());

  24. Self Check String river = “Mississippi”; String greeting = “Hello World”; • What are the implicit parameters, explicit parameters, and return values in the method call river.length()? • What is the result of the call river.replace("p", "s")? • What is the result of the call greeting.replace("World", "Dave").length()?

  25. Answers • The implicit parameter is river. There is no explicit parameter. The return value is 11 • "Missississi" • 12

  26. Self Check String greeting = “Welcome”; String greeting2 = “How are you?”; • What is the effect of the assignment greeting2 = greeting? • After executing the statement greeting2.toUpperCase(); what are the contents of greeting and greeting2?

  27. Answers • Now greeting and greeting2 both refer to the same String object. • Both variables still refer to the same string, and the string has not been modified. Recall that the toUpperCase method constructs a new string that contains uppercase characters, leaving the original string unchanged.

  28. The API Documentation • API: Application Programming Interface • Lists classes and methods in the Java library • http://java.sun.com/j2se/1.5/docs/api/index.html

  29. The API Documentation of the Standard Java Library Figure 13:The API Documentation of the Standard Java Library

  30. The API Documentation for the Rectangle Class Figure 14:The API Documentation of the Rectangle Class

  31. Javadoc Method Summary Figure 15:The Method Summary for the Rectangle Class

  32. translate Method Documentation Figure 16:The API Documentation of the translate Method

More Related