Download presentation
Presentation is loading. Please wait.
Published bySusan Strickland Modified over 9 years ago
1
Utility classes 1.Vector class 2.Random class 3.Date class 4.Scanner
2
Vector is a class contained in the java.util package defines methods to store objects into single unit.it is a dynamic array which can accommodate any number of any type of objects. Vector v1=new Vector() Creats a vector without specifying its initial capacity. Default value is 10 Vector v1=new Vector(n) Creates a vector having initial capacity ‘n’ Note:One limitation of vectors is that we cannot store data items of primittive datatypes like int float,char,long double etc since it can store only objects.Therefore primittive datatypes need to be converted into objects before storing them in vectors. To accomplish this java provides wrapper classes contained in the java.lang package.the wrapper classes are used to convert the primittive datatypes to their respective class instatnces
3
Primittive DataTypeWrapper class booleanJava.lang.Boolean ByteJava.lang.Byte charJava,lang.Character intJava,lang.Integer longJava.lang.Integer shortJava.lang.Short floatJava.lang.Float doubleJava.lang.Double Constructor callingAction Performed Integer a=new Integer(i)Converts integer to Integer object Float f=new Float(ff)Converts float to Float Object Double d=new Double(dd)Converts double to Double object
4
Method CallingAction Performed int i=a.intValue()Converts object to integer float f=b.floatValue()Converts object to float double d=c.doubleValue()Converts object to double long l=x.longValue()Converts object to long Method callingAction performed Str=Integer.toString(i)Converts primitive integer to string str=Float.toString(f)Converts primitive float to string Str=Long.toString(l)Converts primitive float to string
5
Method CallingAction Performed Intval=Integer.valueOf(str)Converts string to integer object Fo=Float.valueOf(str)Converts string to Float Object lo=Double.valueOf(str)Converts string to Double Object Dod=Long.valueOf(str)Converts string to long Object Method CallingAction Performed Int i=Integer.parseInt(str)Converts string to primittive integer Long l=Long.parseLong(str)Converts string to primittive long
6
class wrapper {public static void main(String a[]) {int x=10; Integer xx=new Integer(x); System.out.println(xx); String str=xx.toString(); System.out.println(str); int y=Integer.parseInt(str); System.out.println(y); int y1=xx.intValue(); System.out.println(y1); xx=Integer.valueOf(str); System.out.println(xx); str=Integer.toString(x); System.out.println(str); }
7
import java.util.*; class Vect {public static void main(String a[]) {Vector v=new Vector(); String s=new String("Hello"); String s1="GoodMorning"; v.addElement(s); v.addElement(s1); int ix=10; Integer i=new Integer(ix); v.addElement(i); float fx=10.4f; Float f=new Float(fx); v.addElement(f);
8
System.out.println(v.size());//4 System.out.println(v.capacity()); //10 System.out.println(v.firstElement());//Hello System.out.println(v.lastElement());//10.4 System.out.println(v); //hello GoodMorning 10 10.4 System.out.println(v.elementAt(1));//Good Morning v.removeElementAt(1); System.out.println(v);//hello 10 10.4 v.removeElement(f); System.out.println(v);//hello 10 v.trimToSize(); System.out.println(v.capacity());//2 }
9
Random class The Random class is part of the java.util package It provides methods that generate random numbers Constructor: Random( ) Random r = new Random( );
10
Int nextInt() returns a random integer int nextInt(int n) Returns a random int between 0 and n-1.(inclusive) float nextFloat( ) Returns a random float between 0.0 and 1.0 inclusive double nextDouble() Returns a random double in the range [0, 1.0) boolean nextBoolean() Returns a random boolean value, which is true 50% of the time
11
import java.util.*; class rand { public static void main(String a[]) {Random r=new Random(); System.out.println(r.nextInt()); System.out.println(r.nextInt(10)); System.out.println(r.nextFloat( )); System.out.println(r.nextDouble()); System.out.println(r.nextBoolean());} 90493099 1 0.6003581 0.1208000378695192 true -7262219 8 0.7282712 0.6081510746836057 false
12
Date Class Date() This constructor allocates a Date object and initializes it so that it represents the time at which it was allocated Date(long date) This constructor allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time, namely January 1, 1970, 00:00:00
13
. Class methods boolean after(Date when) This method tests if this date is after the specified date. boolean after(Date when) boolean before(Date when) This method tests if this date is before the specified date. boolean before(Date when) int compareTo(Date anotherDate) This method compares two Dates for ordering. int compareTo(Date anotherDate) boolean equals(Object obj) This method compares two dates for equality. boolean equals(Object obj) long getTime() This method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object. long getTime() void setTime(long time) This method sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT. void setTime(long time) String toString() This method converts this Date object to a String of the form. String toString() Object clone() This method return a copy of this object.
14
import java.util.*; class Dat {public static void main(String a[]) {Date d=new Date();Date d1=new Date(4000); System.out.println(d);System.out.println(d1); System.out.println(d1.after(d));//false System.out.println(d1.before(d));//true System.out.println(d1.compareTo(d));//-1 System.out.println(d.compareTo(d1));//1 Date d2=(Date)d.clone(); System.out.println(d2.compareTo(d));//0 System.out.println(d2.equals(d));//true System.out.println(d1.getTime());//4000 d2.setTime(4000); System.out.println(d2.equals(d1));//true }
15
The java.util.Scanner class is a simple text scanner which can parse primitive types and strings using regular expressions. constructors Scanner(File source) This constructs a new Scanner that produces values scanned from the specified file. Scanner(InputStream source) This constructs a new Scanner that produces values scanned from the specified input stream.. Scanner class
16
import java.util.*; class Dat {public static void main(String a[]) {Scanner s=new Scanner(System.in); int x=s.nextInt();float y=s.nextFloat(); short h=s.nextShort(); double d=s.nextDouble(); byte b=s.nextByte(); String g=s.nextLine();System.out.println("The Output is"); System.out.println(x);System.out.println(y); System.out.println(h);System.out.println(d); System.out.println(b);System.out.println(g); }
17
12 3 4 56 78 ajfvxhqvxdgqs The Output is 12 3.0 4 56.0 78 ajfvxhqvxdgqs
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.