A class named SerialPublication is defined as shown in the class diagram It cont
ID: 3827859 • Letter: A
Question
A class named SerialPublication is defined as shown in the class diagram It contains An enum/enumeration type named Frequency with the value of daily, weekly, monthly, quarterly, yearly and undefined. A protected Instance variable frequency of type Frequency. Two constructors. One takes no parameter and sets the value of frequency to undefined. One takes a String parameter and uses the parameter to call the setFrequency() method. A setFrequency() method that takes a String parameter and sets the instance variable frequency to the matching value in the enum. If there is no matching value in the enum the method sets frequency to undefined. a getFrequency() method that returns the value of frequency in a lowercase String. Write the SerialPublication class in the answer box below.Explanation / Answer
Solution :
class SerialPublication {
protected Frequency frequency;
public SerialPublication(){
frequency = Frequency.UNDEFINED;
}
public SerialPublication(String s){
setFrequency(s);
}
public void setFrequency(String s){
switch(s){
case "DAILY": frequency = Frequency.DAILY; break;
case "WEEKLY" : frequency = Frequency.WEEKLY; break;
case "MONTHLY" : frequency = Frequency.MONTHLY; break;
case "QUATERLY" :frequency = Frequency.QUATERLY; break;
case "YEARLY" : frequency = Frequency.YEARLY; break;
default : frequency = Frequency.UNDEFINED; break;
}
}
public String getFrequency(){
String s;
switch(frequency){
case DAILY: s = "daily" ; break;
case WEEKLY : s = "weekly" ; break;
case MONTHLY : s = "monthly" ; break;
case QUATERLY : s = "quaterly" ; break;
case YEARLY : s = "yearly" ; break;
default : s = "undefined" ; break;
}
return s;
}
}
enum Frequency{
DAILY,
WEEKLY,
MONTHLY,
QUATERLY,
YEARLY,
UNDEFINED
}
if you have any doubts, you can ask in comment section.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.