KnowledgeBoat Logo

Computer Applications

Design a class to overload the method display(…..) as follows:

  1. void display(int num) — checks and prints whether the number is a perfect square or not.
  2. void display(String str, char ch) — checks and prints if the word str contains the letter ch or not.
  3. void display(String str) — checks and prints the number of special characters present in the word str.

Write a suitable main( ) function.

Java

User Defined Methods

ICSE

24 Likes

Answer

public class KboatDisplay
{
    public void display(int num) {
        
        double sroot = Math.sqrt(num);
        double diff = sroot - Math.floor(sroot);
        
        if (diff == 0)
            System.out.println(num + " is a perfect square");
        else
            System.out.println(num + " is not a perfect square");
            
    }
    
    public void display(String str, char ch) {
        
        int idx = str.indexOf(ch);
        
        if (idx == -1)
            System.out.println(ch + " not found");
        else
            System.out.println(ch + " found");
            
    }
    
    public void display(String str) {
        
        int count = 0;
        int len = str.length();
        
        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
            if (!Character.isLetterOrDigit(ch) && 
                !Character.isWhitespace(ch)) {
                count++;
            }
        }
        
        System.out.println("Number of special characters = " + count);
    }
    
    public static void main(String args[]) {
        KboatDisplay obj = new KboatDisplay();
        obj.display(18);
        obj.display("ICSE Computer Applications", 't');
        obj.display("https://www.knowledgeboat.com/");
    }
}

Output

BlueJ output of Design a class to overload the function display(…..) as follows: (a) void display(int num) — checks and prints whether the number is a perfect square or not. (b) void display(String str, char ch) — checks and prints if the word str contains the letter ch or not. (c) void display(String str) — checks and prints the number of special characters present in the word str. Write a suitable main( ) function.

Answered By

12 Likes


Related Questions