190 likes | 318 Views
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!)
E N D
CS1101: Programming Methodologyhttp://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
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.
Mini-Programming Test Are you ready?
API Specification • Important: Bookmark this website! • http://java.sun.com/j2se/1.5.0/docs/api/
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.
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 voidmyMethod(intx, inty) { ... } public voidmyMethod(intx) { ... } Rule 1 public voidmyMethod(double x) { ... } public voidmyMethod(intx) { ... } Rule 2
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 classString 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');
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.
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.
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
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.
Random Numbers (2/3) See API of Random class.
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 48 Next random number is 14 Next random number is 89 Next random number is 7 Next random number is 44
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.
DecimalFormat (2/2) Example: To print a value in 2 decimal places. Enter a value: 90.7281 value = 90.7281 Formatted = 90.73 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)); } }
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
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.