Likes

Java Lab06_Ans3

Take an existing application and refactor it to make use of composition.


solution

1 open the petcomposition project as the main project.
 a select file > open project.
 b browse t d:\labs\06-interfaces\practices.
 c select petcomposition and select the "open as main project chek box.
 d click the open project button.

2 expand the project diretories.

3 run the project. you should see output in the output window.

4 centralize all <name> functionality.
  a create a nameable interface (under the com.example package).
  b complete the nameable interface with setname and getname method signatures.
         public interface nameable {
         
         public void setname(string name);
     
        public string getname();
   }

c create a nameableimpl class (under the com.example package).

d complete the nameableimpl class. it should
  1 implement the nameable interface.
  2 contain a private string field called name.
  3 only accept names less than 20 characters in length.
  4 print "name too long " if a name is too long.

e modify the pet interface.
  1 extend the nameable interface.
  2 remove the getname and setname method signatures (they are inherited now).

f modify the fish and cat classes to use composition.
  1 delete the name field.
  2 delete exitsing getname and setname methods.
  3 add a new nameable field.

5 centralize all walking functionally.

6 modify the petmain class to test the walk method. the walk method can only be called on spider.

1 comment:

Unknown said...

Write a program to create a class that stores the grocery details. In addition, define three methods
that will add the weight, remove the weight, and display the current weight, respectively.