...

View Full Version : Small simple Java program using objects and classes?



bobleny
05-29-2007, 04:10 AM
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!

oracleguy
05-29-2007, 07:04 AM
It would help to know in which language you were looking for an example in. e.g. C++, Java, C#, etc.

bobleny
05-29-2007, 03:12 PM
What do you mean, I said Java.... :p

oracleguy
05-29-2007, 03:14 PM
What do you mean, I said Java.... :p

Yeah, as of 2 minutes ago, lol.

bobleny
05-29-2007, 03:42 PM
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....

ghell
05-30-2007, 07:04 PM
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:
/**
* 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
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum