-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFront3.java
More file actions
24 lines (23 loc) · 723 Bytes
/
Front3.java
File metadata and controls
24 lines (23 loc) · 723 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
/*
* Given a string, we'll say that the front is the first 3 chars of the string. If the string length is less than 3, the front is whatever is there. Return a new string which is 3 copies of the front.
* front3("Java") → "JavJavJav"
* front3("Chocolate") → "ChoChoCho"
* front3("abc") → "abcabcabc"
*/
public class Front3 {
//Class for testing and setting dummy values
public static void main( String [] args ) {
//Output tests
System.out.println( front3( "Hello World" ) );
}
public static String front3( String str ) {
if( str.length() < 3 ) {
str = str + str + str ;
return str;
} else {
str = str.substring( 0, 3 );
str = str + str + str;
return str;
}
}
}