-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathExercise.java
More file actions
103 lines (81 loc) · 3.47 KB
/
Exercise.java
File metadata and controls
103 lines (81 loc) · 3.47 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
92
93
94
95
96
97
98
99
100
101
102
103
package com.booleanuk.core;
import com.booleanuk.helpers.ExerciseBase;
public class Exercise extends ExerciseBase {
/*
A method is a function, a single piece of logic that can run. In Java, a class is a convenient
encapsulation of related state and behaviour; members contain state (values like ints, strings, etc.),
and methods perform behaviours. You've been using String methods in previous exercises, now it's time
to start writing your own.
Methods are defined by a signature line followed by a set of curly braces. The code inside the curly
braces is the logic specific to that method, and any variables created in those curly braces
are not accessible to any other method or class outside that method.
The signature line describes the method and is constructed in the following format:
visibility returnType nameOfMethod(inputs)
You must specify what data type the method outputs (returnType) when it finishes running, or use `void`
if it doesn't output anything. You can name a method anything you like but the name should be descriptive
and short. Methods can also take inputs, which require data types and names, which is can use inside its
logic. See the example below and take some time to break it down and understand it.
*/
/*
0. Example requirement
Create a method that accepts a name and returns a greeting
*/
public String greet(String name) {
return "Hello " + name + "!";
}
/*
1. Increment a number
Complete this method so that it increases the number given by 1 and returns the result
*/
public int increment(int number) {
number++;
return number;
}
/*
2. Construct a friendly greeting
Complete this method so that it accepts a name as an input and returns a friendly greeting
with a smiley face. Example input and output:
Input | Output
-------|-------
Nathan | Hi, Nathan :)
Edward | Hi, Edward :)
*/
public String happilyGreet(String name) {
return "Hi, " + name + " :)";
}
/*
3. Construct an array of numbers
Create a method named constructNumberArray that accepts two whole numbers named lower and upper.
The method must return an array containing all the whole numbers between lower and upper,
including lower and upper. Example input and output:
Input | Output
-------|-------
1, 3 | [1,2,3]
10, 13 | [10,11,12,13]
-1, 1 | [-1,0,1]
*/
public int[] constructNumberArray(int lower, int upper) {
int size = upper - lower + 1;
int[] nums = new int[size];
int index = 0;
for (int n = lower; n <= upper; n++) {
nums[index++] = n;
}
return nums;
}
/*
4. Shout at a dev
Create a method named shout that accepts a string and a whole number.
The method must return the same string in upper case with exclamation marks (!) appended to the end.
The number of exclamation marks appended must be determined by the number provided.
Example input and output:
Input | Output
------------|-------
disaster, 5 | DISASTER!!!!!
error, 10 | ERROR!!!!!!!!!!
*/
public String shout(String s, int n) {
String result = s.toUpperCase();
return result + new String("!").repeat(n);
}
}