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...
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:...
Class Employee (All IN JAVA) public class Employee {public String strName, strSalary; public Employee(){strName = "...
Class Employee (All IN JAVA) public class Employee {public String strName, strSalary; public Employee(){strName = " ";strSalary = "$0";} public Employee(String Name, String Salary){strName = Name;strSalary = Salary;} public void setName(String Name){strName = Name;} public void setSalary(String Salary){strSalary = Salary;}public String getName(){return strName;} public String getSalary(){return strSalary;} public String toString(){return(strName + " has a salary of " + strSalary); Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You...
package design; public class FortuneEmployee { /** * FortuneEmployee class has a main methods where you...
package design; public class FortuneEmployee { /** * FortuneEmployee class has a main methods where you will be able to create Object from * EmployeeInfo class to use fields and attributes.Demonstrate as many methods as possible * to use with proper business work flow.Think as a Software Architect, Product Designer and * as a Software Developer.(employee.info.system) package is given as an outline,you need to elaborate * more to design an application that will meet for fortune 500 Employee Information *...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT