cse 1030: implementing static features mark shtern

17
CSE 1030: Implementing Static Features Mark Shtern

Upload: naomi-cain

Post on 18-Jan-2018

220 views

Category:

Documents


0 download

DESCRIPTION

What is the output? double sum = 0; double x = 2*Math.PI; for (int i = 0; i < ; i++) { int fact = 1; for (int j=0; j

TRANSCRIPT

Page 1: CSE 1030: Implementing Static Features Mark Shtern

CSE 1030: Implementing Static Features

Mark Shtern

Page 2: CSE 1030: Implementing Static Features Mark Shtern

Course Objectives

• Improve development skills• Introduce the concern of the implementer– Hands-on experience: Implementation of a subset

of type.lib– Write API Java documentation– Study advanced programming techniques

Page 3: CSE 1030: Implementing Static Features Mark Shtern

What is the output? double sum = 0; double x = 2*Math.PI; for (int i = 0; i <1000000; i++) { int fact = 1; for (int j=0; j<2*i+1;j++ ) { fact = fact * j; } sum = sum + Math.pow(-1,i) * Math.pow (x,2*i +1)/fact; } System.out.println (“sum = “ + sum);

Sum = Math.sin (x);

Page 4: CSE 1030: Implementing Static Features Mark Shtern

Problem

• Create application that calculates area of rectangle

Solution• Analysis specify exactly input and output• Design make a plan for how the system will

accomplish its goal • Implementation write and compile • Testing test correctness (normal and abnormal

cases)

Page 5: CSE 1030: Implementing Static Features Mark Shtern

PARAMETERSDemo

Page 6: CSE 1030: Implementing Static Features Mark Shtern

Summary• Software Development Cycle• Robustness• Methods – Code Reuse– Complexity Reduction– Maintainability Improvement

• Methods– Private vs Public– Call-by-value (Plug-in arguments for formal parameters)– Primitive type vs Non-primitive types– Return statement, void

Page 7: CSE 1030: Implementing Static Features Mark Shtern

Utility Class

• “A utility class is a class that defines a set of methods that perform common, often re-used functions” from Wikipedia

• A utility class is a class with only class-scope attributes and operations

• Instances are stateless or instances have the same state

Page 8: CSE 1030: Implementing Static Features Mark Shtern

Utility Class

• Attributes– Example

public static final double PI = 3.1415926535;

• Methods– Example public static double min (double a, double b)

• Constructor– No public constructor

Page 9: CSE 1030: Implementing Static Features Mark Shtern

UML

public class Math { public static final double PI = 3.14; public static double sqrt (double a) { double res = Double.NaN; //calculation ..... return res; }}

Page 10: CSE 1030: Implementing Static Features Mark Shtern

Contracts

• Precondition - the client’s responsibility• Postcondition – the implementer’s

responsibility

Page 11: CSE 1030: Implementing Static Features Mark Shtern

javadoc

• API text appears within the class definition as multi-line comments surrounded by /**(two asterisks instead of one) and */

• API text is placed immediately before– every public attribute, constructor, and method in

the class– class header to document the class as a whole

Page 12: CSE 1030: Implementing Static Features Mark Shtern

Ex 01/** Returns the smaller of two int values. @param a An argument. @param b Another argument. @return The smaller of a and b. */ public static int min(int a, int b) { ... }

Page 13: CSE 1030: Implementing Static Features Mark Shtern

Ex 02/** Returns the value of the first argument raised to the second

argument. @param base The base. @param exponent The exponent. @pre. exponent >= 0. @return base<sup>exponent</sup>.*/ public static int pow(int base, int exponent)

• javadoc -tag param -tag pre.:a:"Precondition: " -tag return -d <output directory> <java class>

Page 14: CSE 1030: Implementing Static Features Mark Shtern

EXERCISES

Page 15: CSE 1030: Implementing Static Features Mark Shtern

Ex 01

• Write a static method max3() that takes three int values as arguments and returns the value of the largest one.

• Add an overloaded function that does the same thing with three double values.

Page 16: CSE 1030: Implementing Static Features Mark Shtern

Ex02

• Write a static method odd() that takes three boolean inputs and returns true if an odd number of inputs are true, and false otherwise.

Page 17: CSE 1030: Implementing Static Features Mark Shtern

Ex03

• Write a static method majority() that takes three boolean arguments and returns true if at least two of the arguments have the value true, and false otherwise. Do not use an if statement.