In: Computer Science
Implement each of the following functions and write a basic main() function that tests each. For convenience, you should be able to find this starter class under Mimir's assignment 4 starter code.
Do not change the name, parameters, or returns of any of these functions or of the name of the class itself. There is also no need in this assignment for any global variables. You are strongly encouraged to use your solution for some of these functions in others by making a function call (and not by copying and pasting). You are also strongly encouraged to write your own short, helper functions in order to simply things.
public class StringPractice {
/*
* returns true if c is one of the characters typically used to represent a "face card" in a
* standard deck of playing cards.
*
* These include: jack 'J' or 'j' queen 'Q' or 'q' king 'K' or 'k' ace 'A' or 'a
*/
public static boolean isFace(char c) {
/*
* placeholder just so that the function compiles. fill in your implementation here.
*
* there is a similar placeholder for each of the remaining functions
*/
return true;
}
/*
* returns the index of the first face-card letter in s or -1 if s contains no face-card letters
*/
public static int indexOfFirstFace(String s) {
return -1;
}
/*
* returns the index of the first occurrence of a face-card letter in s starting from index
* startPosition or -1 if there are none at index startPosition or later. Notice that this method
* has the same name as the previous one, but that it takes a different number of arguments. This
* is perfectly legal in Java. It's called "method overloading"
*/
public static int indexOfFirstFace(String s, int startPosition) {
return -1;
}
/*
* returns the index of the last occurrence of a face-card letter in s or -1 if s contains none
*/
public static int indexOfLastFace(String s) {
return -1;
}
/*
* returns s in reverse. For example, if s is "Apple", the method returns the String "elppA"
*/
public static String reversed(String s) {
return "";
}
/*
* returns the number of times that n occurs in h. For example, if h is "Mississippi" and n is
* "ss" the method returns 2.
*/
public static int numOccurrences(String h, String n) {
return 0;
}
/*
* returns true if s is the same backwards and forwards and false otherwise
*/
public static boolean sameInReverse(String s) {
return true;
}
/*
* returns a new String which is the same as s, but with all of the face-card letters removed.
*/
public static String withoutFaces(String s) {
return "";
}
/*
* returns true if s consists only of face-card letters or false otherwise
*/
public static boolean containsOnlyFaces(String s) {
return true;
}
/*
* returns true if s contains no face-card letters or false otherwise
*/
public static boolean containsNoFaces(String s) {
return true;
}
/*
* Passed a reference to a person's name in the form FIRST_NAME LAST_NAME. The method returns a
* reference to a new String in the form LAST_NAME, FIRST_NAME.
*
* For example, if we were passed "Spongebob Squarepants", we'd return "Squarepants, Spongebob".
* You may assume that the method is passed exactly two words separated by a single space.
*/
public static String lastFirst(String s) {
return "";
}
}
1). ANSWER :
GIVENTHAT :
package com.test;
public class StringPractice {
/*
* returns true if c is one of the characters typically
used to represent a
* "face card" in a standard deck of playing cards.
These include : jack
* 'J'or 'j' queen 'Q' or 'q' kind 'k' or 'K' ace 'A'
or 'a'
*/
public static boolean isFace(char c) {
char[] deck = { 'j', 'q', 'k', 'a',
'J', 'Q', 'K', 'A' };
boolean flag = false;
for (int i = 0; i <
deck.length; i++) {
if (c ==
deck[i]) {
flag = true;
break;
}
}
return flag;
}
/*
* returns the index of the first face-card letter in s
or -1 if s contains
* no face-card letters
*/
public static int indexOfFirstFace(String s)
{
char[] deck = { 'j', 'q', 'k', 'a'
};
s = s.toLowerCase();
int index = -1;
for (int i = 0; i <
s.length(); i++) {
for (int j = 0;
j < deck.length; j++) {
if (s.charAt(i) == deck[j]) {
index = i;
return index;
}
}
}
return index;
}
/*
* returns the index of the first face-card letter in s
from a specified
* position or -1 if s contains no face-card
letters
*/
public static int indexOfFirstFace(String s, int
startPosition) {
char[] deck = { 'j', 'q', 'k', 'a'
};
s = s.toLowerCase();
int index = -1;
for (int i = startPosition; i
< s.length(); i++) {
for (int j = 0;
j < deck.length; j++) {
if (s.charAt(i) == deck[j]) {
index = i;
return index;
}
}
}
return index;
}
/*
* returns the index of the last occurrence of a
face-card letter in s or -1
* if s contains non;
*/
public static int indexOfLastFace(String s) {
char[] deck = { 'j', 'q', 'k', 'a'
};
s = s.toLowerCase();
int index = -1;
for (int i = s.length() - 1; i
>= 0; i--) {
for (int j = 0;
j < deck.length; j++) {
if (s.charAt(i) == deck[j]) {
index = i;
return index;
}
}
}
return index;
}
/*
* return s in reverse. For Example if s is "Apple",the
method returns the
* string "elppA"
*/
public static String reversed(String s) {
String rev = "";
for (int i = s.length() - 1; i
>= 0; i--) {
rev +=
s.charAt(i);
}
return rev;
}
/*
* returns the number of times that n occurs in h. For
example, if h is
* "Mississippi" and n is "ss" then the method returns
2.
*/
public static int numOccurrences(String h, String
n) {
return h.split(n).length - 1;
}
/*
* returns true if s is the same backwards and forwards
and false otherwise
*/
public static boolean sameInReverse(String s) {
boolean flag = true;
for (int i = 0, j = s.length() - 1;
i <= j; i++, j--) {
if (s.charAt(i)
!= s.charAt(j)) {
flag = false;
break;
}
}
return flag;
}
/*
* returns new String which is the same as s, but with
all of the face-card
* letters removed.
*/
public static String withoutFaces(String s) {
String s1 = "";
char[] deck = { 'j', 'q', 'k', 'a',
'J', 'Q', 'K', 'A' };
for (int i = 0; i < s.length();
i++) {
boolean flag =
false;
for (int j = 0;
j < deck.length; j++) {
if (s.charAt(i) == deck[j]) {
flag = true;
break;
}
}
if (!flag)
s1 += s.charAt(i);
}
return s1;
}
/*
* returns true if s consists only of face-card letters
or false otherwise
*/
public static boolean containsOnlyFaces(String s)
{
char[] deck = { 'j', 'q', 'k', 'a',
'J', 'Q', 'K', 'A' };
for (int i = 0; i < s.length();
i++) {
boolean flag =
false;
for (int j = 0;
j < deck.length; j++) {
if (s.charAt(i) == deck[j]) {
flag = true;
break;
}
}
if (flag ==
false)
return false;
}
return true;
}
/*
* returns true if s contains no face-card letters or
false otherwise
*/
public static boolean containsNoFaces(String s)
{
char[] deck = { 'j', 'q', 'k', 'a',
'J', 'Q', 'K', 'A' };
for (int i = 0; i < s.length();
i++) {
boolean flag =
false;
for (int j = 0;
j < deck.length; j++) {
if (s.charAt(i) == deck[j]) {
flag = true;
break;
}
}
if (flag ==
true)
return false;
}
return true;
}
/*
* Passed a reference to a person's name in the form of
FIRST_NAME LAST_NAME
* Then the method returns a reference to new String in
the form of
* LAST_NAME,FIRST_NAME.
*
* For Example ,if we were passed "Spongebob
Squarepants", we'd return
* "Squarepants, Spongebob".
* you may assume that the method is passed exactly two
words separated by a
* single space.
*/
public static String lastFirst(String s) {
String[] s1 = s.split(" ");
String s2 = s1[1] + ", " +
s1[0];
return s2;
}
public static void main(String[] args) {
System.out.println(StringPractice.isFace('h'));
System.out.println(StringPractice.indexOfFirstFace("hello"));
System.out.println(StringPractice.indexOfFirstFace("hallj",
2));
System.out.println(StringPractice.indexOfLastFace("hallo"));
System.out.println(StringPractice.reversed("Apple"));
System.out.println(StringPractice.numOccurrences("missisippi",
"pp"));
System.out.println(StringPractice.sameInReverse("hello"));
System.out.println(StringPractice.withoutFaces("jhealloqk"));
System.out.println(StringPractice.containsOnlyFaces("kqwa"));
System.out.println(StringPractice.containsNoFaces("heallo"));
System.out.println(StringPractice.lastFirst("Spongebob
Squarepants"));
}
}
//Please run the main method to execute all the methods. I have called all the methods from main methods.