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;...
Write java code to reverse a linked list. Fill in reverseLists() ReverseLinkedList.java package mid; public class...
Write java code to reverse a linked list. Fill in reverseLists() ReverseLinkedList.java package mid; public class ReverseLinkedList {     private static class ListNode {         int val;         ListNode next;         ListNode() {         }         ListNode(int val) {             this.val = val;         }         ListNode(int val, ListNode next) {             this.val = val;             this.next = next;         }     }     public static void printList(ListNode l1) {         ListNode cur = l1;         while(cur != null) {             System.out.println(cur.val);             cur = cur.next;         }     }     public static ListNode reverseLists(ListNode h1) {     }     public static void...
Using Java, Complete the LinkedListBag down below by using the (LinkedListCollection.java:) package Homework3; public class LinkedListBag...
Using Java, Complete the LinkedListBag down below by using the (LinkedListCollection.java:) package Homework3; public class LinkedListBag extends LinkedListCollection { LinkedListBag() { } public T grab() { T result; Node cursor = head; int rand = (int) (Math.random() * size()); // Some code in here.. result = cursor.getInfo(); remove(result); return result; } } LinkedListCollection.java: package Homework3; public class LinkedListCollection <T> { protected Node<T> head = null; public LinkedListCollection() { } public boolean isEmpty() { return head == null; } public int...
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...
Using Java, Complete LinkedListSet: package Homework3; public class LinkedListSet <T> extends LinkedListCollection <T> { LinkedListSet() {...
Using Java, Complete LinkedListSet: package Homework3; public class LinkedListSet <T> extends LinkedListCollection <T> { LinkedListSet() { } public boolean add(T element) { // Code here return true; } } Homework3 class: public class Homework3 { public static void main(String[] args) { ArrayCollection ac1 = new ArrayCollection(); // Calling Default Constructor ArrayCollection ac2 = new ArrayCollection(2); // Calling overloaded constructor ArraySet as1 = new ArraySet(); ac2.add("Apple"); ac2.add("Orange"); ac2.add("Lemon"); // This can't be added into ac2 as collection is full System.out.println(ac2.remove("Apple")); //...
Using Java Languse, Complete ArraySet.java down below by using (Array collection) : package Homework3; public class...
Using Java Languse, Complete ArraySet.java down below by using (Array collection) : package Homework3; public class ArraySet extends ArrayCollection { public ArraySet() { } public ArraySet(int size) { super(size); } public boolean add(T element) { // Complete your code here return true; } } ArrayCollection.java: package Homework3; public class ArrayCollection { protected static final int DEFAULT_CAPACITY = 100; protected T[] elements; protected int numberOfElements; public ArrayCollection() { this(DEFAULT_CAPACITY); } public ArrayCollection(int size) { elements = (T[]) new Object[size]; numberOfElements =...
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?
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT