-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetters_setters_quiz2.java
55 lines (45 loc) · 1.53 KB
/
getters_setters_quiz2.java
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
import java.util.Scanner;
// Class representing the string reverser
class StringReverser {
private String originalString;
private String reversedString;
// Default constructor
public StringReverser() {
// Initialize the scanner
Scanner scanner = new Scanner(System.in);
// Input a string
System.out.print("Input a string: ");
originalString = scanner.nextLine();
// Reverse the string
reverseString();
}
// Getter for the reversed string
public String getReversedString() {
return reversedString;
}
// Method to reverse the string
private void reverseString() {
char[] charArray = originalString.toCharArray();
int start = 0;
int end = charArray.length - 1;
// Swap characters from the beginning and end of the array
while (start < end) {
char temp = charArray[start];
charArray[start] = charArray[end];
charArray[end] = temp;
start++;
end--;
}
// Create the reversed string
reversedString = new String(charArray);
}
}
public class Main {
public static void main(String[] args) {
// Create an instance of the StringReverser class using the default constructor
StringReverser stringReverser = new StringReverser();
// Get and display the reversed string
String reversedString = stringReverser.getReversedString();
System.out.println("Reverse string: " + reversedString);
}
}