Transcript Summary
CS1101: Programming Methodology
http://www.comp.nus.edu.sg/~cs1101x/
Aaron Tan
This is Week 5
Last week:
Repetition constructs
‘while’, ‘do … while’, ‘for’
Testing and Debugging
This week:
Last week’s Exercise 5 (Prime number)
A mini programming test! (argh!)
Chapter 5: Using Pre-Built Methods
Other classes: Random, DecimalFormat
2
Last week’s exercise 5
Primality test is a classic programming problem.
Given a positive integer, determine whether it is a
prime number or not.
A prime number has two distinct factors (divisors): 1
and itself. Examples: 2, 3, 5, 7, 11, …
Write a program PrimeTest.java. Some sample runs
shown below:
Enter integer: 131
131 is a prime.
Enter integer: 713
713 is not a prime.
3
Mini-Programming Test
Are you ready?
4
API Specification
Important: Bookmark this website!
http://java.sun.com/j2se/1.5.0/docs/api/
5
Overloaded Methods (1/2)
Refer to the abs() method in Math class.
public static int abs(int num)
Returns the absolute value of num.
public static double abs(double num)
Returns the absolute value of num.
Note that there are two ‘versions’ of abs().
This is called overloading.
Just as we have overloaded operators (eg: ‘+’ that
serves as addition or concatenation), we also have
overloaded methods.
6
Overloaded Methods (2/2)
Methods can share the same name as long as
they
have a different number of parameters (Rule 1)
or
their parameters are of different data types when
the number of parameters is the same (Rule 2)
public void myMethod(int x, int y) { ... }
public void myMethod(int x) { ... }
Rule 1
public void myMethod(double x) { ... }
public void myMethod(int x) { ... }
Rule 2
7
Instance Methods and Class Methods (1/3)
There are two types of method.
Instance method
Operates on a variable (instance) of a class
Example: The length() method in String class
String str = "Hello";
int len = str.length();
Class method
Do not need to call it on a variable.
Examples:
double ans = Math.abs(-4.5);
char ch = Character.toUpperCase('e');
8
Instance Methods and Class Methods (2/3)
How do we know a method is an instance
method or a class method?
Look at the API page
If you see the ‘static’ modifier, it is a class method.
If not, then it is an instance method.
abs() is a class method.
length() is an instance
method.
9
Instance Methods and Class Methods (3/3)
Some classes provide only class methods
Example: Math
Some classes provide only instance methods
Example: Scanner
Some classes provide a mix
Example: String
We will learn more about classes and
instances (objects) after the recess.
10
Some Other Classes
I would like to introduce 2 more classes:
Random class
DecimalFormat class
Compare them with
random() method in Math class
printf() method
11
Random Numbers (1/3)
We learnt about the random() method in the Math
class.
We will learn another way here, using the Random
class.
Notes:
Need to import java.util.*;
Random(): constructs a new random number generator
Random whose seed is based on the current time.
int nextInt(int n): returns the next pseudo-random, from the
interval [0, 1, … n-1], uniformly distributed value of a Random
object.
Refer to the API specification for the complete description of
class Random.
12
Random Numbers (2/3)
See API of Random class.
13
Random Numbers (3/3)
Example: To generate 5 random integers in the range [0, 100).
import java.util.*;
public class TestRandom {
public static void main(String[] args) {
Random num = new Random();
for (int i=0; i<5; i++)
System.out.println("Next random number is "
+ num.nextInt(100));
}
}
Next
Next
Next
Next
Next
random
random
random
random
random
number
number
number
number
number
is
is
is
is
is
48
14
89
7
44
14
DecimalFormat (1/2)
To print a decimal value in certain format.
Notes
Need to import java.text.*;
Refer to the API specification for the complete
description of class DecimalFormat.
15
DecimalFormat (2/2)
Example: To print a value in 2 decimal places.
import java.util.*;
import java.text.*;
Enter a value: 90.7281
value = 90.7281
Formatted = 90.73
public class TestDecimalFormat {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
System.out.print("Enter a value: ");
double value = stdIn.nextDouble();
System.out.println("value = " + value);
System.out.println("formatted = " + df.format(value));
}
}
16
Announcement/Reminder
Lab #1
Deadline: 10 September (Wednesday), 2359hr.
CourseMarker
How to check CM feedback after you submit your
program? I will give a demonstration.
Programming Style
I see programs with poor style! (Bad indentation,
poor use of white spaces, etc.)
Please refer to course website, “Resources”,
“Online”, “Java Style Guides”:
http://www.comp.nus.edu.sg/~cs1101x/2_resources/online.html
17
This is Week 5
Next week?
Chapter 10: Arrays
Only sections 10.1 to 10.6
We will cover the other sections some other time.
18
End of file
19