Reputation: 1
In order to make this program run, I have to import the "derp" method out of the Shape class and into the rectangle class. But no matter what myself or the tutors try, we cannot make it work. What I actually need to accomplish is that the "derp" method from Shape needs to be able to work in Rectangle. However, the task of importing the method has us stumped.
public abstract class shape{
shape(){
}
shape(int length, int width, int thing ){
length = 0;
width = 0;
}
public int derp(int thing, int length) {
thing = (int) Math.random() * 9 ;
length = thing;
return length;
}
}
public class Rectangle extends shape {
public static void main(String args[])
{
shape.getLength(Length, Width);
//r1 will take all default value
Rectangle r1 = Rectangle();
//r2 will take all supplied value
Rectangle r2 = Rectangle(4, 5);
//r3 will take supplied length. width will take default value
Rectangle r3 = Rectangle(10);
//r4 will take the same value of r2
Rectangle r4= r2;
//the rest of the code
}
private static void Rectangle(int width2, int length2) {
// TODO Auto-generated method stub
}
}
Upvotes: 0
Views: 136
Reputation: 346
It seems to me like you should reverse the inheritance. It should be Rectangle extending shape, since a rectangle is a shape
EDIT 1 When you reverse the inheritance you should be able to call the derp method on Rectangle.
RectAngle r = new Rectangle();
r.derp(thing, length);
EDIT 2 You shouldn't really hardcode your variables into your Shape instance either. Thats not a really usefull way to do it. There is two ways you can do it. Either let The shape class have variables which is protected (means it will be be inherited). Then you shape class should look like:
public abstract class shape{
protected int length;
protected int width;
shape(){
}
shape(int length, int width){
this.lenght = length;
this.width = width;
}
public int derp() {
int thing = (int) Math.random() * 9 ;
length = thing;
return length;
}
}
Or if you dont wanna make this big change to your class you can just pass the parameters directly into the method like
r.derp(1, 100);
But I do agree with tieTYT that you should spend some time learning some more java syntax. Since this is a very wierd way of doing your calls :).
Upvotes: 2
Reputation: 11
Ok..so your class Shape extends Rectangle...we will ignore that unusual logic.
Rectangle.derp(int thing, int length, int width);// wrong
First of all you cannot call method derp by declaring parameters inside call. You could have a call like this for example:
int thing = 1, length = 2, width = 3;
Rectangle.derp(thing, length, width);//with condition that derp method
//declaration to be public static
Rectangle r1 = Rectangle();//wrong
That is the class constructor. You declare it as public unless you want to make your class a Singleton ..but I doubt. An you instantiate it with the 'new' keyord like this:
Rectangle r1 = new Rectangle();
Same for the one with 1 and 2 parameters.
Complete code for the Rectangle class here:
public class Rectangle {
public Rectangle(int width2, int length2) {
// TODO: Process width and length here
}
public Rectangle() {
// TODO: process default values here
}
public Rectangle(int value) {
// TODO: Process value here
}
public static void derp(int thing, int length, int width) {
}
public static void main(String args[]) {
int thing = 1, length = 2, width = 3;
Rectangle.derp(thing, length, width);
// r1 will take all default value
Rectangle r1 = new Rectangle();
// r2 will take all supplied value
Rectangle r2 = new Rectangle(4, 5);
// r3 will take supplied length. width will take default value
Rectangle r3 = new Rectangle(10);
// r4 will take the same value of r2
Rectangle r4 = r2;
// the rest of the code
}
}
Upvotes: 0
Reputation: 67494
Rectangle.derp(int thing, int length, int width);
This is not a method declaration. This syntax is all wrong. Are you trying to declare derp
as a method you can use, or are you trying to call derp
? As a reader, that is unclear.
Logically, Rectangle
should extend Shape
, not the other way around.
This doesn't do anything:
shape(int length, int width, int thing ){
length = 0;
width = 0;
}
You're just assigning parameters to different values.
Take a break and spend some time learning the syntax of Java.
Upvotes: 0
Reputation: 700
There is no way, this could work, since you are trying to access a Method of the child-class from the superclass. the superclass dont know its children. You either can use the Methods of the superclass from the subclass or place the method from the subclass into the superclass to use it there.
Upvotes: 0
Reputation: 73568
You don't import methods out of classes into other classes. Logically you'd want Rectangle
to extend shape (also should be Shape
in accordance with naming conventions), but you're doing the other way around.
However there are many things that don't make sense in your code, so you might want to explain in clear English what you're trying to accomplish.
Upvotes: 0
Reputation: 2618
Rectangle
should be extending Shape
, not the other way around.
Also remove this line
Rectangle.derp(int thing, int length, int width);
Upvotes: 0