Constructor call is giving error-Inheritance - java

In a child class GreenSlime Im given a constructor with only three parameters (I cannot add any other instance variables). But the code keeps giving error about this line:super(loc,map,log); which I understand that the constructor should have the same amount of parameters. But my specs say that via the parent constructor, sets all fields. fullcharge must always be 4, and the starting value for charge is 0. I do know that I'm passing only 3 parameters instead of 5, but that's the instructions of my project say so. What am I doing wrong and what's the best approach/solution?
import java.io.PrintStream;
public class GreenSlime extends Threat {
public GreenSlime(Coord loc, Map map, PrintStream log)
{
super(loc,map,log);
super.fullCharge = 4;
super.charge = 0;
}
}
import java.io.PrintStream;
public abstract class Threat extends Thing {
protected int charge;
protected final int fullCharge;
public Threat(Coord c, String repr, int fullCharge, Map map, PrintStream log)
{
super(c,repr,map,log);
this.fullCharge = fullCharge;
charge = 0;
}
public abstract void spawn(Coord c);
#Override
public void doAction()
{
while(charge != fullCharge)
{
System.out.println("\"+repr()"+"#"+"getLoc()\" speading");
if(this.canPassThrough())
{
spawn(getPrevLoc().step(Direction.N));
spawn(getPrevLoc().step(Direction.S));
spawn(getPrevLoc().step(Direction.E));
spawn(getPrevLoc().step(Direction.W));
}
charge++;
}
}
}

public GreenSlime(Coord loc, Map map, PrintStream log)
{
super(loc,"",4,map,log);
}
I've supplied an empty string "" for repr, but you may need null or some other value.

In your code, the Threat constructor has a signature that accepts 5 arguments, but where as you're trying to pass only 3 arguments to it.

Related

Putting a Method in an array

So I want to make an array that has methods in it. For example:
public static void givePointBoost(){
points += 30};
or
public static void giveSword(){
Actions.giveItems(Items.diamond_sword);
Actions.givePotion(Potions.slowness);};
As you can see, both of these methods are voids. What I want to do is have an array that has all these voids in it so that I can pick a random method out of it later on. But I can't put it into an array because It says that I can't have an array of voids. When I try to make it an array of objects, It says that it can't switch from object to void. So my question is:
How do you get methods inside of Arrays?
In Java, you do not have delegates or function pointers, which you can store in collections or arrays like objects, so you have to employ the Command Pattern to achieve this. Basically, you wrap a method in an object that you pass on. The receiver can then access the method via the object.
Create a command interface:
interface ICommand {
public void execute();
}
Wrap a method (or multiple) in a class via inheritance...
class SpecificCommand implements ICommand {
public void execute() {
// Do something...
}
}
...or wrap existing methods directly in an anonymous class:
class SomeClass {
private void someMethod(int someValue) {
// Some stuff...
}
public static void main(String[] args) {
List<ICommand> commands = new ArrayList<>();
// Do something...
// Add command directly
ICommand command = new ICommand() {
#Override
public void execute() {
someMethod(42);
}
}
// Do something....
}
}
Call the commands from the list in a loop (or single):
for (ICommand command : commands) {
command.execute();
}
Let's sort things out.
Arrays in Java can only contain objects or primitives.
Collections in Java can only contain objects.
What you're looking for is called a Command Pattern.
https://www.tutorialspoint.com/design_pattern/command_pattern.htm
You'll have a list of objects, each of them with single method, let's say "execute". With polymorphism, each of this objects will do something different.
Here's an example:
import java.util.ArrayList;
import java.util.List;
public class CommandPatternExample {
public static void main(String[] args) {
List<Command> commands = new ArrayList<>();
commands.add(new GiveBoostCmmand("knight"));
commands.add(new GiveItemCommand("sword", "knight"));
for (int i = 0; i < 3; i++) {
commands.get((int)(Math.random() * commands.size())).execute();
}
}
public interface Command {
void execute();
}
static class GiveBoostCmmand implements Command {
private String targetName;
public GiveBoostCmmand(String targetName) {
this.targetName = targetName;
}
public void execute() {
System.out.println("Boosting " + this.targetName);
}
}
static class GiveItemCommand implements Command {
private String itemName;
private String targetName;
public GiveItemCommand(String itemName, String targetName) {
this.itemName = itemName;
this.targetName= targetName;
}
public void execute() {
System.out.println("Giving " + this.itemName + " to " + this.targetName);
}
}
}
Are you trying to say you want the result of the method to be added to the array?
As far as i know, i don't think you can put a method inside an array.
What you could do is create an interface, and provide implementations and then add those objects to an array. That way you could pick a random object and call the method defined in the interface.
The main question is Why do you need methods in an array?
The other solutions using the Command pattern are a great solution. But seeing your code I believe that you also should put that pattern in a specialized class whose purpose will be to initialize the pool of possible actions and select one at random when you need to.
Which would translate to the following UML
|RandomActionCaller |
|------------------------|
|- List<Command> commands|
|------------------------|
|+ selectRandomEffect() |
In the constructor you prepare the basic list of possible outcome, please refer to the other answers about the Command pattern. Maybe also add a method to add more commands to the list of commands from the outside of the class, this can be usefull.
The select random effect method would only select a random number between 0 and commands.size()-1, get the command instance and execute it. If you need to execute it somewhere else in your code just return it from the select random effect method.

Simple pass of variable to new class, then output in Java

I've seen this question asked in several ways, but the code is usually specific to the user, and I get lost a little. If I'm missing a nice clear and simple explanation, I'm sorry! I just need to understand this concept, and I've gotten lost on the repeats that I've seen. So I've simplified my own problem as much as I possibly can, to get at the root of the issue.
The goal is to have a main class that I ask for variables, and then have those user-inputted variables assessed by a method in a separate class, with a message returned depending on what the variables are.
import java.io.*;
public class MainClass {
public static void main(String[] args) {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
String A;
String B;
try {
System.out.println("Is A present?");
A = reader.readLine();
System.out.println("Is B present?");
B = reader.readLine();
Assess test = new Assess();
} catch (IOException e){
System.out.println("Error reading from user");
}
}
}
And the method I'm trying to use is:
public class Assess extends MainClass {
public static void main(String[] args) {
String A = MainClass.A;
String B = MainClass.B;
if ((A.compareToIgnoreCase("yes")==0) &&
((B.compareToIgnoreCase("yes")==0) | (B.compareToIgnoreCase("maybe")==0)))
{
System.out.println("Success!");
}
else {
System.out.println ("Failure");
}
}
}
I recognize that I'm not properly asking for the output, but I can't even get there and figure out what the heck I'm doing there until I get the thing to compile at all, and I can't do THAT until I figure out how to properly pass values between classes. I know there's fancy ways of doing it, such as with arrays. I'm looking for the conceptually simplest way of sending a variable inputted from inside one class to another class; I need to understand the basic concept here, and I know this is super elementary but I'm just being dumb, and reading what might be duplicate questions hasn't helped.
I know how to do it if the variable is static and declared globally at the beginning, but not how to send it from within the subclass (I know it's impossible to send directly from the subclass...right? I have to set it somehow, and then pull that set value into the other class).
In order to pass variables to an object you have either two options
Constructor - will pass parameter when creating the object
Mutator method - will pass parameters when you call the method
For example in your Main class:
Assess assess = new Assess(A, B);
Or:
Assess assess = new Assess();
assess.setA(A);
assess.setB(B);
In your Assess class you have to add a constructor method
public Assess(String A, String B)
Or setter methods
public void setA(String A)
public void setB(String B)
Also, Assess class should not extend the main class and contain a static main method, it has nothing to do with the main class.
Below there is a code example!
Assess.java
public class Assess {
private a;
private b;
public Assess(String a, String b) {
this.a = a;
this.b = b;
}
public boolean check() {
if ((A.compareToIgnoreCase("yes")==0) &&
((B.compareToIgnoreCase("yes")==0) ||
(B.compareToIgnoreCase("maybe")==0)))
{
System.out.println("Success!");
return true;
} else {
System.out.println ("Failure");
return false;
}
MainClass .java
public class MainClass {
public static void main(String[] args) {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
String A;
String B;
try {
System.out.println("Is A present?");
A = reader.readLine();
System.out.println("Is B present?");
B = reader.readLine();
Assess test = new Assess(A, B);
boolean isBothPresent = test.check();
// ................
} catch (IOException e){
System.out.println("Error reading from user");
}
}
I think what you're looking for are method parameters.
In a method definition, you define the method name and the parameters it takes. If you have a method assess that takes a string and returns an integer, for example, you would write:
public int assess(String valueToAssess)
and follow it with code to do whatever you wanted with valueToAssess to determine what integer you wanted to return. When you had decided that i was the int to return, you would put the statement
return i;
into the method; that terminates the method and returns that value to the caller.
The caller obtains the string to be assesed, then calls the method and passes in that string. So it's more of a push than a pull, if you see what I mean.
...
String a = reader.readLine();
int answer = assess(a);
System.out.println("I've decided the answer is " + answer);
Is that what you're looking for?
A subclass will have access to the public members of the superclass. If you want to access a member using {class}.{member} (i.e. MainClass.A) it needs to be statically declared outside of a method.
public class MainClass {
public static String A;
public static String B;
...
}
public class Subclass {
public static void main(String[] args) {
// You can access MainClass.A and MainClass.B here
}
}
Likely a better option is to create a class that has these two Strings as objects that can be manipulated then passed in to the Assess class
public class MainClass {
public String A;
public String B;
public static void main(String[] args) {
// Manipulate A, B, assign values, etc.
Assess assessObject = new Assess(A, B);
if (assessObject.isValidInput()) {
System.out.println("Success!");
} else {
System.out.println("Success!");
}
}
}
public class Assess {
String response1;
String response2;
public Assess (String A, String B) {
response1 = A;
response2 = B;
}
public boolean isValidInput() {
// Put your success/fail logic here
return (response1.compareToIgnoreCase("yes") == 0);
}
}
First you don't need inheritance. Have one class your main class contain main take the main out of Assess class. Create a constructor or setter methods to set the variables in the Assess class.
For instance.
public class MainClass
{
public static void main(String[] Args)
{
Assess ns = new Assess( );
ns.setterMethod(variable to set);
}
}
I'm not 100% sure of your problem, but it sounds like you just need to access variables that exist in one class from a subclass. There are several ways...
You can make them public static variables and reference them as you show in your Assess class. However, they are in the wrong location in MainClass use
public static String A, B;
You can make those variables either public or protected in the parent class (MainClass in your example). Public is NOT recommended as you would not know who or what modified them. You would reference these from the sub-class as if present in the sub-class.
public String A, B; // Bad practice, who modified these?
protected String A, B;
The method that might elicit the least debate is to make them private members and use "accessors" (getters and setters). This makes them accessible programmatically which lets you set breakpoints to catch the culprit that is modifying them, and also let you implement many patterns, such as observer, etc., so that modification of these can invoke services as needed. If "A" were the path to a log file, changing its value could also cause the old log to close and the new one to be opened - just by changing the name of the file.
private String A, B;
public setA(String newValue) {
A = newValue;
}
public String getA() {
return A;
}
BUT ...
Your question says "send to the subclass", but confounded by your knowing how to do this using global variables. I would say that the simplest way is to provide the values with the constructor, effectively injecting the values.
There are other ways, however, your example shows the assessment performed by the constructor. If your Assess class had a separate method to perform the assessment, you would just call that with the variables as arguments.
Your example is confusing since both classes have main methods and the child class does the assessing - I would think you would want the opposite - Have MainClass extend Assess, making "MainClass an Assess'or", let main assign the Strings to Assess' values (or pass them as arguments) to the parent class' "assess" method ("super" added for clarity):
super.setA(local_a);
super.setB(local_b);
super.assess();
or
super.assess(A, B);

Making a static duplicate of non-static integer

For my programming class in first year engineering I have to make a D-game in Java, with only very little knowledge of Java.
In one class I am generating a random integer via
public int rbug = (int)(Math.random() * 18);
every so many ticks. I have to use this integer in another class (in the requirements for an if-loop), and apparently it needs to be static. But when I change the variable to public int static, the value doesn't change any more.
Is there an easy way to solve this problem?
Edit: part of code added:
public int rbug = (int)(Math.random() * 18);
which is used in
public void render(Graphics g){
g.drawImage(bugs.get(rbug), (int)x, (int)y, null);
And in another class:
if(Physics.Collision(this, game.eb, i, BadBug.rbug)){
}
As error for BadBug.rbug I get the message
Cannot make a static reference to a non-static field
Using static to make things easier to access is not a very good ideal for design. You would want to make variables have a "getter" to access them from another class' instance, and possibly even a "setter". An example of this:
public class Test {
String sample = 1337;
public Test(int value) {
this.sample = value;
}
public Test(){}
public int getSample() {
return this.sample;
}
public void setSample(int setter) {
this.sample = setter;
}
}
An example of how these are used:
Test example = new Test();
System.out.println(example.getSample()); // Prints: 1337
example = new Test(-1);
System.out.println(example.getSample()); // Prints: -1
example.setSample(12345);
System.out.println(example.getSample()); // Prints: 12345
Now you might be thinking "How do I get a string from the class that made the instance variable within the class?". That's simple as well, when you construct a class, you can pass a value of the class instance itself to the constructor of the class:
public class Project {
private TestTwo example;
public void onEnable() {
this.example = new TestTwo(this);
this.example.printFromProject();
}
public int getSample() {
return 1337;
}
}
public class TestTwo {
private final Project project;
public TestTwo(Project project) {
this.project = project;
}
public void printFromProject() {
System.out.println(this.project.getSample());
}
}
This allows you to keep single instances of classes by passing around your main class instance.
To answer the question about the "static accessor", that can also be done like this:
public class Test {
public static int someGlobal = /* default value */;
}
Which allows setting and getting values through Test.someGlobal. Note however that I would still say that this is a horrible practice.
Do you want to get a new number every time that you want BadBug.rbug? Then convert it from a variable to a method.

How to call a public method WITHIN a private attribute?

I am new to Java. So the question may seem naive... But could you please help?
Say for example, I have a class as follows:
public class Human {
private float height;
private float weight;
private float hairLength;
private HairState hairTooLong = new HairState() {
#Override
public void cut(Human paramHuman) {
Human.this.decreaseHairLength();
}
#Override
public void notCut(Human paramHuman) {
Human.this.increaseHairLength();
}
};
public float increaseHairLength () {
hairLength += 10;
}
public float decreaseHairLength () {
hairLength -= 10;
}
private static abstract interface HairState {
public abstract void cut(Human paramHuman);
public abstract void notCut(Human paramHuman);
}
}
Then I have another class as follow:
public class Demo {
public static void main(String[] args) {
Human human1 = new Huamn();
Human.HairState.cut(human1);
}
}
The statement
Human.HairState.cut(human1);
is invalid...
I intend to call the public cut() function, which belongs to hairTooLong private attribute.
How should I do it?
Since HairState is private to Human, nothing outside the Human class can even know about it.
You can create a method in Human that relays the call to its private mechanism:
public class Human {
. . .
public float cutHair() {
return hairTooLong.cut(this);
}
}
and then call that from main():
System.out.println(human1.cutHair());
Two other solutions to previous comment:
You can implement a getter which returns the hairTooLong attribute.
You can invoke the the cut() method through the reflection API (but you don't want to go there if you are beginner).
Would suggest either the solution in the previous comment, or the first option presented here.
If you are curious, you can have a look to the reflection API and an example here: How do I invoke a Java method when given the method name as a string?
In java there are four access levels, default, private, public and protected. Visibility of private is only limited to a certain one class only (even subclass cannot access). You cannot call private members in any other class. Here is a basic details of java access levels.
Access Levels
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
For more details check Oracle docs

Java reflection: How to get methods with no parameters only

I'm working on a school assignment about Java reflection. The details are below:
Write a console program that asks the user for a class name, loads
that class and creates an instance of it. We assume that the class has
a constructor without any parameters. Then, the program prints out the
names and values of the public variables of the created object, and
also a list of the public methods that do not specify a parameter. The
program should let the user choose a method and execute that method on
the created object. Afterwards, the program should again show the
public variables with their values and allow the user to choose a
method, and so on. Use the following class to test your
implementation:
public class Counter {
public int c;
public void increment() { c++; }
public void decrement() { c--; }
public void reset() { c = 0; }
}
The problem I am having has to do with the following sentence: "list of the public methods that do not specify a parameter". Is there a way to list only methods with no parameters? I have used getMethods but I end up getting a lot of methods from the Object and Class superclasses with parameters.
For example the following code that I have written:
import java.lang.reflect.*;
import java.io.*;
public class Q1 {
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("What class would you like to run? ");
String className = reader.readLine();
Class c = Class.forName(className);
Object o = c.newInstance();
for (Field f : c.getFields())
System.out.println(f);
for (Method m : c.getMethods())
System.out.println(m);
} catch(IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
Outputs the following:
What class would you like to run? Counter
public int Counter.c
public void Counter.reset()
public void Counter.increment()
public void Counter.decrement()
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
Is there a way to get only the ones with no parameters to be printed? Also is my interpretation of the assignment details right in the first place? Or does the phrase "public methods that do not specify a parameter" possibly mean something else and I have entirely the wrong idea?
Have you looked at the API for the Method class? There's a method called getParameterTypes() that has the answer for what you're looking for, and the API states explicitly what this will return if there are no parameters. Just call this in your for loop on the Methods returned and you should be in like flint.
Just use the Method class' getParameterTypes function. If the return value is 0 then there are no parameters to that function. Key part from the Java doc:
getParameterTypes
public Class[] getParameterTypes()
Returns an array of Class objects that represent the formal parameter types, in declaration order, of the method represented by
this Method object. Returns an array of length 0 if the underlying
method takes no parameters.
Returns:
the parameter types for the method this object represents

Categories

Resources