Your task is to write a simple Java class (program) called Wrapper. Write a meth
ID: 3880398 • Letter: Y
Question
Your task is to write a simple Java class (program) called Wrapper. Write a method with the following signature and return value: public Long addIntegers(Integer a, Integer b) {...your code here...) The method must return the result from the addition of a and b. Try Before Java 5 and After Java 5 syntax Write another method: public Double divideFloats(Float a, Float b).your code here... Write a main method and test your addInteger) and divideFloats0 methods. See what will happen when you divide a by b and b-0. The method must return null if the result from the division is infinity or NaN.Explanation / Answer
import java.util.Scanner;
/* this code is in after JAVA 5 syntax
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.*/
class Wrapper
{
public Long addIntegers(Integer a,Integer b)
{
return (long)a+b;//auto unboxing happening and then auto boxing
public Double divideFloats(Float a,Float b)
{
if(b==0)//auto unboxing happening
{
return (double)0; //auto boxing
}
else
{
return (double)a/b;//auto boxing
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.println("You want to do divideFloats or addIntegers Enter 1 or 2 or 3 to exit respectively");
switch(sc.nextInt())
{
case 1:System.out.println("Enter the dividend and divisor numbers in float:");
System.out.println(new Wrapper().divideFloats(sc.nextFloat(),sc.nextFloat()));
break;
case 2:System.out.println("Enter the two integer numbers:");
System.out.println(new Wrapper().addIntegers(sc.nextInt(),sc.nextInt()));
break;
case 3:return;
}
}
}
}
==============================================================================
/* this code is in before JAVA 5 syntax*/
import java.util.Scanner;
class Wrapper
{
public Long addIntegers(Integer a,Integer b)
{
return new Long((long)a.intValue()+b.intValue());//we are first extracting the primitive types and then again converting them to objects
}
public Double divideFloats(Float a,Float b)
{
if(b.floatValue()==0)
{
return new Double((double)0);//converting primitive types to objects
}
else
{
return new Double((double)a.floatValue()/b.floatValue());//we are first extracting the primitive types and then again converting them to objects
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.println("You want to do divideFloats or addIntegers Enter 1 or 2 or 3 to exit respectively");
switch(sc.nextInt())
{
case 1:System.out.println("Enter the dividend and divisor numbers in float:");
System.out.println(new Wrapper().divideFloats(sc.nextFloat(),sc.nextFloat()));
break;
case 2:System.out.println("Enter the two integer numbers:");
System.out.println(new Wrapper().addIntegers(sc.nextInt(),sc.nextInt()));
break;
case 3:return;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.