Proper Coding

I

ineedyourluvin1

Hello ! I've been programming in Java for about a month now and
have made much progress. Especially with Applets. So my
goal is to become a internet game programmer. You know, games
on the browser and the cellphone. In any case I've got lots of
programming experience in general with C++, VB, etc... The
thing that is hardest for me to understand is as follows :

1.) Base classes
2.) Base class implementation
3.) Derived classes from the base classes
4.) Polymorphism (does'nt that deal with the Base Class +
Implementations ? ?)

That is the first thing. The second thing is finding good examples
on the internet . I've been hacking at it for a couple days now.
I just want to make a circle appear on the screen and move it.

I make a Base Class :

///////////////////////////////////////////////////////////////////////////////////
import java.awt.*;
import java.awt.event.*; // I add many more imports anyway
import java.util.*; // by habbit......

public abstract class shape {
public abstract void setColor(Color c);
public abstract Color getColor();
public abstract move(Integer x, Integer y);
public abstract void draw(Graphics g);
}

OK ! So there is my abstract base class .

So, how would I implement something like this ??? There is a
Graphics and a Color object that caused problems while I was
hacking and this whole thing is stalling me out ! If someone can
show me a clean way to do this with 1,2,3, and 4 listed above,
I'd love it :)

Then I'd be able to get this show on the road with developing
games........... I can already write Applets and make them
work in my browser. They seem dirty in a way and I KNOW
better programming techniques are being used more commonly.
Like the list above (1,2,3, and 4) most likely!
I've gotta clean it up and make my Applets more extensible.

Thanks Much ! ;-)
 
I

ineedyourluvin1

Ok heres what I got out of those tutorials Paul. But I think this
really looks ugly!
Here goes !!!!

////////////switcher.java, the runnable one

package switchTheShapes;
/*
* switcher.java
*
* Created on March 13, 2006, 4:39 AM
*
* To change this template, choose Tools | Options and locate the
template under
* the Source Creation and Management node. Right-click the template
and choose
* Open. You can then make changes to the template in the Source
Editor.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.applet.*;
/**
*
* @author ineedyourluvin
*/
public class switcher extends Applet implements shapeDrawer, Runnable {
//private variables
private Image dbImage;//Image buffer
private Graphics dbg;//Double buffered Graphics object using
dbImage as backbuffer
private Thread th;

//public variables
public shapeDrawer sd ;
public boolean blnChangedShape;
public Font fntIntroText;
public String txtIntro;
public int intSleepTime;
public circle c = new circle();
public square s = new square();

public void init(){
setSize(640, 320);
setBackground(Color.yellow);
blnChangedShape = false;
txtIntro = "This demonstrates the code on my Usenet Post";
intSleepTime = 1000; //sleep 1 second
}

public void start(){
if(th == null){
Thread th = new Thread(this);
th.start();
}
}

public void stop(){
th = null;
}

public void run(){
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

while(true){


if(! blnChangedShape){
sd = c ;
blnChangedShape = true;

}else if(blnChangedShape){
sd = s ;
blnChangedShape = false;

}



repaint();

try{
Thread.sleep(intSleepTime);

}catch(InterruptedException ex){}

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}

public void destroy(){

}

public void paint(Graphics g){
drawIntro(txtIntro, g);
sd.drawShapes(g);
}

public void update(Graphics g){
if(dbImage == null){
dbImage = createImage(this.getSize().width,
this.getSize().height);
dbg = dbImage.getGraphics();
}

dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width,
this.getSize().height);

dbg.setColor(getForeground());
paint(dbg);

g.drawImage(dbImage, 0, 0, this);
}

public void drawIntro(String txtIntro, Graphics g){
fntIntroText = new Font("System", Font.ITALIC, 24);
g.setColor(Color.black);
g.setFont(fntIntroText);
g.drawString(txtIntro, 25, 200);

}

public void drawShapes(Graphics g){

}
}


//////// shapeDrawer , the interface for drawShapes(Graphics g)

package switchTheShapes;
/*
* shapeDrawer.java
*
* Created on March 13, 2006, 4:39 AM
*
* To change this template, choose Tools | Options and locate the
template under
* the Source Creation and Management node. Right-click the template
and choose
* Open. You can then make changes to the template in the Source
Editor.
*/
import java.awt.*;
/**
*
* @author ineedyourluvin
*/
public interface shapeDrawer {
public void drawShapes(Graphics g);
}

////////// Draw a square

package switchTheShapes;
/*
* square.java
*
* Created on March 13, 2006, 5:48 AM
*
* To change this template, choose Tools | Options and locate the
template under
* the Source Creation and Management node. Right-click the template
and choose
* Open. You can then make changes to the template in the Source
Editor.
*/
import java.awt.*;
/**
*
* @author ineedyourluvin
*/
public class square implements shapeDrawer{

public void drawShapes(Graphics g){
g.setColor(Color.blue);
g.drawRect(35, 45, 60, 60);
g.fillRect(35, 45, 60, 60);
}

}


///////// Draw a circle

package switchTheShapes;
/*
* circle.java
*
* Created on March 13, 2006, 4:47 AM
*
* To change this template, choose Tools | Options and locate the
template under
* the Source Creation and Management node. Right-click the template
and choose
* Open. You can then make changes to the template in the Source
Editor.
*/
import java.awt.*;
/**
*
* @author ineedyourluvin
*/
public class circle implements shapeDrawer {

public void drawShapes(Graphics g){
g.setColor(Color.red);
g.drawOval(40,25, 35, 35);
g.fillOval(40,25, 35, 35);
}
}

I Hope this will help everyone understand where I'm at now on this. I
did get *a little* better
and have somewhat better understanding of interfaces. Interfaces DO
things right ?

Thanks much !
 
I

ineedyourluvin1

Ok heres what I got out of those tutorials Paul. But I think this
really looks ugly!
Here goes !!!!

////////////switcher.java, the runnable one

package switchTheShapes;
/*
* switcher.java
*
* Created on March 13, 2006, 4:39 AM
*
* To change this template, choose Tools | Options and locate the
template under
* the Source Creation and Management node. Right-click the template
and choose
* Open. You can then make changes to the template in the Source
Editor.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.applet.*;
/**
*
* @author ineedyourluvin
*/
public class switcher extends Applet implements shapeDrawer, Runnable {
//private variables
private Image dbImage;//Image buffer
private Graphics dbg;//Double buffered Graphics object using
dbImage as backbuffer
private Thread th;

//public variables
public shapeDrawer sd ;
public boolean blnChangedShape;
public Font fntIntroText;
public String txtIntro;
public int intSleepTime;
public circle c = new circle();
public square s = new square();

public void init(){
setSize(640, 320);
setBackground(Color.yellow);
blnChangedShape = false;
txtIntro = "This demonstrates the code on my Usenet Post";
intSleepTime = 1000; //sleep 1 second
}

public void start(){
if(th == null){
Thread th = new Thread(this);
th.start();
}
}

public void stop(){
th = null;
}

public void run(){
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

while(true){


if(! blnChangedShape){
sd = c ;
blnChangedShape = true;

}else if(blnChangedShape){
sd = s ;
blnChangedShape = false;

}



repaint();

try{
Thread.sleep(intSleepTime);

}catch(InterruptedException ex){}

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}

public void destroy(){

}

public void paint(Graphics g){
drawIntro(txtIntro, g);
sd.drawShapes(g);
}

public void update(Graphics g){
if(dbImage == null){
dbImage = createImage(this.getSize().width,
this.getSize().height);
dbg = dbImage.getGraphics();
}

dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width,
this.getSize().height);

dbg.setColor(getForeground());
paint(dbg);

g.drawImage(dbImage, 0, 0, this);
}

public void drawIntro(String txtIntro, Graphics g){
fntIntroText = new Font("System", Font.ITALIC, 24);
g.setColor(Color.black);
g.setFont(fntIntroText);
g.drawString(txtIntro, 25, 200);

}

public void drawShapes(Graphics g){

}
}


//////// shapeDrawer , the interface for drawShapes(Graphics g)

package switchTheShapes;
/*
* shapeDrawer.java
*
* Created on March 13, 2006, 4:39 AM
*
* To change this template, choose Tools | Options and locate the
template under
* the Source Creation and Management node. Right-click the template
and choose
* Open. You can then make changes to the template in the Source
Editor.
*/
import java.awt.*;
/**
*
* @author ineedyourluvin
*/
public interface shapeDrawer {
public void drawShapes(Graphics g);
}

////////// Draw a square

package switchTheShapes;
/*
* square.java
*
* Created on March 13, 2006, 5:48 AM
*
* To change this template, choose Tools | Options and locate the
template under
* the Source Creation and Management node. Right-click the template
and choose
* Open. You can then make changes to the template in the Source
Editor.
*/
import java.awt.*;
/**
*
* @author ineedyourluvin
*/
public class square implements shapeDrawer{

public void drawShapes(Graphics g){
g.setColor(Color.blue);
g.drawRect(35, 45, 60, 60);
g.fillRect(35, 45, 60, 60);
}

}


///////// Draw a circle

package switchTheShapes;
/*
* circle.java
*
* Created on March 13, 2006, 4:47 AM
*
* To change this template, choose Tools | Options and locate the
template under
* the Source Creation and Management node. Right-click the template
and choose
* Open. You can then make changes to the template in the Source
Editor.
*/
import java.awt.*;
/**
*
* @author ineedyourluvin
*/
public class circle implements shapeDrawer {

public void drawShapes(Graphics g){
g.setColor(Color.red);
g.drawOval(40,25, 35, 35);
g.fillOval(40,25, 35, 35);
}
}

I Hope this will help everyone understand where I'm at now on this. I
did get *a little* better
and have somewhat better understanding of interfaces. Interfaces DO
things right ?

Thanks much !
 
B

BWill

Hello ! I've been programming in Java for about a month now and
have made much progress. Especially with Applets. So my
goal is to become a internet game programmer. You know, games
on the browser and the cellphone. In any case I've got lots of
programming experience in general with C++, VB, etc... The
thing that is hardest for me to understand is as follows :

1.) Base classes
2.) Base class implementation
3.) Derived classes from the base classes
4.) Polymorphism (does'nt that deal with the Base Class +
Implementations ? ?)

That is the first thing. The second thing is finding good examples
on the internet . I've been hacking at it for a couple days now.
I just want to make a circle appear on the screen and move it.

I make a Base Class :

///////////////////////////////////////////////////////////////////////////////////
import java.awt.*;
import java.awt.event.*; // I add many more imports anyway
import java.util.*; // by habbit......

public abstract class shape {
public abstract void setColor(Color c);
public abstract Color getColor();
public abstract move(Integer x, Integer y);
public abstract void draw(Graphics g);
}

OK ! So there is my abstract base class .

So, how would I implement something like this ??? There is a
Graphics and a Color object that caused problems while I was
hacking and this whole thing is stalling me out ! If someone can
show me a clean way to do this with 1,2,3, and 4 listed above,
I'd love it :)

Then I'd be able to get this show on the road with developing
games........... I can already write Applets and make them
work in my browser. They seem dirty in a way and I KNOW
better programming techniques are being used more commonly.
Like the list above (1,2,3, and 4) most likely!
I've gotta clean it up and make my Applets more extensible.

Thanks Much ! ;-)

If you understand inheritance, you can understand abstract classes. You
can't make objects of an abstract class but you can inherit from it. The
inside of an abstract class can be just like any other class but for one
difference--it can contain abstract methods. Including abstract methods
in an abstract class dictates that all children of your abstract base
class must provide an actual method with that signature and return type.
If you're not comfortable with the idea, there's little reason for you
to be making base classes in your own code.

What Java calls an 'interface' is declared like a class but with the
interface keyword; the only kind of thing that goes in an interface are
public abstract methods (again, which are just signatures and return
types: you don't wrtie actual bodies of code for them). When your class
implements an interface (with the implements keyword), you must write
actual methods for every abstract method in the interface. So the
interface is like a contract that says 'every class that implements me
must implement all the methods listed in me'.

So far, interfaces get you jack squat you couldn't get without a few
post-it notes reminding you to include certain methods in a class if its
supposed to be able to do those things. The real usefulness of
interfaces is that you can declare references of an interface type, e.g.
given an interface called Rideable, I could make a Rideable reference:

Rideable r;

This reference can be assigned any object which is of a class which
implements Rideable (or of a subclass of a class which implements
Rideable). The reference typing rules ensure that we can call one of the
methods listed in the Rideable interface via 'r' and always be sure that
the object referenced has that method.

Interfaces also serve as Java's compromise with multiple inheritance: a
class can implement multiple interfaces and therefore can be said to
take on the abilities of multiple kinds of things (though nothing in the
language even encourages two classes implementing the same abstract
method to do anything remotely like the other, so interfaces represent
rather weak kind of multiple inheritance).

In sum, dont' sweat making abstract classes or interfaces in your own
code. You're just one man working on one project and you're just
starting, so presumably the diagram of the number of classes you write
should be quite small. Still, both of these language features play a
prominent role in the standard libraries, so you should understand what
they are and how to use them--just don't think you need to write your
own yet.
 
A

Alex Hunsley

BWill said:
Including abstract methods
in an abstract class dictates that all children of your abstract base
class must provide an actual method with that signature and return type.

Not quite: extending classes can choose to implement only some of the
superclass abstract method, and also be abstract. So you can have
several base classes, some base classes specialising other base classes
with partial implementation and being abstract.

Example:

abstract class BaseClassA {
public abstract void doThing();
public abstract int getSomething();
}

abstract class BaseClassB extends BaseClassA {
public void doThing() {
System.out.println("doThing() called\n");
System.out.println(" - I called getSomething() and got "
+ getSomething());
}

public abstract int getSomething();
}

public class ConcreteClass extends BaseClassB {
public int getSomething() {
return 7;
}

public static void main(String[] args) {
ConcreteClass concreteClass = new ConcreteClass();
concreteClass.doThing();
}
}
 
I

ineedyourluvin1

Paul, thankyou very much. The sites you mentioned which helped alot are
in
my Favs for continual use until I'm comfortable with those. Ok..
BWill and Alex now...

First, BWill, if you are still checking this thread.
What I've gotten out of your post is that I need to understand and know
how to use Interfaces and Abstract Classes but are really not important
YET. I take it I'm going to be needing them in the near future when
tasks get more complex to make my life easier. You gave the
example of the Interface rideable and making a reference to it
called r , ( Rideable r ; ) . So let us say I made :

//Create a bike class , bike.java
public class bike implements Rideable {
// First I will Override the Rideable methods , I'll
// guess what they are ;-)

// Private Variables

// Public Variables

public bike(){

}

public void accelerate() {

}

public void decelerate() {

}

public void breaks(boolean pressed){

}

// OK that implements the Rideable methods
// It's based on the idea that all Rideable things can
// accelerate, decelerate, and have breaks to stop with.
// Therefore a bike is Rideable ! ;-)

} // End bike class

////////////////////////////////////////////////////////////////////////////////////////////////

//Choose a vehicle , chooseVehicle.java
public class chooseVehicle implements Rideable {
//Private Variables
private Rideable r ;

//Public Variables
public int intVehicleSelection ;

public chooseVehicle(){
}




public static void main(String args[]) {
intVehicleSelection = 0;

//Some code to handle stuff

whichVehicle(intVehicleSelection);
}

public void whichVehicle(int vehicle){

switch(vehicle){

//These are all bikes

case 0: r = bmxDirtbike ;
break;

case 1: r = kawasakiCrotchRocket;
break;

}

}
}

So to sum up this code, BWill , a BMX Dirtbike and a kawasaki are
both bikes and all bikes are Rideable. Is this the MAIN case
where interfaces are always used in the professional world ?
Does it look like I understand the concept and how to use it ?
I'm sorry about just typing code up on the fly in this thread
and not writing a full program with the actual Rideable
Interface code also but we should all know what the Rideable
Interface should look like right ? Not sure if Rideable r ; should
be private but ill start hacking later tonight ;-) Thanks much ....

So, Alex if you are here, I dig what you did with the
BaseClassA extends BaseClassB . I'm going to study
that one further because that looks to me like it could
shorten development time and simplify things (just what
I want). Reason being you implemented a method
( doThing() ) in BaseClassB ! Then you made
ConcreteClass which extends BaseClassB which
extends BaseClassA and implemented int getSomething()
which called doThing() in BaseClassB which has
a return type of int and got 7 passed to it. int getSomething()
was overridden in ConcreteClass because it is
abstract and needed to be implemented so when
you did ConcreteClass concreteclass = new ConcreteClass() ;
and because ConcreteClass is a BaseClassA and a BaseClassB,
you were just able to do concreteClass.doThing and the
doThing method in BaseClassB was able to print the
value stored in int getSomething()'s return value !
Very neat ! Very elegant !

I might have gone off on a tangent and screwed up somewhere
but do you all think that I might have a good idea now of this
complex subject of object orientation that almost nobody
can grasp ? (I say that cuz all my class mates in college
had a very hard time with this type of thing and we were
learning C++) .

Thanks much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top