Type Here to Get Search Results !

Day 13: HackerRank 30 Days Of Code Solution By CodingHumans | Abstract Classes |

0

Day 13: Abstract Classes

Aim

You will be able to know about the abstract class in this problem . Whats actually abstract class are in inheritance
So what is an abstract class :
An abstract class is a  class which  can have an abstract methods as well as defined methods, but it cannot be instantiated (which means  you cannot create a new instance of it). For using an abstract class, you must create and instantiate a subclass that extends the abstract class. Any abstract methods declared in an abstract class must be implemented by its subclasses unless the subclass is also abstract.

Mission
Given a Book class and a Solution class, write a MyBook class that does the following:
Inherits from Book
Has a parameterized constructor taking these  parameters:
string title 
string author 
int price

Implements the Book class' abstract display() method so it prints these 3 lines 
Title:, a space, and then the current instance's title.
Author:, a space, and then the current instance's author.
Price:, a space, and then the current instance's price.

Note: As  these classes are being written in the same file, you must not use an access modifier (e.g.: ) when declaring MyBook or your code will not execute.

Input Style

You are not responsible for reading any input from stdin. The Solution class creates a Book object and calls the MyBook class constructor (passing it the necessary arguments). It then calls the display method on the Book object.

Output Style

The void display() method should print and label the respective title,author, and price  of the MyBook object's instance (with each value on its own line) like so:

Title: $title
Author: $author
Price: $price

Note: The $ is prepended to variable names to indicate they are placeholders for variables.

Sample Input

The following input from stdin is handled by the locked stub code in your editor:

The BillGates
Coding Humans
500

Sample Output

The style output should be  printed by your display() method:

Title: The BillGates
Author: Coding Humans
Price: 500



Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.

Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.

HAPPY CODING 😁






Solution:
( java )



import java.util.*;

abstract class Book {
    String title;
    String author;
    
    Book(String title, String author) {
        this.title = title;
        this.author = author;
    }
    
    abstract void display();
}
class MyBook extends Book{
    int price;
    
    MyBook(String title,String author,int price ){
        super(title,author);
        this.price=price;
    } 
    void display()
    {
    System.out.println("Title: "+title);
    System.out.println("Author: "+author);
    System.out.println("Price: "+price);
    }
}
public class Solution {
   
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String title = scanner.nextLine();
        String author = scanner.nextLine();
        int price = scanner.nextInt();
        scanner.close();

        Book book = new MyBook(title, author, price);
        book.display();
    }
}


If you have any doubts regarding this problem or  need the solution in other programming languages then leave a comment down below . 


Post a Comment

0 Comments

Top Post Ad

Below Post Ad