In: Computer Science
Write the class declarations for the following objects' descriptions. NO CODE. That means only properties and method declarations. Like...
class Lamp {
int mBulbs; void Explode();
};
1) A Laptop has a color, brand, and processor speed. You can open, close, turn on, and turn off a laptop.
2) A monitor has a screen size and a brightness.
3) Oh wait, a laptop has a monitor itself. To do this, would you add a line to answer #1 or answer #2, and what line would you add?
Answer:
1)
class Laptop {
String color;
String brand;
float proc_speed;
void open();
void close();
boolean turn_on();
boolean turn_off();
}
class Monitor{
float screen_size;
float brightness;
}
I would like to add monitor property in the Monitor class.
A monitor is a display which should be specified in the Monitor class itself.
But in our question, " a laptop has a monitor itself". For this, we can use the inheritance concept to do this. Use extends keyword to inherit the properties of Monitor class into Laptop.
Above description is the solution to this problem.
The following are the attribute values of display types of laptops.
HD, FHD, QHD, UHD, AMOLED, LED, LCD, OLED etc.
The final concluded Classes for the given requirement are as follows:
class Laptop {
String color; // Black, Red etc
String brand; // Dell, HP, Acer, Asus etc
float proc_speed; // the values will be 2.9, 3.1 in G.Hz.
void open(); // It doesn't return a value
void close(); // It doesn't return a value
boolean turn_on(); // It should be boolen type, since it is either return true(1) or false(0) state
boolean turn_off(); // It should be boolen type, since it is either return true(1) or false(0) state
}
class Monitor{
float screen_size; // 15.6, 16.0 in inch etc
float brightness; // 209, 301 nits etc
String monitor_type; // added in the Monitor class values like AMOLED, LED, LCD etc.
}