CS1101: Programming Methodology http://www.comp.nus.edu.sg/~cs1101x/ Aaron Tan
Jan 21, 2016
CS1101: Programming Methodology http://www.comp.nus.edu.sg/~cs1101x/
Aaron Tan
2
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
3
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: 131131 is a prime.
Enter integer: 713713 is not a prime.
4
Mini-Programming Test
Are you ready?
5
API Specification Important: Bookmark this website!
http://java.sun.com/j2se/1.5.0/docs/api/
6
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.
7
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
8
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');
9
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.
10
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.
11
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
12
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.
13
Random Numbers (2/3) See API of Random class.
14
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 random number is 48Next random number is 14Next random number is 89Next random number is 7Next random number is 44
15
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.
16
DecimalFormat (2/2)
Example: To print a value in 2 decimal places.
import java.util.*; import java.text.*;
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)); } }
Enter a value: 90.7281value = 90.7281Formatted = 90.73
17
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
18
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.
19
End of file