2

I am trying to give a test is that this particular method

public myOwnString changeCase(){
}

This method is trying to return a new myOwnString where the case of each letter is changed to the opposite.

Example 
Input : String a = "Hello"
Output : "Hello" -> "hELLO"

This is the code I was thinking it might work for the changeCase() :

if (Character.isUpperCase(a)){
            data[i] = Character.toLowerCase(a);
        }
        else if (Character.isLowerCase(a)){
            data[i] = Character.toUpperCase(a);

I understand that the word "Character" is a wrapper class and isnt allow in my coding(google informed unless its wrong). Just needed to find another way without using any of the blacklist, which is String, StringBuilder, or Wrapper classes. The main class isnt suppose to create any string because it is created in the tester class.

I would think I need to use Arrays.toString(), but i would think i have to do it in the beginning of the method before creating the changes.


Main class

public class myOwnString {

//declare the char array
private char data[];

//Main Constructor
public myOwnString(char[] data){
    this.data = data;
    }
}

public myOwnString changeCase(){
    for(int i = 0 ; i < this.data.length ; i++){
        char a = this.data[i];  
    if (Character.isUpperCase(a)){
            this.data[i] = Character.toLowerCase(a);
        }
        else if (Character.isLowerCase(a)){
            this.data[i] = Character.toUpperCase(a);
        }
    }

    return new myOwnString(data);
}


}

Test class:

public class testmyOwnString {

public static void main(String[] args) {
String a = "Welcome to this world";
    char[] input = a.toCharArray();
    myOwnString quote = new myOwnString(input);

    System.out.println("The string is : \"" + a + "\"");
    System.out.println("\n--------------------------------\n");

    System.out.println("To invert the string : " + quote.changeCase());
    System.out.println("\n--------------------------------");
  • What is the problem/question? – Scott Hunter Oct 17 '14 at 02:04
  • 1
    So this is a homework assignment where you are deliberately forbidden to use some classes we would otherwise use with certainty? If we assume that you'll only need to handle ASCII characters correctly, you can check their `int` value and add / subtract the respective amount. Check out this [ASCII character table](https://en.wikipedia.org/wiki/ASCII#ASCII_printable_code_chart), for example. – 5gon12eder Oct 17 '14 at 02:04
  • @ScottHunter The problem I am having is trying to create a method where it will return a result where let say in the tester, I created a string a = "Hello". When using the changeCase() method to the string, the result is "hELLO", the opposite of the string status. The professor is asking not to use create any string, use StringBuilder, or Wrapper classes in the main class. – user4146749 Oct 17 '14 at 02:10
  • @5gon12eder that is what I am guessing that I must use a int to go it the other way around. – user4146749 Oct 17 '14 at 02:11
  • Oh, deleted my comment too early… @Bohemian I don't think this is a duplicate of the linked question because the OP is already showing the solution shown in that answer but is mentioning that (s)he must not use the `Character` class due to arbitrary restrictions. – 5gon12eder Oct 17 '14 at 02:11
  • @5gon12eder isnt "Character" a wrapper class? I searched it up and most sources said it was a wrapper indeed, unless I am wrong and looking at the wrong info. – user4146749 Oct 17 '14 at 02:13
  • @user4146749 You don't need the `Character` class if your assignment says you must not use it. Check the table to see what to add / subtract to what characters and then do it for each primitive `char` (not the wrapped `Character`). – 5gon12eder Oct 17 '14 at 02:15
  • @5gon12eder does it look like this int ch=s.charAt(i); if(ch>64&&ch<91) { ch=ch+32; System.out.print( (char) ch); – user4146749 Oct 17 '14 at 02:50
  • @5gon12eder I looked pretty similar, but OK go for it – Bohemian Oct 17 '14 at 03:05

1 Answers1

0

Here is my suggestion using ASCii code.

First, we need to calculate a distance between 'a' and 'A'

Then, determine whether a character is uppercase or lowercase. The last thing is that + or - the gap from the original character to convert it.

public class MyOwnString {

    private char data[];
    public static void main(String[] args) {
        String a = "Welcome to this world";
        char[] input = a.toCharArray();
        MyOwnString quote = new MyOwnString(input);

        System.out.println("The string is : \"" + a + "\"");
        System.out.println("\n--------------------------------\n");

        System.out.println("To invert the string : " + quote.changeCase());
        System.out.println("\n--------------------------------");
    }

    public MyOwnString(char[] data){
        this.data = data;
    }

    public String changeCase(){
        int gap = 'A' - 'a';
        for(int i = 0; i < this.data.length; i++){
            char a = this.data[i];  
            if (a >= 'a' && a <= 'z') {
                this.data[i] = (char)(a + gap);
            } else if (a >= 'A' && a <= 'Z') {
                this.data[i] = (char)(a - gap);
            }
        }
        return new String(this.data);

    }
}

And, I am confused whether you have to return MyOwnString instead of String. If you really want to return MyOwnString object in changeCase() method, here is modified code (note that you must implement toString() method to print out the string):

public class MyOwnString {

    private char data[];
    public static void main(String[] args) {
        String a = "Welcome to this world";
        char[] input = a.toCharArray();
        MyOwnString quote = new MyOwnString(input);

        System.out.println("The string is : \"" + a + "\"");
        System.out.println("\n--------------------------------\n");

        System.out.println("To invert the string : " + quote.changeCase());
        System.out.println("\n--------------------------------");
    }

    public MyOwnString(char[] data){
        this.data = data;
    }

    public MyOwnString changeCase(){
        int gap = 'A' - 'a';
        char[] tmpData = new char[this.data.length];
        for(int i = 0; i < this.data.length; i++){
            char a = this.data[i];  
            if (a >= 'a' && a <= 'z') {
                tmpData[i] = (char)(a + gap);
            } else if (a >= 'A' && a <= 'Z') {
                tmpData[i] = (char)(a - gap);
            } else {
                tmpData[i] = a;
            }
        }
        return new MyOwnString(tmpData);

    }

    public String toString() {
        return new String(this.data);
    }
}
MaxHeap
  • 1,138
  • 2
  • 11
  • 20
  • The tasks was that each method must return a new MyOwnString object. I am wondering that if I wanted to do the toUppercase and toLowercase method seperately, i would have to keep creating tempData for both, but with different request. – user4146749 Oct 17 '14 at 07:10