Cars program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javasomething
    New Member
    • Feb 2007
    • 9

    Cars program

    [B]I need to finish this assignment can someone help me plz.

    The Car Class


    The purpose of this assignment is to learn how to write your own class that serves as a template for creating objects. The Car class represents an automobile. Each car has a color, miles driven, and the amount of gasoline in the tank (gallons).

    The Car class should have both a default and parameterized constructor. The default constructor should initialize all of the instance variables. (The default miles driven and gallons should be zero.) The parameterized constructor will accept a color parameter. Each constructor should call the private method drawShape() that creates the car. (The car's shape does not need to be elaborate.)

    Write the methods described here:

    drawShape is a private method that creates the GCompound representing the car

    setColor accepts a color parameter and sets the color of the car

    drive accepts miles and speed as parameters. Miles must be greater than zero. Speed must be between 0 and 100. As long as the car has gas in the tank, add the miles to the car's total miles, and reduce the amount of gasoline based on the miles per gallon, which can be estimated using the following formula:
    miles per gallon = 55 - speed/2

    getMilesDriven returns the number of miles the car has driven

    fill accepts an amount of gas as a parameter and adds it to the car's gallons; the amount must be greater than zero

    isEmpty returns true if the car's tank is empty, and false if it is not

    toString returns a String containing the car's miles driven and gallons in the tank




    The UML class diagram for the Car class is displayed on the right. Do not create any instance variables or methods other than those shown.

    To test your Car class, create an Applet called TestDrive that extends GraphicsProgram . Create and display three cars. Use each of your constructors at least once. Fill each tank and drive each car. Then, below each car, print its information using the toString() method. (Your cars do not need to move in this assignment.)

    Sample output is shown here:



    Have fun!


    Car
    _______________ _________
    - color
    - miles driven
    - gallons
    _______________ _________

    + Car()
    + Car(color)
    - drawShape()
    + setColor(color)
    + drive(miles, speed)
    + getMilesDriven( )
    + fill(amount)
    + isEmpty()
    + toString()
  • abctech
    New Member
    • Dec 2006
    • 157

    #2
    Originally posted by javasomething
    [B]I need to finish this assignment can someone help me plz.

    The Car Class


    The purpose of this assignment is to learn how to write your own class that serves as a template for creating objects. The Car class represents an automobile. Each car has a color, miles driven, and the amount of gasoline in the tank (gallons).

    The Car class should have both a default and parameterized constructor. The default constructor should initialize all of the instance variables. (The default miles driven and gallons should be zero.) The parameterized constructor will accept a color parameter. Each constructor should call the private method drawShape() that creates the car. (The car's shape does not need to be elaborate.)

    Write the methods described here:

    drawShape is a private method that creates the GCompound representing the car

    setColor accepts a color parameter and sets the color of the car

    drive accepts miles and speed as parameters. Miles must be greater than zero. Speed must be between 0 and 100. As long as the car has gas in the tank, add the miles to the car's total miles, and reduce the amount of gasoline based on the miles per gallon, which can be estimated using the following formula:
    miles per gallon = 55 - speed/2

    getMilesDriven returns the number of miles the car has driven

    fill accepts an amount of gas as a parameter and adds it to the car's gallons; the amount must be greater than zero

    isEmpty returns true if the car's tank is empty, and false if it is not

    toString returns a String containing the car's miles driven and gallons in the tank




    The UML class diagram for the Car class is displayed on the right. Do not create any instance variables or methods other than those shown.

    To test your Car class, create an Applet called TestDrive that extends GraphicsProgram . Create and display three cars. Use each of your constructors at least once. Fill each tank and drive each car. Then, below each car, print its information using the toString() method. (Your cars do not need to move in this assignment.)

    Sample output is shown here:



    Have fun!


    Car
    _______________ _________
    - color
    - miles driven
    - gallons
    _______________ _________

    + Car()
    + Car(color)
    - drawShape()
    + setColor(color)
    + drive(miles, speed)
    + getMilesDriven( )
    + fill(amount)
    + isEmpty()
    + toString()
    Hey buddy,
    This seems like your first post, welcome to TSDN and the Java forum. Before posting do take a look at the posting guidelines here

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by javasomething
      [B]I need to finish this assignment can someone help me plz.

      The Car Class


      The purpose of this assignment is to learn how to write your own class that serves as a template for creating objects. The Car class represents an automobile. Each car has a color, miles driven, and the amount of gasoline in the tank (gallons).

      The Car class should have both a default and parameterized constructor. The default constructor should initialize all of the instance variables. (The default miles driven and gallons should be zero.) The parameterized constructor will accept a color parameter. Each constructor should call the private method drawShape() that creates the car. (The car's shape does not need to be elaborate.)

      Write the methods described here:

      drawShape is a private method that creates the GCompound representing the car

      setColor accepts a color parameter and sets the color of the car

      drive accepts miles and speed as parameters. Miles must be greater than zero. Speed must be between 0 and 100. As long as the car has gas in the tank, add the miles to the car's total miles, and reduce the amount of gasoline based on the miles per gallon, which can be estimated using the following formula:
      miles per gallon = 55 - speed/2

      getMilesDriven returns the number of miles the car has driven

      fill accepts an amount of gas as a parameter and adds it to the car's gallons; the amount must be greater than zero

      isEmpty returns true if the car's tank is empty, and false if it is not

      toString returns a String containing the car's miles driven and gallons in the tank




      The UML class diagram for the Car class is displayed on the right. Do not create any instance variables or methods other than those shown.

      To test your Car class, create an Applet called TestDrive that extends GraphicsProgram . Create and display three cars. Use each of your constructors at least once. Fill each tank and drive each car. Then, below each car, print its information using the toString() method. (Your cars do not need to move in this assignment.)

      Sample output is shown here:



      Have fun!


      Car
      _______________ _________
      - color
      - miles driven
      - gallons
      _______________ _________

      + Car()
      + Car(color)
      - drawShape()
      + setColor(color)
      + drive(miles, speed)
      + getMilesDriven( )
      + fill(amount)
      + isEmpty()
      + toString()
      What part do you need help with?

      Comment

      • javasomething
        New Member
        • Feb 2007
        • 9

        #4
        I need to create my own class then transport that into a graphics program applet. I dont know how to start it.
        I dont have enought time to finish it so can someone plz help me fast.
        thank you very much

        Comment

        • abctech
          New Member
          • Dec 2006
          • 157

          #5
          Originally posted by javasomething
          I need to create my own class then transport that into a graphics program applet. I dont know how to start it.
          I dont have enought time to finish it so can someone plz help me fast.
          thank you very much
          Hi,
          Have you started with your task?

          Your class should be something like this:
          class Car{

          //make the variable declarations
          //declare and define the specified constructors
          //declare and define the specified methods

          };

          After having created this class you will be testing it for which you will create an Applet, but thats the second part.

          Now you have all the needed details in the specification of your assignment. Make an attempt based on whatever you have understood so far and post it.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by javasomething
            I need to create my own class then transport that into a graphics program applet. I dont know how to start it.
            I dont have enought time to finish it so can someone plz help me fast.
            thank you very much
            You should have started earlier to give yourself enough time to finish this. It doesn't need more than a day anyway if you paid attention in your classes.

            Now no-one is going to write it for you here so you better start on it and post whenever you get a specific problem.

            Comment

            • javasomething
              New Member
              • Feb 2007
              • 9

              #7
              i really need to finish this buy today plz

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by javasomething
                i really need to finish this buy today plz
                You really need to show some effort. Have you written anything at all?

                Comment

                • javasomething
                  New Member
                  • Feb 2007
                  • 9

                  #9
                  yes i have done this:

                  import java.util.*;
                  import java.text.*;
                  import java.awt.*;
                  import acm.graphics.*;

                  /*represents a car class*/
                  public class CAR extends GCompound
                  {
                  //declare instance variables//
                  private int gallons;
                  private boolean isEmpty;
                  private String toString;
                  private int fill;
                  private int drive;
                  private int MilesDriven;
                  private Color color;

                  /**Creates the defalut constructor*/
                  public CAR()
                  {
                  //Initialize instance variables
                  MilesDriven=0;
                  gallons=0;
                  color=Color.RED ;
                  toString=" ";
                  fill=0;
                  drive=0;

                  drawShape();
                  }

                  /**Creates the parameterized constructor*/
                  public CAR(int MilesDriven, int gallons, Color color)
                  {
                  //Initialize instance variables
                  this.MilesDrive n=MilesDriven;
                  this.gallons=ga llons;
                  this.color=colo r;

                  drawShape();
                  }

                  /**Sets how many gallons are needed*/
                  public void setGallons(int g)
                  { gallons=g; }

                  /**Gets the gallons that we tell it to get.*/
                  public int getGallons()
                  { return gallons; }

                  /**This method will return a boolean if the gas is empty.*/
                  public boolean isEmpty()
                  {
                  if (gallons <= 0)
                  {
                  return true;
                  }
                  else
                  {
                  return false;
                  }
                  }

                  /**This method will set how many miles and gallons are left.*/
                  public void setToString(Str ing string)
                  {
                  this.toString = string+MilesDri ven+gallons;
                  }

                  /**This method will tell us how many miles and gallons are left.*/
                  public String getToString()
                  {
                  return toString;
                  }

                  /**This method say to add the gallons to the car.*/
                  public void setFill(int amount)
                  {
                  if(amount>0)
                  {
                  amount += gallons;
                  }
                  }

                  /**This method will add the gallons to the car.*/
                  public int getFill()
                  {
                  return fill;
                  }

                  /**This method sets to how many miles the car has driven.*/
                  public void setMilesDriven( )
                  {
                  this.MilesDrive n=MilesDriven;
                  }

                  /**This method returns the number of miles the car has driven.*/
                  public int getMilesDriven( )
                  {
                  return MilesDriven;
                  }

                  /**This method will tell us the speed and miles the car has driven.*/
                  public void drive(double miles, int speed)
                  {
                  if(miles > 0)
                  {
                  miles += MilesDriven;

                  if(speed < 100)
                  {
                  MilesDriven=55-speed/2;
                  }
                  if(speed > 0)
                  {
                  //returns nothing//
                  }
                  }
                  }

                  /**This method will create the actual car and all of its parts*/
                  private void drawShape()
                  {
                  //Creates the top half of the car
                  GRect carsHead = new GRect(100, 100, 40, 40);
                  carsHead.setFil led(true);
                  carsHead.setFil lColor(color);
                  add(carsHead);

                  //Creates the bottom half of the car
                  GRect carsBottom = new GRect(100, 120, 60, 20);
                  carsBottom.setF illed(true);
                  carsBottom.setF illColor(Color. BLUE);
                  add(carsBottom) ;

                  //Creates the first window of the car
                  GRect firstWindow = new GRect(120, 105, 11, 11);
                  firstWindow.set Filled(true);
                  firstWindow.set FillColor(Color .WHITE);
                  add(firstWindow );

                  //Creates the second window of the car
                  GRect secondWindow = new GRect(102, 105, 11, 11);
                  secondWindow.se tFilled(true);
                  secondWindow.se tFillColor(Colo r.WHITE);
                  add(secondWindo w);

                  //Creates the first wheel of the car
                  GOval firstWheel = new GOval(105, 130, 20, 20);
                  firstWheel.setF illed(true);
                  firstWheel.setF illColor(Color. BLACK);
                  add(firstWheel) ;

                  //Creates the second window of the car
                  GOval secondWheel = new GOval(135, 130, 20, 20);
                  secondWheel.set Filled(true);
                  secondWheel.set FillColor(Color .BLACK);
                  add(secondWheel );
                  }
                  }


                  does this look correct??

                  Comment

                  • RedSon
                    Recognized Expert Expert
                    • Jan 2007
                    • 4980

                    #10
                    This is very difficult to read with out code tags, and proper indentation. Perhaps you should try fixing that first and then more people will be able to help you.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by javasomething
                      yes i have done this:

                      import java.util.*;
                      import java.text.*;
                      import java.awt.*;
                      import acm.graphics.*;

                      /*represents a car class*/
                      public class CAR extends GCompound
                      {
                      //declare instance variables//
                      private int gallons;
                      private boolean isEmpty;
                      private String toString;
                      private int fill;
                      private int drive;
                      private int MilesDriven;
                      private Color color;

                      /**Creates the defalut constructor*/
                      public CAR()
                      {
                      //Initialize instance variables
                      MilesDriven=0;
                      gallons=0;
                      color=Color.RED ;
                      toString=" ";
                      fill=0;
                      drive=0;

                      drawShape();
                      }

                      /**Creates the parameterized constructor*/
                      public CAR(int MilesDriven, int gallons, Color color)
                      {
                      //Initialize instance variables
                      this.MilesDrive n=MilesDriven;
                      this.gallons=ga llons;
                      this.color=colo r;

                      drawShape();
                      }

                      /**Sets how many gallons are needed*/
                      public void setGallons(int g)
                      { gallons=g; }

                      /**Gets the gallons that we tell it to get.*/
                      public int getGallons()
                      { return gallons; }

                      /**This method will return a boolean if the gas is empty.*/
                      public boolean isEmpty()
                      {
                      if (gallons <= 0)
                      {
                      return true;
                      }
                      else
                      {
                      return false;
                      }
                      }

                      /**This method will set how many miles and gallons are left.*/
                      public void setToString(Str ing string)
                      {
                      this.toString = string+MilesDri ven+gallons;
                      }

                      /**This method will tell us how many miles and gallons are left.*/
                      public String getToString()
                      {
                      return toString;
                      }

                      /**This method say to add the gallons to the car.*/
                      public void setFill(int amount)
                      {
                      if(amount>0)
                      {
                      amount += gallons;
                      }
                      }

                      /**This method will add the gallons to the car.*/
                      public int getFill()
                      {
                      return fill;
                      }

                      /**This method sets to how many miles the car has driven.*/
                      public void setMilesDriven( )
                      {
                      this.MilesDrive n=MilesDriven;
                      }

                      /**This method returns the number of miles the car has driven.*/
                      public int getMilesDriven( )
                      {
                      return MilesDriven;
                      }

                      /**This method will tell us the speed and miles the car has driven.*/
                      public void drive(double miles, int speed)
                      {
                      if(miles > 0)
                      {
                      miles += MilesDriven;

                      if(speed < 100)
                      {
                      MilesDriven=55-speed/2;
                      }
                      if(speed > 0)
                      {
                      //returns nothing//
                      }
                      }
                      }

                      /**This method will create the actual car and all of its parts*/
                      private void drawShape()
                      {
                      //Creates the top half of the car
                      GRect carsHead = new GRect(100, 100, 40, 40);
                      carsHead.setFil led(true);
                      carsHead.setFil lColor(color);
                      add(carsHead);

                      //Creates the bottom half of the car
                      GRect carsBottom = new GRect(100, 120, 60, 20);
                      carsBottom.setF illed(true);
                      carsBottom.setF illColor(Color. BLUE);
                      add(carsBottom) ;

                      //Creates the first window of the car
                      GRect firstWindow = new GRect(120, 105, 11, 11);
                      firstWindow.set Filled(true);
                      firstWindow.set FillColor(Color .WHITE);
                      add(firstWindow );

                      //Creates the second window of the car
                      GRect secondWindow = new GRect(102, 105, 11, 11);
                      secondWindow.se tFilled(true);
                      secondWindow.se tFillColor(Colo r.WHITE);
                      add(secondWindo w);

                      //Creates the first wheel of the car
                      GOval firstWheel = new GOval(105, 130, 20, 20);
                      firstWheel.setF illed(true);
                      firstWheel.setF illColor(Color. BLACK);
                      add(firstWheel) ;

                      //Creates the second window of the car
                      GOval secondWheel = new GOval(135, 130, 20, 20);
                      secondWheel.set Filled(true);
                      secondWheel.set FillColor(Color .BLACK);
                      add(secondWheel );
                      }
                      }


                      does this look correct??
                      You didn't have to post all the code. We can't compile and test that because we don't have the acm.graphics package that you are using (please you don't have to post it.)

                      What you should do is tell us how far you've got and what you now need help with. Are you getting any compile time errors, exceptions? Is the program doing what it is supposed to do?

                      Comment

                      • javasomething
                        New Member
                        • Feb 2007
                        • 9

                        #12
                        I fixed my indentation. there might be some false. And i have the applet at the bottom too.

                        Also how do I create an applet that simulates cars driving at differing speeds and displays the fuel cost for each car.


                        import java.util.*;
                        import java.text.*;
                        import java.awt.*;
                        import acm.graphics.*;

                        /*represents a car class*/
                        public class CAR extends GCompound
                        {
                        //declare instance variables//
                        private int gallons;
                        //private boolean isEmpty;
                        //private String toString;
                        //private int fill;
                        //private int drive;
                        private int milesDriven;
                        private Color color;

                        /**Creates the defalut constructor*/
                        public CAR()
                        {
                        //Initialize instance variables
                        milesDriven=0;
                        gallons=0;
                        color=Color.RED ;
                        // toString=" ";
                        //int fill;
                        // drive();

                        drawShape();
                        }

                        /**Creates the parameterized constructor*/
                        public CAR(int milesDriven, int gallons, Color color)
                        {
                        //Initialize instance variables
                        this.milesDrive n=milesDriven;
                        this.gallons=ga llons;
                        this.color=colo r;

                        drawShape();
                        }

                        /**Sets how many gallons are needed*/
                        public void setGallons(int g)
                        { gallons=g; }

                        /**Gets the gallons that we tell it to get.*/
                        public int getGallons()
                        { return gallons; }

                        /**This method will return a boolean if the gas is empty.*/
                        public boolean isEmpty()
                        {
                        if (gallons <= 0)
                        {
                        return true;
                        }
                        else
                        {
                        return false;
                        }
                        }

                        /**This method will set how many miles and gallons are left.*/
                        public void setToString(Str ing string)
                        {
                        String toString = " ";
                        toString = string+milesDri ven+gallons;
                        }

                        /**This method will tell us how many miles and gallons are left.*/
                        public String getToString()
                        {
                        return toString;
                        }

                        /**This method say to add the gallons to the car.*/
                        public void fill(int amount)
                        {
                        if(amount>0)
                        {
                        amount += gallons;
                        }
                        }

                        /*This method will add the gallons to the car.
                        public int getFill()
                        {
                        return fill;
                        }*/

                        /**This method sets to how many miles the car has driven.*/
                        public void setmilesDriven( )
                        {
                        this.milesDrive n=milesDriven;
                        }

                        /**This method returns the number of miles the car has driven.*/
                        public int getmilesDriven( )
                        {
                        return milesDriven;
                        }

                        /**This method will tell us the speed and miles the car has driven.*/
                        public void drive(double miles, int speed)
                        {
                        if(miles > 0)
                        {
                        miles += milesDriven;

                        if(speed < 100)
                        {
                        milesDriven=55-speed/2;
                        }
                        if(speed > 0)
                        {
                        //returns nothing//
                        }
                        }
                        }

                        /**This method will create the actual car and all of its parts*/
                        private void drawShape()
                        {
                        //Creates the top half of the car
                        GRect carsHead = new GRect(100, 100, 40, 40);
                        carsHead.setFil led(true);
                        carsHead.setFil lColor(color);
                        add(carsHead);

                        //Creates the bottom half of the car
                        GRect carsBottom = new GRect(100, 120, 60, 20);
                        carsBottom.setF illed(true);
                        carsBottom.setF illColor(Color. BLUE);
                        add(carsBottom) ;

                        //Creates the first window of the car
                        GRect firstWindow = new GRect(120, 105, 11, 11);
                        firstWindow.set Filled(true);
                        firstWindow.set FillColor(Color .WHITE);
                        add(firstWindow );

                        //Creates the second window of the car
                        GRect secondWindow = new GRect(102, 105, 11, 11);
                        secondWindow.se tFilled(true);
                        secondWindow.se tFillColor(Colo r.WHITE);
                        add(secondWindo w);

                        //Creates the first wheel of the car
                        GOval firstWheel = new GOval(105, 130, 20, 20);
                        firstWheel.setF illed(true);
                        firstWheel.setF illColor(Color. BLACK);
                        add(firstWheel) ;

                        //Creates the second window of the car
                        GOval secondWheel = new GOval(135, 130, 20, 20);
                        secondWheel.set Filled(true);
                        secondWheel.set FillColor(Color .BLACK);
                        add(secondWheel );
                        }
                        }


                        This is the applet:

                        import java.util.*;
                        import java.text.*;
                        import java.awt.*;
                        import acm.program.*;
                        import acm.graphics.*;
                        import java.awt.event. *;

                        /**Represents a car object*/
                        public class TestDrive extends GraphicsProgram
                        {
                        GLabel carLabel;

                        /**This method will draw the cars in the applet*/
                        public void run()
                        {
                        //Size of the applet//
                        setSize(800,600 );
                        //Add the MouseListeners events//
                        addMouseListene rs();

                        //First car from your left//
                        CAR c1 = new CAR(100,25,Colo r.RED);
                        add(c1, 20, 20);

                        //Second car in the middle//
                        CAR c2 = new CAR(235,26,Colo r.BLUE);
                        add(c2, 250, 20);

                        //Third car on your right//
                        CAR c3 = new CAR(456,20,Colo r.CYAN);
                        add(c3, 480, 20);
                        }

                        /**This method will show the gallons and miles when pressed on an object*/
                        public void mousePressed(Mo useEvent event)
                        {
                        Object obj = getElementAt(ev ent.getX(), event.getY());
                        if (obj instanceof CAR)
                        {
                        CAR car = (CAR)obj;
                        String text = car.getMilesDri ven() + " Miles and "
                        + car.getGallons( ) + " gallons";
                        carLabel = new GLabel(text);
                        add(carLabel, event.getX(), event.getY());
                        }
                        }

                        /**This method will remove the label as soon as mouse has been released*/
                        public void mouseReleased(M ouseEvent event)
                        {
                        remove(carLabel );
                        }
                        }

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by javasomething
                          I fixed my indentation. there might be some false. And i have the applet at the bottom too.



                          Also how do I create an applet that simulates cars driving at differing speeds and displays the fuel cost for each car.





                          import java.util.*;

                          import java.text.*;

                          import java.awt.*;

                          import acm.graphics.*;



                          /*represents a car class*/

                          public class CAR extends GCompound

                          {

                          //declare instance variables//

                          private int gallons;

                          //private boolean isEmpty;

                          //private String toString;

                          //private int fill;

                          //private int drive;

                          private int milesDriven;

                          private Color color;



                          /**Creates the defalut constructor*/

                          public CAR()

                          {

                          //Initialize instance variables

                          milesDriven=0;

                          gallons=0;

                          color=Color.RED ;

                          // toString=\" \";

                          //int fill;

                          // drive();



                          drawShape();

                          }



                          /**Creates the parameterized constructor*/

                          public CAR(int milesDriven, int gallons, Color color)

                          {

                          //Initialize instance variables

                          this.milesDrive n=milesDriven;

                          this.gallons=ga llons;

                          this.color=colo r;



                          drawShape();

                          }



                          /**Sets how many gallons are needed*/

                          public void setGallons(int g)

                          { gallons=g; }



                          /**Gets the gallons that we tell it to get.*/

                          public int getGallons()

                          { return gallons; }



                          /**This method will return a boolean if the gas is empty.*/

                          public boolean isEmpty()

                          {

                          if (gallons <= 0)

                          {

                          return true;

                          }

                          else

                          {

                          return false;

                          }

                          }



                          /**This method will set how many miles and gallons are left.*/

                          public void setToString(Str ing string)

                          {

                          String toString = \" \";

                          toString = string+milesDri ven+gallons;

                          }



                          /**This method will tell us how many miles and gallons are left.*/

                          public String getToString()

                          {

                          return toString;

                          }



                          /**This method say to add the gallons to the car.*/

                          public void fill(int amount)

                          {

                          if(amount>0)

                          {

                          amount += gallons;

                          }

                          }



                          /*This method will add the gallons to the car.

                          public int getFill()

                          {

                          return fill;

                          }*/



                          /**This method sets to how many miles the car has driven.*/

                          public void setmilesDriven( )

                          {

                          this.milesDrive n=milesDriven;

                          }



                          /**This method returns the number of miles the car has driven.*/

                          public int getmilesDriven( )

                          {

                          return milesDriven;

                          }



                          /**This method will tell us the speed and miles the car has driven.*/

                          public void drive(double miles, int speed)

                          {

                          if(miles > 0)

                          {

                          miles += milesDriven;



                          if(speed < 100)

                          {

                          milesDriven=55-speed/2;

                          }

                          if(speed > 0)

                          {

                          //returns nothing//

                          }

                          }

                          }



                          /**This method will create the actual car and all of its parts*/

                          private void drawShape()

                          {

                          //Creates the top half of the car

                          GRect carsHead = new GRect(100, 100, 40, 40);

                          carsHead.setFil led(true);

                          carsHead.setFil lColor(color);

                          add(carsHead);



                          //Creates the bottom half of the car

                          GRect carsBottom = new GRect(100, 120, 60, 20);

                          carsBottom.setF illed(true);

                          carsBottom.setF illColor(Color. BLUE);

                          add(carsBottom) ;



                          //Creates the first window of the car

                          GRect firstWindow = new GRect(120, 105, 11, 11);

                          firstWindow.set Filled(true);

                          firstWindow.set FillColor(Color .WHITE);

                          add(firstWindow );



                          //Creates the second window of the car

                          GRect secondWindow = new GRect(102, 105, 11, 11);

                          secondWindow.se tFilled(true);

                          secondWindow.se tFillColor(Colo r.WHITE);

                          add(secondWindo w);



                          //Creates the first wheel of the car

                          GOval firstWheel = new GOval(105, 130, 20, 20);

                          firstWheel.setF illed(true);

                          firstWheel.setF illColor(Color. BLACK);

                          add(firstWheel) ;



                          //Creates the second window of the car

                          GOval secondWheel = new GOval(135, 130, 20, 20);

                          secondWheel.set Filled(true);

                          secondWheel.set FillColor(Color .BLACK);

                          add(secondWheel );

                          }

                          }





                          This is the applet:



                          import java.util.*;

                          import java.text.*;

                          import java.awt.*;

                          import acm.program.*;

                          import acm.graphics.*;

                          import java.awt.event. *;



                          /**Represents a car object*/

                          public class TestDrive extends GraphicsProgram

                          {

                          GLabel carLabel;



                          /**This method will draw the cars in the applet*/

                          public void run()

                          {

                          //Size of the applet//

                          setSize(800,600 );

                          //Add the MouseListeners events//

                          addMouseListene rs();



                          //First car from your left//

                          CAR c1 = new CAR(100,25,Colo r.RED);

                          add(c1, 20, 20);



                          //Second car in the middle//

                          CAR c2 = new CAR(235,26,Colo r.BLUE);

                          add(c2, 250, 20);



                          //Third car on your right//

                          CAR c3 = new CAR(456,20,Colo r.CYAN);

                          add(c3, 480, 20);

                          }



                          /**This method will show the gallons and miles when pressed on an object*/

                          public void mousePressed(Mo useEvent event)

                          {

                          Object obj = getElementAt(ev ent.getX(), event.getY());

                          if (obj instanceof CAR)

                          {

                          CAR car = (CAR)obj;

                          String text = car.getMilesDri ven() + \" Miles and \"

                          + car.getGallons( ) + \" gallons\";

                          carLabel = new GLabel(text);

                          add(carLabel, event.getX(), event.getY());

                          }

                          }



                          /**This method will remove the label as soon as mouse has been released*/

                          public void mouseReleased(M ouseEvent event)

                          {

                          remove(carLabel );

                          }

                          }


                          Please, Please desist from posting all your code like this. It is not neccessary. If you really have to post any code do so using code tags. Now you are using a nonstandard graphics package acm.graphics. You will need to consult its API or help to be able to use it for your benefit.

                          Comment

                          • javasomething
                            New Member
                            • Feb 2007
                            • 9

                            #14
                            Ok I wont post the whole thing.

                            How do i Create an Applet that simulates cars driving at differing speeds and displays the fuel cost for each.
                            my simulation should display at least 3 cars. For each car, display the fuel cost and speed. Initially, your fuel cost for each car is zero. Drive one car at 50 MPH, one at 60 MPH, and one at 70 MPH.

                            I rellay need to finish this.

                            I need help fast plz. Can someone post the code for this. plz

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by javasomething
                              Ok I wont post the whole thing.

                              How do i Create an Applet that simulates cars driving at differing speeds and displays the fuel cost for each.
                              my simulation should display at least 3 cars. For each car, display the fuel cost and speed. Initially, your fuel cost for each car is zero. Drive one car at 50 MPH, one at 60 MPH, and one at 70 MPH.

                              I rellay need to finish this.

                              I need help fast plz. Can someone post the code for this. plz
                              No one is going to write the code for you. We are only here to assist not to do your homework for you. You need to design your applet first. How you are going to represent your cars. Whether you are going to use images e.t.c. The movement should not be too much of a problem. Just use threads for it and make sure the threads are pausing a time proportional to the speed of the cars.

                              Comment

                              Working...