This chapter deals with the idea of objects. An object is ANY entity we want to
ID: 3621980 • Letter: T
Question
This chapter deals with the idea of objects. An object is ANY entity we want to represent in a computer, either real or imaginary. To describe an object, we actually describe a class of similar objects, giving us the chance of create more than one object with the same description. For example, if we want to represent a chair in Java, we create the class Chair as follows:public class Chair {
}
Inside this class declaration we need to specify two things: which attributes we want to keep for every chair and which behaviors we want any chair to be able to do. The attributes are known as the instance variables and the behaviors are known as the methods. For example, for every chair we could specify attributes like its color and the number of legs. Among the behaviors we can care to mention we could have the ability to retrieve the color and the number of legs the chair has (these are called accessor methods) and the ability to change its color and the number of legs it has (these are called mutator methods). All these attributes and behaviors can be described in Java as follows:
public class Chair {
String color; // instance variable for color of the Chair
int legs; // instance variable to hold the number of legs
// Accessor Methods
public String getColor() { // method to return the current color
return this.color;
}
public int getLegs() { // method to return the current number of legs
return this.legs;
}
// Mutator Methods
public void setColor(String newColor) { // method to change color
this.color = newColor;
}
public void setLegs(int newLegs) { // method to change # of legs
this.legs = newLegs;
}
}
Once we have written this description of a Chair inside the file Chair.java we can actually compile this file, but it will not run. To use it we need another file, known as the driver file. The driver file is another class that will contain a main method. Inside the main method variables of type Chair will be created (instantiated) and used. For example, the following driver class creates two Chairs, set their colors and their number of legs, and prints some of these values:
public class ChairDriver {
public static void main (String[ ] args) {
Chair firstChair; // first Chair declared
Chair secondChair; // second Chair declared
firstChair = new Chair(); //Creation of first Chair (instantiation)
secondChair = new Chair(); //Creation of second Chair (instantiation)
firstChair.setColor(“red”); //Setting color of first Chair
secondChair.setColor(“red”); //Setting color of second Chair
firstChair.setLegs(4); //Setting number of legs for first Chair
secondChair.setLegs(3); //Setting number of legs for 2nd Chair
System.out.println(“First chair is ”+this.getColor());
System.out.println(“Second chair has ”+this.getLegs()+ “ legs”);
}
SECTION 1: Chose any object you want to describe in the computer. Please come up with original objects. Do not copy them from other sources, or select an object somebody else has used in this discussion. There are infinite number of objects, real or imaginary, please be creative. Once you have chosen your object, write a correct class description (as shown above) to represent your object in Java. Give appropriate attributes (at least two) and appropriate behaviors (at least two), all written in correct Java syntax. You may use TextPad to check that the object file compiles.
SECTION 2: Once your class description is finished, write a syntactically correct Java driver class for it. The main method should declare and create objects of the class described in Section 1 and it should use all the methods that were also defined in that Section. This driver must compile and execute in TextPad
Explanation / Answer
// save following file as Bookd.java and run.
// section 1 is
public class Book {
public String Category, Title, Author;
public float Price;
public void setCategory (String c){
Category = c;
}
public void setTitle (String t){
Title = t;
}
public void setAuthor (String a){
Author = a;
}
public void setPrice (float p){
Price = p;
}
public String getCategory(){
return Category;
}
public String getTitle(){
return Title;
}
public String getAuthor(){
return Author;
}
public float getPrice(){
return Price;
}
}
// section 2 is
public class Bookd {
public static void main(String[] args)
{
Book b1 = new Book();
Book b2 = new Book();
b1.setAuthor("Ritchie");
b1.setCategory("Computer Science");
b1.setTitle("C Programming langauge");
b1.setPrice(27.5F);
b2.setAuthor("A.K.Ravi");
b2.setCategory("Aero Space");
b2.setTitle("Air Trans");
b2.setPrice(1.45F);
System.out.println("Book 1 is Authored by" + b1.getAuthor()+ " and its name is "+ b1.getTitle() );
System.out.println("Book 2 belong to " + b2.getCategory()+ " and its cost is " + b2.getPrice());
}
}