1 / 14

SE1021 Software Development II

SE1021 Software Development II. Lab 8 (Continued)/New JavaFX Containers and Controls. Overview. Lab 8 Questions? When is this due? What about the demo? Functional Programming. Lab 8: This Week (9). Load and save images in the .bmp file format

stanleyd
Download Presentation

SE1021 Software Development II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SE1021 Software Development II Lab 8 (Continued)/New JavaFX Containers and Controls

  2. Overview • Lab 8 Questions? • When is this due? • What about the demo? • Functional Programming

  3. Lab 8: This Week (9) • Load and save images in the .bmp file format • Apply the following transforms to the current image: • Make red-channel version of the image (removes green and blue channels) • Make red-gray version of the image (alternate lines) • Create a second window (an additional stage) for entering filter parameters Provide handlers for the Blur, Sharpen, and Edge buttons. • The appropriate graphical user interface to support the operations listed above. • Implementing logging to a log file • Correct any issues identified in your previous submission, if your instructor has provided feedback to you in a timely manner. (that’s me)

  4. Lambda Expressionhttps://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood • Better to use a lambda expression than say, an inner class • The compiler will generate a new class file for each anonymous inner class • If you use a lot of anonymous inner classes, this is many class files that need to be loaded and verified before being used which can impact startup performance • Java/JVM handles lambdas by “invokedynamic” bytecode instruction • Generate an invokedynamic call site (lambda factor) • When this is invoked returns an instance of the Functional interface to which the lambda is being converted • Convert the body of the lambda expression into a method that will be invoked through the invokedynamic instruction

  5. Functional Interface/Lambda Expressions • A functional interface is one that only contains one method • Help compiler by specifying: @FunctionalInterface • Lambda Expression syntax is (argument) -> body • Lambda expressions allow us to treat code as data • Pass code to methods for execution

  6. Lambda Syntax • ( formal-parameter-list ) -> { expression-or-statements } • For the formal parameter list, if the list consists of a single parameter, the parentheses can be omitted • If it is empty, need empty parenthesis • x • () • For the expression or statements, if you place the expression between the opening and closing braces {}, need to use a return statement to return value, otherwise, don’t need a return • (int x, int y) -> x+y • (x, y) -> { returnx+y; } • (int x, int y) -> { System.out.println(x+y); returnx+y; }

  7. Lambda Examplehttp://www.informit.com/articles/article.aspx?p=2191423&seqNum=2 import java.awt.EventQueue; public class LambdaDemo { public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { System.out.println("Running"); } }; EventQueue.invokeLater(r); EventQueue.invokeLater(() -> System.out.println("Running")); }} @FunctionalInterface public interface Runnable { public abstract void run(); }

  8. Another Lambda Example @FunctionalInterface interface Converter{ double convert(double input); } public class LambdaDemo{ public static void main(String[] args) { // Convert Fahrenheit to Celsius System.out.println(convert(input -> (input-32)*5.0/9.0, 98.6)); // Convert Kilometers to Miles System.out.println(convert(input -> input/1.609344, 8)); } static double convert(Converter converter, double input) { return converter.convert(input); } }

  9. Functional Programminghttp://msoe.taylorial.com/se1021/Functional • Can (over) simplify software into two components • Data • Operations • Our approach thus far has been utilizing object oriented concepts to group operations with data • Functional programming treats operations as data • This is what we have been looking at with the lambda expression

  10. Object Oriented Programming • Use of classes to group data with related operations • Objects are created with related data and operations specific to that data and class purpose • Encapsulation • Inheritance • Code re-use • Functionality and behavior are tied together

  11. Functional Programming Definition • The ever-popular Wikipedia definition: https://en.wikipedia.org/wiki/Functional_programming) • “In computer science, functional programming is a programming paradigm – a style of building the structure and elements of computer programs – that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.”

  12. Functional Programming (simplified) • Operations are treated as data • As such, can be passed to methods as parameters • Applied to data • Popular for event-driven programming (again – lambda expression) and concurrency tasks where we execute the same portion of code simultaneously

  13. Java and Functional Programming • Originally Java was difficult to use for functional programming because designed to just be object oriented • Strongly typed language, limitations • With Java 8, functional programming has become easier • Lambda Expressions • Introduction of new types (interfaces): Predicate, Function and Consumer used for functional pipelines • Lazy Evaluation

  14. Functional Programming Characteristics • Functional programming like a mathematical function that for a given set of inputs, the function will return the same value each time it’s called • Need to have the following constraints: • The function call can be replaced by the result. • Functions are not allowed to have side effects. • The compiler is allowed to rearrange the function invocations or run them on different threads without changing the result. • Program flow is driven by data dependencies rather than the sequence of instructions. • Functional programming requires data to be immutable. If the value must change, a new piece of data is created with the new value.

More Related