cs1101: programming methodology comp.nus.sg/~cs1101x

19
CS1101: Programming Methodology http://www.comp.nus.edu.sg/~cs1101x/ Aaron Tan

Upload: jariah

Post on 21-Jan-2016

28 views

Category:

Documents


0 download

DESCRIPTION

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!) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS1101: Programming Methodology comp.nus.sg/~cs1101x

CS1101: Programming Methodology http://www.comp.nus.edu.sg/~cs1101x/

Aaron Tan

Page 2: CS1101: Programming Methodology comp.nus.sg/~cs1101x

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

Page 3: CS1101: Programming Methodology comp.nus.sg/~cs1101x

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.

Page 4: CS1101: Programming Methodology comp.nus.sg/~cs1101x

4

Mini-Programming Test

Are you ready?

Page 5: CS1101: Programming Methodology comp.nus.sg/~cs1101x

5

API Specification Important: Bookmark this website!

http://java.sun.com/j2se/1.5.0/docs/api/

Page 6: CS1101: Programming Methodology comp.nus.sg/~cs1101x

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.

Page 7: CS1101: Programming Methodology comp.nus.sg/~cs1101x

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

Page 8: CS1101: Programming Methodology comp.nus.sg/~cs1101x

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');

Page 9: CS1101: Programming Methodology comp.nus.sg/~cs1101x

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.

Page 10: CS1101: Programming Methodology comp.nus.sg/~cs1101x

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.

Page 11: CS1101: Programming Methodology comp.nus.sg/~cs1101x

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

Page 12: CS1101: Programming Methodology comp.nus.sg/~cs1101x

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.

Page 13: CS1101: Programming Methodology comp.nus.sg/~cs1101x

13

Random Numbers (2/3) See API of Random class.

Page 14: CS1101: Programming Methodology comp.nus.sg/~cs1101x

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

Page 15: CS1101: Programming Methodology comp.nus.sg/~cs1101x

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.

Page 16: CS1101: Programming Methodology comp.nus.sg/~cs1101x

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

Page 17: CS1101: Programming Methodology comp.nus.sg/~cs1101x

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

Page 18: CS1101: Programming Methodology comp.nus.sg/~cs1101x

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.

Page 19: CS1101: Programming Methodology comp.nus.sg/~cs1101x

19

End of file