Lab1 Solution

advertisement
ICS201: Introduction to Computer Science Computer Science and Software Engineering Department University of Hail
Prepared by: Ebtisam Hamed
Lab 02 : Classes and Objects
SOLUTION
LAB 02
Objectives
• Be able to declare a new class
• Be able to write a constructor
• Be able to write getter methods that return a value
• Be able to write setter methods that take arguments
• Be able to instantiate an object (create object)
• Be able to use calls to instance methods to access and change the state of an Object.
Pre-Lab
Fill in blanks with one word from the list
1. Each method can specify parameters that represent additional information the method requires to perform
2.
3.
4.
5.
its task correctly.
Instance variables of the numeric primitive types are initialized to 0 and instance variables of type Boolean
are initialized to false .
Each parameter must specify both a(n) type and a(n) name .
A(n) class normally consists of one or more methods that manipulate the attributes that belong to a
particular object.
Classes often provide public methods to allows clients of the class to set or get the values of private
instance variables.
Task 1: defining class Television
1. We will define television class. diagram for this class as shown
below.
2.
Each object that is created from the television class must be able to
hold information about that instance of a television in fields. So a
television object will have the following attributes:
a. brand. attribute will hold the brand name.
b. screenSize. The screenSize attribute will hold the size of the
television screen.
c. powerOn. The powerOn attribute will hold the value true if the
power is on, and false if the power is off.
d. channel. The channel attribute will hold the value of the station that the television is showing.
e. volume. The volume attribute will hold a number value representing the loudness (0 being no
sound).
3. The television object will also be able to control the state of its attributes. These controls become
methods in our class.
a. setChannel. The setChannel method will store the desired station in the channel field.
b. power. 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.
c.
d.
e.
f.
getChannel. The getChannel method will return the value stored in the channel field.
getVolume. The getVolume method will return the value stored in the volume field.
getBrand. The getBrand method will return the constant value stored in the brand field.
getScreenSize. The getScreenSize method will return the constant value stored in the screensize
field.
4. Create a constructor definition that has two parameters, brand and a screen size. These parameters will
bring in information
Task 2 :Running the application
1. You can only execute (run) a program that has a main method, so write a driver program named
TelevisionDemo.java ) Make sure it is in the same directory as Television.java.
a. Declare Television object called portable.
b. Instantiate portable to be a Sharp 19 inch television.
c. Use a call to the power method to turn the power on.
d. Use the suitable method to change the channel to 56.
e. Print out the values of brand , screenSize and channel as output to your application
2. Compile and debug this class.
3. Run TelevisionDemo.
4.
class Television{
private String brand;
private int screenSize;
private boolean powerOn;
private int channel;
private int volume;
public Television(String brand,int size)
{this.brand=brand;
screenSize=size;}
public void setChannel(int station){
channel=station;}
public void power(){
this.powerOn=!powerOn;}
public int getChannel(){
return channel;}
public int getVolume(){
return volume;}
public String getBrand(){
return brand;}
public int getScreenSize(){
return screenSize;}}
public class TelevisionDemo{
public static void main(String[] args){
Television portable = new Television("sharp",19);
portable.power();
portable.setChannel(56);
System.out.println("Brand of TV " + portable.getBrand()+" Screen size is "+
portable.getScreenSize()+" and channel now is "+portable.getChannel());
}}
Download