public class Dog 1Many instance variales in a class will hav


public class Dog

1.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.

2.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.

3. 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?

4.Consider the line below which was taken from the main method in number 2 above.

backdoor.setState(“open”);

What is the implicit parameter that is passed by this method call? What is the explicit parameter?

Solution

DoorTester.java

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
Door sideDoor = new Door(\"Side\",\"closed\");
sideDoor.setState(\"open\");
System.out.println(\"The side door is \" + sideDoor.getState());

}
}

Door.java


public class Door {
   private String side;
   private String state;
   public Door(String side, String state){
       this.side = side;
       this.state = state;
   }
   public String getSide() {
       return side;
   }
   public void setSide(String newSide) {
       this.side = newSide;
   }
   public String getState() {
       return state;
   }
   public void setState(String newState ) {
       this.state = newState ;
   }
  
}

Output:

The front door is open
Expected: open
The back door is closed
Expected: closed
The back door is open
Expected: open
The side door is open

3. 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?

Answer: state is an implicit parameter and newState is an explicit parameter.

What is the implicit parameter that is passed by this method call? What is the explicit parameter?

Answer: “open” is the implicit parameter and newState is the explicit parameter

 public class Dog 1.Many instance variales in a class will have corresponding mutator that allow you to change the value of the variable. Here is a mutator meth
 public class Dog 1.Many instance variales in a class will have corresponding mutator that allow you to change the value of the variable. Here is a mutator meth
 public class Dog 1.Many instance variales in a class will have corresponding mutator that allow you to change the value of the variable. Here is a mutator meth

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site