Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class Heading that can be used to store one of the principal compass dir

ID: 3667146 • Letter: W

Question

Write a class Heading that can be used to store one of the principal compass directions, 'N', 'E', 'S', 'W'. It will have the following methods Constructor-builds a Heading as one of N', 'E', 'S', 'W', defaults to 'N'. You do NOT need to do any data validation (i.e., don't need to check the inputs or raise exceptions). get-retrieve the Heading as a string. _repr_-see output right-charges the heading by turning right by 90 degrees,i.e. 'N' right arrow 'E' right arrow 'S' right arrow 'W' right arrow 'N' left-changes the heading by turning left by 90, i.e., the reverse of the above Constructor using an argument, repr, get, right left Constructor called without an argument.

Explanation / Answer


enum compass{ N,E,S,W} // Enumerated Data type is declared to create variables which excepts only N,E,S,W

class Heading{
   compass dir;   // variable to store Direction
   //Constructor without an argument
   Heading(){
       dir = compass.N;
   }
   //Constructor with an argument
   Heading(compass dir){
       this.dir = dir;
   }
   compass get(){
       return dir;
   }
  
  
   void right(){
       switch (dir){
           case N:
               dir = compass.E;
               break;
           case E:
               dir = compass.S;
               break;
           case S:
               dir = compass.W;
               break;
           case W:
               dir = compass.N;
               break;
       }
   }
  
   void left(){
       switch (dir){
           case N:
               dir = compass.W;
               break;
           case E:
               dir = compass.N;
               break;
           case S:
               dir = compass.E;
               break;
           case W:
               dir = compass.S;
               break;
       }
   }
  
   @Override
   public String toString(){
       return ("Heading("+(dir)+")");
   }
}


// Diver program

public class HeadingCompass {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       // validation with an argument
       Heading h = new Heading(compass.E);
       System.out.println(" Constructor with an argument");
       System.out.println(h);
       System.out.println(h.get());
       h.right();
       System.out.println(h);
       h.right();
       System.out.println(h);
       h.right();
       System.out.println(h);
       h.left();
       System.out.println(h);
      
       // validation without an argument
       System.out.println(" Constructor without an argument");
       h = new Heading();
       System.out.println(h);
       h.left();
       System.out.println(h);
       h.right();
       h.right();
       System.out.println(h);
      
   }

}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote