Professional Documents
Culture Documents
2-2classes Oop
2-2classes Oop
2-2classes Oop
College Requirements
CSCR2105
Classes
Lecture 2.2
The String Class
O Because strings are so common, we don't have to
use the new operator to create a String object
8
int a, b, c; // ax^2 + bx + c
double dis, root1, root2;
Scanner scan = new Scanner (System.in);
System.out.print ("Enter the coefficient of x squared: ");
a = scan.nextInt();
System.out.print ("Enter the coefficient of x: ");
b = scan.nextInt();
System.out.print ("Enter the constant: ");
c = scan.nextInt();
dis= Math.pow(b, 2) - (4 * a * c);
root1 = ((-1 * b) + Math.sqrt(dis)) / (2 * a);
root2 = ((-1 * b) - Math.sqrt(dis)) / (2 * a);
+ 9
The Date Class
java.util.Date class. You can use the Date class to create an instance for the
current date and time and use its toString method to return the date and time as
a string.
java.util.Date
The + sign indicates
public modifer +Date() Constructs a Date object for the current time.
+Date(elapseTime: long) Constructs a Date object for a given time in
milliseconds elapsed since January 1, 1970, GMT.
+toString(): String Returns a string representing the date and time.
+getTime(): long Returns the number of milliseconds since January 1,
1970, GMT. 10
+setTime(elapseTime: long): void Sets a new elapse time in the object.
The Date Class Example
For example, the following code
java.util.Date date = new java.util.Date();
System.out.println(date.toString());
11
The Random Class
From random1: 734 660 210 581 128 202 549 564 459 961
From random2: 734 660 210 581 128 202 549 564 459 961
Class Libraries
O A class library is a collection of classes that we can use
when developing programs
O The Java standard class library is part of any Java
development environment
O Its classes are not part of the Java language perose, but
we rely on them heavily
O Various classes we've already used (System ,
Scanner, String) are part of the Java standard class
library
O Other class libraries can be obtained through third party
vendors, or you can create them yourself
14
Packages
O The classes of the Java standard class library are organized
into packages
O Some of the packages in the standard class library are:
Package Purpose
java.lang General support
java.applet Creating applets for the web
java.awt Graphics and graphical user interfaces
javax.swing Additional graphics capabilities
java.net Network communication
java.util Utilities
javax.xml.parsers XML document processing 15
The import Declaration
O When you want to use a class from a package,
you could use its fully qualified name
java.util.Scanner;
java.util.Random;
O Or you can import the class, and then use just the class
name
import java.util.Scanner;
17
Summary
O String
O Math
O Date
O Random
18
NOW:
Object-Oriented
Programming: Inheritance