forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise.java
More file actions
35 lines (30 loc) · 848 Bytes
/
Exercise.java
File metadata and controls
35 lines (30 loc) · 848 Bytes
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
/**
* Exercise on encapsulation and generalization.
*/
public class Exercise {
public static int numberOfParentheses(String input){
int count = 0;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
count += openAndClose('(',')',c);
}
return count;
}
/*
* This function returns different values when input char is equal to open or close char
*/
public static int openAndClose(char open, char close, char input){
if(input == open){
return 1;
} else if(input == close){
return -1;
} else{
return 0;
}
}
public static void main(String[] args) {
String s = "((3 + 7) * 2)((";
int count = numberOfParentheses(s);
System.out.println(count);
}
}