CS1101: Programming Methodology Aaron Tan.

Slides:



Advertisements
Similar presentations
Computer Programming Lab(7).
Advertisements

CS110 Programming Language I
Computer Programming Lab 8.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
Numerical Data Recitation – 01/30/2009
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
CS 180 Recitation 8 / {30, 31} / Announcements Project 1 is out –Due 10:00pm –Start early! –Use the newsgroup for your questions –LWSN B146.
***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Computer Programming Lab(5).
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology
Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder.
WEEK 4 Class Activities Lecturer’s slides.
CS1101: Programming Methodology Aaron Tan.
***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class The Math class provides a convenient way to access higher math functions. The Math class is automatically.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
Lecture 8 Using casts, Strings and WordUtil. Agenda Generating random numbers Casts – Casting a double into an int – Casting an int into a char – Casting.
Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with.
CS1101: Programming Methodology Aaron Tan.
Using Java Class Library
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
The Math Class Methods Utilizing the Important Math Operations of Java!
CS1101: Programming Methodology
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
CS1101: Programming Methodology Preparing for Practical Exam (PE)
CS1101: Programming Methodology Preparing for Practical Exam (PE)
CS1101: Programming Methodology
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.6 Reading Input with java.io Produced by Harvey.
CS1020 Data Structures and Algorithms I Lecture Note #6 Vector and ArrayList.
C OMMON M ISTAKES CSC Java Program Structure  String Methods.
Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging using eclipse The Java API.
CS1010: Programming Methodology
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
CIS Intro to JAVA Lecture Notes Set 5 26-May-05.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Building Java Programs
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Formatting Output & Enumerated Types & Wrapper Classes
Crash course in the Java Programming Language
Java Course Review.
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
INPUT STATEMENTS GC 201.
Building Java Programs
Building Java Programs
MSIS 655 Advanced Business Applications Programming
Java so far Week 7.
Building Java Programs
More on iterations using
Presentation transcript:

CS1101: Programming Methodology 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: is a prime.  Enter integer: is not a prime.

4 Mini-Programming Test Are you ready?

5 API Specification  Important: Bookmark this website! 

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 48 Next random number is 14 Next random number is 89 Next random number is 7 Next 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: value = Formatted = 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”:

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