Download presentation
Presentation is loading. Please wait.
Published byNaomi Cain Modified over 8 years ago
1
CSE 1030: Implementing Static Features Mark Shtern
2
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
3
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);
4
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)
5
PARAMETERS Demo
6
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
7
Utility Class “A utility class is a class that defines a set of methods that perform common, often re-used functions” from Wikipediaclass A utility class is a class with only class-scope attributes and operations Instances are stateless or instances have the same state
8
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
9
UML public class Math { public static final double PI = 3.14; public static double sqrt (double a) { double res = Double.NaN; //calculation..... return res; }
10
Contracts Precondition - the client’s responsibility Postcondition – the implementer’s responsibility
11
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
12
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) {... }
13
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 exponent. */ public static int pow(int base, int exponent) javadoc -tag param -tag pre.:a:"Precondition: " -tag return -d
14
EXERCISES
15
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.
16
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.
17
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.