Thank you very much in advance! I need some hints on how to create a class calle
ID: 3763628 • Letter: T
Question
Thank you very much in advance!
I need some hints on how to create a class called ZeroSum.java. I need to take all of the methods from OverloadingSum.java and change them to return all zeros in ZeroSum.java. Next, and create a class called RealSum.java. This class, RealSum, will extend the ZeroSum class. Having all zeros for our sums isn't very useful. We will need to override the parent, or super class methods to produce real results. Create the necessary methods to override all of those in the ZeroSum.java. When you are done run your classes against DrivingSum.java.
public class OverloadingSum{
public int sum(int a,int b)
{
return a+b;
}
public int sum(int a,int b,int c)
{
return a+b+c;
}
public double sum(int a,short c, double b)
{
return a+b+c;
}
public double sum(double a,int b,short c)
{
return a+b+c;
}
public static void main(String args[])
{
int result;
OverloadingSum obj=new OverloadingSum();
result = obj.sum(10,10,10);
System.out.println("Sum is: "+result);
result = obj.sum(20,20);
System.out.println("Sum is: "+result);
}
}
Explanation / Answer
I have created two classed called ZeroSum and RealSum.
ZeroSum Class:
It contains the Different Overloaded methods of Sum.
These all methods will return zero's.
RealSum Class:
This class is inherits the ZeroSum Class public ..
So all the methods of ZeroSum class are overriding in RealSum Class
DrivingSum Class:
This class have the both the objects of ZeroSum and RealSum Classe.
It perform the sum operation by calling both the objects
// ZeroSum Class in which all the methods will return as zero's
class ZeroSum
{
public int sum(int a,int b)
{
return 0;
}
public int sum(int a,int b,int c)
{
return 0;
}
public double sum(int a,short c, double b)
{
return 0;
}
public double sum(double a,int b,short c)
{
return 0;
}
}
// RealSum Class is extending the ZeroSum Class methods and overriding in RealSum Class
class RealSum extends ZeroSum
{
public int sum(int a,int b)
{
return a+b;
}
public int sum(int a,int b,int c)
{
return a+b+c;
}
public double sum(int a,short c, double b)
{
return a+b+c;
}
public double sum(double a,int b,short c)
{
return a+b+c;
}
}
// Driver Class Called DrivingSum will have the main methods to handle the methods
// of RealSum class
public class DrivingSum {
public static void main(String[] args) {
int x = 10;
int y = 20;
// x + y = 30....yay?
ZeroSum zero= new ZeroSum();
RealSum real= new RealSum();
System.out.println("Calling ZeroSum " + zero.sum(x, y) );
System.out.println("Calling RealSum " + real.sum(x, y) );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.