Our Odd Numbers code in Java.
Odd Numbers Class File:
package arithmetic;
public class OddNumbers {
private int start; // Our starting point
private final int stop; // where we will stop
private String result; // Store result here.
public OddNumbers(int first, int last) {
start = first;
stop = last;
result = "Odd numbers between " + first + " and " + last + " are: \n";
}
String prepResult() {
/*
* Loop from start to stop and rip out odd numbers;
*/
while (start <= stop) {
if (start % 2 != 0) {
result += start + "; "; // Mind the '+' in front of the '='
}
start++; // increase start by 1(same as start = start + 1
}
return result;
}
}
Main Class:
package arithmetic;
public class Arithmetic {
public static void main(String[] args) {
System.out.println("Welcome to our demonstration sequels");
System.out.println("Hope you enjoy (and follow) the lessons.");
System.out.println("");
/* Use the Odd Number class. */
OddNumbers odd_data = new OddNumbers(1, 100); // odd numbers between 1 and 100
System.out.println(odd_data.prepResult());
System.out.println();
}
}
Try it out!
Elegance (0.0)