Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Struggling with Objects and classes

  • 24-11-2012 9:57pm
    #1
    Closed Accounts Posts: 3,596 ✭✭✭


    Hi

    Im in second year programming and its all Object Orientated programming, im really struggling, does anyone know of any books or websites that can help me, we had an assessment on Tuesday and i definitely got zero in it.


    forgot to add, its java im studying


Comments

  • Closed Accounts Posts: 799 ✭✭✭Logical_Bear


    what language?there's loads of online tutourials and guides


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    what language?there's loads of online tutourials and guides

    Java, Im ok with one class, but when its gets to multiple classes, I struggle bigtime


  • Registered Users, Registered Users 2 Posts: 92 ✭✭jgh_


    threein99 wrote: »

    Java, Im ok with one class, but when its gets to multiple classes, I struggle bigtime

    Can you clarify what it is you're struggling with? Are there specific concepts that are tripping you up?


  • Registered Users, Registered Users 2 Posts: 8,366 ✭✭✭batistuta9


    threein99 wrote: »
    Java, Im ok with one class, but when its gets to multiple classes, I struggle bigtime

    In what way do you find you're struggling with the multiple classes exactly?

    Is it do with inheritance or composition or is it something else that you have difficulty with.

    If you're OK working with one class it'll not be too difficult working with multiple classes once you get your head around it but it'll be easier for people to explain to you when they know what you want to know exactly

    This site http://thenewboston.org/tutorials.php is quite good for programming, it's on youtube too if you want to use that instead.

    But i've no idea really how well it explains multiple classes & the relationships between them on it, i haven't used it for that


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    My college had great ways of explaining these things...

    A car class is a blueprint of a car. A car object is a car created based on that blueprint.

    Actually I think that "head first java" book explains things like that. OP give that book a try, it not only teaches you java, it teaches you OOP aswell.


  • Advertisement
  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    You're probably already sick of reading metaphors about cars and fruit, I know I was... the best thing to do would be to start a small project to explore how it actually works on the ground.

    Here's a good project:
    Have 4 robots fight each other to the death.
    Each robot will have a different name, colour, their own number of health points and their attacks do different levels of damage.
    They will each have a unique special attack.
    The fight will be managed by a fight-manager (or referee) class, with methods to add() new robots before the fight begins and a doturn() method that tells the participating robots to select a random opponent and attack it once using a random move.
    Print the moves each robot does on their turn, who they do it to and how much health they have left. Continue this until there is one robot left standing.

    Attempt this, then if you get stuck anywhere, you'll know what your question is. Also when you're reading about OOP, you'll have a better mental model to plug this new information into... instead of trying to understand layers upon layers of abstract car and fruit metaphors. It becomes more interesting too, when you have a project where you can make use of the things you're reading about.

    The thing about these metaphors is they make perfect sense when you already understand OOP, but unless you get your hands dirty with it little by little, you're left juggling too many metaphors at once and it's incomprehensible.


  • Registered Users, Registered Users 2 Posts: 1,686 ✭✭✭RealistSpy


    Like what DonkeyStyle \o/ said you need to actually practice. I believe that's the best way to learn and trust me, objects and classes are very very important. Practice and debug is the answer to your problem.


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    You're probably already sick of reading metaphors about cars and fruit, I know I was... the best thing to do would be to start a small project to explore how it actually works on the ground.

    Here's a good project:
    Have 4 robots fight each other to the death.
    Each robot will have a different name, colour, their own number of health points and their attacks do different levels of damage.
    They will each have a unique special attack.
    The fight will be managed by a fight-manager (or referee) class, with methods to add() new robots before the fight begins and a doturn() method that tells the participating robots to select a random opponent and attack it once using a random move.
    Print the moves each robot does on their turn, who they do it to and how much health they have left. Continue this until there is one robot left standing.

    Attempt this, then if you get stuck anywhere, you'll know what your question is. Also when you're reading about OOP, you'll have a better mental model to plug this new information into... instead of trying to understand layers upon layers of abstract car and fruit metaphors. It becomes more interesting too, when you have a project where you can make use of the things you're reading about.

    The thing about these metaphors is they make perfect sense when you already understand OOP, but unless you get your hands dirty with it little by little, you're left juggling too many metaphors at once and it's incomprehensible.

    Thanks for that will give that ago, heres the question from the test we had during the week, I literally didnt know where to begin with it:

    Create a class called building that maintains the name of the building, the address of the building, the size of the building in square metres, details regarding the manager of the building and the rooms that are in the building. The manager information includes the name of the manager, the home address of the manager and the manager’s salary. A building can have at most five rooms, but may have no rooms. The number of each room, its length, width and height must be maintained. Given the attached class BuildingDriver.java, write the required classes providing constructors and other methods as appropriate (e.g. accessor methods).

    Modify the BuildingDriver class (clearly indicating the new code using comments as shown in the code below) to create a new building called “The College” which has five rooms where two are roomOne, two are roomTwo and one is roomThree. You are the manager of the new college.

    Include detailed comments in your code as you deem necessary.

    Marks will be awarded for adhering to best programming practices (good use of comments, naming conventions, using functions etc.).





    import java.io.*;

    public class BuildingDriver{
    public static void main (String args[])
    {
    // create three Manager objects
    Manager joe = new Manager("Joe", "Joe's House, Barcelona", 10000f);
    Manager peter = new Manager("Peter", "Pete's Place, Paris", 23456f);
    Manager bertie = new Manager("Bertie", "Berties House,Dublin",33444f);

    // create three Room objects
    Room roomOne = new Room(1, 10f, 10f, 10f);
    Room roomTwo = new Room(2, 20f, 20f, 10f);
    Room roomThree = new Room(3, 30f, 30f, 15f);

    //put the rooms in an array so that they can be added to a building
    Room[] rooms = new Room[3];
    rooms[0] = roomOne;
    rooms[1] = roomTwo;
    rooms[2] = roomThree;

    // create three Building objects each one with a manager
    Building library = new Building("Public Library","Tullow Street,
    Carlow", 3000, joe);
    Building house = new Building("","French", 120, peter);

    // notice the different constructor called here
    Building museum = new Building("National Museum of Ireland","Some
    Street, Dublin", 5000, bertie, rooms);

    //print the details of the Building objects
    System.out.println (library.toString());
    System.out.println (house.toString());
    System.out.println (museum.toString());

    //add your extra driver class code here…
    //…
    //…end of code that you have added
    }
    }


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    jgh_ wrote: »
    Can you clarify what it is you're struggling with? Are there specific concepts that are tripping you up?

    I think my biggest problem is with the mutator methods


  • Registered Users, Registered Users 2 Posts: 27,370 ✭✭✭✭GreeBo


    When I started I used to think of every noun in the problem as requiring its own class (unless its a primitive)
    So if you have a noun that has other nouns as part of its description, then you need to create more classes to store that information.
    e.g.
    Write the classes to be used in a system that will be used in a manufacturing factory. The factory creates its own engines and seats for its cars. It also paints the cars. Car owners can choose from 2 different types of wheels for their car.
    Reading that Im thinking I need:
    1) A car class (To describe the car)
    2) An engine class (To describe the engine)
    3) A seat class (to describe the seats)
    4) A Wheel class (To describe the wheels)

    Now we know that a car will have an engine, some seats and some wheels, so that means that inside the car class you need to have references to the engine, seats and wheels.
    The wheels, engine and seats dont care what car they are in, they just need to be able to listen and respond to orders.
    The orders you can give are the public methods (accessors) of the class.
    For example a seat needs to be able to move forwards and backwards, so you might have methods called moveForward(int amount) and moveBackwards(int amount).
    If the car wants the seat to move forward 2 "places" it calls the moveForward() method on the seat in question and passes it the value 2. Now the car doesnt care how the seat moves this 2 places, so long as it does. To accomplish this movement the seat class will probably have some internal methods,(say, unlock seat, lock seat) that are private. No one but the class itself can call these methods. It would be dangerous if someone could call the "unlockSeat()" method, only the seat knows when it should lock/unlock to only it can do it.
    The description says that we can have 2 types of wheels, so maybe we need to have two classes for this.
    Lets say we have a StandardWheels() class and a SuperWheels() class. Both of these types of wheels will need to respond to the same orders (move forwards, backwards, stop) A nice way to ensure this is to define an interface called Wheel that defines the types of thing *Any* Wheel should be able to do. Now when defining our two wheel types we can say that they "implement" the Wheel interface. This means that you know exactly what you can ask these wheels to do and that they must respond to these things.


    So if you re-read the task you got in college, what classes would you create?
    (Remembering that objects are just instances of classes, at the risk of throwing yet another analogy at you, a class tells you what type of thing you are dealing with. An object tells you the specifics of this single, particular thing)

    /edit
    Perhaps this is too much to be throwing at you in one go, so feel free to ask questions.
    btw
    A mutator just means that it changes some property about the class.
    e.g.
    the car class has 2 methods:
    getColour() that tells you what colour the car is (An accessor, it accesses something about the class)
    setColour(Colour theColour) that sets the colour of the car, the car doesnt know what colour it is until you tell it, or change it. (This is a mutator, it is mutating (changing) some property of the class)


  • Advertisement
Advertisement