posted 7 years ago
Hi,
The following is apart of an assignment (I changed the code to be brief) and I am not posting it for my work to be done for me - I am just extremely Lost.
I am just confused as to how to handle this. Do I use something from java.io library (Scanner)? How do I handle a parameter that is just a bunch of instances??
My apologies in advance for any confusion but I am at a loss. Any help is appreciated.
Thanks in Advance
The following is apart of an assignment (I changed the code to be brief) and I am not posting it for my work to be done for me - I am just extremely Lost.
I am just confused as to how to handle this. Do I use something from java.io library (Scanner)? How do I handle a parameter that is just a bunch of instances??
My apologies in advance for any confusion but I am at a loss. Any help is appreciated.
Thanks in Advance
posted 7 years ago
Hi Nyah, Welcome to JR
What do you want to do with the instance passed to the method? You can use it just like any other instance.

What do you want to do with the instance passed to the method? You can use it just like any other instance.
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook
Nyah Bell
Greenhorn
Posts: 5
posted 7 years ago
well - unlike your example - there are only constructors within class A and B, so there are no return values (this is what is confusing me)
I am trying to add code to this:
the code that I need to insert is to manipulate the parameter e that is passed
in my original post, inside of main, i created
I need to get to whats inside of the instances of A and B
(almost like your example of A.methodA but when attempting this ( I am using Netbeans IDE) it is not possible to access the variables that way
I am trying to add code to this:
the code that I need to insert is to manipulate the parameter e that is passed
in my original post, inside of main, i created
I need to get to whats inside of the instances of A and B
(almost like your example of A.methodA but when attempting this ( I am using Netbeans IDE) it is not possible to access the variables that way
posted 7 years ago
In the Class A or Class B all the fields are public. So you can access them using <instanceName>.<fieldName>. In the example I provided I had made the fields are private and then provided Getters/Setters to access/manipulate the fields.
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook
Nyah Bell
Greenhorn
Posts: 5
posted 7 years ago
Am not able to understand what you are trying to do with the code you have provided in the above post. Firstly its not understandable and secondly its not syntactically correct.
Nyah Bell wrote:
...
that instance does not have a name, it is just sitting there on the right side - what would I put in the <instanceName> part? 'A' is being passed, so can I do something like:
A.B.method
Am not able to understand what you are trying to do with the code you have provided in the above post. Firstly its not understandable and secondly its not syntactically correct.
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook
Nyah Bell
Greenhorn
Posts: 5
posted 7 years ago
okay sorry,
I just did not want to post up my entire actual code but I will post my actual code and go from there.
All of that was given as the teacher's code - he has said not to change it because it should work fine and the only changes we should make is adding our code where he told us to add it (as noted with the comments)
I am expected to use a stack to bind variables, but before I can do so, I need to figure out how to get to those variables. For example, in main, Expr e = new Block(new . . . . . );
e is then passed as a parameter
What can I do within the eval method to get to the variables that were instantiated in e ("x", new Declaration("x"........) etc.?
It is of course perfectly okay if you cannot help because I definitely appreciate you trying to understand me (I can be confusing) and understand what I am trying to do. This is much appreciated.
I just did not want to post up my entire actual code but I will post my actual code and go from there.
All of that was given as the teacher's code - he has said not to change it because it should work fine and the only changes we should make is adding our code where he told us to add it (as noted with the comments)
I am expected to use a stack to bind variables, but before I can do so, I need to figure out how to get to those variables. For example, in main, Expr e = new Block(new . . . . . );
e is then passed as a parameter
What can I do within the eval method to get to the variables that were instantiated in e ("x", new Declaration("x"........) etc.?
It is of course perfectly okay if you cannot help because I definitely appreciate you trying to understand me (I can be confusing) and understand what I am trying to do. This is much appreciated.
posted 7 years ago
In the eval method-
Firstly you need to cast "e" to Block to be able to use Block class's instance fields. That way you would have to proceed to access other class's instance fields. And before casting try to add an instanceof check. The reason for casting is that- Which fields can be accessed is determined by the Type of the reference.
So if you have the type of reference as Expr and your type of instance is Block, to access the left field in the Block instance you need to have the Type of reference as "Block" and not "Expr". Because the association of the fields with the reference type is done at the compile time.
Firstly you need to cast "e" to Block to be able to use Block class's instance fields. That way you would have to proceed to access other class's instance fields. And before casting try to add an instanceof check. The reason for casting is that- Which fields can be accessed is determined by the Type of the reference.
So if you have the type of reference as Expr and your type of instance is Block, to access the left field in the Block instance you need to have the Type of reference as "Block" and not "Expr". Because the association of the fields with the reference type is done at the compile time.
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |