EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
9th Edition
ISBN: 9781337671385
Author: FARRELL
Publisher: CENGAGE LEARNING - CONSIGNMENT
Expert Solution & Answer
Book Icon
Chapter 11, Problem 3PE

Explanation of Solution

Program:

File name: “NewspaperSubscription.java

//Define an abstract class named NewspaperSubscription

public abstract class NewspaperSubscription

{

    //Declare required instance variables

    protected String name;

    protected String address;

    protected double rate;

    //Define required getter function for the

    //instance variable name

    public String getName()

    {

        //Return the value

        return name;

    }

    //Define required setter function for the instance

    //variable name

    public void setName(String n)

    {

        //Set the value of the instance variable name by

        //assigning the value in the function parameter to

        //the variable name

        name = n;

    }

    //Define required getter function for the instance

    //variable address

    public String getAddress()

    {

        //Return the value

        return address;

    }

    //Define the required getter function getRate() for

    //the instance variable rate

    public double getRate()

    {

        //Return the value

        return rate;

    }

    //Declare the prototype of an abstract function setAddress()

    public abstract void setAddress(String s);

}

File name: “PhysicalNewspaperSubscription.java

//Define a class named PhysicalNewspaperSubscription

//inheriting the class NewspaperSubscription

public class PhysicalNewspaperSubscription extends NewspaperSubscription

{

    //Define the overriden method setAddress()

    public void setAddress(String a)

    {

        //Declare and initialize required Boolean variables

        //to store the result of the condition if the given

        //address includes a digit or not

        boolean hasDigit = false;

        address = a;

        //Traverse the character array using a for each loop

        for(int x = 0; x < a.length(); ++x)

            //If the current character is a digit, then

            //assign true to the variable hasDigit         

            if(Character.isDigit(a.charAt(x)))

                hasDigit = true;

            //If the value of the variable hasDigit is true,

            //then assign 15 to the instance variable rate

            if(hasDigit)

                rate = 15.00;

            //Otherwise, display an appropriate message and

            //assign 0 to the instance variable rate

            else

            {

                rate = 0;

                //Print the result

System.out.print("\nAddress must contain a digit   ");

            }

    }

}

File name: “OnlineNewspaperSubscription.java

//Define a class named OnlineNewspaperSubscription

//extending the class NewspaperSubscription

public class OnlineNewspaperSubscription extends NewspaperSubscription

{

    //Define the overriden method setAddress()

    public void setAddress(String a)

    {

        //Declare and initialize required Boolean variables

        //to store the result of the condition if the given

        //address includes a sign or not

        boolean hasAtSign = false;

        address = a;

        //Traverse the character array using a for each loop

        for(int x = 0; x < a...

Blurred answer
Students have asked these similar questions
This is the question - Create an abstract NewspaperSubscription class with fields for the subscriber name, address, and rate. Include get and set methods for the name field and get methods for the address and subscription rate; the setAddress() method is abstract. Create two subclasses named PhysicalNewspaperSubscription and OnlineNewspaperSubscription. The parameter for the setAddress() method of the PhysicalNewspaperSubscription class must contain at least one digit; otherwise, an error message is displayed and the subscription rate is set to 0. If the address is valid, the subscription rate is assigned $15. The parameter for the setAddress() method of the OnlineNewspaperSubscription class must contain an at sign (@) or an error message is displayed. If the address is valid, the subscription rate is assigned $9. Code I was given - public class DemoSubscriptions {     public static void main(String args[])     {         PhysicalNewspaperSubscription pnsGood = new…
Create an abstract NewspaperSubscription class with fields for the subscriber name, address, and rate. Include get and set methods for the name field and get methods for the address and subscription rate; the setAddress() method is abstract. Create two subclasses named PhysicalNewspaperSubscription and OnlineNewspaperSubscription. The parameter for the setAddress() method of the PhysicalNewspaperSubscription class must contain at least one digit; otherwise, an error message is displayed and the subscription rate is set to 0. If the address is valid, the subscription rate is assigned $15. The parameter for the setAddress() method of the OnlineNewspaperSubscription class must contain an at sign (@) or an error message is displayed. If the address is valid, the subscription rate is assigned $9. Finally, write an application that declares several objects of both subscription subtypes and displays their data fields
Create a subclass of the Employee class from Question 2 named ShiftSupervisor. Add the salary and bonus attributes to it, and override_init_and_str_accordingly. Add the appropriate accessor and mutator methods and check the functionality by prompting a user to input the data for a Shift Supervisor object. Use_str__ to print the result.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
  • Text book image
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT