cse 1030: implementing static features mark shtern

Post on 18-Jan-2018

220 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

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

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

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)

PARAMETERSDemo

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

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

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

UML

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

Contracts

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

responsibility

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

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) { ... }

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>

EXERCISES

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.

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.

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.

top related