Results 1 to 10 of 10
- 01-27-2011, 02:31 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
Am I understanding this correctly?
My logic/questions are in the comments. Thanks in advance.
Java Code:import java.util.Scanner; public class BoxTester { public static void main (String[]args) { Scanner keyboard = new Scanner(System.in); double height = 0; double width = 0; double depth = 0; while(true) { System.out.print("Please enter the height, width, depth ie 4 5 6: "); height = keyboard.nextDouble(); width = keyboard.nextDouble(); depth = keyboard.nextDouble(); /**The code below tells the BoxTester program that box is going to be a Box object. * = new Box calls the Constructor of the Box class and states that there is a new * instance of itself. * * box can now be used as a reference to anything we wish to do with this object. * * Box() accepts the values of the user input ie (height = keyboard.nextDouble()), etc. and * places them in the order specified ie Box(height, width, depth) <-- parameters?. * * It then passes those variables and their values (arguments?) to .GetSurfaceArea and .GetVolume. */ Box box = new Box(height, width, depth); System.out.println("Surface Area: " + box.GetSurfaceArea()); //calls the .GetSurfaceArea method from the Box class System.out.println("Volume: " + box.GetVolume()); //calls the .GetVolume method from the Box class } } }
- 01-27-2011, 02:35 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 952
- Rep Power
- 12
I don't see any questions.
-Gary-
- 01-27-2011, 02:38 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
Am I right in assuming that Box(height, width, depth) are parameters? (the height, width, depth part)
Also, when the user inputs their values for height, width, and depth, is the information passed called arguments?
Is my explanation in the comments correct?Last edited by phixion; 01-27-2011 at 02:40 AM.
- 01-27-2011, 02:42 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 952
- Rep Power
- 12
OK, I see your questions now. Your understanding is pretty good. You haven't posted the code for the Box class, but my guess is that the Box() constructor will store the passed values of height, width and depth (yes, those are parameters) in a set of instance variables within the class. Then the GetSurfaceArea() and GetVolume() methods (better style would be to name them getSurfaceArea() and getVolume()) would read the values from those instance variables.
The terms parameter and argument are used pretty interchangeably, although technically, a method takes parameters, and the Java program takes arguments at the command line. Very few hard-core pedants would call you out on that.
-Gary-
- 01-27-2011, 02:44 AM #5
The "things" inside brackets are called parameters or alternatively arguments. In the method signature where you also include the parameter type they are called formal paramaters. In the method call where you enter the parameter only (because the complier already knows the type) they are called actual parameters.
- 01-27-2011, 03:12 AM #6
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
Here is the Box class. I have a few more questions. (Sorry, ha. I'm just trying to get a firm understanding of this stuff before I move forward).
Are instance variables essentially attributes of that particular object?
Below, I'm not completely understanding what is taking place from the methods Box() to SetCube. I'm not 100% sure why I'm constantly declaring the same variables.
This will by my last post for the week, ha.
Java Code:public class Box { // instance variables private double height; private double width; private double depth; /** * Constructor for objects of class Box */ public Box() { // initialise instance variables height = 0; width = 0; depth = 0; } public Box(double h, double w, double d) { height = h; width = w; depth = d; } public Box(double edgeLength) { height = edgeLength; width = edgeLength; depth = edgeLength; } public void SetBox (double h, double w, double d) { // Modifying method height = h; width = w; depth = d; } public void SetCube(double edgeLength) { //Modifying method height = edgeLength; width = edgeLength; depth = edgeLength; } public double GetSurfaceArea() { return (2*height*width) + (2*height*depth) + (2*depth*width); //double surfacearea; //surfacearea = (2*height*width) + (2*height*depth) + (2*depth*width); //return surfacearea; } public double GetVolume() { return height*width*depth; //double volume; //volume = height*width*depth; //return volume; } }
- 01-27-2011, 03:26 AM #7
Just like you have a name and I have a different name. So a Person object would have an instance variable called name and each Person object can have a different value stored in that variable.
Below, I'm not completely understanding what is taking place from the methods Box() to SetCube.
I'm not 100% sure why I'm constantly declaring the same variables.
- 01-27-2011, 03:36 AM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 952
- Rep Power
- 12
See if this is clearer:
Java Code:public class Box { // instance variables private double height; private double width; private double depth; /** * Constructor for objects of class Box */ public Box() { // initialise instance variables [COLOR="Blue"] // setting them all to zero is arbitrary, but to // some extent you need to be arbitrary in a // default constructor [/COLOR] [COLOR="Blue"]this.[/COLOR]height = 0; [COLOR="Blue"]this.[/COLOR]width = 0; [COLOR="Blue"]this.[/COLOR]depth = 0; } public Box(double h[COLOR="Blue"]eight[/COLOR], double wi[COLOR="Blue"]dth[/COLOR], double d[COLOR="Blue"]epth[/COLOR]) { [COLOR="Blue"]// setting the instance variables to the values passed as parameters[/COLOR] [COLOR="Blue"]this.[/COLOR]height = h[COLOR="Blue"]eight[/COLOR]; [COLOR="Blue"]this.[/COLOR]width = w[COLOR="Blue"]idth[/COLOR]; [COLOR="Blue"]this.[/COLOR]depth = d[COLOR="Blue"]epth[/COLOR]; } public Box(double edgeLength) { [COLOR="Blue"] // making a cubical Box // note that if there is no ambiguity, we do not need to specify [I]this.[/I][/COLOR] height = edgeLength; width = edgeLength; depth = edgeLength; } public void [COLOR="Blue"]s[/COLOR]etBox (double h[COLOR="Blue"]eight[/COLOR], double w[COLOR="Blue"]idth[/COLOR], double d[COLOR="Blue"]epth[/COLOR]) { // Modifying method [COLOR="Blue"] // sets instance variables of an already-existing Box to // the values passed. Note that it might make more sense // to have separate setHeight(), setWidth() and setDepth() // methods [/COLOR] [COLOR="Blue"]this.[/COLOR]height = h[COLOR="Blue"]eight[/COLOR]; [COLOR="Blue"]this.[/COLOR]width = w[COLOR="Blue"]idth[/COLOR]; [COLOR="Blue"]this.[/COLOR]depth = d[COLOR="Blue"]epth[/COLOR]; } public void [COLOR="Blue"]s[/COLOR]etCube(double edgeLength) { //Modifying method height = edgeLength; width = edgeLength; depth = edgeLength; } public double [COLOR="Blue"]g[/COLOR]etSurfaceArea() { return (2 * height * width) + (2 * height * depth) + (2 * depth * width); } public double [COLOR="Blue"]g[/COLOR]etVolume() { return height * width * depth; } }
Method names should start with a lower-case letter. It makes it easier to distinguish them from constructors.
-Gary-
- 01-27-2011, 03:52 AM #9
- 01-27-2011, 06:13 AM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 952
- Rep Power
- 12
Similar Threads
-
I can't get this to run correctly
By LostinJavaLand in forum New To JavaReplies: 4Last Post: 07-15-2010, 07:49 AM -
Did I do this THREAD correctly?
By TimHuey in forum New To JavaReplies: 4Last Post: 04-24-2010, 06:54 AM -
need help in understanding collection
By ShinTec in forum Advanced JavaReplies: 2Last Post: 04-24-2010, 03:49 AM -
Help on understanding a program
By newbie225 in forum New To JavaReplies: 1Last Post: 11-10-2009, 01:53 AM -
how to scale correctly ?
By h9h in forum Java 2DReplies: 10Last Post: 10-29-2009, 08:06 AM
Bookmarks