Java

Java 1.6: reversing array with temporary value

For an array of 5 numbers:

swapValues.png

public class Exercise3 {

public static int[] reverse(int[] numbers) {

int temp;

temp = numbers[0];
numbers[0] = numbers[5];
numbers[5] = temp;

temp = numbers[1];
numbers[1] = numbers[4];
numbers[4] = temp;

temp = numbers[2];
numbers[2] = numbers[3];
numbers[3] = temp;

return numbers;

}
}

Advertisements

Java 1.5: averaging numbers

average.png

import java.util.Scanner;

public class Main {

    static int [] num = {0,0,0};

    public static void main(String[] args) {

        int sum;
        double average;

        Scanner scan = new Scanner(System.in);

        System.out.print("Enter the first number: ");
        num[0] = scan.nextInt();
        // reads and stores the first integer using nextInt method

        System.out.print("Enter the second number: ");
        num[1] = scan.nextInt();

        System.out.print("Enter the third number: ");
        num[2] = scan.nextInt();

        sum = num[0] + num[1] + num[2];

        average = sum / 3.0;
        // dividing by 3 would give the nearest whole number

        System.out.println("The sum is " + sum);
        System.out.println("The average is " + average);
    }
}

Java 1.4: sequence

sequence

import java.util.Scanner;

public class Main {

    static double [] sequence = {0,0,0};

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        System.out.println("Enter first double: ");
        sequence[0] = scan.nextDouble();

        System.out.println("Enter second double: ");
        sequence[1] = scan.nextDouble();

        sequence[2] = sequence[1] * (sequence[1] / sequence[0]);

        System.out.println("The third double is therefore " + sequence[2]);
    }
}

Java 1.3: area of rectangle II

areaOfRect2

import java.util.Scanner;

public class Main {

    static int [] side = {0, 0};

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        // scan is the variable name
        int area;

        System.out.print("Enter length: ");
        side[0] = scan.nextInt();

        System.out.print("Enter height: ");
        side[1] = scan.nextInt();

        area = side[0] * side[1];

        System.out.println("The area of your rectangle is: " + area);
    }
}

Java 1.2: area of rectangle I

areaOfRect1

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

    // area of rectangle

        int length;
        int height;
        int area;

        System.out.println("Enter a length: ");
        Scanner in = new Scanner(System.in);
        length = in.nextInt();

        System.out.println("Enter a height: ");
        height = in.nextInt();

        area = length * height;
        System.out.println("The area of your rectangle is " + area);
    }
}

Java 1.1: swapping numbers in array using temporary value

javaSwapArray

public class Main {

    public static void main(String[] args) {

        int [] x = {4, 3, 2, 1};

        if ( x[0] > x[3] ) {
            int temp = x[0];
            x[0] = x[3];
            x[3] = temp;
        }

        System.out.println("Swapping [0] & [3], the array should be 1, 3, 2, 4.");

        System.out.println(">> Your code gives: "   + x[0] + ", "
                                                    + x[1] + ", "
                                                    + x[2] + ", "
                                                    + x[3]);

        if ( x[1] > x[2] ) {
            int temp = x[1];
            x[1] = x[2];
            x[2] = temp;
        }

        System.out.println("Swapping [1] & [2], the answer should be 1, 2, 3, 4.");

        System.out.println(">> Your code gives: "   + x[0] + ", "
                                                    + x[1] + ", "
                                                    + x[2] + ", "
                                                    + x[3]);

    }
}

Java 1.0 swapping numbers using temporary value

javaSwappingNumbersTemp

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int x,y, temp;
        System.out.println("Enter x and y");
        Scanner in = new Scanner(System.in);

        System.out.println("Select an x value:");
        x = in.nextInt();
        System.out.println("Select a y value:");
        y = in.nextInt();

        System.out.println("Before swapping\n" +
                            "x = " + x + " \n " +
                            "y = " + y);

        temp = x;
        x = y;
        y = temp;

        System.out.println("After swapping\nx = "+x+"\ny = "+y);
    }
}