#Inheritance/ IO

100 messages ยท Page 1 of 1 (latest)

vapid crane
#

        String [] xy = new String[2];

        Scanner scan = new Scanner(name);
        for (int i = 0; i < 2; i++) {
            xy[i] = scan.next();
        }
        rows = Integer.parseInt(xy[0]);
        columns = Integer.parseInt(xy[1]);
        
        double [][] tempArray = new double[rows][columns];
        
        while(scan.hasNext()) {
            scan.next(scan.nextLine());
        }

        for(int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                tempArray[i][j] = Double.parseDouble(scan.next());

            }
        }

        super(tempArray);


    }

    protected double[][] array;
    protected int rows;
    protected int columns;

    //Creates an empty matrix.
    public Matrix(int row, int column) {
        double[][] matrix = new double[row][column];

        this.array = matrix;
        this.rows = row;
        this.columns = column;

    }
    //Creates a matrix using an array.
    public Matrix(double[][] matrix) {
        double [][] tempArray = new double[matrix.length][matrix[0].length];
        for(int i = 0; i < matrix.length; i++) {
            for(int j = 0; j < matrix[i].length; j++) {
                tempArray[i][j] = matrix[i][j];
            }
        }
        this.rows = matrix.length;
        this.columns = matrix[0].length;
        this.array = tempArray;
    }

Umm.. so basically for an assignment I have in school I have to read and write an array from a file. I have certain requirements that I must follow. The first bit of code is under a child class "FileMatrix" and the second bit is the parent class "Matrix". I'm not fully sure if what i wrote even works. It is supposed to do as follows: read the first two digits from the file, set those as the rows and column length, then read the rest of the file and insert those values into the new found array. I can't even test this though because my "super (tempArray);" doesn't work. It says "Constructor call must be the first statement in the constructor."

If anyone could help me that would be great, im just really lost haha

Thanks!

ruby ibexBOT
#

โŒ› This post has been reserved for your question.

Hey @vapid crane! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

fervent gorge
#

sounds like you would use a static from for this, rather than a constructor

vapid crane
#

sadly it has to be a constructor...

#

i'm not even sure how to do the rest of it, but im tryna start somewhere haha

#

I've done other parts to this assignment, however this is like the core of the assignment

fervent gorge
#

you'd probably use the Matrix(int, int) constructor then

#

you can get the first 2 ints immediately for super

#

the FileMatrix(File) would use the FileMatrix(Scanner) constructor, like it says, by making a Scanner from the File

vapid crane
#

how would you suppose I make the FileMatrix(File) use the FileMatrix(Scanner) , i guess im unsure how to actually make 1 method use the other

#

I just tried changing the super(tempArray); => super(rows, columns); but its the same message

#

Also I appreciate your help ๐Ÿ™‚ it means a lot

#

I also just realized something... super() has to be the first statement in the constructor... i finally understand haha, but how am I supposed to find the values of "rows" and "columns" before super(rows, columns) if super has to be first?

fervent gorge
fervent gorge
#

that's why the FileMatrix(File) constructor uses the FIleMatrix(Scanner) constructor

vapid crane
#

but if I start my super like that, the scanner wouldn't be open already though? so would the input.nextInt even work?

fervent gorge
#

idk what you mean by that

vapid crane
#

one moment ill ss

fervent gorge
#

the FileMatrix(File) constructor creates a Scanner, and possibly populating the matrix; you could populate the matrix in either constructor

vapid crane
#

I gotchu

#

that makes sense

#

Im sorry i feel like you're just giving it all to me but i appreciate it. Under FileMatrix(File); are you saying that in my initial "super()" I would try and make it access the FileMatrix(Scanner)? Like do I open a scanner within the super() under FileMatrix(File)

#

because FileMatrix(File) needs "super()" as well

fervent gorge
#

you wouldn't have a super call in FileMatrix(File)

vapid crane
#

I thought super was required

#

under all child class constructors

fervent gorge
#

you have to delegate to a super constructor somehow, and that can be through another constructor of the same class

vapid crane
#

ohhhh thats important to know

#

haha

fervent gorge
#

so each constructor needs to have either this or super call (unless there exists a 0-arg constructor in the parent)
a super call is just delegating to a super constructor directly
a this call is delegating to another constructor, which in turn must delegate to a super constructor itself

#

to clarify;
this refers to the current class
super refers to the parent class
this is usually a superset of super, having more stuff, due to inheritance, and super can be used to exclude overridden things to get the parent's definition
however, constructors are not inherited, so this() and super() don't overlap

vapid crane
#

Okay okay its starting to make sense

#

hm I see that "this()" also needs to be the first statement however,...



    public FileMatrix(Scanner sc) {
        super(sc.nextInt(), sc.nextInt());


    }

    private FileMatrix(File name) {
        
        Scanner scan = new Scanner(name);
        this(scan);

        double [][] tempArray = new double[rows][columns];
        
        while(scan.hasNext()) {
            scan.next(scan.nextLine());
        }

        for(int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                tempArray[i][j] = Double.parseDouble(scan.next());

            }
        }
#

So my this(scan) doesnt work haha

#

also I tried created a scanner inside of this() and didnt work

fervent gorge
#

how come?

vapid crane
#

how come I tried to make the scanner inside of this()?

#

or why wont it work?

fervent gorge
#

what's wrong with it

#

it should work

vapid crane
#

"Scanner cannot be resolved to a variable"

#

this(Scanner sc = new Scanner(name));

#

thats what i tried

#

name is the file name

fervent gorge
#

you can't have a declaration as an expression

#

Type name is a declaration, it creates the variable name

#

for local variables, they're exclusively statements, they can't be used as expressions

#

the value new Scanner(name) is an expression that evaluates to the Scanner instance we need

#

(also btw name isn't a good variable name for a File, you could use file like the description says; similarly for the Scanner, for consistency)

vapid crane
#

Im not sure I fully understand what you mean

#

or at all lol

fervent gorge
#

this is kinda getting deep into the specifications of java as a language so... yeah i wouldn't expect you to get it first try tbh, lol. don't worry about it

vapid crane
#

haha

#

"the value new Scanner(name) is an expression that evaluates to the Scanner instance we need" this line especially makes no sense to me

fervent gorge
#

basically there are 2 main pieces of code in any language, statements and expressions
statements do things, expressions are values
some pieces of code can be either statements or expressions depending on the context in which they're used
(some code won't fit into statement vs expression, like declarations for some languages, including java; statements and expressions are afaik universal though, while declarations are not)

#

statements do things, they're the verbs of code. in java, they always end with ;, except for flow and block statements (we can get into this later)
so statements are the main body of the code; things like super(); for example, as you've seen in the error, it has to be the first statement in a constructor

vapid crane
#

okok I gotchu, statements do things, expressions hold values

#

so are variables expressions then?

fervent gorge
#

expressions are nouns, they have values, things like 5, 1 + 1, new Scanner() are all expressions
these can be used as parts of statements, to form more complete sentences, in a way

#

although i suppose the statement is the sentence itself

fervent gorge
vapid crane
#

kk

fervent gorge
#

the variable itself, say sc or tempArray, are expressions, they don't do anything so they can't be statements

#

but creating or assigning to the variable are indeed actions, so double[][] tempArray; or Scanner scan = new Scanner(name); are statements

fervent gorge
vapid crane
#

kk

#

i see

fervent gorge
#

declarations, at least in java, can't be expressions.

#

but plain assignments, like line = sc.nextLine() maybe, can be either statements or expressions. in their expression form, they have the value of the thing being assigned, so for example x = 5 evaluates to 5, doing int x; System.out.println(x = 5); would print 5

vapid crane
#

alright

#

hmm

fervent gorge
#

since declarations are statements, and you can't have statements before super/this, you would have to just use the value itself, new Scanner(file), since you can't create a new variable.

#

oh also, with knowing statements and expressions, you can define the syntax for various statements in more formal terms, which can let you see the full capabilities sometimes
for example, declaring a variable is

Type name;
Type name = expr;
```(simplified, there's more than this)
vapid crane
#

Ok i wrote that and there's no errors. However im confused lol. everything you said makes sense, its just "new Scanner(file)" If I havent initialized Scanner, new Scanner does that I guess, but doesn't assign it a varibale name..

fervent gorge
#

you have initialized a Scanner, you just haven't put it into a local variable

vapid crane
#

which the other constrcutor would do then?

#

I initialized it, but Im allowing the other constructor to assign it the local variable "sc"

#

?

fervent gorge
#

just new Scanner(file); is also both a statement and an expression; it does a thing, it creates an instance of the class
when used as an expression, the value is the object that was created (or more accurately, a reference to the object)
so with Scanner sc = new Scanner(file);, that's doing 3 things;

  • creating a variable sc of type Scanner
  • creating a new Scanner instance
  • assigning that instance to the variable sc
fervent gorge
vapid crane
#

yeah thats what I mean I think

fervent gorge
#

just to be clear, the process for parameters isn't "assignment", it just acts like it

#

it's generally just called "passing arguments"

vapid crane
#

this(new Scanner(file)) that goes over to the constructer FileMatrix(Scanner sc) which makes the "new Scanner(file) assigned to the variable name "sc"

fervent gorge
#

/run

class X {
  X() {
    this(1);
  }

  X(int i) {
    System.out.println(i);
  }

  public static void main(String[] args) {
    new X();
  }
}
night crystalBOT
#

Here is your java(15.0.2) output @fervent gorge

1
vapid crane
#

It's just in school rn, we jumped from that^ to what im doing now

#

it was such a big jump, all my peers are so thrown off as well

#

everything you've taught me will be passed down to some class mate haha

#

well I have some writing to do now, and see if I can figure more out. Thanks a lot ๐Ÿ™‚ im gonna see what i can do with this knowledge

#

bro you are blessed af

#

with your help Im now able to create the entire array

#

and print it

#

now I have other things to do in the assignment haha but you were blessed