Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Homework HelpRSS Feeds

An application that combines several classes and interface

Posted By: Easy Tutor     Category: Java     Views: 2086

Write an application that combines several classes and interface. The abstract class ROBOT has concret
subclasses ROBOTA, ROBOTB and ROBOTC. Class ROBOTA1 extends ROBOTA. Class ROBOTB1 and ROBOTB2 extends ROBOTB. Class ROBOTC1 extends ROBOTC. The LOCOMOTIVE interface declares three mthod name forward(), reverse(), and stop(). It is implemented by classes ROBOTB and ROBOTC. The SOUND interface declare method beep() method. It is implemented by classes ROBOTA1, ROBOTB1 and ROBOTC. Define all of the classes and implement the interfaces as specified. Create one instance of each class than invoke the beep() method of all the objects that are of type SOUND and also invoke the stop() method of all objects that are of type LOCOMOTIVE.

Code for An application that combines several classes and interface in Java

interface LOCOMOTIVE
{
    void forward();
    void reverse();
    void stop();
}

interface SOUND
{
   void beep();
}

class ROBOT
{
}

class ROBOTA extends ROBOT
{
}

class ROBOTB extends ROBOT implements LOCOMOTIVE
{
      publicvoid forward()
      { System.out.println("ROBOTB.FORWARD()"); }

      publicvoid reverse()
        { System.out.println("ROBOTB.REVERSE()"); }

      publicvoid stop()
        { System.out.println("ROBOTB.STOP()"); }
}

class ROBOTC extends ROBOT implements LOCOMOTIVE,SOUND
{
      publicvoid forward()
      { System.out.println("ROBOTC.FORWARD()"); }

      publicvoid reverse()
        { System.out.println("ROBOTC.REVERSE()"); }

      publicvoid stop()
        { System.out.println("ROBOTC.STOP()"); }

      publicvoid beep()
      { System.out.println("ROBOTC.BEEP()"); }
}

class ROBOTA1 extends ROBOTA implements SOUND
{
    publicvoid beep()
    {  System.out.println("ROBOTA1.BEEP()"); }
}

class ROBOTB1 extends ROBOTB implements SOUND
{
    publicvoid beep()
    {  System.out.println("ROBOTA1.BEEP()"); }   
}

class ROBOTB2 extends ROBOTB 
{
}

class ROBOTC1 extends ROBOTC
{
}

class  INSTANCEOF_example
{
       public final staticint totclass=7;
    publicstaticvoid main(String args[]) 
    {
    ROBOT  r[] = new ROBOT[totclass];
    r[0]=new ROBOTA();
    r[1]=new ROBOTB();
    r[2]=new ROBOTC();
    r[3]=new ROBOTA1();
    r[4]=new ROBOTB1();
    r[5]=new ROBOTB2();
    r[6]=new ROBOTC1();

          //for BEEP() method
          System.out.println("\nINSTANCEOF SOUND INTERFACE");
          for(int i=0;i<totclass;i++)
          {    
               if(r[i] instanceof SOUND)
               {
                    r[i].beep();
                }
         }

         System.out.println("\n\nINSTANCEOF LOCOMOTIVE INTERFACE");
         for (int i=0;i<totclass ;i++ )
         {
               if(r[i] instanceof LOCOMOTIVE)
               {
                    r[i].stop();
                }
         }
    }
}
  
Share: 


Didn't find what you were looking for? Find more on An application that combines several classes and interface Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of An application that combines several classes and interface is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!