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

Create a class named NewspaperSubscriber with fields for a subscribers street ad

ID: 3557215 • Letter: C

Question

Create a class named NewspaperSubscriber with fields for a subscribers street address and the subscription rate. Include get and set methods for the subscribers street address, and include get and set methods for the subscription rate. The set method for the rate is abstract. Include an equals() method that indicates two Subscribers are equal if they have the same street address. Create child classes named SevenDaySubscriber, WeekdaySubscriber, and WeekendSubscriber. Each child class constructor sets the rate as follows: SevenDaySubscribers pay $4.50 per week, WeekdaySubscribers pay $3.50 per week, and WeekendSubscribers pay $2.00 per week. Each child class should include a toString() method that returns the street address, rate, and service type. Write an application named Subscribers that prompts the user for the subscribers street address and requested service, and then creates the appropriate object based on the service type. Do not let the user enter more than one subscription type for any given street address. Save the files as NewspaperSubscriber.java, WeekdaySubscriber.java, WeekendSubscriber. java, SevenDaySubscriber.java, and Subscribers.java.

Explanation / Answer

public abstract class NewsPaperSubscriber

{

                protected String address;

                protected double rate;

                public NewsPaperSubscriber() throws Exception

                {

                  setAddress();

                  setRate();

                 }

    public boolean equals(NewsPaperSubscriber nps)

                {

         boolean result;

         if (address.equals(nps.address))

                   result = true;

         else

           result = false;

         return result;

       }

                               

                public String getAddress()

                {

          return address;

                }

                public double getRate()

                {

                  return rate;

                }

                               

                public void setAddress() throws Exception

                {

                   String inputString = new String();

                  char newChar;

                  System.out.print("Enter address of subscriber ");

                  newChar = (char)System.in.read();

                  while(newChar >='A' && newChar <='z'|| newChar == ' ' || (newChar>='0' && newChar <= '9'))

                  {

                   inputString = inputString + newChar;                

           newChar = (char)System.in.read();

                  }

          System.in.read();

                  address = inputString;

                }

        public abstract void setRate();

               

               

}

public class SevenDaySubscriber extends NewsPaperSubscriber

{

                String subType;

                public SevenDaySubscriber() throws Exception

                {

                  super();

          setType();

                }

   

                public void setType()

                {

                   subType = "Seven day";

        }

        public void setRate()

                {

                  rate = 4.5;

                }

                public String toString()

        {

         return(address + " rate: " + rate +

                                " service type : " + subType);

        }

               

}

public class WeekdaySubscriber extends NewsPaperSubscriber

{

                String subType;

                public WeekdaySubscriber() throws Exception

                {

                  super();

          setType();

                }

   

                public void setType()

                {

                   subType = "Weekday";

        }

        public void setRate()

                {

                  rate = 3.5;

                }

                public String toString()

        {

         return(address + " rate: " + rate +

                                " service type : " + subType);

        }

               

}

public class WeekendSubscriber extends NewsPaperSubscriber

{

                String subType;

                public WeekendSubscriber() throws Exception

                {

                  super();

          setType();

                }

   

                public void setType()

                {

                   subType = "Weekend";

        }

        public void setRate()

                {

                  rate = 2.0;

                }

                public String toString()

        {

         return(address + " rate: " + rate +

                                " service type : " + subType);

        }

               

}

public class Subscribers

{

    public static void main(String[] args) throws Exception

    {

                NewsPaperSubscriber[] subArray = new NewsPaperSubscriber[6];

                int x;

                for(x = 0; x<subArray.length;++x)

        {

          char selection;

                  System.out.print("Please select the type of ");

                  System.out.println("subscribtion you want:");

          System.out.println("   1 - Seven day");

          System.out.println("   2 - Weekday only");

          System.out.println("   3 - Weekend only");  

                  selection = (char)System.in.read();

                  System.in.read(); System.in.read();

                  if (selection=='1')

           subArray[x] = new SevenDaySubscriber();

          else if(selection == '2')

             subArray[x] = new WeekdaySubscriber();

          else

             subArray[x] = new WeekendSubscriber();

       

          // check for duplicates

          for(int y = 0; y<x; ++y)

             if(subArray[x].equals(subArray[y]))

                                {

                                System.out.println("Sorry - you just entered a duplicate subscription");

                                --x;

                }

       }

     

                System.out.println(" The subscriptions are: ");

                for(x = 0; x < subArray.length; ++x)

                  System.out.println("#" + (x+1) + "   " + subArray[x].toString());

               

   }

}

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