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

Given the following class and interface definitions: public abstract class Pfunk

ID: 672569 • Letter: G

Question

Given the following class and interface definitions:

public abstract class Pfunker implements Comparable {
    /**
     * LOLLYPOP < ATLANTEAN < CLONE < PILL < PYRAMID < FLASHLIGHT < ATOMIC_DOG
     */
    public enum Level {LOLLYPOP, ATLANTEAN, CLONE, PILL, PYRAMID,
                                   FLASHLIGHT, ATOMIC_DOG}
    private Level level;
    private String name;

    public Pfunker(String name, Level level) {
        this.name = name;
        this.level = level;
    }
}
public interface Comparable {
    /**
     * Compares this object with the specified object for order. Returns a
     * negative integer, zero, or a positive integer as this object is less
     * than, equal to, or greater than the specified object.
   */
    public int compareTo(Object o);
}

Write the minimum concrete class named ConcretePfunker which is a subclass of Pfunker. You compare one Pfunker to another by comparing their levels. You may want to use Enum's ordinal() method, which "Returns the ordinal [int] of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero)." The body of the one non-constructor method you need to write can be done in one line.

I am super confused. Please include explanation. Thank you!

Explanation / Answer

//Save Level as new java class
//Level.java
/**
* LOLLYPOP < ATLANTEAN < CLONE < PILL < PYRAMID < FLASHLIGHT < ATOMIC_DOG
*/
public enum Level {LOLLYPOP, ATLANTEAN, CLONE, PILL, PYRAMID,
   FLASHLIGHT, ATOMIC_DOG}

----------------------------------------------------------------------------------------------------------------

//Pfunker.java
//The class implments the Comparable
public abstract class Pfunker implements Comparable
{
  
    private Level level;
    private String name;

    //Constructor of the class
    public Pfunker(String name, Level level)
    {
        this.name = name;
        this.level = level;
    }
      
    //Returns level by calling ordinal method
    //that returns the integer values starts from 0,1,2 etc
    public int getLevel()
    {
       return level.ordinal();
    }
  
    public String getName()
    {
       return name;
    }
  
  
    //Returns the string representation of Class Pfunker
    @Override
    public String toString()
    {
      
       return name+" "+level;
    }  

}

----------------------------------------------------------------------------------------------------------------

//ConcretePfunker.java
public class ConcretePfunker extends Pfunker
{

   //constructor of class
   public ConcretePfunker(String name, Level level)
   {
       //calling parent class constructor to set name and level
       super(name, level);      
   }

  
   /*Override thecompareTo method that returns
      the integer value by comparing tow Pfunker ordinal level
      values */
   @Override
   public int compareTo(Object object)
   {
       Pfunker pfunker=(Pfunker)object;
       if(getLevel()<pfunker.getLevel())
           return -1;
       else if(getLevel()>pfunker.getLevel())
           return 1;
       else
           return 0;  
   }
}

----------------------------------------------------------------------------------------------------------------

/**The java program Tester that creates an array and set the values
* of names and Level by calling the enum Level constants
* and call sort method on Arrays calss to check if the method
* compareTo is working .
* print the results before and after the sorting*/
//Tester.java
import java.util.Arrays;
public class Tester
{
   public static void main(String[] args)
   {
//create an array of type ConcretePfuncker of size 5     
       ConcretePfunker[] array=new ConcretePfunker[5];
      
       array[0]=new ConcretePfunker("CP1", Level.ATLANTEAN);
       array[1]=new ConcretePfunker("CP2", Level.ATOMIC_DOG);
       array[2]=new ConcretePfunker("CP3", Level.CLONE);
       array[3]=new ConcretePfunker("CP4", Level.FLASHLIGHT);
       array[4]=new ConcretePfunker("CP5", Level.LOLLYPOP);
      

       System.out.println("Before sorting ");
       for (ConcretePfunker concretePfunker : array)
       {
           System.out.println(concretePfunker);
       }
        //sorts the objects using compareTo method to sort the object of class ConcretePfunker objects
       Arrays.sort(array);
       System.out.println("After sorting by Level order ");
       for (ConcretePfunker concretePfunker : array)
       {
           System.out.println(concretePfunker);
       }
      
      
      
   }
}

----------------------------------------------------------------------------------------------------------------

Sample Output:

Before sorting
CP1 ATLANTEAN
CP2 ATOMIC_DOG
CP3 CLONE
CP4 FLASHLIGHT
CP5 LOLLYPOP
After sorting by Level order
CP5 LOLLYPOP
CP1 ATLANTEAN
CP3 CLONE
CP4 FLASHLIGHT
CP2 ATOMIC_DOG

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