{"id":23553042,"url":"https://github.com/rhildred/greenfoot-5thingsaboutobjects","last_synced_at":"2025-05-15T17:30:21.534Z","repository":{"id":146397134,"uuid":"49145633","full_name":"rhildred/Greenfoot-5thingsAboutObjects","owner":"rhildred","description":"Objects-first Introduction to programming","archived":false,"fork":false,"pushed_at":"2016-02-22T02:46:44.000Z","size":31,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"gh-pages","last_synced_at":"2024-12-26T11:14:31.577Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rhildred.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-06T16:04:58.000Z","updated_at":"2016-01-17T04:42:32.000Z","dependencies_parsed_at":"2023-05-06T04:46:20.985Z","dependency_job_id":null,"html_url":"https://github.com/rhildred/Greenfoot-5thingsAboutObjects","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhildred%2FGreenfoot-5thingsAboutObjects","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhildred%2FGreenfoot-5thingsAboutObjects/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhildred%2FGreenfoot-5thingsAboutObjects/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rhildred%2FGreenfoot-5thingsAboutObjects/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rhildred","download_url":"https://codeload.github.com/rhildred/Greenfoot-5thingsAboutObjects/tar.gz/refs/heads/gh-pages","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239293947,"owners_count":19615041,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-26T11:13:54.535Z","updated_at":"2025-02-17T12:41:58.903Z","avatar_url":"https://github.com/rhildred.png","language":"Java","readme":"# 3 things about objects ... [introducing Greenfoot](https://github.com/rhildred/Greenfoot-5thingsAboutObjects)\n\nI worked in banking for a number of years. I had a bank account when I started, but I had to learn or at least apply a new level of focus to the kinds of things/objects that bankers deal with.\n\n###Account\n\nAs I said I had a bank account. Accounts have properties and behaviors. An account has a balance, and a primary account number property. You can deposit and withdraw from an account.\n\n##Thing 1 \n\nObjects are made of behavior and properties **encapsulated** together. An instance of an object like account 12345 belongs to a class of objects. The class of the object is like a template that a new object is created from. When an instance of an object is created from a template there is a special behavior `new` that is performed. In Java when `new` is performed to make a new instance of an object a method with the same name as the class is called automatically.\n\n###Customer\n\nWhen I started writing banking software I had been a customer of a bank since high school already. I actually had more than 1 bank account, a chequeing account, savings and Mastercard.\n\n## Thing 2\n\nObjects are interrelated. A customer can have more than 1 account. You don't actually have a plain account as a customer, what you have is a specialization of an account, like a savings account. An account is a generalization from which specializations **inherit** properties like a balance, an account number .... There are 2 ways an object can be related to an object:\n1. aggregation - a customer has 1 - infinity accounts\n2. **inheritance** - an account is a generalization of all of the different types of accounts like savings chequeing  ...\n\n###Using an Account\n\nWhen I make a withdrawal from my Visa, the account checks to make sure I have sufficient credit limit to cover the withdrawal. When I use Interac with my chequeing account, the account is checked to make sure that my balance is sufficient to cover the withdrawal.\n\n##Thing 3\n\nTo use an object, I perform some behavior on it, in the above case withdrawing. Depending on the specialization of the account, I do withdrawing in slightly different ways. Performing the same behavior with an object in different specialized ways is called **polymorphism**.\n\n#My First Game\n\nIt was after my banking software days were over that I wrote my first game as a teaching tool for Javascript. It was recently enough that serious games were starting to become important and this game was to help people with a learning disability. Like many games, my game had a playing surface, a `world` or board on which it was played. It also had actors in this world, some controlled by the computer, and some controlled by the player.\n\n##World\n\nThe world is the board that the game is played on. In [Greenfoot](http://www.greenfoot.org/), the gaming environment that runs [this repository](https://github.com/rhildred/Greenfoot-5thingsAboutObjects), you make your world by specializing or extending a World object that is built in to Greenfoot. \n\n![From Greenfoot Tutorial](http://www.greenfoot.org/images/tutorials/wombat/scenario-main.png?1325954072 \"From Greenfoot Tutorial\")\n\nGenerally a Greenfoot world creates the actors in that world:\n\n```\n    /**\n     * Prepare the world for the start of the program.\n     * That is: create the initial objects and add them to the world.\n     */\n    private void prepare()\n    {\n        PoisonousShroom poisonousshroom = new PoisonousShroom();\n        addObject(poisonousshroom,1,2);\n        PoisonousShroom poisonousshroom2 = new PoisonousShroom();\n        addObject(poisonousshroom2,6,5);\n        Leaf leaf = new Leaf();\n        addObject(leaf,4,0);\n        Leaf leaf2 = new Leaf();\n        addObject(leaf2,3,6);\n        Leaf leaf3 = new Leaf();\n        addObject(leaf3,5,3);\n        Wombat wombat = new Wombat();\n        addObject(wombat,1,0);\n    }\n\n```\n\nYou can get Greenfoot to write the `prepare` method by creating new actors on the world and using the`Controls/Save The world` menu item. \n\n##Actor\n\nA Greenfoot actor, like World, is a specialization of a built in actor. The built in actor can `act` every once in a while. Each actor knows how to act for themselves (polymorphism).\n\n\n##Making the wombat scenario more \"gamey\"\n\nThe poor wombat in [tutorial 1](http://www.greenfoot.org/doc/tut-1) could only reach food that was on the edge of the screen. I added a method that let the user show the wombat where to go by tapping or mouse clicking past the wombat in the direction that you are leading him:\n\n```\n\n    private void optChangeDirection() {\n\t\tif (Greenfoot.mousePressed(null)) {\n\t\t\tMouseInfo mouse = Greenfoot.getMouseInfo();\n\t\t\tint px = mouse.getX();\n\t\t\tint py = mouse.getY();\n\t\t\tint hx = this.getX();\n\t\t\tint hy = this.getY();\n\t\t\tswitch (direction) {\n\t\t\tcase WEST:\n\t\t\tcase EAST:\n\t\t\t\tif (py \u003c= hy) {\n\t\t\t\t\tsetDirection(NORTH);\n\t\t\t\t} else {\n\t\t\t\t\tsetDirection(SOUTH);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase NORTH:\n\t\t\tcase SOUTH:\n\t\t\t\tif (px \u003c= hx) {\n\t\t\t\t\tsetDirection(WEST);\n\t\t\t\t} else {\n\t\t\t\t\tsetDirection(EAST);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n```\n\n##Poisonous Shrooms\n\nThe other thing that I added to the wombat scenario was poisonous shrooms. To do that I needed a few steps. I needed to generalize leaves to be food, generally non poisonous.\n\n```\nimport greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)\n\n/**\n * Write a description of class Food here.\n * \n * @author (your name) \n * @version (a version number or a date)\n */\npublic class Food extends Actor\n{\n    \n    public boolean bPoisonous(){\n        return false;\n    }\n}\n```\n\nThen I made Leaf extend Food rather than actor:\n\n```\n\nimport greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)\n\n/**\n * Leaf - a class for representing leafs.\n *\n * @author Michael Kolling\n * @version 1.0.1\n */\npublic class Leaf extends Food\n{\n}\n\n```\n\nThen I made my new PoisonousShroom class:\n\n```\nimport greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)\n\n/**\n * Write a description of class PoisonousShroom here.\n * \n * @author (your name) \n * @version (a version number or a date)\n */\npublic class PoisonousShroom extends Food\n{\n    public boolean bPoisonous(){\n        return true;\n    }\n}\n\n```\n\nFinally I changed the Wombat code to take advantage of the new structure:\n\n```\n    /**\n     * Check whether there is a leaf in the same cell as we are.\n     */\n    public boolean foundFood()\n    {\n        Actor food = getOneObjectAtOffset(0, 0, Food.class);\n        if(food != null) {\n            return true;\n        }\n        else {\n            return false;\n        }\n    }\n    \n    /**\n     * Eat a leaf.\n     */\n    public void eatFood()\n    {\n        Food food = getOneObjectAtOffset(0, 0, Food.class);\n        if(food != null) {\n            // eat the leaf...\n            World world = getWorld();\n            if(food.bPoisonous()){\n                world.removeObject(food);\n                world.removeObject(this);\n                Greenfoot.stop();\n            }else{\n                world.removeObject(food);\n                leavesEaten = leavesEaten + 1; \n            }\n        }\n    }\n```\n\nI hope that you will agree that this makes the Wombat more of a game.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frhildred%2Fgreenfoot-5thingsaboutobjects","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frhildred%2Fgreenfoot-5thingsaboutobjects","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frhildred%2Fgreenfoot-5thingsaboutobjects/lists"}