title: Generating Random instances of subclasses using a Abstract Superclass
body: <p>So I have been busting my head over this program assignment and I have no idea how do finish it. I have already completed 80% of it but the last part I don't know even the general idea. The question is </p>
  
  <p>"Then write a method <code>randomVehicle</code> that randomly generates <code>Vehicle</code> references, with an equal probability for constructing cars and trucks, with random positions. Call it 10 times and draw all of them."</p>
  
  <p>I have a main method, an Abstract Superclass <code>Vehicle</code> and two subclasses of Car and Truck. I know how to do probability using loops but I have no idea how to take that probability answer (1 will be truck and 2 will be car) and use it to reference the predefine shapes of the car and and truck.</p>
  
  <p>In other words, how would I create the program that after I hit compile and run, will make these randomly decided car and truck and display it.</p>
  
  <p>Sorry if the question is confusing but I am just learning about abstract classes.</p>
  
  <p>Here is the code thus far:</p>
  
  <p>Main Method</p>
  
  <pre><code>import java.awt.*;
  @SuppressWarnings("serial")
  class drawTest extends Canvas{
  public drawTest(){
      setSize(800, 600);
      setBackground(Color.white);
  }
  public static void main(String[] argS){
      drawTest canvas = new drawTest();  
      Frame aFrame = new Frame();
      aFrame.setSize(800, 600);
      aFrame.add(canvas);   
      aFrame.setVisible(true);
  }
  public void paint(Graphics canvas){
      Vehicle car = new CarTest();
      car.paint(canvas);
      paint(canvas);
  
  }
  }
  </code></pre>
  
  <p><code>CarTest</code> Class</p>
  
  <pre><code>import java.awt.*;
  @SuppressWarnings("serial")
  public class CarTest extends Vehicle{
  
  public void paint(Graphics canvas){
      canvas.drawOval(10+super.x, 30+super.y, 15, 15);    // Front Wheel
      canvas.drawOval(45+super.x, 30+super.y, 15, 15);    // Back Wheel
      canvas.drawRect(5+super.x, 10+super.y, 60, 20);     // Bottom of Car
      canvas.drawRect(15+super.x, 5+super.y, 40, 5);      // Bottom of Car
  }
  }
  </code></pre>
  
  <p><code>TructTest</code> Class</p>
  
  <pre><code>import java.awt.*;
  @SuppressWarnings("serial")
  public class TruckTest extends Vehicle{
  
  public void paint(Graphics canvas){
      canvas.drawRect(30+super.x, 5+super.y, 100, 30);    // Cargo Section
      canvas.drawOval(30+super.x, 35+super.y, 15, 15);    // Wheel Under Cargo
      canvas.drawOval(45+super.x, 35+super.y, 15, 15);    // Wheel Under Cargo
      canvas.drawOval(100+super.x, 35+super.y, 15, 15);   // Wheel Under Cargo
      canvas.drawOval(115+super.x, 35+super.y, 15, 15);   // Wheel Under Cargo
      canvas.drawRect(5+super.x, 15+super.y, 20, 20);     // Driver Section
      canvas.drawOval(5+super.x, 35+super.y, 15, 15);     // Wheel Under Driver
  }
  }
  </code></pre>
  
  <p>Abstract <code>Vehicle</code> Class</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  @SuppressWarnings("serial")
  public abstract class Vehicle extends JApplet{
  
  public Vehicle(){
          // Generates Random position
      x = (int)(Math.random()*700);
      y = (int)(Math.random()*550);
  }
  public abstract void paint(Graphics canvas);
  
  public void randomVehicle(){
      int carTruckChoice = (int)(1+Math.random()*2);
          if (carTruckChoice == 1){
              // don't know if this is the right implementation 
          }
          else if (carTruckChoice == 2){
              // don't know if this is the right implementation 
  
          }
  }
  public int x;
  public int y;
  }
  </code></pre>