Posts

Showing posts from January, 2019

A list of values of unknown datatypes - Generic wildcard example in Java

The Generics feature of the Java language allows programmers write reusable code. Imagine that you want to build a generic function to manipulate a list of values. The manipulation can be a type of calculation or a way of organizing values.  By using Generics without wildcard, the datatype of the list can be broadly defined; but with wildcard, the datatype(s) of the list can be undefined when the function is built. That allows even more flexibility. With wildcard, a function can be built in a way to generically accept a list of Strings, a list of Integers, a list of Doubles or a list of Longs etc.  In addition, the function can accept a list of values of various datatypes.  To build such a function, we need to use the generic wildcard syntax: <?> Here is a small program of demonstrating the use of generic wildcard: import java.util.* ; class MyInventoryOfUnknownDemo { // This function accepts a collection of unknown static void printCo...

Some Choices of Charting API for Android

Image
A small test of charting by Calmalgo.com Charting is a common feature of many types of apps.  There are so many different chart types like line charts, bar charts, pie charts, histograms, scatter charts, radar charts, candle stick charts and so on.  For some chart types, there can be features like zooming, scrolling, panning, real-time plotting, logarithmic axes, displaying in 3D and so on.  It is good that there are many API packages to choose from.  Here is a list of packages that you may find useful. (This list maybe further updated.) General Purpose/ Various Chart Types: AChartEngine Available License: Apache Androidplot Available License: Apache AnyChart Available License: Various Types GraphView Available License: Apache HelloCharts Available License: Apache HighCharts Available License: Various Types MPAndroidChart Available License: Apache shinobicharts Available License: Various Types Purposeful/ Decor/ Exotic Chart Types...

A Small Test of Lambda Expression in Android Studio

Image
My earlier post verified that Android Studio version 3.2.1 by default uses Java version 8.  Since that Java version, the Lambda Expression language feature has been available.  I wanted to experiment a few lines of Lambda in Android Studio. The test result of my few lines of code showed that I was able to build a minimal test app having Lambda Expression used, as shown in this screenshot: A small test of Lambda function in Android Studio by Calmalgo.com Here is the simple function tested: Function < Integer [], Double > mean = ( array ) -> { double sum = 0 d ; for ( int num: array ) sum += num ; return sum / array . length ; }; Note that the java.util.function package that I used requires Android SDK API Level 24 or higher.  Refer to the official Android Studio User Guide  about the Java 8 language features.

A Small Test after Installation of Android Studio

Image
I recently installed Android Studio release 3.2.1 (for Windows 64-bit). That is the latest release at the time of writing.  The installation was smooth, and ended without error messages on my Windows 10 desktop.  Like Eclipse that I had fun with some years ago, Android Studio downloaded a lot of plug-in's after the initial installation.  As a small test, I created the simplest "Hello World" type of project.  I entered a few lines of code and checked if they would be compiled well.  I launched the phone emulator of my choice (Google Pixel 2), and ran the app. The emulator displayed the number array according to my test code. The installation of Android Studio was good. In Android Studio, click Help > About, we can see the version information: In addition to Android Studio version 3.2.1, we can see JRE (Java Runtime Environment) version 1.8.0_152-release.  That is a version number that I wanted to double check. Before I started installing...

Using Predefined Interfaces for Lambda in Java

There are convenient predefined Interfaces in Java for writing functions in Lambda Expression.  By using those Interfaces, we don't need to write our own (in many cases) and that can help simplifying code. Those Interfaces are defined in java.util.function package that has been available since Java 8.  Import that package before using the Interfaces. import java.util.function.* ; import java.util.Arrays ; class PredefInterface4LambdaDemo { public static void main ( String args []) { Integer [] numHousesSold = {5, 4, 5, 6, 8, 9, 9, 10, 9, 9, 6, 5}; Arrays . sort ( numHousesSold ); System . out . println ( "For the number of houses sold in the last 12 months: " ) ; // This function applies an operation to an obejct of Integer[], and // return the result as an object of Double. Function < Integer [], Double > mean = ...

An example of Lambda Expression with Generics in Java

Lambda Expression is an important feature of contemporary programming languages.  It has been available in Java since version 8.  There are many reasons that Lambda is useful.  Here is an example of practical use of Lambda with Generics. There are often scenarios that we want to create a simple function quickly to apply to a variable.  We don't want to scroll way up or down the screen or go to another file to create a function there.  To quickly create a function for immediate use, we want to write it in the space right below the variable.  In the past, we could do so by an Anonymous Class that its syntax is quite awkward.  Now with Lambda, we can write a simple function, right below a variable, with clear and intuitive syntax. Earlier I provided with examples of array operations with Generics.  I will reuse the same arrays of numbers in the previous posts. import java.util.Arrays ; interface ArrayNum < T extends Number >{ ...

The var Identifier (Java 10 and Later)

Java 10 was first released in Mar 2018 and its language update can be considered quite new at the time of this writing.  An important part of the release was about the new var identifier. According to the Oracle's Java release document, "you can declare local variables with non-null initializers with the var identifier." Consider this example of declaring a variable with an assignment of a non-null initializer: URL url = new URL ("http://www.amazon.com/"); Instead of specifying the Class or primitive type before a variable, with Java 10 or later we can declare the variable by using the var identifier: var url = new URL ("http://www.amazon.com/"); The Class of the new variable url is automatically inferred from the context. Declaring the datatype before the variable is no longer necessary. Now consider the variables that I declared with a generic class type in my earlier post: GenericArrayStat <Integer> numHousesSold = new Ge...

A Few Functions in a Java's Generic Class

Here are the three functions that I didn't include in my earlier post.  The constructor function is revised, and variable T[] sortedNumArray is added.  All those can be plugged into the GenericNumberStat class. T [] numArray ; T [] sortedNumArray ; GenericArrayStat ( T [] numArray ) { this . numArray = numArray ; T [] tmpArray = numArray . clone (); Arrays . sort ( tmpArray ); this . sortedNumArray = tmpArray ; } Double median () { int len = sortedNumArray . length ; double med ; if ( len % 2 == 0) { double sumOfMidNums = sortedNumArray [ len /2]. doubleValue () + sortedNumArray [ len /2 - 1]. doubleValue (); med = sumOfMidNums / 2; } else { med = sortedNumArray [ len /2]. doubleValue (); } return med ; } ...

An Example of Java Generics

Generics is a useful feature in the Java language (and other contemporary languages). Generics lets us build classes or functions without defining specific data type(s).  That allows those coding blocks to be more reusable, and bugs to be more detectable at compile time.  There are many ways to incorporate Generics in code.  Below is a practical example. The GenericNumberStat class will accept an array of Object T .  What is T ?  In this example, T is a Number . That means an array of any one of the sub-classes of  Number  can be passed to the constructor of this class. When we create a function, the traditional way requires us to define the specific parameter type(s).  With Generics, we don't need to define the specific types in the function. We define them when we instantiate an object. I provided with the mean() function to show how to use the generic array.  (To keep the code concise, I haven't included median() , minimum() a...