190 likes | 206 Views
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.
E N D
MoreJavaSubset101 AP Computer Science A
Double class Constructor – Double(double d) Methods- intcompareTo( Object other ) double doubleValue( ) boolean equals( Object other ) String toString( )
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?
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.
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?
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.
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?
Answer answer1 = true; answer2 = false;
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?
Answer s = “1.342” Notice that the s is in quotes meaning it is a String, and not a double
Integer class Constructor – Integer(int k) Methods- intcompareTo( Object other ) intintValue( ) boolean equals( Object other ) String toString( )
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.
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?
Answer num = 5 // primitive int s = “5” // String Object
Random class Constructor- Random( ) Methods- • double nextDouble( ) • int nextInt( int n )
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?
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.
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?
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 );