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...
Want to see the full answer?
Check out a sample textbook solutionChapter 11 Solutions
EBK JAVA PROGRAMMING
- Write a class named Employee which implements Comparable Interface with name and salary as data field. Add a constructor with argument name and salary. Add getter and Setter for name and salary. Add CompareTo() method for the data Name. Write a class named Manager which inherits the Employee class. Add an instance variable, named department, of type String. Add a constructor in the subclass class with arguments name, salary, department and a toString() method that prints the manager’s name, department, and salary [Hint: Add a toString( ) method in the super class as well] Create 3 objects of Manager with following arguments 1. (Zayed, 500), 2. ( Ahmed, 700) 3. ( Faisal, 600) Create an Array object Add the above 3 objects into an Array object and Sort by Name and Display Create an object Employee class with argument (Zayed, 500, IT) Display the 1. Name 2. Salary 3. Department by using tostring() method.arrow_forwardTASK 1 Draw the UML class diagrams for the following classes: An abstract Java class called Person that has the following attributes: a String called idNumber a Date called dateOfBirth representing the date of birth. a String for name A class called RideRecord with the following attributes: a Driver attribute an enum called type of RideType a Date called date a double called fee A class called Passenger that extends the Person class and has the following attributes and behaviours: an ArrayList that contains RideRecord objects called rideHistory a public method called takeRide that takes a RideRecord with the following signature:public void takeRide(RideRecord rideRecord); a public boolean method called hasRideHistory that returns whether or not the Passenger has taken a ride before. A Driver class that extends the Person class and has the following attributes and behaviors: a String called licenseCode a public method called giveRide that takes as a parameter a Passenger…arrow_forwarda:) Define a Polygon interface that has methods area() and perimeter(). b:) Then implement classes for Triangle, Quadrilateral, Pentagon, Hexagon, and Octagon, which implement this interface, with the obvious meanings for the area() and perimeter() methods. c:) Implement classes, IsoscelesTriangle, Equilateral-Triangle, Rectangle, and Square, which have the appropriate inheritance relationships. d:) Write a simple user interface, which allows users to create polygons of the various types, input their geometric dimensions, and then output their area and perimeter. e:) For extra effort, allow users to input polygons by specifying their vertex coordinates and be able to test if two such polygons are similar. using all a to e parts develop a programarrow_forward
- Create an interface MessageDecoder that has a single abstract method decode(cipherText), where cipherText is the message to be decoded.The method will returnthe decoded message. Create a class SubstitutionCipher that implements the interface MessageDecoder as described above. The constructorshould have one parameter called shift. Define the method decode so that each letter is shifted by the value in the shif. For example, if shift is 3, a will be replaced by d, b will be replaced bt e, c will be replaced by f, and so on. Hint: You may wish to define a private methodthat shifts a single character. Create a class ShuffleCipher that implements the interface MessageDecoder. The constructor should haveone parameter called n. Define the method decode so that the message is shuffledn times.To perform one shuffle, split the message in half and then take the charactersfrom each half alternatively. For example, if the messsage is "abcdefghi", the halves are "abcde" and "fghi". The shuffled…arrow_forwardWrite a class named Employee which implements Comparable Interface with name and salary as data fields Add a constructor with argument name and salary. Add getter and Setter for name and salary. Add CompareTo() method for the data Salary. Write a class named Manager which inherits the Employee class. Add an instance variable, named department, of type String. Add a constructor in the subclass class with arguments name, salary, department and a toString() method that prints the manager’s name, department, and salary [Hint: Add a toString( ) method in the super class as well] Create 3 objects of Manager with following arguments 1. (Zayed, 500), 2. ( Ahmed, 700) 3. ( Faisal, 600) Add the above 3 objects into an ArrayLIst object and Sort by Salary and Display. Create an object Employee class with (Zayed, 500,IT) and display the 1. Name 2. Salary 3. Department by using tostring() method.arrow_forwardCreate a project Vehicles in bluej. The project has Cars, Buses ,Trucks and Bikesclasses ,all these classes inherit from an interface Vehicle. Pedal_bikesand Motor_bikesare subclasses of abstract class Bikes. A.Show the relationship(class diagrams)between above mentioned classesand interfacesand write the necessary class definitions. B.Interface should have 3 methods: drive(), start() and stop(). C.abstract class Bikesshould have 2abstract methods: speed( ) and getModel( ). D.Which one of the following assignments are legal, why or why not? Justify. Pedal_bikes bik= new Motor_bikes( ); Vehiclev1 = new Buses (); Bikesbi= new Bikes( ) please use BlueJ program.arrow_forward
- Which (if any) of the following conditions are necessary for using polymorphism? Select all correct answers. The classes involved are in the same hierarchy. None of the classes can be abstract. All of the subclasses involved override the same method. The object reference cannot be of an interface type. An object reference of the superclass is used to call any methods. A superclass object is referenced by a subclass reference. Which of the following (if any) are true of abstract methods? Select all correct answers. Abstract methods must have a method body. Abstract methods cannot be declared in a class that isn't abstract itself. Abstract methods cannot be private or static. Abstract methods can only be declared in an interface. Abstract methods cannot be called. Constructor methods can be abstract.arrow_forwardWhich (if any) of the following conditions are necessary for using polymorphism? Select all correct answers. The classes involved are in the same hierarchy. None of the classes can be abstract. All of the subclasses involved override the same method. The object reference cannot be of an interface type. An object reference of the superclass is used to call any methods. A superclass object is referenced by a subclass reference. Which of the following (if any) are true of abstract methods? Select all correct answers. Abstract methods must have a method body. Abstract methods cannot be declared in a class that isn't abstract itself. Abstract methods cannot be private or static. Abstract methods can only be declared in an interface. Abstract methods cannot be called.arrow_forward1. Define an interface Measurable. It has a single abstract method: double getArea(); 2. Define an abstract class Shape which has two fields: type and name both of type Strin a. Provide constructor with a single input parameter: name. b. Provide accessor methods for both fields. C. Provide setter method for name. d. Provide toString() method that simply returns the "(type): (name)" e. Shape class implements Measurable interface without implementing its only abs method. 3.arrow_forward
- Q2. Write a program (Abstract+Interface) Define a class named Point containing: Two private instance variables x and y of type int. A full parametrized constructor Getters for all instance variables Define an abstract class named Shape containing: An instance variable named s of type Point. A full parametrized constructor A method named getS that returns the value of instance variable An abstract method named Area that has a return type double. Define an interface named Printable containing a method print without parameters and returning void. Define a class named Rectangle which is extended from class Shape, implements the interface printable and containing: Three instance variables named Length, Width of type int and an instance variable named d of type Point. A constructor with two parameters of type Point which initializes all the instance variables. The lengthis the difference in y-coordinates of any top and bottom point. length= Math.abs(s.getY()-d.getY()); The…arrow_forwardYour assignment for this course is to implement a beginner level Book Management System (BMS) in Java language. BMS contains 2 classes: Bookand BookList. The Book class has the following attributes: code: a String object, that hold the book’s code. title: a String object, that hold the book’s title. qua: int variable, that hold the number of books with the same code in the library. price: a double variable, that hold the book’s price. The BookList class contains only one data member: ArrayList<Book> t. When running the program display the menu as below:1. Input & add book(s) to the end.2. Display all books.3. Search a book for given code.4. Update the book’s price for given code.5. Find the (first) max price value.6. Sort the list ascendingly by code.7. Remove the book having given code.8. Load data from file.0. Exit.Notes:(1) The book’s code must be unique in the list.(2) Display all books in format (code, title, quantity, price).(5)…arrow_forwardCreate a Cycle class, with subclasses Unicycle, Bicycle and Tricycle. Demonstrate that an instance of each type can be upcast to Cycle via a ride( ) method. Using the above Cycle hierarchy, add a balance( ) method to Unicycle and Bicycle, but not to Tricycle. Create instances of all three types and upcast them to an array of Cycle. Try to call balance( ) on each element of the array and observe the results. Downcast and call balance( ) and observe what happens.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT