Question

In: Computer Science

JAVA package stringPractice1068; public class SomePracticeStringMethods { /* returns true if c is a punctuation mark...

JAVA

package stringPractice1068;

public class SomePracticeStringMethods {
/* returns true if c is a punctuation mark or false otherwise
*
* Punctuation mark is defined as:
* apostrophe '
* comma ,
* period .
* semicolon ;
* colon :
* exclamation point !
* question mark ?
*
* (You don't have to worry about any others)
*/
public static boolean isPunct(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 punctuation mark in s or
* -1 if s contains no punctuation marks
*/
public static int indexOfFirstPunct(String s) {
   return -1;
}

/*
* returns the index of the first occurrence of a punctuation mark 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 indexOfFirstPunct(String s, int startPosition) {
   return -1;
}

/*
* returns the index of the last occurrence of a punctuation mark in s or -1 if s
* contains none
*/
public static int indexOfLastPunct(String s) {
   return -1;
}

/*
* returns s in reverse. For example, if s is "Apple", the method returns the
* String "elppA"
*/

Solutions

Expert Solution

package stringPractice1068;

public class SomePracticeStringMethods {

    public static boolean isPunct(char c) {
        return c == '\'' || c == ',' || c == '.' || c == ';' || c == ':' || c == '!' || c == '?';
    }

    public static int indexOfFirstPunct(String s) {
        return indexOfFirstPunct(s, 0);
    }

    public static int indexOfFirstPunct(String s, int startPosition) {
        for(int i = startPosition; i < s.length(); ++i) {
            if(isPunct(s.charAt(i))) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOfLastPunct(String s) {
        return indexOfLastPunct(s, s.length()-1);
    }

    public static int indexOfLastPunct(String s, int endPosition) {
        for(int i = endPosition; i >= 0; --i) {
            if(isPunct(s.charAt(i))) {
                return i;
            }
        }
        return -1;
    }
}

Related Solutions

Fix the following java code package running; public class Run {    public double distance; //in...
Fix the following java code package running; public class Run {    public double distance; //in kms    public int time; //in seconds    public Run prev;    public Run next;    //DO NOT MODIFY - Parameterized constructor    public Run(double d, int t) {        distance = Math.max(0, d);        time = Math.max(1, t);    }       //DO NOT MODIFY - Copy Constructor to create an instance copy    //NOTE: Only the data section should be...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void main(String[] args) { Rectangle r1 = new Rectangle(0,0,100,150); System.out.println(r1);    Rectangle r2 = new Rectangle(50,75,100,150); System.out.println(r2);    Rectangle r3 = r1.intersection(r2);    } } Write a program that takes both Rectangle objects, and uses the intersection method to determine if they overlap. If they do overlap, then print it's coordinates along with its width and height. If there is no intersection, then have the...
The language is java package hw; public class MyLinkedList<E> { SLLNode<E> head = null; public MyLinkedList()...
The language is java package hw; public class MyLinkedList<E> { SLLNode<E> head = null; public MyLinkedList() {} // O(1) public MyLinkedList(E[] elements) { // O(elements.length) for(int i=elements.length-1;i>=0;i--) add(elements[i]); } public void printLinkedList() { // T(N) = O(N) System.out.print("printLinkedList(): "); SLLNode<E> node = head; while(node != null) { System.out.print(node.info + " "); node = node.next; // move to the next node } System.out.println(); } public void add(E e) { // T(N) = O(1) SLLNode<E> newNode = new SLLNode<E>(e); newNode.next = head;...
Task Generics: GenericStack Class. Java. package Modul02; public class GenericStack<E> { private java.util.ArrayList<E> list = new...
Task Generics: GenericStack Class. Java. package Modul02; public class GenericStack<E> { private java.util.ArrayList<E> list = new java.util.ArrayList<>(); public int size() { return list.size(); } public E peek() { return list.get(size() - 1); } public void push(E o) { list.add(o); } public E pop() { E o = list.get(size() - 1); list.remove(size() - 1); return o; } public boolean isEmpty() { return list.isEmpty(); } @Override public String toString() { return "stack: " + list.toString(); } } package Modul02; public class TestGenericStack...
Given the following Java code: class C { public int foo(C p) { return 1; }...
Given the following Java code: class C { public int foo(C p) { return 1; } } class D extends C { public int foo(C p) { return 2; } public int foo(D p) { return 3; } } C p = new C(); C q = new D(); D r = new D(); int i = p.foo(r); int j = q.foo(q); int k = q.foo(r); (Remember that in Java every object is accessed through a pointer and that methods...
What is the punctuation mark that is required to end every SQL statement?
What is the punctuation mark that is required to end every SQL statement?
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
Create a Java class with the name (identifier) MyProgram and place it in the Questions package....
Create a Java class with the name (identifier) MyProgram and place it in the Questions package. You will need to create a main method for this class and place all of your code inside of that method. Your program should complete the following steps: - Prompt the user to enter a name for a Student. - Use the Scanner class to read in the user input and store the result in a variable. - Use the Random class to generate...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT