I'm just learning the ropes of overloaded methods. Here is an assignment in which I have to write the overloaded methods in the incomplete CircleStats class, but I have no idea how to set them up. I am supposed to find the circumference of a circle using its diameter/radius.
I understand that I have to make functions for finding circumference using the diameter, then using the radius, but don't know where to go from there. I also know that the radius will be double and the radius will be int, but am completely ousted with how to fill in the //code goes here part.
It would also be wonderful if someone could explain how to set up the math in each overload method to calculate circumference.Thanks in advance!
EDIT: In lines 21 and 22, it should be cStats.calcCircleCircumf instead of cStats.calcCircleArea
/* The calcCircleCircumf( ) method in the CircleStatsTester class is overloaded. Write the overloaded methods in the incomplete CircleStats class.*/
class CircleStats
{
CircleStats()
{
}
//…code goes here
}
public class CircleStatsTester
{
public static void main(String[] args)
{
int diameter = 5;
double radius = 2.5;
CircleStats cStats = new CircleStats();
System.out.println("The area = " + cStats.calcCircleArea(diameter));
System.out.println("The area = " + cStats.calcCircleArea(radius));
}
}
That is basically impossible.
Overloading a method consists in having two methods with the same name, but with a different signature (i.e. with a different number of arguments and/or arguments of different types).
Since a radius and a diameter have the same type, Java has no way to distinguish
calcCircleCircumf(double radius)
from
calcCircleCircumf(double diameter)
So you need two methods with different names:
computeCircumferenceFromRadius(double radius)
computeCircumferenceFromDiameter(double diameter)
and this is not overloading anymore.
Note about naming: these methods are part of a class named CircleStats. So puttin "Circle" in the method name is redundant and only adds noise. Using complete words makes the code more readable though, and that's the convention in Java. Hence computeCircumferenceFromRadiusand not calcCircleCircumf.
Even though this looks odd to me, but I think this is what you are asking.
public int calcCircleCircumf(int radius)
{
return (int)(2 * Math.PI * radius);
}
public double calcCircleCircumf(double diameter) //method overloading
{
return (2 * Math.PI * (diameter/2));
}
I think this is what you want.
Method overloading means that you have same method names, but different method signature.
Method signature includes
Method name
Method parameter type
Method parameter sequence
To overload a method, simply create another method of the same method name with a different signature.
Example:
public int getArea(int x)
{
}
public double getArea(double x) //Signature is different, method overloading occurs
{
}
public int getArea(int x, int y) //Signature is different, method overloading occurs
{
}
Note that the return type does not affect the method signature.
It is incorrect if you want to implement method overloading, yet providing different method names.
Related
Say I want to call a built in function (meaning I cannot change it):
foo(int x, int y)
And I want the parameters of x and y to be random, but be the same random number. I know I can do this with
double rand = Math.random();
foo(rand, rand);
However, is there any way to do this in one line, without declaring a separate variable? Such as
foo(Math.random(), Math.random());
but have them be the same. Just curious if there's a simple way to do this I'm not aware of that avoids adding the extra line. Maybe something I can feed into the second parameter that basically tells it "make this the same as the first parameter"? Any help is greatly appreciated.
Yeah so this can be easily done by creating another method that takes 1 parameter and calls the original method. This ensures the value is always the same when calling this method. If you don't want the original foo(int, int) to be used publicly, don't expose it and hide it as package private or private.
foo(int value) {
foo(value, value);
}
foo(Math.random());
Define a lambda or a method
Function<Integer, Integer> fnc = v -> foo(v,v);
fnc.apply((int)(Math.random()*10));
or
public static int foo(int x) {
return foo(x, x);
}
public int static foo(int x, int y) {
return x + y;
}
No. The random() method generates a new number between 0.0 and 1.0 every time it is called. Even alternatives such as using the Random and ThreadLocalRandom classes behave the same way with their respective methods.
You have two options:
You can store the result of calling Math.random() and pass that value through x and y, just as you mentioned in your post.
You can have only one parameter in your function:
foo(int x)
Now you can just pass the desired value. However, if you want to have a function that has two parameters, you can call the method as such:
foo(int x) {
foo(x, x);
}
So, I have to create 68 different summing methods using the datatypes, int, float, double and short. The class is called, OverloadingSums. Here is an example of one the methods.
public double sum(double x, float y, int z)
{
return x+y+z;
}
Now I'm being asked to create a class called ZeroSum, essentially copying OverloadingSum and returning everything to zero. Here is an example.
public double sum(double x, float y, int z)
{
return 0;
}
Then I'm being asked to create another class called RealSum, which will extend the ZeroSum class. I'm a little confused about the wording of this assignment, not sure if the stackoverflow community could help but I'm just extremly confused.
Here is the assignment requirements:
Now that we have thoroughly explored overloading we are going to
explore overriding. Create a class called ZeroSum.java. Take all of
the methods from OverloadingSum.java and change them to return all
zeros in ZeroSum.java. Next, 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.
This is what I have in my main method:
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 ZeroSum " + real.sum(x,y) );
}
}
So, from how I'm understanding this I wrote the following code:
public class ZeroSum extends RealSum{
public double sum(double x, float y, int z)
{
return super.sum(x,y,z);
}
This will grab the method from RealSum, instead of using the sum method located in the ZeroSum class. So when I run the main method, zero.sum(x,y) gives me 30.
The confusion comes from the fact that the assignment asks me to set everything in ZeroSum returning to zero. If I extend ZeroSum to RealSum, it doesn't really make a difference. Am I doing this correctly? or Am I just overthinking this way too much?
The goal of this assignment is to explore overriding concept, where function executed depends on type of object it called upon on run time, so you will have something like this :
public class ZeroSum {
public double sum(double x, float y)
{
return 0;
}
}
public class RealSum extends ZeroSum{
public double sum(double x, float y)
{
return x+y;
}
}
public class DrivingSum {
public static void main(String[] args) {
int x = 10;
int y = 20;
// x + y = 30....yay?
ZeroSum zero= new ZeroSum();
ZeroSum real= new RealSum();
System.out.println("Calling ZeroSum " + zero.sum(x,y) ); //here the sum will return zero
System.out.println("Calling ZeroSum " + real.sum(x,y) ); //here the sum will return 30
}
I believe he's trying to make a point in which whatever you write in the parent class is transferred (this is called inheritage) to the child. You can override this by writing a method in the child class using the same name and arguments as the parent class' method.
I also believe you read the assignment a bit wrong, it said:
This class, RealSum, will extend the ZeroSum class.
I interpret this as RealSum is the child to ZeroSum, not the other way around as such:
public class RealSum extends ZeroSum{
//code code code
}
This means everything in ZeroSum is set to 0 and RealSum set new values, not using the super. I'm not betting my hand this is correct but try to read the assignment again after a break and some fresh air :)
Hope this helps!
I'm trying to create a class Figure with subclasses like rectangle, triangle and circle. However, when I try to compile them I get the same error in all of them, which makes me believe that the error is in the Figure class. The error: cannot reference height before supertype constructor has been called.
Figure:
public abstract class Figure{
public double width;
public double height;
public Figure(double width, double height){
this.width = width;
this.height = height;
}
public abstract double area();
public abstract double perimeter();
}
Ellipse:
public class Ellipse extends Figure{
private double a;
private double b;
public Ellipse(){
a = width/2;
b = height/2;
}
public double area(){
return (Math.PI * a * b);
}
public double perimeter(){
return (2 * Math.PI * Math.sqrt((Math.pow(a,2) + Math.pow(b,2)))/2);
}
}
Thank you very much.
So your constructor for Ellipse does not call the inherited constructor of Figure. Yet you are are trying to use variables of Figure that are assigned values in the constructor of Figure. You need to update your Ellipse constructor to call the super with values. I recommend the following.
public Ellipse(int w, int h){
super(w,h);
a = width / 2;
b = width / 2;
}
You need call super class constructor in Ellipse:
public Ellipse(){
super(_width, _height)
a = width/2;
b = height/2;
}
You need to initialized you height and width, that are currently present in your Figure class. You can do this by specifically calling the Figure constructor from Ellipse, something like this.
public Ellipse(){
super(23, 23);
a = width /2;
b = height/2;
}
The super() method calls your Figure constructor, which will then initialized height and weight allowing you to use them to calculate a and b accordingly.
Every class has to call the constructor of its superclass as the first instruction in each of its own constructors[1]
This is done implicitly if the superclass has a default constructor (one without parameters). But if it has only a constructor with parameters, then you have to write that call yourself. You do that by using the word super and passing the appropriate parameters.
This is done so that the logical part of the current class that is inherited from the superclass can properly be initialized.
You have two possible solutions:
Add a default constructor to Figure. Perhaps one with default width and height of zero:
public Figure() {
this( 0.0, 0.0 );
}
This makes sense if there is a sensible default value for the width and height.
Change the constructors of the subclasses so that they accept (at least) width and height as well:
public Ellipse(double width, double height){
super(width, height);
a = width/2;
b = height/2;
}
This makes sense if there is no sensible default for the width and height.
The way you have tried doing this, there would be no values for width and height, so you couldn't possibly use them in the subclass (e.g. width/2 would be zero, because there was nothing that put any value in width, because nobody called the constructor of Figure). When you create an instance, there is no other place where the superclass constructor that sets width and height will be called - therefore all constructors have to start by calling their superclass constructors first.
[1] It's also OK to call one of the other constructors of the same class - if it has more than one - provided that ultimately, the constructor that gets called calls one of the supeclass constructors.
You need to call super as the very first thing in each constructor of every subclass of Figure:
public Ellipse(double ewidth, double eheight){
super(ewidth, eheight);
a = width/2;
b = height/2;
}
That is necessary because you use width and height, which are a member of Figure and are only getting initialized when the Figure constructor is executed, which is what the super call does. You basically used an uninitialized variable.
So why did I call the new parameter ewidth instead of width? Would I have named them width, the fields of the same name in the parent class would have been obscured by the local variable of the same name. In your example that would not have been a problem because the code path in both constructors is dead simple, but I'd like to raise awareness for this because it is a source of obscure bugs :)
Similar question has already been answered.
Refer to this:
java call superclass from a subclass constructor?
To add that if you had a non-parameterised constructor in your superclass then you wouldn't have to place an explicit call to the constructor of the superclass.
Also check this:https://docs.oracle.com/javase/tutorial/java/IandI/super.html
There is a note on this page: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
I'm taking an object oriented programming class and I'm having some difficulty understanding how to build a circle object that lets the user declare what the radius is.
I created a data class and in it I put my instance variable, my getter and setter methods, my constructor, and then the basic computational function methods to compute the area and perimeter of the circle with a given radius.
Here is that class:
package shapesoo;
public class CircleDataClass {
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public CircleDataClass(double radius) {
this.radius = radius;
}
public double getArea(){
return Math.PI * radius * radius;
}
public double getCircumference(){
return 2 * Math.PI * radius;
}
}
Then, I am creating a test class that builds the circle with the given radius and in my main method I create the new circle object with:
CircleDataClass myCircle = new CircleDataClass(radius);
I don't have radius declared anywhere in this test class so that's why I am getting a run-time error. But what I want is a user to input the value for that radius parameter that I have in my constructor and then have that radius passed to this circle object. Do I create a separate method in my main class that asks for the value of the radius? I think I am getting confused with what getters/setters/cosntructors are doing and how to pass the radius variable around to different classes.
EDIT: If I put this in, is the instance variable from my data class even used?
public static void main(String[] args) {
String shapeType = getShape();
if (shapeType.equalsIgnoreCase("Circle")){
String r = JOptionPane.showInputDialog("What is the radius: ");
double radius = Double.parseDouble(r);
CircleDataClass myCircle = new CircleDataClass(radius);
}
}
I know how to do this without using object-oriented principles and I am aware this must seem elementary to many of you but I would appreciate any help on it.
I don't have radius declared anywhere in this test class so that's why I am getting a run-time error.
Ok, that's normal
But what I want is a user to input the value for that radius parameter that I have in my constructor and then have that radius passed to this circle object. Do I create a separate method in my main class that asks for the value of the radius?
Yes. Basically, you can read the standard input for the user to enter a number and then instantiates the circle with it. See the Scanner class.
I think I am getting confused with what getters/setters/cosntructors are doing and how to pass the radius variable around to different classes.
The constructor is used to create and initialise an instance of a class.
The getter(s) gives read-only access to properties of your instance. In your case, if you want to check what is the radius of a particular instance of circle, you can have it through myCircle.getRadius().
The role of setter(s) is to mutate an instance, i.e. to change internal properties. It may not be good practice to have setters and can be better to create a new object when there is a need to change a property. This really depends on your design and context.
I need to put this (Math.random() * 37) into a working method.
This is what I made with a tutorial but there is an error on the first row "identifier expected". What is wrong with this code? Please help.
public static double hodKulickou (double)
{
return (Math.random() * 37);
}
You almost got it right. You should consider understanding the principles behind your methods declaration.
In your case you did not provide a local name for the method to assign to the double value it was expecting. Read on if you're interested in understanding the principles as I understand them.
Each word in the method declaration serves a purpose. From left to right.
public - means this method can be accessed publicly and not just by code in that class.
static - means the methods is static and belongs to that class, is not a object member.
void - means the methods does not return any value.
yourmethod name - any name you see fit.
Now the interesting part. The method parameters. These are the values you may pass into the method. In the paramaters you include the type and a name for the value.
eg: methodName(int anInteger)
This means the method can expect an integer type to be passed to is and the method will call that integer anInteger for use within it's body. For whatever purpose you see fit.
You have to give a name to the double parameter of your method.
public static double hodKulickou (double name) {
return (Math.random() * 37);
}
Of course, since you are not using the double parameter, you can just remove it :
public static double hodKulickou () {
return (Math.random() * 37);
}
Did you really followed the instructions of the tutorial ? You must provid an identifier to your parameters. That's exactly what the error message tells you btw.
public static double hodKulickou (double identifier) {
return (Math.random() * 37);
}
However, you pass a parameter to your function but don't use it so actually it should be :
public static double hodKulickou () {
return (Math.random() * 37);
}
Also, since you don't use the parameter, you can omit it:
public static double hodKulickou () { // <-- skipped
return (Math.random() * 37);
}
Your method doesn't appear to need a parameter, this
public static double hodKulickou (double)
should be
public static double hodKulickou ()
or
public static double hodKulickou (int val) { return (Math.random() * val); }
if you want to pass the "37" in.