Computer Science 225
Advanced Programming

Spring 2017, Siena College

Ternary BlueJ Project

Click here to download a BlueJ project for Ternary.


Ternary Source Code

The Java source code for Ternary is below. Click on a file name to download it.


Ternary.java

/*
 * Example Ternary: an example of the ternary operator in Java
 *
 * Jim Teresco, Siena College, Computer Science 225, Spring 2017
 *
 * $Id: templateapp.java 2975 2016-08-30 02:35:29Z terescoj $
 */

public class Ternary {

    public static void main(String[] args) {

        // Suppose we're looking to print two distinct but similar messages
        // based on the value passed in as the first command-line parameter

        // we can do this with a simple if-else
        if (args[0].equals("0")) {
            System.out.println("Hey, it's a zero!");
        }
        else {
            System.out.println("Hey, it's a one or something!");
        }

        // or.. we can simplify to a single statement using the ternary operator
        System.out.println("Hey, it's a " +
            ( args[0].equals("0") ? "zero" : "one or something") +
            "!");

    }
}