Madi Posted December 11, 2005 Share Posted December 11, 2005 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 More sharing options...
Weirdy Posted December 11, 2005 Share Posted December 11, 2005 (edited) 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 Edited December 11, 2005 by Weirdy Link to comment Share on other sites More sharing options...
Elazul Yagami Posted December 11, 2005 Share Posted December 11, 2005 i'm typing this IN a java class.so yeah, i got a good idea.post your code and i'll answer your questions. Link to comment Share on other sites More sharing options...
someboddy Posted December 11, 2005 Share Posted December 11, 2005 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 RH O M E W O R KY O U R S E L F Link to comment Share on other sites More sharing options...
Madi Posted December 11, 2005 Author Share Posted December 11, 2005 (edited) 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.javaimport 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 December 11, 2005 by Madi Link to comment Share on other sites More sharing options...
Weirdy Posted December 11, 2005 Share Posted December 11, 2005 ohh ok, so JAVA uses "extends" instead of a symbol as a operator eh interesting Link to comment Share on other sites More sharing options...
someboddy Posted December 12, 2005 Share Posted December 12, 2005 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);Andclass 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 More sharing options...
Madi Posted December 13, 2005 Author Share Posted December 13, 2005 Thanks for the help ! I got a 100 after fixing it, My teacher didnt even know what was wrong with it! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now