Question

Assume the only console output method available is display(c), as shown below, which you include in your solution. This means
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Using method overloading, we can use the same method name for multiple methods by changing the argument type and number of parameters.

  1. display('c') invokes display(char c)
  2. display("String") invokes display(String str)
  3. display(5) invokes display(int val)
  4. display(5.555) invokes display(double val)
  5. diaplay("repeat", 5) invokes display(String str, int rep) and so on.

Program: Special.java

  public class Special{      public static void display(char c){             System.out.print(c);    }               public static void display(String str){                 for(int i=0;i<str.length();i++){                     display(str.charAt(i));                 }       }               public static void displayln(String str){               display(str);           display('\n');  }               public static void display(int val){            int rev = 0;            if(val == 0){                   display((char)48);              }               else{                   while(val!=0){                          rev = rev*10+val%10;                            val = val/10;                   }                       while(rev!=0){                          display((char) (48+(rev%10)));                          rev = rev/10;                   }               }       }               public static void display(double val){                 int int_part = (int)val;                double float_part = val - int_part;             display(int_part);              display('.');           int precision = 100000000;              int float_val = (int)(float_part*precision);            precision /= 10;                while(precision!=0){                    display((int)(float_val/precision));                    float_val = float_val%precision;                        precision /= 10;                }       }               public static void displayln(){                 display('\n');  }               public static void displayRepeat(String str, int rep){          for(int i=0;i<rep;i++){                      display(str);           }       }       public static void displayTriangle(String str){                 for(int i=0; i<str.length();i++){                    for(int j=0; j<=i;j++){                              display(str.charAt(i));                         }                       displayln();            }       }                       public static void main(String[] args){                 display('c');           displayln();            display("String");              displayln();            displayln("New Line string");           displayln();            displayRepeat("Repeat", 5);             displayln();            displayTriangle("triangle");            display(365);           displayln();            display(14.242);        } } /**************OUTPUT******************* c String New Line string RepeatRepeatRepeatRepeatRepeat t rr iii aaaa nnnnn gggggg lllllll eeeeeeee 365 14.24200000 ***************************************/

Screenshot:

public class Specialt public static void display (char c) { System.out.print(c); public static void display (String str) { fo

public static void displayRepeat (String str, int rep) { for(int i=0;i<rep;i++) { display (str); } public static void display

Please don't forget to give a Thumbs Up.

Add a comment
Know the answer?
Add Answer to:
Assume the only console output method available is display(c), as shown below, which you include in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Purpose: Once you have completed and tested the functionality of your MyStringBuilder class, you will do...

    Purpose: Once you have completed and tested the functionality of your MyStringBuilder class, you will do a rough comparative test to see how long some of the operations take relative to the predefined StringBuilder class and the predefined String class. Details: You will complete a simple simulation to compare the times required for three operations: append(char c) , insert(int loc, char c) and delete(int start, int stop) These methods are defined in the StringBuilder and MyStringBuilder classes, but you will...

  • this is for java class Define a utility class for displaying values of type double. Call...

    this is for java class Define a utility class for displaying values of type double. Call the class DoubleOut. Include all the methods from the class DollarFormat in Listing 6.14, all the methods from the class OutputFormat of Self-Test Question 30, and a method called scienceWrite that displays a value of type double using e notation, such as 2.13e–12. (This e notation is also called scientific notation, which explains the method name.) When displayed in e notation, the number should...

  • using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...

  • Question 2 (80 marks) This programming assignment is divided into two parts: (a) You will develop...

    Question 2 (80 marks) This programming assignment is divided into two parts: (a) You will develop a Java class called StringSummary based on the UML class diagram depicted in Figure 1 A user will enter a string of sentence (Figure 2), and your program will provide a summary of the sentence as shown in sample output Figure 3. The class contains several private attributes: • str stores the sentence input by user. • specialChars stores number of special characters (i.e....

  • Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the O...

    Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){    char str[1000], ch;    int i, frequency = 0;    clock_t t; struct timespec ts, ts2;       printf("Enter a string: ");    gets(str);    int len = strlen(str);    printf("Enter a character...

  • Please read the problem carefully and answer the 2 questions below code: /***************************************************************** * Program: palindrome.c...

    Please read the problem carefully and answer the 2 questions below code: /***************************************************************** * Program: palindrome.c * * Purpose: implements a recursive function for determining *   if a string is a palindrome * * Authors: Steven R. Vegdahl, Tammy VanDeGrift, Martin Cenek * *****************************************************************/ #include #include #include /***************************************************************** * is_palindrome - determines whether a string of characters is a palindrome * * calling sequence: *    result = is_palindrome(str, first_index, last_index) * * parameters - *    str - the string to test *    first_index -...

  • 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend...

    1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Deposit Cash. B. Withdraw Cash X. Exit Enter your Selection: 3. Prompt for the value menuChoice. 4. If menuChoice is "A", display "Do Deposit" and redisplay the menu. If menuChoice is "B", display "Do withdrawal" and redisplay the menu. If...

  • You will need to think about problem solving. There are several multi-step activities. Design, compile and...

    You will need to think about problem solving. There are several multi-step activities. Design, compile and run a single Java program, StringEx.java, to accomplish all of the following tasks. Add one part at a time and test before trying the next one. The program can just include a main method, or it is neater to split things into separate methods (all static void, with names like showLength, sentenceType, lastFirst1, lastFirst), and have main call all the ones you have written...

  • My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all th...

    My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...

  • Please help me. package a1; public class Methods {    /*    * Instructions for students:...

    Please help me. package a1; public class Methods {    /*    * Instructions for students: Use the main method only to make calls to the    * other methods and to print some testing results. The correct operation of    * your methods should not depend in any way on the code in main.    *    * Do not do any printing within the method bodies, except the main method.    *    * Leave your testing code...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT