1Write the code in java to create a class that models a door
1.Write the code in java to create a class that models a door object.
2. Add instance variables to your Door class for the name of the door and its state. Experience has shown that we almost always want to limit the visibility of instance variables inside the same class, so make the access modifiers of state and name private. And because the state and name properties have values like “open” or “front”, let the instance variables you create be of type String. For instance a door object might have a name like “Front” or “Side” to distinguish it from other doors. Another property that could describe a door is its state: “open” or “closed”. Properties of objects are described in code by using nouns like “state” or “name” to create instance variables that hold values.
3. For instance, the operations for a door object could be “open” and “close”. An operation on an object corresponds to a Java method and is described in code by using a verb like “open” or “close”. Invoking a method may change the value of an instance variable. For example, invoking close() would change the value of the state variable from “open” to “closed”.
Declare methods for open and close. Because we usually want to allow free access to the methods a class contains, make the access modifier for each method public.
4. Add a constructor for the Door class that receives two arguments: the name of the door and its initial state. Because we want to use the constructor outside of the class, make the access modifier for the constructor public.
5. It is often convenient to have accessor methods that operate on a single instance variable. Here is an accessor method for the name variable:
public String getName()
{
return name;
}
The word String in front of getName() indicates that the method returns a String when it is invoked. The body is simple and just returns the value of name.
Add this method to your class and write a similar accessor method for the instance variable state.
6.Many instance variales in a class will have corresponding mutator that allow you to change the value of the variable. Here is a mutator method for the name variable:
public void setName(String newName)
{
name=newName;
}
the word void in front of setName() indicates that the method does not return a value when it is invoked. The body is simple and copies the value of the parameter variable newName to instance variable name.
7.Use netbean IDE and compile and run the code below:
/**
A class to test the Door class.
*/
public class DoorTester
{
/**
Tests the methods of the Door class
@param args not used
*/
public static void main(String[] args)
{
Door frontDoor = new Door(\"Front\", \"open\");
System.out.println(\"The front door is \" + frontDoor.getState());
System.out.println(\"Expected: open\");
Door backDoor = new Door(\"Back\", \"closed\");
System.out.println(\"The back door is \" + backDoor.getState());
System.out.println(\"Expected: closed\");
// Use the mutator to change the state variable
backDoor.setState(\"open\");
System.out.println(\"The back door is \" + backDoor.getState());
System.out.println(\"Expected: open\");
// Add code to test the setName mutator here
}
}
Create a third Door object called “sideDoor” the name property “Side” and an initial state of “closed”. Verify that the object was properly created. Use the mutator to change the state of object sideDoor to “open”. Verify that the mutator is working.
8. Consider the variable state in the class Door and the variable newState in the mutator for state. What kind of variable is state? What kind of variable is newState? When do these variables exist?
9.Consider the line below which was taken from the main method in number 7 above.
backdoor.setState(“open”);
What is the implicit parameter that is passed by this method call? What is the explicit parameter?
Solution
Answering the first 6 parts. This will help.
class Door {
private String doorName;
private String doorState;
public void open (){
doorState =\"open\";
}
public void close (){
doorState =\"closed\";
}
public door ( String doorName, String doorState){
doorName=\"\";
doorState =\"\";
}
public String getName (){
return doorName;
}
public String doorState (){
return doorState;
}
public void setName (){
doorName = new Name,
}
public void setState (){
doorState =newstate;
}
}


