1 / 19

MoreJavaSubset101

Learn how to use the Double class constructor and methods to work with double values in Java. Explore compareTo, doubleValue, equals, and toString methods. Also, understand the Integer class constructor and methods, as well as the Random class constructor and methods.

tbridges
Download Presentation

MoreJavaSubset101

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. MoreJavaSubset101 AP Computer Science A

  2. Double class Constructor – Double(double d) Methods- intcompareTo( Object other ) double doubleValue( ) boolean equals( Object other ) String toString( )

  3. compareTo intcompareTo(Object other) If other is not a Double, throws an exception. Otherwise, returns a negative integer, zero, or a positive integer depending on whether this Double’s value is less than, equal to, or greater than others’s value. Example: Double a = new Double( 1.5 ); Double b = new Double( 1.5 ); Double c = new Double( 1.8 ); int d = a.compareTo( b ); int e = a.compareTo( c ); What value will d and e have after the above code segment is executed?

  4. Answer d will have the value 0. e will be a negative number. It doesn’t specify that it will be -1 so you cannot assume it will be.

  5. doubleValue() double doubleValue( ) Returns the double value represented by this double. Example: Double a = new Double( 1.5 ); Double b = new Double( 1.5 ); Double c = new Double( 1.8 ); double e = a.doubleValue( ); double f = b.doubleValue( ); double g = c.doubleValue( ); What will the value of e, f, and g be after the above code segment is executed?

  6. Answer d=1.5 e=1.5 f=1.8 Note: sometimes extra zeros will be added to double number Note: there is a difference between the primitive double and the Object Double.

  7. equals boolean equals( Object other ) Returns true if other is a Double with the same values as this Double; otherwise returns false. Example Double a = new Double( 1.5 ); Double b = new Double( 1.5 ); Double c = new Double( 1.8 ); boolean answer1 = a.equals( b ); boolean answer2 = b.equals( c ); What will the value of answer1 and answer2 be after the above code is executed?

  8. Answer answer1 = true; answer2 = false;

  9. toString String toString( ) Returns a String representation of this Double. Example: Double a = new Double( 1.342 ); String s = a.toString( ); System.out.println( s ); What will the value of s be after the above code is executed?

  10. Answer s = “1.342” Notice that the s is in quotes meaning it is a String, and not a double

  11. Integer class Constructor – Integer(int k) Methods- intcompareTo( Object other ) intintValue( ) boolean equals( Object other ) String toString( )

  12. Similar to the Double class • The Integer class is very similar to the Double class. It works the same way as a the Double class except it is used to store integers as opposed to doubles. • We will only go over the intValue method.

  13. intValue int intValue( ) Returns the integer represented by this Integer. Example: Integer a = new Integer( 5 ); int num = a.intValue( ); String s = a.toString( ); What is the value of num and s after the above code is executed?

  14. Answer num = 5 // primitive int s = “5” // String Object

  15. Random class Constructor- Random( ) Methods- • double nextDouble( ) • int nextInt( int n )

  16. nextDouble double nextDouble( ) Returns the next pseudorandom double value between 0.0 and 1.0. Example: Random rand = new Random( ); double num = rand.nextDouble( ); What will the value of num be after the above code is executed?

  17. Answer • We don’t know exactly what the value will be because it will be random. All we know is that it will be in between 0.0 and 1.0. For example it could be 0.999 or 0.52. It will vary every time the code is executed.

  18. nextInt int nextInt( int n ) Returns the next psuedorandom integer value between 0( inclusive) and n(exclusive). How would you call this method to print a random number between 0 and 10?

  19. Answer First you have to create a Random object. Random r = new Random( ); Then you call the nextInt method. int answer = r.nextInt( 11 ); • Notice we passed 11 in as a parameter because 11 is exclusive, meaning it will stop at 11 but not include 11.( In other words it is stopping at 10 ) Random r = new Random( ); int answer = r.nextInt( 11 );

More Related