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

(a) Create a class, called NewsPaperSubscriber, which has two instance variables

ID: 3913958 • Letter: #

Question

(a) Create a class, called NewsPaperSubscriber, which has two instance variables for a subscriber’s street address and the subscription rate. Include a constructor that receives one parameter to initialize the street address. Include getter and setter methods for instance variables. The setter method, setRate(), for the rate is abstract. Also, include an equals() method that indicates two subscribers are equal if they have the same street address.

(b) Create child classes of the NewsPaperSubscriber class, called SevenDaySubscriber, WeekdaySubscriber, WeekendSubscriber. Each child class has a constructor that accepts a parameter to initialize the street address and calls the setRate() method to set the rate as follows: ?

SevenDaySubscriber pays $10.5 per week. ?

WeekdaySubscriber pays $7.5 per week. ?

WeekendSubscriber pays $4.5 per week.

Each child class should include a toString() method that returns a String containing the street address, rate, and service type.

(c) Write a client class, called Subscribers, that maintains a list of NewsPaperScribers created by a user. The client class prompts a user for the subscriber’s address and requested service, and then creates the appropriate object based on the service type. The client class does not allow the user to enter more than one subscription type for any given street address. Display the list of subscribers.


2. (10 points) Solve the following problem (Ch. 11).
(a) Create an exception class called IceCreamConeException, whose constructor accepts a String for a message to be passed to the parent of the class so it can be used in a getMessage() call for display. The received message by the IceCreamConeException class constructor should indicate the reason for the thrown exception (e.g, Non-acceptable flavor, or number of scoops) and the value causing the exception.

(b) Create a class called IceCreamCone which has two instance variables, Flavor as a String and Scoops as an integer, and a constant String array of flavor choices, called FLAVORS. Allow at least four flavors of your choice. The IceCreamCone class has a constructor that receives two parameters to initialize the Flavor and Scoops variables. The constructor uses two methods for setting the instance variables: setFlavor() and setScoops(). The setFlavor() method throws an IceCreamConeException, if the user’s entry does not match a valid flavor from the list of flavor choices. The setScoops() method throws an IceCreamConeException when the scoop quantity exceeds four. Also, the IceCreamCone class includes a getter method for the FLAVORS list, and a toString method.

(c) Write a client class that builds a list of IceCreamCone objects using an array of maximum size of 10. The client allows a user to enter from the keyboard values for an ice cream flavor and number of scoops. The client class catches and handles the exception if it is thrown due to creating unacceptable IceCreamCone object that does not satisfy the IceCreamCone conditions. The exception is handled by printing an appropriate message. Display an appropriate message when an IceCreamCone object is created successfully and when one is not. At the end, display the list of accepted IceCreamCone items. Show examples of IceCreamCone objects with valid and invalid values.

Explanation / Answer

Below is the solution for the first 3 parts for the question(Please split the question other sub-parts):

NewsPaperSubscriber.java

package NewsPaper;

public abstract class NewsPaperSubscriber {

   String streetAddress;

   Float subscriptionRate;

  

  

   public NewsPaperSubscriber() {

   }

   public NewsPaperSubscriber(String streetAddress) {

       super();

       this.streetAddress = streetAddress;

   }

  

   public String getStreetAddress() {

       return streetAddress;

   }

   public void setStreetAddress(String streetAddress) {

       this.streetAddress = streetAddress;

   }

   public Float getSubscriptionRate() {

       return subscriptionRate;

   }

   public abstract void setRate();

   @Override

   public boolean equals(Object obj) {

       if (this == obj)

           return true;

       if (obj == null)

           return false;

       if (getClass() != obj.getClass())

           return false;

       NewsPaperSubscriber other = (NewsPaperSubscriber) obj;

       if (streetAddress == null) {

           if (other.streetAddress != null)

               return false;

       } else if (!streetAddress.equals(other.streetAddress))

           return false;

       return true;

   }

  

  

}

SevenDaySubscriber.java

package NewsPaper;

public class SevenDaySubscriber extends NewsPaperSubscriber {

   SevenDaySubscriber(String address){

       super(address);

       this.setRate();

   }

  

   @Override

   public void setRate() {

       this.subscriptionRate=new Float(10.5);

   }

   @Override

   public String toString() {

       return "Street Address=" + streetAddress + ", Subscription Rate= $" + subscriptionRate + ", Service Type=Seven-day";

   }

}

WeekdaySubscriber.java

package NewsPaper;

public class WeekdaySubscriber extends NewsPaperSubscriber {

   WeekdaySubscriber(String address){

       super(address);

       this.setRate();

   }

  

   @Override

   public void setRate() {

       this.subscriptionRate=new Float(7.5);

   }

   @Override

   public String toString() {

       return "Street Address=" + streetAddress + ", Subscription Rate= $" + subscriptionRate + ", Service Type=Weekday";

   }

}

WeekendSubscriber.java

package NewsPaper;

public class WeekendSubscriber extends NewsPaperSubscriber {

   WeekendSubscriber(String address){

       super(address);

       this.setRate();

   }

  

   @Override

   public void setRate() {

       this.subscriptionRate=new Float(4.5);

   }

   @Override

   public String toString() {

       return "Street Address=" + streetAddress + ", Subscription Rate= $" + subscriptionRate + ", Service Type=Weekend";

   }

}

Subscribers.java

package NewsPaper;

import java.util.ArrayList;

import java.util.Scanner;

public class Subscribers {

   private static ArrayList<NewsPaperSubscriber> subscriptions;

   public static Scanner sc;

  

   public ArrayList<NewsPaperSubscriber> getSubscriptions() {

       return subscriptions;

   }

   public void setSubscriptions(ArrayList<NewsPaperSubscriber> subscriptions) {

       this.subscriptions = subscriptions;

   }

  

   public static void printMainMenu(){

       System.out.println("(1) List all Subscriptions");

       System.out.println("(2) Add Subscription");

       System.out.println("(3) Quit Program");

   }

  

   public static String printAll() {

      

       String ret="";

       if(subscriptions!=null)

       {

           for(NewsPaperSubscriber obj: subscriptions)

           {

               ret=ret+" "+obj.toString();

           }

       }

      

       return ret;

   }

  

   public static void addSubsciption() {

      

       System.out.println("Enter Street address:");

       sc.nextLine();

       String add=sc.nextLine();

       int opt=0;

       boolean flag=true;

      

       if(subscriptions==null)

       {

           subscriptions=new ArrayList<NewsPaperSubscriber>();

       }

      

       for(NewsPaperSubscriber obj: subscriptions)

       {

           if(add.equals(obj))

           {

               System.out.println("There is already a subscription on this street address");

               flag= false;

           }

       }

      

       if(flag){

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

           System.out.println("(2) Weekday subscription");

           System.out.println("(3) Weekend subscription");

           System.out.println("Enter add subscription type:");

           opt=sc.nextInt();

           switch(opt){

           case 1:

               {

                   SevenDaySubscriber obj=new SevenDaySubscriber(add);

                   subscriptions.add(obj);

                   break;

               }

           case 2:

           {

               WeekdaySubscriber obj=new WeekdaySubscriber(add);

               subscriptions.add(obj);

               break;

           }

           case 3:

           {

               WeekendSubscriber obj=new WeekendSubscriber(add);

               subscriptions.add(obj);

               break;

           }

           }

       }

      

   }

  

   public static void main(String[] args) {

      

       sc = new Scanner(System.in);

      

       printMainMenu();

           int option = sc.nextInt();

          

           while(true){

               switch(option){

               case 0:

               {

                   printMainMenu();

                   option = sc.nextInt();

                   break;

               }

               case 1:

                   {

                       System.out.println(printAll());

                       option = 0;

                       break;

                   }

               case 2:

               {

                   addSubsciption();

                   option = 0;

                   break;

               }

               case 3:

               {

                   System.exit(0);

                   break;

               }

               }

           }

   }

}