CISC 1115
Introduction to Programming Using Java
Techniques I


Techniques

Many problems have standard solutions or approaches; these are called techniques. A problem may often have several techniques to solve it. We'll be presenting more techniques as the language features used become available:

Toggling a Boolean value

'First-time' Logic

Program P07.1 PrintPositives

Write some code to read in a header value, followed by that number of integers, printing out all positives, in the format:
The positive numbers are:
…
…
If there are no positive numbers, nothing (including the prefixing title) should be printed.
import java.io.*;
import java.util.*;

public class PrintPositives {
	public static void main(String [] args) throws Exception {
		Scanner scanner = new Scanner(new File("numbers.text"));

		boolean isFirst = true;

		int n = scanner.nextInt();

		for (int i = 1; i <= n; i++) {
			int num = scanner.nextInt();
			if (num > 0) {
				if (isFirst) {
					System.out.println("The positive numbers are: ");
					isFirst = false;
				}
				System.out.println(num);
			}
		}
	}
}

'Not-First-time' Logic

Program P07.2 CommaLogic

Write a program that prompts the user for an integer and print out the numbers from 1 to that integer separated by commas, e.g.: 1, 2, 3, 4, 5
import java.util.*;

public class CommaLogic {
        public static void main(String [] args) throws Exception {
                Scanner keyboard = new Scanner(System.in);

                System.out.println("Print from 1 to wwhere? ");
                int n = keyboard.nextInt();

                boolean isFirst = true;

                for (int i = 1; i <= n; i++) {
                        if (isFirst) 
                                isFirst = false;
                        else
                                System.out.print(", ");
                        System.out.print(i);
                }
        }
}
Notes

Swapping / Exchanging

Sorting

Rotation / Shifting

Maximum (Minimum)

Brute Force

Testing all permutations
public class BruteForceMax {
	public static void main(String [] args) {
		System.out.println(max(2, 3, 1));
	}

	public static int max(int x, int y, int z) {
		int max;
		if (x >= y && x >= z) 
			max = x;
		else if (y >= x && y >= z) 
			max = y;
		else 
			max = z;
		return max;
	}
}

'Semi brute' force

public class SemiBruteForceMax {
	public static void main(String [] args) {
		System.out.println(max(2, 3, 1));
	}

	public static int max(int x, int y, int z) {
		int max;
		if (x > y)
			if (x > z)
				max = x;
			else
				max = z;
		else
			if (y > z)
				max = y;
			else
				max = z;
		return max;
	}
}

Cascaded

public class CascadedMax {
	public static void main(String [] args) {
		System.out.println(max(2, 3, 1));
	}

	public static int max(int x, int y, int z) {
		int max = x;
		if (y > max) max = y;
		if (z > max) max = z;
		return max;
	}
}

Method Composition

public class CompositionMax {
	public static void main(String [] args) {
		System.out.println(max(2, 3, 1));
	}

	public static int max(int x, int y, int z) {
		return max(max(x, y), z);
	}

	public static int max(int x, int y) {
		if (x > y) return x;
		return y;
	}
}

Files used in this Lecture

Lab for Lecture 7