Lab Objectives - Be able to declare a new class - Be able to write a constructor
ID: 3541054 • Letter: L
Question
Lab Objectives
- Be able to declare a new class
- Be able to write a constructor
- Be able to write instance methods that return a value ? Be able to write instance methods that take arguments
- Be able to instantiate an object
- Be able to use calls to instance methods to access and change the state of an object
Introduction
Everyone is familiar with a television. It is the object we are going to create in this lab. First we need a blueprint. All manufacturers have the same basic elements in the televisions they produce as well as many options. We are going to work with a few basic elements that are common to all televisions.
Think about a television in general. It has a brand name (i.e. it is made by a specific manufacturer). The television screen has a specific size. It has some basic controls. There is a control to turn the power on and off. There is a control to change the channel. There is also a control for the volume. At any point in time, the television
Explanation / Answer
public class Television {
private final String MANUFACTURER ;
private final int SCREEN_SIZE ;
private boolean powerOn;
private int channel;
private int volume = 0;
//Constructor to set the television to specific brand and specific size
//and set the power on value to false i.e television is off
public Television(String mANUFACTURER, int sCREEN_SIZE) {
super();
MANUFACTURER = mANUFACTURER;
SCREEN_SIZE = sCREEN_SIZE;
this.powerOn = false;
}
// setChannel. The setChannel method will store the desired station in the
// channel field.
public void setChannel(int channel) {
this.channel = channel;
}
// The power method will toggle the power between on and off, changing the
// value stored in the powerOn field from true to false or from false to true.
public void setPowerOn(boolean powerOn) {
this.powerOn = powerOn;
}
public boolean isPowerOn() {
return powerOn;
}
// The increaseVolume method will increase the value stored in the volume
// field by 1.
public void increaseVolume() {
if (this.volume == 100)
this.volume = 100;
else
this.volume = volume + 1;
}
// The decreaseVolume method will decrease the value stored in the volume
// field by 1.
public void decreaseVolume() {
if (this.volume == 0)
this.volume = 0;
else
this.volume = volume - 1;
}
// The getChannel method will return the value stored in the channel field.
public int getChannel() {
return channel;
}
// The getVolume method will return the value stored in the volume field.
public int getVolume() {
return volume;
}
//The getManufacturer method will return the constant value stored in the MANUFACTURER field.
public String getMANUFACTURER() {
return MANUFACTURER;
}
//The getScreenSize method will return the constant value stored in the SCREEN_SIZE field.
public int getSCREEN_SIZE() {
return SCREEN_SIZE;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.