-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathExercise.java
More file actions
91 lines (66 loc) · 2.94 KB
/
Exercise.java
File metadata and controls
91 lines (66 loc) · 2.94 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.booleanuk.core;
public class Exercise {
// The block of code below is a method definition. So far, you've been using methods created by other people
// such as .length() and .charAt(n)
// From this exercise on, you'll be writing code inside methods very often! Methods are a container for a specific
// piece of logic that the class can run. For now, just implement your solutions between the comments that look like:
// WRITE YOUR CODE BETWEEN THIS LINE...
// ... AND THIS LINE
// See the example method below
public void example() {
// 0. Print the word "Hello"
// WRITE YOUR CODE BETWEEN THIS LINE...
System.out.println("Hello");
// ... AND THIS LINE
}
public int[] one() {
int[] numbers = {42, 13, 17, 91};
// 1. Values contained in an array are each stored at a unique numeric index, starting from 0 ascending in order.
// E.g. The first value is at index 0, the second at index 1, the third at index 3.
// Using an index, change the number 17 in the numbers array to 68
// WRITE YOUR CODE BETWEEN THIS LINE...
numbers [2] = 68;
// ... AND THIS LINE
return numbers;
}
public String two() {
String[] teachers = {"Nathan", "Ed", "Dave", "Carlo", "Lewis", "Jules", "John", "Chris"};
// 2. Using an array index, change the value of the teacher variable below to be the fourth
// teacher contained in the teachers array
// WRITE YOUR CODE BETWEEN THIS LINE...
String teacher = "Carlo";
teachers[3] = teacher;
// ... AND THIS LINE
return teacher;
}
public String[] three() {
// 3. Create a string array named cars that contains three names of car manufacturers: Audi, BMW and Dodge
// WRITE YOUR CODE BETWEEN THIS LINE...
String[] car = {"Audi", "BMW", "Dodge"};
// ... AND THIS LINE
// Then change the code below to remove the dummyArray completely and return the cars array you created above instead.
String[] dummyArray = {};
return car;
}
public int four() {
int[] numbers = {42, 13, 17, 91};
// 4. Using array indices, set the value of the result variable below to the
// sum of every number in the numbers array
// WRITE YOUR CODE BETWEEN THIS LINE...
int result = 0;
for (int i = 0; i < 4; i++) {
result += numbers[i];
}
// ... AND THIS LINE
return result;
}
public float[] five() {
// 5. Create an array called floats that contains 3 floating point numbers: 9.62, 23.17 and 3.14
// WRITE YOUR CODE BETWEEN THIS LINE...
float[] floats = {9.62F, 23.17F, 3.14F};
// ... AND THIS LINE
// Then change the code below to remove the dummyArray completely and return the floats array you created instead
float[] dummyArray = {};
return floats;
}
}