Java: compile and test before posting; create a class called TelevisionChannel t
ID: 3593044 • Letter: J
Question
Java: compile and test before posting;
create a class called TelevisionChannel that has three fields: name (String), number (int), and cable (boolean that represents whether the channel is a cable channel) and implement the following methods for that class:
a default constructor that initializes the fields to an empty string, 0, and false respectively
a secondary constructor that takes three parameters
accessor methods for each field
mutator methods for each field
a "business" method returning the number of digits in the channel number. For instance, if the channel number is 21, the method should return 2; if the channel number is 412, the method should return 3
To test your methods create a client class called TelevisionChannelClient.
Explanation / Answer
TelevisionChannelClient.java
public class TelevisionChannelClient {
public static void main(String[] args) {
TelevisionChannel t = new TelevisionChannel();
t.setNumber(11);
t.setName("bbb");
t.setCable(true);
System.out.println("Name: "+t.getName());
System.out.println("Number: "+t.getName());
System.out.println("Is Cable: "+t.isCable());
System.out.println("Business: "+t.business());
System.out.println("-------------------------------");
TelevisionChannel t1 = new TelevisionChannel("aaa", 412, true);
System.out.println("Name: "+t1.getName());
System.out.println("Number: "+t1.getName());
System.out.println("Is Cable: "+t1.isCable());
System.out.println("Business: "+t1.business());
}
}
TelevisionChannel.java
public class TelevisionChannel {
private String name;
private int number;
private boolean cable;
public TelevisionChannel() {
name = "";
number = 0;
cable = false;
}
public TelevisionChannel(String name, int number, boolean cable) {
this.name = name;
this.number = number;
this.cable = cable;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public boolean isCable() {
return cable;
}
public void setCable(boolean cable) {
this.cable = cable;
}
public int business() {
int count = 0;
while(number > 0) {
count++;
number/=10;
}
return count;
}
}
Output:
Name: bbb
Number: bbb
Is Cable: true
Business: 2
-------------------------------
Name: aaa
Number: aaa
Is Cable: true
Business: 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.