Sarahisme Posted May 10, 2005 Posted May 10, 2005 would anyone be able to help me with some questions about two very short java classes? i have come up with some answers of my own, but yeah, i'm not sure how correct my answers are.... the 4 questions are: 1. The data attributes m, n and grid in the HeatGrid class are declared private, and methods such as gridHeight(), gridWidth() and copyGrid() are provided to extract their value. Why is the better class design than simply declaring those attributes as public? My answer: So other classes accessing them cannot modify those data atributes, i am not sure why this is better class design. 2. Why must the (global) constants in HeatSim.java be declared static ? My Answer: because HeatSim is what you run first and so you don't create a HeatSim object, you just access its methods, thus the need to make them static. 3. Assuming HeatGrid has been fully implemented, during the execution of the statement HeatGrid = new HeatGrid(inGridFile), how many objects of HeatGrid, double [][] and Scanner are created? My Answer: 1 HeatGrid object, 1 doulbe[][] object and 1 scanner object Which of these objects must still exist after the statement completes execution? 4. For each of the classes HeatSim and HeatGrid, identify the client and supplier classes (including library classes). My Answer: So far this is what i have done. for the HeatSim class, i said that the supplier classea are: double int boolean String Math Class viewgrid class array and the clients for the HeatSim class are: HeatGrid class if anyone can help me i will but up the VERY short code to these two classes. I am really stuck and any help woould be greatly appriciated! Lots o love Sarah
Aeternus Posted May 12, 2005 Posted May 12, 2005 Did you get the code for the two classes with this? Just asking as the last two questions seem to assume you know the structure of the classes. 1) I'd say its just so that if the inner working to produce the height and the width values change (ie you might calculate them somehow instead of storing them if you changed the class for some reason), the actual interface to the class/object doesnt change (ie its still ObjectName->gridWidth() etc). 2) For this i would guess its because static variables are created and exist as a single memory location, before an object is made. Therefore a) If one wants to access the constant before an object is made (since its a constant, this might be needed for reason and you say it should be global so that backs it up) they can and b) Since its a constant and unchanging, the value won't change with different objects and so creating a new variable in memory for each object seems wasteful, therefore using static would make sense (ie only one memory address/variable for all instances of that static variable). 3/4) No idea as i can't see the code and don't want to assume anything to guess Not sure if what i'm saying is right, just what i think might be right. I'm sure someone else will come along and correct me.
Sarahisme Posted May 17, 2005 Author Posted May 17, 2005 thanks for that Aeternus, that helped a lot! Sarah
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now