Access over 35 million academic & study documents

The code snippet below demonstrates method overloading in Java The

Content type
User Generated
Showing Page:
1/3
The code snippet below demonstrates method overloading
in Java. The main method calls method self, but passes it
different parameters. The return types also differ. The first
two methods return the value of input parameter. The third
does not take any parameter but simply prints the string
\"self\". Note that all methods have the same parameter
types and return types. This need not be the case always.
Your task is to write overloaded methods all named -
convert. - The first method should take an integer and
return a double corresponding to the integer. For example,
passing 5 must return 5.0. - The second method should
take a double and return its integer value. For example,
the method should take 3.142 and return 3. Use the code
snippet for writing and testing your code. public class
MethodOverloading { public static int self(int a){
return a; } public static double self(double a){
return a; } public static void self(){
System.out.println(\"self\"); } //Write your method
convert for int to double //Write your method convert for
double to int public static void main(String[] args) {
self(); int a = self(10); double b = self(3.142);
System.out.println(a); System.out.println(b);
double c = convert(10); int d = convert(3.142);
System.out.println(c); System.out.println(d); } }
Solution
MethodOverloading.java

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/3
public class MethodOverloading {
public static int self(int a){
return a;
}
public static double self(double a){
return a;
}
public static void self(){
System.out.println(\"self\");
}
//Write your method convert for int to double
public static double convert(int n){
return Double.parseDouble(n+\"\");
}
//Write your method convert for double to int
public static int convert(double n){
return (int)n;
}
public static void main(String[] args) {
self();
int a = self(10);
double b = self(3.142);
System.out.println(a);

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/3

Sign up to view the full document!

lock_open Sign Up
Unformatted Attachment Preview
The code snippet below demonstrates method overloading in Java. The main method calls method self, but passes it different parameters. The return types also differ. The first two methods return the value of input parameter. The third does not take any parameter but simply prints the string \"self\". Note that all methods have the same parameter types and return types. This need not be the case always. Your task is to write overloaded methods all named convert. - The first method should take an integer and return a double corresponding to the integer. For example, passing 5 must return 5.0. - The second method should take a double and return its integer value. For example, the method should take 3.142 and return 3. Use the code snippet for writing and testing your code. public class MethodOverloading { public static int self(int a){ return a; } public static double self(double a){ return ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Indeed
4.5
Sitejabber
4.4