Results 1 to 5 of 5
  1. #1
    3awash Guest

    Default A question about encapsulation

    so I had a cylinder class, I was asked to make it encapsulated which I did:

    import java.util.Scanner;


    public class Cylinder {
    private int radius;
    private int height;
    public double volume;

    public int getRadius() {
    return radius;
    }

    public void setRadius(int radius) {
    this.radius = radius;
    }

    public int getHeight() {
    return height;
    }

    public void setHeight(int height) {
    this.height = height;
    }

    public double getVolume() {
    return volume;
    }

    public void setVolume(double volume) {
    this.volume = volume;
    }


    public void calculateVolume () {
    volume = (3.41 * radius * radius * height);

    }

    public void getData(){

    System.out.println("Please enter radius");
    Scanner num = new Scanner (System.in);
    radius = num.nextInt();
    System.out.println("Please enter height");
    height = num.nextInt();
    }


    public void display(){

    System.out.println("Volume of cylinder is : "+ volume);
    }


    }


    and this is my main function:


    public class CylinderTestDrive {


    public static void main (String []args){

    Cylinder c = new Cylinder ();
    c.getData();
    c.calculateVolume();
    c.display();


    }
    }

    The thing is I thought that was it, when my instructor saw it he said that I still need to make changes after encapsulating it by using scanner and calling another function or something. any help? I'm completely stuck.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I still need to make changes after encapsulating it by using scanner and calling another function or something.

    I'm not really sure what this means.

    But a few things strike me about your Cylinder:

    (1) You have "public double volume;". It certainly shouldn't be public - it should be private like radius and radius. Making things private as you type the code is a decent habit (you can always change the accessibility later if you need to)

    (2) Do you really need the volume member at all? After all it can be calculated from the other two when it is needed. Also you have a setVolume() method and it is far from clear what this is supposed to do because the volume isn't something that a cylinder can arbitrarily change given it's radius and height.

    Perhaps the assignment instructions might make it clear exactly what methods and variables the class is supposed to have. (ie what "encapsulate" actually means in this context).

    (3) It seems strange - to my way of thinking - for Cylinder to have a getData() method. Cylinders don't talk: they don't ask people how tall or fat they should be. The code involving Scanner should be part of the main() method although, again, the assignment instructions might say something different.

  3. #3
    3awash Guest

    Default

    I don't know whats his deal, I had my volume private then he said it doesn't have to be that way and I should write public instead.
    Also interesting what you said about the getData since he told me to delete it because it's not relevant anymore to the work, though when I did I got a lot of errors because other steps were related to it.
    This was an original assignment that we've done before, now we're making changes in it by making it encapsulated and making 'the changes needed' as he said.
    I've emailed him asking what is it exactly that needs to be changed, I doubt he'd tell me but I'll keep you updated if he does.

  4. #4
    3awash Guest

    Default

    ok he replied:
    "You need to add the get methods and set methods. The set methods will be from the previous getData method."

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by 3awash View Post
    ok he replied:
    "You need to add the get methods and set methods. The set methods will be from the previous getData method."
    I'd like to argue that the three properties length, height and volume are functionally dependent, i.e. given two of them you can calculate the third one. Let's pick the volume as the functionally dependent property; you don't need a setVolume( ... ) method then. It's also arguable whether or not a negative length or height make sense. The two remaining setters should guard your object against foolish values. Your class doesn't reflect those notions.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 12:05 AM
  2. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  3. Are abstraction and encapsulation concepts not in C?
    By hitesh_public in forum New To Java
    Replies: 1
    Last Post: 08-20-2010, 02:23 PM
  4. encapsulation & constructors
    By buckskinner1776 in forum New To Java
    Replies: 4
    Last Post: 05-10-2010, 06:35 PM
  5. java-Encapsulation
    By lenah in forum Advanced Java
    Replies: 5
    Last Post: 01-05-2009, 09:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •