Home > Programming > OOP with Java (TDT4100): Exercise 1

OOP with Java (TDT4100): Exercise 1

Exercise 1Given you managed to get Eclipse, JExercise and the preconfigured project running properly (described in my previous post), you should be ready to tackle the first exercise. I was planning on giving a more thorough review and explanation of the code, but unfortunately because of time constraints, I won’t be able to. I will, however, try to comment parts where the code gets difficult to follow.

Part 1: A simple Hello World application

1
2
3
4
5
6
7
8
package oving1;
 
public class HelloWorld {
 
	public static void main(String[] args) {
		System.out.println("Hello World!");
	}
}

Part 2: An application that finds the larger of two numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package oving1;
 
public class Larger {
	public static void main(String[] args) {
		// Takes the first (0) and second (1) command line arguments and compares what number is the largest
		int i = Integer.parseInt(args[0]);
		int j = Integer.parseInt(args[1]);
		if (i >= j) {
			System.out.println(i);
		} else {
			System.out.println(j);
		}
		/*
		 * Can also be done as an inline if-else:
		 * Basically it says: If i is larger or equal to j, then print i, else print j
		 *  System.out.println(i >= j ? i : j);
		 */
	}
}

Part 3: An application that that sums integers read from System.in

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package oving1;
 
import java.util.Scanner;
 
public class Sum {
 
	public static void main(String[] args) {
		// Creates a new Scanner object to read input from the user through System.in
		Scanner sc = new Scanner(System.in);
		// Create a new integer to hold our sum
		int sum = 0;
		// Loop through the input until we reach the end of the line
		while (sc.hasNext()) {
			// Try to convert each character in the input to an integer and add it to our sum
			try {
				int n = Integer.parseInt(sc.next());
				sum += n;
			// Catch the exception that occurs if converting is impossible, print what has happened and end the program
			} catch (NumberFormatException e) {
				System.out.println(e.getMessage());
				return;
			}
		}
		// Print the sum
		System.out.println("The sum is: " + sum);
	}
}

Part 4: Finding elements in an array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package oving1;
 
public class ArrayMethods1 {
	/**
	 * Takes an array and an integer as parameters and returns the index of where the number is *first* found.
	 * Returns -1 if the number is not present.
	 * @param array
	 * @param num
	 * @return
	 */
	public static int indexOf (int[] array, int num) {
		int result = -1;
		// Loop through the array using a for-loop
		for (int i = 0; i < array.length; i++) {
			// Set the index and break out of the loop if we've found a match
			if (array[i] == num) {
				result = i;
				break;
			}
		}
		// Return
		return result;
	}
 
	/**
	 * Takes an array and a String-object as parameters and returns the index of the *last* occurence of the given String.
	 * Returns -1 if the String is not present.
	 * @param array
	 * @param text
	 * @return
	 */
	public static int lastIndexOf (String[] array, String text) {
		int result = -1;
		// Loop through the array using a for-loop, starting from the back
		for (int i = array.length - 1; i >= 0; i--) {
			// Compare each String and break through the loop if we find an occurence
			if (array[i].equals(text)) {
				result = i;
				break;
			}
		}
		// Return
		return result;
	}
 
	/**
	 * Takes an array of integers as the first parameter and an integer as the second.
	 * The method searches through the array starting at the index given as the second paramter and returns the index of the lowest value.
	 * @param array
	 * @param startIndex
	 * @return
	 */
	public static int indexOfSmallest (int[] array, int startIndex) {
		// Return -1 if the start index is greater than the total number of elements in the array
		if (startIndex >= array.length) return -1;
		// Set the first value
		int result = startIndex;
		// Search through the array starting at startIndex and get the index of the lowest value
		for (int i = startIndex; i < array.length; i++) {
			if (array[result] > array[i]) result = i;
		}
		// Return
		return result;
	}
}

Closing comments:
That’s the solution for exercise 1. It might be hard to follow without the more concrete assignment descriptions for each part, but I hope the learning curve hasn’t been too steep and that you’ve learned something useful out of these four snippets. It should give you an idea of how what Java syntax looks like, and we’ve also scratched the surface of four important tools of Java:

  • The while-loop
  • The for-loop
  • Arrays
  • Simple logic (if-then-else)

Hopefully, I’ll have exercise 2 drafted in the near future. Good luck further!

Categories: Programming Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.