Results 1 to 6 of 6

Thread: Classes Problem

  1. #1
    FreeQy is offline Member
    Join Date
    Nov 2011
    Location
    Bolzano, Italy
    Posts
    3
    Rep Power
    0

    Default 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.

    "

    so i created this program:

    Java 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..

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Classes Problem

    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?

    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.

  3. #3
    FreeQy is offline Member
    Join Date
    Nov 2011
    Location
    Bolzano, Italy
    Posts
    3
    Rep Power
    0

    Default 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...

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Classes Problem

    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.

  5. #5
    FreeQy is offline Member
    Join Date
    Nov 2011
    Location
    Bolzano, Italy
    Posts
    3
    Rep Power
    0

    Default Re: Classes Problem

    ...and how do i do that?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

Similar Threads

  1. Complicated problem between classes
    By uhertz in forum New To Java
    Replies: 3
    Last Post: 06-20-2011, 01:58 AM
  2. Problem with classes
    By liakos in forum NetBeans
    Replies: 14
    Last Post: 03-10-2011, 07:34 PM
  3. inner classes problem
    By smallmos1 in forum New To Java
    Replies: 7
    Last Post: 11-18-2010, 03:07 PM
  4. problem with using classes and output
    By sjaakie in forum New To Java
    Replies: 3
    Last Post: 10-10-2010, 01:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •