cse 1030: implementing static features mark shtern
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; jTRANSCRIPT
![Page 1: CSE 1030: Implementing Static Features Mark Shtern](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/1.jpg)
CSE 1030: Implementing Static Features
Mark Shtern
![Page 2: CSE 1030: Implementing Static Features Mark Shtern](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/5.jpg)
PARAMETERSDemo
![Page 6: CSE 1030: Implementing Static Features Mark Shtern](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/10.jpg)
Contracts
• Precondition - the client’s responsibility• Postcondition – the implementer’s
responsibility
![Page 11: CSE 1030: Implementing Static Features Mark Shtern](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/11.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/13.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/14.jpg)
EXERCISES
![Page 15: CSE 1030: Implementing Static Features Mark Shtern](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/15.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/16.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081806/5a4d1b6a7f8b9ab0599b2ece/html5/thumbnails/17.jpg)
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.