Subclass Constructor isnt recognized - java

I'm trying to construct a subclass and I get error that the constructor is undefined?!?!
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class LinearAlgebra
{
public class Matrix
{
public int m;
public int n;
public float Matrix[][];
void Matrix(int tempm, int tempn)
{
m = tempm;
n = tempn;
Matrix = new float[m-1][n-1];
}
}
public static void main(String[] args)
{
LinearAlgebra Geometry = new LinearAlgebra();
LinearAlgebra.Matrix Rotation = Geometry.new Matrix(3,3);
System.out.println("hello");
}
}
Sorry have been out of java for a while. I looked at some java tutorials and think I'm doing the same but it is not working
https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

void Matrix(int tempm, int tempn)
This is a method not a constructor. You must remove the return type void:
Matrix(int tempm, int tempn)
You should also name your local variables to start with a lowercase, geometry instead of Geometry. Using an uppercase confuses the reader because it makes it seem as if the nested class Matrix is a static class, not an inner class. Same for the Rotation variable.

Related

Netbeans class' method not recognized

I'm having a fairly odd problem with Netbeans.
I'm trying to calculate the sum of an ArrayList, but I am not able to call the method public int sumOfHand() onto my this.hands variable.
I've restarted Netbeans numerous times, created new classes and tried to calculate the sum using the .reduce() method using streams, but none of it helped.
Thanks for any suggestion!
import java.util.List;
import java.util.stream.Stream;
import java.util.ArrayList;
import java.util.Collections;
public class Hand implements Comparable<Hand>{
public ArrayList<Card> hands;
public Hand() {
this.hands = new ArrayList<>();
}
public void add(Card card) {
this.hands.add(card);
}
public void print() {
this.hands.forEach(crd -> {System.out.println(crd);});
}
public void sort() {
Collections.sort(this.hands, (crd1, crd2) -> crd1.compareTo(crd2));
}
#Override
public int compareTo(Hand otherHand) {
// sumOfHand() not recognized here, 'Cannot find symbol'
return this.hands.sumOfHand() - otherHand.sumOfHand();
}
public int sumOfHand() {
int sum = 0;
for (Card tc : this.hands) {
sum += tc.getValue();
}
return sum;
}
}
You can't call a int sumOfHand() of this.hands as hands is an ArrayList.class and int sumOfHand() is a method of Hand.class. To call this method you need to use this.sumOfHands() inside your Hand.class.

How can I call a method from a separate class that contains a randomized value and use that value to print in my main class?

I am new to coding and I am trying to figure out how to call a method from a separate class and use it in my main class. I know that it is easier to do it in the main class but I was asked to do it in a separate class. The random number that is printed to range from 1 to 3 but using this method I just get 0.
here's the main class:
package example;
public class Example {
public static int number;
public static void main(String[] args) {
otherClass a = new otherClass();
a.assignNumber();
System.out.println(number);
}
}
and my separate class:
package example;
import java.util.Random;
public class otherClass {
public int assignNumber(){
Random num = new Random();
int number = num.nextInt(4) + 1;
return number;
}
}
Default value of number variable is 0 since its type is int. You need to assign the return value of function to number variable.
package example;
public class Example {
public static void main(String[] args) {
otherClass a = new otherClass();
int number = a.assignNumber();
System.out.println(number);
// Or
System.out.println(a.assignNumber());
}
}
The number variable in 'Example' class differs from the number in 'otherClass'.
The easiest way to fix your code is to fix the assignment part:
number = a.assignNumber();
But you should probably check your code again: It is better to use local int variable instead of static field.
package example;
public class Example {
public static void main(String[] args) {
otherClass a = new otherClass();
int number = a.assignNumber();
System.out.println(number);
}
}
There is no real use for the number variable.
It can all be condensed to
package example;
public class Example {
public static void main(String[] args) {
System.out.println(new otherClass().assignNumber());
}
}

I cant access a class I defined from the main loop?

I have an extremely simple piece of code where I am calling a method that I defined, and it wont run because it says I haven't defined the method?
class Main {
public static void main(String[] args) {
go(7,3);
}
}
/// in a separate file named go.java -->
class go{
public static int go(int x, int y){
if(x <= 1)
return y;
else
return go(x - 1, y) + y;
}
}
You have two options, use the class name go to identify the class containing the method
public static void main(String[] args) {
go.go(7,3);
}
or static import it like
import static go.go;
public class Main {
public static void main(String[] args) {
go(7,3);
}
}
First thing first, Class name should start with upper case, your another class should be Go.java , to access static method
Refer Class name that is Go.go(7,3);

Iterate through inherited classes and call their static methods

I want to iterate through some classes, that inherit from the same superclass.
They all have the same static method, but how can I call it?
I tried it like this, but that does not work:
public abstract class Tower {
private static int text = 5;
public static int getText() {
return text;
}
}
public class aTower extends Tower {
private static int text = 10;
public static int getText() {
return text;
}
}
public class Main {
public static void main(String [] args) {
LinkedList<Class<?extends Tower>> towers = new LinkedList<>();
towers.add(aTower.class);
for (int i = 0; i < towers.size(); i++) {
towers.get(i).getText(); //Does not work
}
}
}
Context
I want to have a list of classes that inherit from Tower for calling static methods of them, for getting e.g. their texture. Is there any other way to do that?
Edit: The main goal is, that I will have many different Towerclasses in a list, and there should be a menu with every towerclass. To paint the menu, I want to get for example the texture, the name, etc. When you click on the menu entry, then you should get an object of the specific tower and you can build it somewhere. But I do not like the idea of having a list of more or less unused instances, and therefore I thought having a static method is the right solution.
Check out Java Interfaces though as already stated they wouldn't be static methods.
public interface ITower {
public String getText();
}
from this point you define your tower objects that implement ITower and then inside main:
public class Main {
public static void main(String [] args) {
List<ITower> towers = new LinkedList<>();
// create your tower objects and add them to the list
towers.add(new ATower());
towers.add(new BTower());
for (ITower iObj : towers) {
iObj.getText();
}
}
}
By converting your static method to an instance method, and using a static field within each subclass, you can get this to work:
public abstract class Tower {
private static int text = 5;
public int getText() {
return text;
}
}
and
public class aTower extends Tower {
private static int text = 10;
#Override public int getText() {
return text;
}
}
and
public class Main {
public static void main(String [] args) {
LinkedList<aTower> towers = new LinkedList<>();
towers.add(new aTower());
for (int i = 0; i < towers.size(); i++) {
towers.get(i).getText();
}
}
}
In your original main() method, you created a List instance for Class objects. I think you had really intended for this to contain aTower instances. This is important because class Class will not have a getText() method.
Polymorphism doesn't work with static methods. Why do you want getText() to be static?
If you have a no-arg constructor in Tower you can use reflection to create an instance from a class name.
Class<Tower> towerClass = (Class<Tower>) Class.forName(classString);
Tower tower = towerClass.newInstance();
tower.staticMethod();

Java OOP: referencing subclass object

I have an ArrayClass and mergeSortArray extends it. And mergeSortArray contains a mergeSort() method. However, since I used super to call a constructor from the superclass, I do not know how to refer to the mergeSortArray (the subclass object / array) and pass it as a parameter in the mergeSort method. In fact, is this even feasible ? I know I can do this in a NON- OOP way. However, I am keen to know how to do this in an OOP way.
Please correct me if I have said incorrect, as I am new to Java and I want to learn more about it.
// ArrayClass Object
import java.util.*;
import java.io.*;
import java.math.*;
public class ArrayClass{
public int[] input_array;
public int nElems;
public ArrayClass(int max){
input_array = new int [max];
nElems = 0;
}
public void insert(int value){
input_array[nElems++] = value;
}
public void display(){
for(int j = 0; j < nElems; j++){
System.out.print(input_array[j] + " ");
}
System.out.println("");
}
}
import java.io.*;
import java.util.*;
import java.math.*;
class mergeSortArray extends ArrayClass{
public mergeSortArray(int max){
super(max);
}
public void methodOne(){
int[] output_array = new int[super.nElems];
mergeSort( // ************* // ,output_array,0, super.nElems -1);
}
................
}
I am not sure what I should put to replace ****** such that I can pass mergeSortArray as a parameter into the mergeSort method.
There isn't a mergeSortArray. You inherit input_array like (and no need for super.nElems you inherit that too),
mergeSort( input_array, output_array, 0, nElems - 1);
Your sub-class will inherit everything that is protected or greater visibility (not private), however your ArrayClass gives you both public fields
public int[] input_array;
public int nElems;
They should probably be protected and have accessor methods (getters).
protected int[] input_array;
protected int nElems;
public int size() {
return nElems;
}
public int[] getInputArray() {
return input_array;
}
First of all, I suggest you not to have public fields on OO code. You got two public fields (input_array and nElems), you should change them to private and create acessors if you need.
Then, to refer to those fields on the subclass, you can either use a protected acessor if you wish to hide the acessor from the rest of the API or a public one, if it's part of your public API. That way, on your ArrayClass:
public class ArrayClass {
private int[] input_array;
private int nElems;
//this may be public
protected int[] getInputArray() {
return input_array;
}
and when you get to call your mergeSort method, you can use getInputArray()

Categories

Resources