Jump to content

HELP!?


Madi

Recommended Posts

Ok im freaking stuck with my Java lab and i need help lol, im suppose to show 2 examples of inheritance, and 2 levels of composition, does anyone know how to do this? ill post my stupid code if some one does....

Link to comment
Share on other sites

inheritance eh

 

only type of inheritence I know of is with C# (critically acclaimed as a big rip off of java) and C++

 

with C# it's usually

 

class NameOfClass: NameOfOtherClass
{
    private/public <datatype> PropertyName;
    NameOfPropertyFromOtherClass = SomeValue;

    void NameOfFunction()
    {
     }

     void NameOfFuntionFromOtherClass()
     {
     }
}

I believe that you cannot inherit functions and properties that are sealed or private; I think protected properties can be inherited but not accessed from outside the class

 

...something like that

 

edit: btw, the "<>" in this example are not to denote templates :thumbsup1:

Edited by Weirdy
Link to comment
Share on other sites

Do not fear, someboddy is here! I suppose you only have one lab per week, so I'm not too late.

 

I will not write all the setters and getters. Only the important stuff.

 

Lets say we have a class Person:

public class Person
{
private String id;
private String name;
private int age;
public Person(String id,String name,int age)
{
 this.id=id;
 this.name=name;
 this.age=age;
}
public String toString()
{
 return "name:"+name+"\nid:"+id+"\nage:"+age;
}
}

 

Now, lets create a Student class inheriting Person:

public class Student extends Person
{
private String university;
private int year;
public Student(String id,String name,int age,String university,int year)
{
 super(id,name,age);
 this.university=university;
 this.year=year;
}
public String toString()
{
 return super.toString()+"\nyear number "+year+" at "+university;
}
}

 

Now, since I hate to work hard, I will mix the second example of inheritance with one of the composition levels. We will have a MarriedPerson class:

public class MarriedPerson extends Person
{
private Person marriedTo;
public MarriedPerson(String id,String name,int age,Person marriedTo)
{
 super(id,name,age);
 this.marriedTo=marriedTo;
 if(marriedTo instanceof MarriedPerson)
  ((MarriedPerson)marriedTo).marriedTo=this;
}
public String toString()
{
 return super.toString()+"\nmarried to "+marriedTo.getName();
}
}

Now, note that if I'd wrote in the toString function return super.toString()+"\nmarried to "+marriedTo.getName(); and marriedTo is an instanceof MarriedPerson, the function will cause an endless recursion.

 

----------------------------------------------------------------------------

 

About the other level of composition, we will have a Point2D class and a Circle class

 

Here is the Point2D class:

public class Point2D
{
private double x;
private double y;
public Point2D(double x,double y)
{
 this.x=x;
 this.y=y;
}
public Point2D(Point2D source)//copy constructor
{
 this(source.x,source,y)
}
}

 

And here is the Circle class:

public class Circle
{
private Point2D center;
private double radius;
public void setCenter(Point2D center)
{
 if(center!=null)
  this.center=new Point2D(center);
}
public Circle(Point2D center,double radius)
{
 setCenter(center);
 this.radius=radius;
}
}

 

Thats it, nice and simple.

---------------------------------------------------------------------------

 

I know you are only at the beginning of learning JAVA, but when you feel experienced enough to program games, feel free to try jmonkeyengine.com.

 

You might also find help in gamedev.net's forums.

 

And for the next time,

D O - Y O U R

H O M E W O R K

Y O U R S E L F

Link to comment
Share on other sites

E:D:I:T

heres my horrible code, were suppose to draw something and i can get everything but the speakers to show up

 

// Crash.java

import java.util.Random;

import java.util.*;

import java.awt.*;

import java.applet.*;

 

 

public class Crash extends Applet

{

public void paint(Graphics g)

{

 

  Mac mac = new Mac(g,150,100,500,300);

}

}

 

class Mac extends Computer

  private int tX; //Top X coordinate

  private int tY; // Top  Y coordinate

    private int width; // width of the computer

    private int height; // height of the computer

    Speakers speakers;

 

    public Mac(Graphics g, int x, int y, int w, int h)

      super(g,x,y,w,h);

    tX = x;

    tY = y;

    width = w;

    height = h;

    speakers = new Speakers(g,x,y,w,h);

   

   

}

 

}

 

class Computer

   

private int tX;

private int tY;

private int mW; //Monitor width

private int mH;//Monitor height

private int sX; //screen X

private int sY;//screen Y

private int sW;//screen width

private int sH;//screen height

    Keyboard keyboard;

   

public Computer(Graphics g,int x,int y,int w,int h)

 

    tX = x;

    tY = y;

    mW = w;

    mH = h;

    sX = x+50;

      sY = y+15;

      sW = w-100;

      sH = h-30;

      keyboard = new Keyboard(g,x,y,w,h);

      drawMonitor(g);

    drawScreen(g);

 

   

    }

    public void drawMonitor(Graphics g)

    {   

      g.drawRect(tX,tY,mW,mH);

    }

    public void drawScreen(Graphics g)

    {   

      g.drawRect(sX,sY,sW,sH);

    } 

 

}

 

 

   

class Speakers extends Mac

{

private int lX;//left speaker x

private int lY;//left speaker y

private int rX; // right speaker x

private int rY; //right speaker y

private int sW;//speaker width

private int sH;//speaker width

 

 

public Speakers(Graphics g,int x,int y,int w,int h)

{

 

  super(g,x,y,w,h);

 

  lX=x+400;

  lY=y+400;

    sW=w-10;

  sH=h-10;

    drawSpeakers(g);

}

 

public void drawSpeakers(Graphics g)

{

  g.drawRect(10,10,10,10);

}

 

}

 

 

 

class Keyboard

private int kX; //keyboard x

private int kY; //keyboard y

private int kW; //keyboard width

private int kH; //keyboard height

Random rndInt = new Random();

 

    public Keyboard(Graphics g,int x,int y,int w,int h)

    {

        kX= x-10;

    kY= y+300;

    kW= w+20;

    kH= h-200;

    //  drawCrash(g);

      drawKeyboard(g);

       

    }

    public void drawKeyboard(Graphics g)

    { 

    g.drawRect(kX,kY,kW,kH);

    }

   

  /*/ public void drawCrash(Graphics g)

    {

      int k;

    for(k=1;k <= 1;k--)

    { 

    int a = rndInt.nextInt(255);

    int b = rndInt.nextInt(255);

    int c = rndInt.nextInt(255);

    Color myRed = new Color(a,b,c);

    g.setColor(myRed);

    g.drawRect(200,115,400,270);

    g.fillRect(200,115,400,270); 

    }

    }

    /*/

   

 

 

}

Edited by Madi
Link to comment
Share on other sites

for(k=1;k <= 1;k--)

 

I know this code is inside a comment, but it is still an endless loop. Yea, I know, eventualy k will cross the minimum line for integers and become a positive number(or throw an exception. I never tried it on JAVA), but this is still bad programing.

 

 

 

public Mac(Graphics g, int x, int y, int w, int h)

...

speakers = new Speakers(g,x,y,w,h);

And

class Speakers extends Mac

...

public Speakers(Graphics g,int x,int y,int w,int h)

{

 

super(g,x,y,w,h);

 

Wrong wrong wrong. This is called endless recursion. When a Mac is created, it creates a new Speaker. Speaker extends Mac, and has a super call in it's constructor, therefore another Mac constructor is called, which creates another Speaker, which is Mac, so it creates another Speaker, and so on until you run out of memory.

 

 

 

There is another issue. You create new instances on every paint, when you can use the same ones over and over. I never realy worked with applets, but I belive they have an initialization method. Create an instance of the Mac there, and call the paint methods on the paint function.

 

 

 

import java.util.Random;

import java.util.*;

 

This is not realy a problem, only a style issue. When you inport java.util.*, java.util.Random is imported as well, so you don't need to import him specifically. If you want the reader of the code to know you have a special interest in the Random class, write it in a comment. That's why you have them.

 

 

 

Also, I suggest using colors for the different parts of your Mac.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...