Classes Problem

• 11-05-2011, 02:49 PM
FreeQy
Classes Problem
Hi, i'm new to java...
i have an applet assignment to do, and i got stuck..

"• Write a class Diner with the following features:
• attributes name (String), gender (Color),
locationX, locationY (int)
• A constructor
Diner(String dinerName, Color genderColor, int x, int y)
that sets up this diner with the specified characteristics.
• A method draw(Graphics page) that draws the diner.

Then write a class DinnerTable
extending Japplet that:
• creates 4 diners;
• draw the table and the diners
as shown in the picture.

http://i41.tinypic.com/fyjyoj.jpg"

so i created this program:

Code:

```package diner; import java.awt.Graphics; import java.awt.Color; public class Diner {     public enum DinerGender {male, female}         private DinerGender gender;     private String name;     private int locationX, locationY;             public Diner(String dinerName, DinerGender genderInit, int x, int y)     {         name = dinerName;         gender = genderInit;         locationX = x;         locationY = y;             }         public void draw (Graphics page, int leftedge)     {         final int OVAL_WIDTH = 75;         final int OVAL_HEIGHT = 75;                 page.setColor(Color.black);         page.drawString(name, locationX, locationY);                 page.setColor(Color.cyan);         page.fillOval(100, 25, OVAL_WIDTH, OVAL_HEIGHT);           }  } package diner; import javax.swing.JApplet; import java.awt.Graphics; import java.awt.Color; public class DinnerTable extends JApplet {     private final int APPLET_HEIGHT = 300;     private final int APPLET_WIDTH = 400;         private Diner d1,d2,d3,d4;         public void init()     {         d1 = new Diner ("Mike", Diner.DinerGender.male, 100, 60);         d2 = new Diner ("Sarah", Diner.DinerGender.female, 240, 60);         d3 = new Diner ("John", Diner.DinerGender.male, 100, 260);         d4 = new Diner ("Anna", Diner.DinerGender.female, 240, 260);                                         setBackground (Color.black);         setSize (APPLET_WIDTH, APPLET_HEIGHT);                           }         public void paint (Graphics page)     {         page.setColor (Color.gray);         page.fillRect (100, 110, 175, 125);                 d1.draw(page);         d2.draw(page);         d3.draw(page);         d4.draw(page);                             }     }```
so i have to create the 4 diners with the second class, from the first one...
i have to specify if it's a girl or a boy..and so the program should create a pink or a cyan oval...with a name or it
and i can't understand how to do that...

and also about the position of the ovals, don't know how to specify that..
• 11-05-2011, 03:00 PM
Norm
Re: Classes Problem
Quote:

have to specify if it's a girl or a boy..and so the program should create a pink or a cyan oval...with a name or it
and i can't understand how to do that...
When you create the class object, you should specify: boy or girl so the drawing method knows what color to use.
I see that your code already does that in the constructor call. Can you explain what your problem is?

Quote:

also about the position of the ovals
Read the API doc for the Graphics class's drawing methods. The doc will tell you how to set the position of the drawing.
• 11-05-2011, 03:20 PM
FreeQy
Re: Classes Problem
i know how to create the constructor, but then i don't know how to create the pink or the cyan oval...
and for these pink or cyan to set the position...
• 11-05-2011, 03:23 PM
Norm
Re: Classes Problem
Quote:

i don't know how to create the pink or the cyan oval..
Look at your code. It currently (lines 33-34) draws one cyan oval. You need to add logic there to decide on color and location for the oval being drawn.
• 11-05-2011, 03:35 PM
FreeQy
Re: Classes Problem
...and how do i do that?
• 11-05-2011, 03:37 PM
Norm
Re: Classes Problem
You would use an if/else statement