Jump to content

help with java

Featured Replies

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

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.

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.