Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts

    Small simple Java program using objects and classes?

    Does any one know of a small simple Java program that uses objects and classes that works? I would like to analyze the source. I am following a few guides, but if I could get a working program, that would be great.

    So, if any one know where I can get it, please say so.

    Thanks!
    Last edited by bobleny; 05-29-2007 at 03:12 PM.
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    It would help to know in which language you were looking for an example in. e.g. C++, Java, C#, etc.
    OracleGuy

  • #3
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts
    What do you mean, I said Java....
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by bobleny View Post
    What do you mean, I said Java....
    Yeah, as of 2 minutes ago, lol.
    OracleGuy

  • #5
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Yeah, stupid mistake. I meant to, but I forgot... I don't know how....

    It would help. All of the guides, I've found have been this is what it looks like, but if you use what they give you, it doesn't work. I might be doing something wrong. Thats why I would like a simple Java program with objects and classes.

    I was able to do the hello world thing....
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...

  • #6
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    Classes should be based on real world objects, like boats, cars, buildings, modems, cables, cameras etc. For example, a camera can store pictures and take pictures, export pictures to a pc and probably have some method of scrolling through the images that are inside it. A camera class would look something like this:
    Code:
    /**
     * Class to represent a camera.
     */
    public class Camera
    {
      private int storedPictures;
      private Picture[] pictures;
    
      /**
       * Creates a camera that can contain a certain number of images.
       *
       * @param capacity Number of pictures the camera can store.
       */
      public Camera(int capacity)
      {
        // Set up pictures array and set the number of pictures taken to 0
        pictures = new Picture[capacity];
        storedPictures = 0;
      }
    
      /**
       * @return the capacity of the camera.
       */
      public int getCapacity()
      {
        // The capacity of the camera is the size of the picture array
        return pictures.length;
      }
    
      /**
       * Take a picture and store it in the camera.
       *
       * @param picture The picture to store.
       * @throws CameraOutOfSpaceException when the camera does not have 
       */
      public void takePicture(Picture picture) throws CameraOutOfSpaceException
      {
        // Check that there is enough space to store the picture.
        if(storedPictures + 1 >= getCapacity())
          throw new CameraOutOfSpaceException("Camera out of space");
    
        // Store the picture and increase the number of stored pictures.
        pictures[storedPictures++] = picture;
      }
    
      ...
    }
    You should be able to see how this is useful, if you wanted to set up 10 cameras to take pictures of different things you could just do something like
    Code:
    Camera lobbyCamera = new Camera(200);
    lobbyCamera.takePicture(...);
    ...
    If you have captured a Camera's behaviour and attributes properly you should be able to use the Camera class in every situation where you would be able to use a Camera in real life. If you later wanted to change some things, for example to make a NightVisionCamera or an InfraredCamera you could subclass a camera and just change the takePicture method to take pictures in different ways, for example. You could then have a bunch of cameras set up in different places regardless of whether they were night vision, infrared or normal cameras and get each of them to take a picture in exactly the same way by refering to all of them as a Camera.

    Note that I was too lazy to write a lot of the code so not everything is there, there is no CameraOutOfSpaceException class or Picture class and it needs both of them to work, but I didn't write them.

    EDIT: I just had a thought, probably the simplest class you could write that would make a whole program would be a Dice (Die? - class names shouldn't really be plural) class. All you would have to do is have a public int roll() { ... } that returns a randomly generated number from 1 to 6 (if it was a fair 6 sided dice, if not you could subclass it with a 20 sided dice, an unfair dice or give it a number of sides in the constructor)

    You could then use this dice class in any dice based game, or even just a simple program that rolls a dice 20 times and adds up the results or something. You could also use multiple dice and check for double 6s or whatever.
    Last edited by ghell; 05-30-2007 at 07:19 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
    •