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.
- Sometimes one solution is better than the others
- Sometimes different solutions are appropriate in different settings
- We've already seen a couple of techniques:
- Taking the (weighted) average of 2, 3, … numbers
- Testing for even/odd, and divisibility in general
- Repeating a body of code n times
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
- Equally useful is NOT doing something the first time
- For example, print several numbers with commas between them
- Can't always print a comma (and space) before printing the number, e.g.,
, 1, 2, 3
- Can't always print a comma (and space) after printing the number, e.g.,
1, 2, 3,
- Either
- print a comma before every number EXCEPT the first, or, (this is the one we'll use for now)
- print a comma after every number EXCEPT the last
boolean isFirst = true;
for (.......) {
if (isFirst)
isFirst = false; // don't want to do anything first time, but we have to toggle isFirst
else {
… // all the other times, do what needs to be done
}
…
- Note the toggling of the
isFirst boolean variable is in the same place (within the true part of the if (isFirst)
conditional), but the code to be performed is now in the false part (since we want it to be done everytime except the first.
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);
}
}
}
- As mentioned above, the comma-logic above could also be done using last-time logic; we will discuss that later
- Last-time logic is often more difficult to use than first-time (why?)
- We could also have a combination of first and not-first logic; i.e., doing one action the first time only and some other
action every other time.
Swapping / Exchanging
Sorting
- Arranging a sequence of values (e.g. integers) so they are in either ascending or descending order
- We're just going to scratch the surface here
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;
}
}
- Breaks the problem up somewhat
- Still explodes in size
- Also forms a complex set of nested if's
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;
}
}
- n variables → n-1 conditionals
- we'll see this is a standard way of taking a maximum
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;
}
}
- Notice the overloading of the methid name
max: there is a 2-arg method
and a 3-arg method
- The 3-arg uses the 2-arg method by calling it on two values, and then calling it again with the result of the first
call and the third value
- Methods using method results as their arguments is a technique known as composition