Question

Programming project in Java: You are allowed to use the following methods from the Java API:...

Programming project in Java:

  • You are allowed to use the following methods from the Java API:

    • class String
      • length
      • charAt
    • class StringBuilder
      • length
      • charAt
      • append
      • toString
    • class Character
      • any method

Create a class called HW2 that contains the following methods:

1. isAlphabeticalOrder takes a String as input and returns a boolean: The method returns true if all the letters of the input string are in alphabetical order, regardless of case. The method returns false otherwise. Do not use arrays to solve this problem.

> HW2.isAlphabeticalOrder("ac!ffG1h") true > HW2.isAlphabeticalOrder("ac!nfG1h") false

2. removeNchars takes a String, an int and a char and returns a String: The output string is the same as the input string except that the first n occurrences of the input char are removed from the string, where n represents the input integer. If there are not n occurrences of the input character, then all occurrences of the character are removed. Do not use arrays to solve this problem.

> HW2.removeNchars("Hello there!", 2, 'e')
"Hllo thre!"
> HW2.removeNchars("Hello there!", 10, 'e')
"Hllo thr!"
> HW2.removeNchars("Hello there!", 1, 'h')
"Hello tere!"

3. removeString takes two Strings and returns a String:
The output string should be the same as the first input string except that every(*) occurrence of the second input string should be removed.
(*) If a string exists twice as an overlap (ex: "ellelle" contains two "elle"), the first occurrence is removed.
(*) You do not remove strings that are created by the removal of other strings (ex: "elellele" will create "elle" after removing "elle"). Do not use arrays to solve this problem.

> HW2.removeString("ellelle", "elle")
"lle"
> HW2.removeString("elellele", "elle")
"elle"
> HW2.removeString("ellellelle", "elle")
"ll"

4.  moveAllXsRight takes a char and a String as input and returns a String:
The output string should be the same as the input string except that every occurrence of the input character should be shifted one character to the right. If it is impossible to shift a character to the right (it is at the end of the string), then it is not shifted. Do not use arrays to solve this problem.

> HW2.moveAllXsRight('X', "abcXdeXXXfghXiXXjXX")
"abcdXefXXXghiXjXXXX"

5. moveAllXsdown takes a char and a two dimensional array of char as input and returns nothing:
The method should take every occurrence of the input character and shift it "down" to the next row of the array, and at the same column. If it is impossible to to shift the character down (it is at the "bottom" row or the row below does not have that column), then it is not shifted. Do not use Strings to solve this problem.

> char[][] board = {{'a','b','c','X'},{'d','X','e','f','X'},{'X','X','i'},{'X','j','k','l'}};
> HW2.moveAllXsDown('X', board)
> board
{ { a, b, c, f }, { d, j, e, X, X }, { X, X, i }, { X, X, k, l } }

6. moveXDownLeft takes a char and a two dimensional array of char as input and returns nothing:
The method should find the first occurrence of the char in the array (searching from "top" to "bottom" and "left" to "right"), and it should slide that character in the array down and to the left as far as it can go. Any characters on that diagonal are slide up and to the right to fill. Do not use Strings to solve this problem.

> char[][] board = {{'a','b','c','X'},{'d'},{'e','f','g','h'},{'i','j','k'},{'l','m','n','o'}};
> HW2.moveXDownLeft('X', board)
> board
{ { a, b, c, f }, { d }, { e, i, g, h }, { X, j, k }, { l, m, n, o } }

7. moveXDownRight: takes a char and a String as input and returns a String. The returned String should take the first occurrence of the input char in the string, and shift it "down" (by skipping over the special end-of-line character '\n', and "right" (by moving it one character to the right each time you pass an end-of-line character). Do not use arrays to solve this problem.

> HW2.moveXDownRight('X', "Xabc\nd\nefgh\nijk\nlmnop")
"gabc
d
efph
ijk
lmnoX"

I've already got started on this project, completing the first method as follows:

public class Hw2 {
   public static boolean isAlphabeticalOrder(String s) {
       s = s.toUpperCase();
       String res = "";
       //removing all chars
       for (int i = 0; i < s.length(); i++)
           if (Character.isLetter(s.charAt(i)))
               res = res + s.charAt(i);
       s = res;
       // iteraring each char and checking it is less than previous one
       for (int i = 0; i < s.length() - 1; i++) {
           if (!(s.charAt(i) <= s.charAt(i + 1)))
               return false;
       }
       return true;
   }

}

Thanks!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, Please find the solutions and post the remaining questions seperately. We are authorised to answer only one question per post, but still i extended my self and did three. Thanks.

public class HW2 {
    public static void main(String[] args) {
        System.out.println(isAlphabeticalOrder("abcmnr"));
        System.out.println(isAlphabeticalOrder("pac"));
        System.out.println(removeNchars("yadayada", 2, 'a'));
        System.out.println(removeString("ellelle", "elle"));
    }

    static boolean isAlphabeticalOrder(String s) {
        char temp = s.charAt(0);
        int i = 0;
        s = s.toLowerCase();
        for (int j = 1; j < s.length(); j++) {
            if (Character.toString(temp).compareTo(Character.toString(s.charAt(j))) > 0) {
                return false;
            }
            temp = s.charAt(j);
        }
        return true;
    }

    static String removeNchars(String s, int n, char ch) {
        int count = 1;
        StringBuilder sb = new StringBuilder("");
        for (int i = 0; i < s.length(); i++) {
            if (ch == s.charAt(i) && count <= n) {
                count++;//increase count until n
                continue;
            }
            sb.append(s.charAt(i));
        }
        return sb.toString();
    }

    static String removeString(String source, String remove) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < source.length(); i++) {
            if (i + remove.length() <= source.length() && remove.equalsIgnoreCase(source.substring(i, i + remove.length()))) {
                i = i + remove.length();
            }
            sb.append(source.charAt(i));
        }
        return sb.toString();
    }

}

Sample out:

Screens:

Add a comment
Know the answer?
Add Answer to:
Programming project in Java: You are allowed to use the following methods from the Java API:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This program should be run on Visual Studio. Please use printf and scanf as input and output. Tha...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and upload for testing by zyLabs You will write 2 functions which resemble functions in the cstring library. But they will be your own versions 1. int cstrcat(char dstDchar src) which concatenates the char array srcl to char array dstD, and returns the...

  • C++ Programming Question: This programming assignment is intended to demonstrate your knowledge of the following: ▪...

    C++ Programming Question: This programming assignment is intended to demonstrate your knowledge of the following: ▪ Writing a while loop ▪ Write functions and calling functions Text Processing [50 points] We would like to demonstrate our ability to control strings and use methods. There are times when a program has to search for and replace certain characters in a string with other characters. This program will look for an individual character, called the key character, inside a target string. It...

  • Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include...

    Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...

  • Can you help me make these two methods listed below work? I have code written but...

    Can you help me make these two methods listed below work? I have code written but it is not working. I would appreciate any advice or help. Function "isAPalidrome" accepts an array of characters and returns an integer. You must use pointer operations only, no arrays. This function should return 1 (true) if the parameter 'string' is a palindrome, or 0 (false) if 'string' is not a palindrome. A palindrome is a sequence of characters which when reversed, is the...

  • Loops | Methods | Arrays(programming language java) in software (ATOM) Write a toolkit that includes some...

    Loops | Methods | Arrays(programming language java) in software (ATOM) Write a toolkit that includes some common functions a user might want or need An encryption/ decryption method. Both methods should take in a String (the String that needs to be changed), and an encryption value. The methods need to change the String by the value Write a method that takes an integer array as an argument. The method should return the sum of all of the elements Write a...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. 1. write the code using function 2. Please try to implement a function after the main function and provide prototype before main function. Total Characters in String Array 10 points Problem 2 Declare a string array of size 5. Prompt the user enter five strings that are...

  • Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names,...

    Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names, and read in course names from an input file. Then do the output to show each course name that was read in. See Chapter 8 section on "C-Strings", and the section "Arrays of Strings and C-strings", which gives an example related to this lab. Declare the array for course names as a 2-D char array: char courseNames[10] [ 50]; -each row will be a...

  • Hello, I'm having trouble with this certain part to a project of mine. Say I have...

    Hello, I'm having trouble with this certain part to a project of mine. Say I have two strings, String left and String right. This is part of an expression evaluation problem, and I need to have a certain piece of the expression in left and a certain piece of the expession in right. The code I have works with everything except if * or / come before + or -. No parentheses included, I have another method that deals with...

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT