Computer Science 202
Introduction to Programming

Fall 2013, The College of Saint Rose

LittlePrimes BlueJ Project

Click here to download a BlueJ project for LittlePrimes.


LittlePrimes Source Code

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


LittlePrimes.java

/*
 * Example LittlePrimes: print whether a small number (1-10) is prime or composite.
 * Demonstrates multiple cases performing the same code in a switch statement.
 *
 * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012
 *
 * $Id: LittlePrimes.java 1920 2012-09-27 14:51:20Z terescoj $
 */

import java.util.Scanner;

public class LittlePrimes {

    public static void main(String[] args) {

        // we'll read in a number with a Scanner
        Scanner kbd = new Scanner(System.in);

        System.out.print("Enter a number 1-10: ");
        int number = kbd.nextInt();

        switch (number) {
            case 1:
            case 4:
            case 6:
            case 8:
            case 9:
            case 10:
                System.out.println(number + " is composite.");
                break;
            case 2:
            case 3:
            case 5:
            case 7:
                System.out.println(number + " is prime.");
                break;
            default:
                System.out.println(number + " is out of the allowed range (1-10)");
                break;
        }

    }
}