Question

Write the example code to describe overloading and submit here. You need to put comments for...

  • Write the example code to describe overloading and submit here.
  • You need to put comments for your code.
  • You need to add at least one more overloaded method to the example code.

public class OverloadingExample {
public static void main(String[] args) {
OverloadingExample t = new OverloadingExample();
t.methodX(5,9,2.2);
t.methodX(5,9,"Hi");
t.methodX(5,9,2);
t.methodX(5,9);
}

public void methodX(int a){
System.out.println("This is the method X with 1 parameters!");
}

public void methodX(int a,int b){
System.out.println("This is the method X with 2 parameters!");
}

public void methodX(int a, int b, int c){
System.out.println("This is the method X with 3 parameters!");
}

public void methodX(int a, int b, double c){
System.out.println("This is the method X with 3 parameter but last is a double!");
}

public void methodX(int a, int b, String c){
System.out.println("This is the method X with 3 parameter but last is a string!");
}
}

  • Put the outputs   (results after you run your code) at the end of your code as a multi-line comment.

/*

Your result

*/

  • Don't forget to put your name and a short description of your code on the top on your code. java
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Overloading occurs when two methods share the same name but declartions are different.Based on number of arguments and

type of parameters the overloading method can be invoked. Method return type is not involved.

code:

public class OverloadingExample {
   public static void main(String[] args) {
       OverloadingExample t = new OverloadingExample();
       t.methodX(5,9,2.2);
       t.methodX(5,9,"Hi");
       t.methodX(5,9,2);
       t.methodX(5,9);
       t.methodX(4,"overloading");             //new method
   }

   public void methodX(int a){
       System.out.println("This is the method X with 1 parameters!");
       System.out.println("a="+a);
   }

   public void methodX(int a,int b){
       System.out.println("This is the method X with 2 parameters!");
       System.out.println("a="+a+",b="+b);
   }

   public void methodX(int a, int b, int c){
       System.out.println("This is the method X with 3 parameters!");
       System.out.println("a="+a+",b="+b+",c="+c);
   }

   public void methodX(int a, int b, double c){
       System.out.println("This is the method X with 3 parameter but last is a double!");
       System.out.println("a="+a+",b="+b+",c="+c);
   }
   public void methodX(int a, int b, String c){
       System.out.println("This is the method X with 3 parameter but last is a string!");
       System.out.println("a="+a+",b="+b+",c="+c);
   }
   public void methodX(int a,String c){
       System.out.println("This is the new method X with 2 parameter but last is a string!"); //new method
       System.out.println("a="+a+",c="+c);
   }
}

Output:

/*

This is the method X with 3 parameter but last is a double!
a=5,b=9,c=2.2
This is the method X with 3 parameter but last is a string!
a=5,b=9,c=Hi
This is the method X with 3 parameters!
a=5,b=9,c=2
This is the method X with 2 parameters!
a=5,b=9
This is the new method X with 2 parameter but last is a string!
a=4,c =overloading

*/

Add a comment
Know the answer?
Add Answer to:
Write the example code to describe overloading and submit here. You need to put comments for...
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
  • Write four overloaded methods called randomize. Each method will return a random number based on the...

    Write four overloaded methods called randomize. Each method will return a random number based on the parameters that it receives: randomize() - Returns a random int between min and max inclusive. Must have two int parameters. randomize() - Returns a random int between 0 and max inclusive. Must have one int parameter. randomize() - Returns a random double between min and max. Must have two double parameters. randomize() - Returns a random double between 0 and max. Must have one...

  • read the code and comments, and fix the program by INSERTING the missing code in Java...

    read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....

  • Java please answer A to I please dont type the answer on paper please use the...

    Java please answer A to I please dont type the answer on paper please use the computer A.   Explain why alpha cannot be accessed by other members of its class. B.   In the program, how can you determine the type of access modifier? C.   Describe the differences and similarities of beta and gamma in program, within the context of access specifiers and class member accessibility. D.   Explain how objects, a and b, are passed to the method. E.    Why...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • java 1. Write a method header on line three with the following specs: Returns: a boolean...

    java 1. Write a method header on line three with the following specs: Returns: a boolean Name: beTrue Parameters: none public class Main {       {        return true;    } } 2. Write a method header on line two with the following specs: Returns: a String Name: makeCapital Parameters: a String named "name" You should not be writing code on any line other than #2 public class Main {       {    String ans = name.toUpperCase();...

  • For the following activity, you will submit your entire program to the code runner, from your...

    For the following activity, you will submit your entire program to the code runner, from your first import statement to your last curly bracket. Please download the Lesson 35 Activity template (Links to an external site.) and use it to write your code. Additionally, you can download the Lesson 35 Coding Activity (PDF) (Links to an external site.) to print or use as an offline reference. If you need a refresher on how to use this Code Runner, watch the...

  • in java, finish the method 17: Chapter 8: Handling a number as different types (Method Overloading)...

    in java, finish the method 17: Chapter 8: Handling a number as different types (Method Overloading) J o pulli UCILISIS lule zyBooks catalog Help/FAQ 31.47 Chapter 8: Handling a number as different types (Method Overloading) Write an overloaded method called divide_by_two() that can handle a number between 1-5 as an int, double, or String. • The method should be able to handle the String version with upper or lower case letters in any location in the String. ACTIVITY 31.47.1: Chapter...

  • last question : Don't be negative and source Code : of don't be negative Repeat the...

    last question : Don't be negative and source Code : of don't be negative Repeat the assignment "don't be so negative" except solve the problem using recursion. Specifically, create an array of 10 random integers between -10 and +10 then create a method say "findNegatives" that takes as input parameters the array, and possibly other parameters, then calls itself (direct recursion); method find Negatives( ...method parameters here...) ...some stopping criteria... find Negatives(...method parameters here...) Output: Array position #0 #1 Value...

  • I need to make this code access the main method I suppose, but I don't know...

    I need to make this code access the main method I suppose, but I don't know how to make that happen... Help please! QUESTION: Write a method called switchEmUp that accepts two integer arrays as parameters and switches the contents of the arrays. Make sure that you have code which considers if the two arrays are different sizes. Instructor comment: This will work since you never go back to the main method, but if you did, it wouldn't print out...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

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