JAVA!!! A character can be represented as a user-defined type (a.k.a. object): L
ID: 3696884 • Letter: J
Question
JAVA!!! 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: 0 should print the following:
The character “A” should be replaced with 0.
Explanation / Answer
public class Letter {
private char letter;
private int code;
public Letter() {
letter = 'A';
code = 0;
}
/**
* @param letter
* @param code
*/
public Letter(char letter, int code) {
this.letter = letter;
this.code = code;
}
/**
* @return the letter
*/
public char getLetter() {
return letter;
}
/**
* @return the code
*/
public int getCode() {
return code;
}
/**
* @param letter the letter to set
*/
public void setLetter(char letter) {
this.letter = letter;
}
/**
* @param code the code to set
*/
public void setCode(int code) {
this.code = code;
}
@Override
public String toString() {
return "Letter: "+letter+
" Code: "+code;
}
public void print(){
System.out.println("The character ""+letter+"" should be replaced with "+code+".");
}
}
############## Test Class #####################
public class TestLetter {
public static void main(String[] args) {
Letter letter = new Letter('C',2);
letter.print();
}
}
/*
Output:
The character "C" should be replaced with 2.
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.