Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA CODE: Activity 1. A character can be represented as a user-defined type (a.

ID: 3697801 • Letter: J

Question

JAVA CODE:

Activity 1.
A character can be represented as a user-defined type (a.k.a. object): Letter. You will
implement this new type, with the above attributes, and corresponding methods : i)
constructor(s) - Letter, ii) accessor(s), a.k.a. getter(s) – getLetter, getNumber, iii)
mutator(s), a.k.a. setter(s), – setLetter, setNumber, and iv) a print method that prints the
information in any current Letter. The signatures of these methods are included in the file
Letter.java that is provided to you.
The print method for an object whose attributes are as follows:
character: “A”
number: 1
should print the following:
The character “E” should be replaced
with 2.

***added info***

A,a,E,e,I,i,O,o,U,u 0,1,2,3,4,5,6,7,8,9

java

The print method for an object whose attributes are as follows: character: “A” number: 1 should print the following: The character “E” should be replaced with 2.

Explanation / Answer

Note: a DataType is a class too. More Information here : http://introcs.cs.princeton.edu/java/32class/

// The idea behind Setters and Getters is to "encapsulate" the class information and hide it. This prevent possible //problems later. Ok the code :)


/**
*
* @author Phoenix
*/
public class Letter {
  
    private char letter;
    private int number;
  
    public Letter() {
        // If you need something special to initialize, this is the place to
        // code it. Note :( replace with this constructor if you want to use Polymorph :
        // public Letter(char letter){ this letter = letter;} but standard way is to use setters.
    }

    public char getLetter() {
        return letter;
    }

    public void setLetter(char letter) {
        this.letter = letter; // using "this" statement we are first taking our "char letter" and asign it to a incoming variable.
  
  
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        // USE THIS IF U WANT TO VALIDATE MATCH IN DATATYPE
    /* if(number == 0 && letter == 'A'){this.number = number;}else{System.out.println("Error: Invalid Input");}
        if(number == 1 && letter == 'a'){this.number = number;}else{System.out.println("Error: Invalid Input");}
        if(number == 2 && letter == 'E'){this.number = number;}else{System.out.println("Error: Invalid Input");}
        if(number == 3 && letter == 'e'){this.number = number;}else{System.out.println("Error: Invalid Input");}
        if(number == 4 && letter == 'I'){this.number = number;}else{System.out.println("Error: Invalid Input");}
        if(number == 5 && letter == 'i'){this.number = number;}else{System.out.println("Error: Invalid Input");}
        if(number == 6 && letter == 'O'){this.number = number;}else{System.out.println("Error: Invalid Input");}
        if(number == 7 && letter == 'o'){this.number = number;}else{System.out.println("Error: Invalid Input");}
        if(number == 8 && letter == 'U'){this.number = number;}else{System.out.println("Error: Invalid Input");}
        if(number == 9 && letter == 'u'){this.number = number;}else{System.out.println("Error: Invalid Input");}
      */
        // THIS IS THE STANDARD WAY
        this.number = number; // using "this" statement we are first taking our "char letter" and asign it to a incoming variable.
  
      
    }
  
    public void Print(){
        // As you say "that prints the
        //information in any current Letter", the information of this letter
        // will be those contained in letter and number
        switch(letter){
            case 'A': System.out.println('0');
            break;
            case 'a': System.out.println('1');
            break;
            case 'E': System.out.println('2');
            break;
            case 'e': System.out.println('3');
            break;
            case 'I': System.out.println('4');
            break;
            case 'i': System.out.println('5');
            break;
            case 'O': System.out.println('6');
            break;
            case 'o': System.out.println('7');
            break;
            case 'U': System.out.println('8');
            break;
            case 'u': System.out.println('9');
            break;
            default: System.out.println("Error 0x0 Bad DataType Range");
                   
        }
  
  
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       // Some examples:
       Letter test = new Letter();
       test.setLetter('a');
       System.out.println(test.getLetter());
       test.Print();
       //
    }
  
}

I comment this code, and add some validations if you need. Have a Good Day :)