2. Plan
โช Discuss some Java standard classes
โช Write some programs using these classes
โช Learn about these classes BUT ALSO about OOP
generally
3. Recallโฆ
โช We looked at classes and objects.
โช We saw how we can define our own classes and create
objects from those classes.
โช Instead of writing our own classes, we can get used to
OOP using Java standard classes.
โ Date, SimpleDateFormat
โ Scanner
โ DecimalFormat
โ Math
4. Date class
โช Date objects have methods and attributes that can be
used to represent dates
โ millisecond precision
โช When Date objects are created, it set to the current
time.
โช The date is internally stored as a count of the number
of milliseconds since midnight 1 January, 1970.
โ Does it matter how it is stored internally?
5. Date class
โช Start by importing java.util:
import java.util.*
โช Then, in main, declare and create an object:
Date now = new Date();
โช Date objects have a method called toString() which converts
the stored time into a string that can be printed
System.out.print(now.toString());
6. Date class
โช Date also has some methods
โ boolean after(Date d)
โ boolean before(Date d)
โช Example: Write a method (in main()) that
โ Creates a Date object
โ Does some stuff โ This is so that when you run it, the two Date
objects will be different.
โ Creates another Date object
โ Confirms that the first Date object is older the second Date
object.
7. public class L3 {
public static void main(String[] args) {
Date d1 = new Date();
for (int i = 0; i < 1e6;i++) {
int j = i * i;
}
Date d2 = new Date();
if (d1.before(d2))
System.out.println("d1 is older than d2");
else
System.out.println("d1 is newer than d2");
}
}
}
Creates a Date object
Does some stuff
Creates another
Date object
Confirms that the
first Date object is
older the second
Date object.
8. SimpleDateFormat class
โช Date class has a method toString() which converts the
internal stored data format into a string.
โช But what if we want more options?
โช SimpleDataFormat objects have methods that can convert
Date objects.
9. SimpleDateFormat class
โช Start by importing java.text:
import java.text.*;
โช Create a SimpleDateFormat object and pass the format:
SimpleDateFormat SDF = new SimpleDateFormat(โdd/MMM/yyyyโ);
โช SimpleDateFormat objects have a method called format() โ it
takes a Date object and returns a string of that date in the
correct format.
System.out.println(SDF.format(d1));
10. SimpleDateFormat class
โช These are some of the formatting options1.
1Taken from "An Introduction to Object Orientated Programming with Java (5th ed.)" by C.ThomasWu
11. SimpleDateFormat class
โช Exercise: Write a program
that:
โ creates a Date object
โ Displays it with one format
โ Displays it with another
format
โ Displays what day of the year
it is
12. Scanner class
โช Scanner class is used to create objects that read data from a
source.
โช This source might be from aโฆ
โ Keyboard โ reading what a user is typing
โ File โ reading text or data from a file.
13. Scanner class
โช Start by importing java.util:
import java.util.*;
โช Then, in main(), declare and create a Scanner object. On creation,
we pass it where reads from:
Scanner scan = new Scanner(System.in);
OR
Scanner scan = new Scanner(s); //s is a String
OR
Scanner scan = new Scanner(f); // f is a file
14. Scanner class
โช Scanner objects have a method next() which returns the next
input value as a String.
โช Instead of the next() method, which returns a String:
โ int nextInt() โ returns the next input value as an int.
โ byte nextByte() โ returns the next input value as a byte
โ float nextFloat() โ returns the next input value as a float
โ โฆ
โช Also several functions which check if there is anything in the
scanner:
โ bool hasNextInt() โ returns true if the scanner object has an integer input
โ bool hasNextFloat() โ returns true if the scanner object has a float input
โ โฆ
15. Scanner class
โช Exercise: Reading in a userโs name from the console:
โ Create a String and a scanner.
โ Prompt for a name
โ Use scanner to read in what comes next from its source
โ Print the name to the screen.
16. public class L3 {
public static void main(String[] args) {
String firstName;
Scanner scan = new Scanner(System.in);
System.out.print("Please enter your name: ");
firstName = scan.next();
System.out.println("Welcome, " + firstName);
}
}
Create a String and
a scanner
Prompt for a name
Use scanner to read
in what comes next
from its source
Print the name to
the screen
17. Scanner class
โช Exercise: Write a program that reads in a personโs first name,
surname and age, greets them by name and tells them what year
they were born in.
18. String firstName, secondName;
int age;
Scanner scan = new Scanner(System.in);
System.out.print("Enter first name: ");
firstName = scan.next();
System.out.print("Enter surname name: ");
secondName = scan.next();
System.out.print("Enter age: ");
age = scan.nextInt();
System.out.println("Hello, " + firstName + " " + secondName +
". You were born in " + (2019โage) + ".");
19. Constants
โช Similar to a variable, precede with the keyword final
โช Does not take up memory space
โช Wherever it appears in your code is replaced the constant
value when you program compiles.
โช e.g.
โช Similar to #define in C
final double PI = 3.14159
โฆ
area = 2* 3.14159 * radius;
final double PI = 3.14159;
โฆ
area = 2* PI * radius;
20. Class methods vs Object methods
โช Also called static methods vs instance methods
โช Object methods (instance methods):
โ Belong to object, not the class
โ Need to create the object first
โ Each object created from the class has its own copy of the method
โช Class method:
โ Belong to the class
โ They can be called without creating an object first
โ To be shared among all objects from the same class
โช Revisit in more detail when we write our own classes.
21. Math class
โช Math class: contains class constants and methods to
perform mathematical operations.
22. Math class
โช Part of the java.lang package, automatically included so
donโt need to import specifically.
โช NB: These are class methods and class constants
โช Because they are class methods, call them without
declaring an object:
y = Math.abs(x);
y = 2 * Math.PI * r;
y = Math.random();
โช Math.random() - Returns a random number between 0.0
and 1.0
23. Math class
โช Taken from Table 3.7 from โAn
Introduction to Object Orientated
Programming with Java (5th ed.)โ
by C. Thomas Wu
24. Math class
โช Write a program that sets the height and radius of a cylinder
randomly. Height is between 1.0 and 3.0, radius is between 5.0
and 8.0. Calculate the surface area and volume of this cylinder.
25. public class L3 {
public static void main(String[] args) {
double h, r, a, v;
h = Math.random() * 2.0 + 1.0;
r = Math.random() * 3.0 + 5.0;
a = 2*Math.PI * r * (h+r);
v = Math.PI * Math.pow(r, 2) * h;
}
}
h = Math.random();
h = Math.random() * 2.0;
h = Math.random() * 2.0 + 1.0;
h ranges from 0 to 1
h ranges from 0 to 2
h ranges from 1 to 3
26. DecimalFormat class
โช DecimalFormat class is used to provide a standard format for
displaying float point numbers.
โช We can use format specifiers like %0.1f
โช But what if we wanted to let someone else specify how many
decimal points we should use?
โช DecimalFormat objects are created with a specified format and
then any value passed to them is converted into this format.
27. DecimalFormat class
โช Start by importing java.text:
import java.text.*;
โช In main, declare and create an object. On creation, we pass a
pattern (string) that illustrates the number of decimal places:
DecimalFormat df = new DecimalFormat("0.00"); //2 dec places
DecimalFormat df = new DecimalFormat("0.000"); //3 dec places
28. DecimalFormat class
โช DecimalFormat objects have a methods format() which returns a
String version of the number to the correct number of decimal
places
float f = 1.2345f;
DecimalFormat df = new DecimalFormat("0.0");
System.out.println(f); //1.2345
System.out.println(df.format(f)); //1.2
29. DecimalFormat class
โช Amend the math class program from earlier to display the surface
area and volume correct to 1 decimal place.
30. public class L3 {
public static void main(String[] args) {
double h, r, a, v;
h = Math.random() * 2.0 + 1.0;
r = Math.random() * 3.0 + 5.0;
a = 2*Math.PI * r * (h+r);
v = Math.PI * Math.pow(r, 2) * h;
DecimalFormat df = new DecimalFormat("0.0");
System.out.println(df.format(a));
System.out.println(df.format(v));
}
}