class LeapYear {
public static void main(String[] args) {
System.out.println(input(Integer.parseInt(args[0]))); //you must pass an argument when you start the program, like this: java LeapYear 1999
}
static String input(int number) {
if(number % 400 == 0) { //1st test: is the number evenly divisible by 400?
return "Leap year!"; //if it is, get out of the condition fork
} else if(number % 100 == 0) { //2nd test if test #1 didn't lead to a return: evenly divisible by 100?
return "Not a leap year!"; //if it is not, get out of the fork
} else if(number % 4 == 0) { //last
SJP: Adjust a string's length by Omorashi, literature
Literature
SJP: Adjust a string's length
public class AdjustStringToLength {
/*Given a string and a certain length,
*this program's task is to adjust
*the string's length to the given length.
*
*If the string is shorter than the given length,
*append as many zeros to it as needed.
*If the string is longer, cut it by the given length.
*
*Feel free to change the value of someLength and
*the content of str to see how the program reacts!
*/
public static void main(String[] args) {
int someLength = 4;
String str = "ThisIsJava!";
while(str.length() < someLength) {
str = str+"0";
SJP: Abstract + Constructors by Omorashi, literature
Literature
SJP: Abstract + Constructors
public abstract class Console {
/*This is a so-called abstract class, which means that you cannot instantiate
*objects from it. So if you try something like this in the main method:
*Console c = new Console();
*you'll get an error message from the compiler.
*/
String developer;
String colour;
int coolness;
/*This is an abstract method. It ha
Simple Java Programs: 1101 by Omorashi, literature
Literature
Simple Java Programs: 1101
class DecToBin {
public static String number(int x) {
String result = "";
do {
result = x % 2 + result; //"2" can be replaced with any other base below 10
x = x / 2;
} while(x != 0);
return result;
}
public static void main(String[] args){
System.out.println(number(Integer.parseInt(args[0])));
}
}
/*If you want to transform a decimal number into a binary,
*you divide the decimal number by 2 again and again until
*only zero remains (this is what this code does).
*Like this:
*
*13/ 2 = 6 remainder 1
*6 / 2 = 3 remainder 0
*3 / 2 = 1 remain
Simple Java Programs: Lottery by Omorashi, literature
Literature
Simple Java Programs: Lottery
public class LotteryNumbers {
public static int[] calculateNumbers() {
int[] numbers = new int[6]; //this array contains six variables, ranging from numbers[0] through numbers[5]
for(int i = 0; i < numbers.length; i++) {
int randomNumber = (int) (49 * Math.random()) + 1; //calculate a random number between 1 and 49, using the method random() from the class java.lang.Math
if(!isIn(numbers, randomNumber)) {
numbers[i] = randomNumber;
} else {
i--; //makes the calculation run again if a double number was detected
}
}
ret