Java 是否将所有元素的模型设置为参数值?

Java 是否将所有元素的模型设置为参数值?,java,arrays,arraylist,param,setvalue,Java,Arrays,Arraylist,Param,Setvalue,我真的需要一些帮助与此代码。 我不知道该怎么做第三部分。它表示将使用模型来设置值,但这几乎是反复试验,没有任何进展 setSize( 500,400 ); carList = new ArrayList<Auto>( ); ad = new AutoDisplay( carList ); setVisible( true ); ad.setEraseColor( getBackground( ) ); //

我真的需要一些帮助与此代码。 我不知道该怎么做第三部分。它表示将使用模型来设置值,但这几乎是反复试验,没有任何进展

      setSize( 500,400 );

      carList = new ArrayList<Auto>( );

      ad = new AutoDisplay( carList );
      setVisible( true );
      ad.setEraseColor( getBackground( ) );
      // fill carList with several cars
      fillWithCars( );
   }

   // ***** 1.  This method has been coded as an example
   /** Fills the carList with hard-coded Auto objects
   *    The instance variable carList is the ArrayList
   *    to be filled with Auto objects
   */
   public void fillWithCars()
   {
     // clear carList before adding cars
     carList.clear();
     // Reset the number of Autos to 0
     // This is needed so that the animation feedback works correctly
     Auto.clearNumberAutos();

     Auto car1 = new Auto("BMW", 0, 0.0);
     Auto car2 = new Auto("Ferrari", 100, 500.0);
     Auto car3 = new Auto("Jeep", 1000, 90.0);
     Auto car4 = new Auto("Ferrari", 10, 3.0);
     Auto car5 = new Auto("BMW", 4000, 200.0);
     Auto car6 = new Auto("Ferrari", 1000, 50.0);

     carList.add(car1);
     carList.add(car2);
     carList.add(car3);
     carList.add(car4);
     carList.add(car5);
     carList.add(car6);
     animate();
   }

   // ***** 2.  Student writes this method
   /**  Prints carList to console, elements are separated by a space
   *    The instance variable carList is the ArrayList to be printed
   */
   public void printAutoList()
   {
     // Note:  To animate the algorithm, put this method call as the
     // last element in your for loop
     //    animate(car);
     //  where car is the variable name for the current Auto object
     //  as you loop through the ArrayList object
     // Part 2 student code starts here:
System.out.println(carList);
Auto car;
for (int i = 0; i < carList.size();i++)
{
    car=carList.get(i);
    animate(car);
}
     // Part 2 student code ends here.
   }

   // ***** 3.  Student writes this method
   /** Sets the model of all the elements in carList to parameter value
   *    The instance variable carList is the ArrayList to be modified
   *  @param model the model to assign to all Auto objects in carList
   */
   public void setModelValues(String model)
   {
     // Note:  To animate the algorithm, put this method call as the
     // last statement in your for loop
     //    animate(car);
     //  where car is the variable name for the current Auto object
     //  as you loop through the ArrayList object
     // Part 3 student code starts here:
       for (Auto car: carList)
       {
        carList.add(carList.get(car).toString());  
        System.out.println(model.toString(odel.toArray()));
         animate(car);
       }
     // Part 3 student code ends here.
   }

   // ***** 4.  Student writes this method
   /** Finds maximum number of miles driven
   *   Instance variable carList is the ArrayList to search
   *  @return the maximum miles driven by all the Auto objects
   */
   public int findMaximumMilesDriven()
   {
     // Note:  To animate the algorithm, put this method call as the
     // last statement in your for loop
     //    animate(car, maximum);
     //  where car is the variable name for the current Auto object
     //  and maximum is the int variable storing the current maximum
     //  number of miles for all Auto elements you have already tested
     //  as you loop through the ArrayList object
     // Part 4 student code starts here:



     return 0; // replace this statement with your return statement

     // Part 4 student code ends here.
   }


   // ***** 5.  Student writes this method
   /** Finds number of times parameter model is found in the carList
   *   Instance variable carList is the ArrayList in which we search
   *  @param model  the model to count
   *  @return   the number of times model was found
   */
   public int countFound(String model)
   {
     // Note:  To animate the algorithm, put this method call as the
     // last statement in your for loop
     //    animate(car, num);
     //  where car is the variable name for the current Auto object
     //  and num is the int variable storing the current number of
     //  Auto elements whose model is equal to the method's parameter
     //  as you loop through the ArrayList object
     // Part 5 student code starts here:



     return 0; // replace this statement with your return statement

     // Part 5 student code ends here.
   }

   public void startActivity( int act )
   {
      ad.setActivity( act );
      boolean goodInput = false;
      String answer = "";
      String message = "";
      switch( act )
      {
       case  0:
        fillWithCars( );
          JOptionPane.showMessageDialog( null, "carList filled with new values" );
          break;

       case  1:
        printAutoList( );
          JOptionPane.showMessageDialog( null, "carList printed" );
          break;

       case  2:
        answer = JOptionPane.showInputDialog( null, "Enter a car model" );
          if (answer != null )
          {
           ad.setSearchModel( answer );
           setModelValues( answer );
           if ( ad.getCurrentModelValuesSet( ) )
             message = "\nYour result is correct";
           else
             message = "\nYour result is not correct";
           JOptionPane.showMessageDialog( null, "car models set to " + answer + message );
          }
          break;

      case 3:
       int a = findMaximumMilesDriven( );
         if ( a == ad.getCurrentMaximumMilesDriven( ) )
          message = "\nYour result is correct";
         else
          message = "\nYour result is not correct";
         JOptionPane.showMessageDialog( null, "The maximum number of miles driven is " + a + message );
         break;

      case  4:
       answer = JOptionPane.showInputDialog( null, "Enter a car model" );
         if ( answer != null )
         {
          ad.setSearchModel( answer );
          int frequency = countFound( answer );
          if ( frequency == ad.getCurrentCountModelFound( ) )
           message = "\nYour result is correct";
          else
           message = "\nYour result is not correct";
          if ( frequency > 1 )
            JOptionPane.showMessageDialog( null, answer + " found " + frequency + " times" + message );
          else if ( frequency == 1 )
            JOptionPane.showMessageDialog( null, answer + " found " + frequency + " time" + message );
          else
            JOptionPane.showMessageDialog( null, answer + " not found" + message );
        }
        break;
      }
      enableButtons( );
   }

   public static Auto getCurrent( )
   {
    return current;
   }

   public static ArrayList getCarList( )
   {
    return carList;
   }

   private void animate( Auto au )
   {
    if ( ad.getActivity( ) == 1 || ad.getActivity( ) == 2 )
    {
     try
     {
      current = au;
      ad.setCarList( carList );
      ad.setCurrentAuto( au );
      ad.setCurrentIndex( au.getIndex( ) );
      repaint( );
      Thread.sleep( 4000 );
     }
     catch ( InterruptedException e )
     {
      System.out.println( "IE Exception " + e.getMessage( ) );
      System.out.println( e.toString( ) );
     }
    }
    else
    {
      // call to animate has wrong number of arguments
      JOptionPane.showMessageDialog( null, "Wrong number of arguments to animate method" );
      System.exit( 1 );
    }
   }

   private void animate( Auto au, int studentResult )
   {
    if ( ad.getActivity( ) == 3 || ad.getActivity( ) == 4 )
    {
     try
     {
      current = au;
      ad.setCarList( carList );
      ad.setCurrentAuto( au );
      ad.setCurrentIndex( au.getIndex( ) );
      ad.setStudentResult( studentResult );
      repaint( );
      Thread.sleep( 4000 );
     }
     catch ( InterruptedException e )
     {
      System.out.println( "IE Exception " + e.getMessage( ) );
      System.out.println( e.toString( ) );
     }
    }
    else
    {
      // call to animate has wrong number of arguments
      JOptionPane.showMessageDialog( null, "Wrong number of arguments to animate method" );
      System.exit( 1 );
    }
   }

   private void animate( )
   {
    if ( ad.getActivity( ) == 0 )
    {
     try
     {
      ad.setCarList( carList );
      repaint( );
      Thread.sleep( 4000 );
     }
     catch ( InterruptedException e )
     {
      System.out.println( "IE Exception " + e.getMessage( ) );
      System.out.println( e.toString( ) );
     }
    }
    else
    {
      // call to animate has wrong number of arguments
      JOptionPane.showMessageDialog( null, "Wrong number of arguments to animate method" );
      System.exit( 1 );
    }
   }

   public void paint( Graphics g )
   {
     if ( ( ( current != null ) || firstTime ) && ( ad.getActivity( ) != 0 ) )
     {
       super.paint( g );
       if ( ad.getCurrentAuto( ) != null )
           ad.updateAutoDisplay( current, g );
       firstTime = false;
     }
     else if ( ad.getActivity( ) == 0 )
     {
       super.paint( g );
       ad.updateAutoDisplay( g );
     }
   }

   public static void main( String [] args )
   {
      app = new ArrayListPractice( );
      app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }

   public void disableButtons( )
   {
     fillValues.setEnabled( false );
     printAutoList.setEnabled( false );
     setValues.setEnabled( false );
     countFrequency.setEnabled( false );
     findMaximum.setEnabled( false );
   }

   public void enableButtons( )
   {
     fillValues.setEnabled( true );
     printAutoList.setEnabled( true );
     setValues.setEnabled( true );
     countFrequency.setEnabled( true );
     findMaximum.setEnabled( true );
   }

   private class ButtonHandler implements ActionListener
   {
    private boolean on = true;
    public void actionPerformed( ActionEvent e )
    {
      printAutoListT t = new printAutoListT( app );

      if ( e.getSource( ) == fillValues )
      {
        disableButtons( );
        fillValues.requestFocus( );
        ad.setActivity( 0 );
        t.start( );
      }
      else if ( e.getSource( ) == printAutoList )
      {
        disableButtons( );
        printAutoList.requestFocus( );
        ad.setActivity( 1 );
        t.start( );
      }
      else if ( e.getSource( ) == setValues )
      {
        disableButtons( );
        setValues.requestFocus( );
        ad.setActivity( 2 );
        t.start( );
      }
      else if ( e.getSource( ) == findMaximum )
      {
        disableButtons( );
        findMaximum.requestFocus( );
        ad.setActivity( 3 );
        t.start( );
      }
      else if ( e.getSource( ) == countFrequency )
      {
        disableButtons( );
        countFrequency.requestFocus( );
        ad.setActivity( 4 );
        t.start( );
      }
    }
  }

  private class printAutoListT extends Thread
  {
     ArrayList<Auto> arr;
     ArrayListPractice s1;
     public printAutoListT ( ArrayListPractice s )
     {
      arr = ArrayListPractice.carList;
      s1 = s;
     }
     public void run( )
     {
      startActivity( ad.getActivity( ) );
     }
  }
}
自动类:

import java.text.DecimalFormat;
import java.awt.Graphics;

public class Auto
{
    // Static instance variable - number of Auto objects references created
    private static int numberAutos = 0;
    // Instance variables
    private String model;           //  model of auto
    private int milesDriven;       //  number of miles driven
    private double gallonsOfGas;   //  number of gallons of gas
    private int index;        //  car number

    // Default constructor:
    //  Initializes model to a blank String
    //  milesDriven are autoinitialized to 0, gallonsOfGas to 0.0
    public Auto( )
    {
      model = "";
      index = numberAutos;
      numberAutos++;
    }

    // Overloaded constructor:
    // Allows client to set beginning values for
    //   model, milesDriven, and gallonsOfGas.
    //   Calls mutator methods to validate new values.
    public Auto( String startModel,
                 int startMilesDriven,
                 double startGallonsOfGas )
    {
      model = startModel;
      setMilesDriven( startMilesDriven );
      setGallonsOfGas( startGallonsOfGas );
      index = numberAutos;
      numberAutos++;
    }

    // Accessor method:
    // Returns current value of index
    public int getIndex( )
    {
      return index;
    }

    // Accessor method:
    // Returns current value of model
    public String getModel( )
    {
      return model;
    }

    // Accessor method:
    // Returns current value of milesDriven
    public int getMilesDriven( )
    {
      return milesDriven;
    }

    // Accessor method:
    //  Returns current value of gallonsOfGas
    public double getGallonsOfGas( )
    {
      return gallonsOfGas;
    }

    // Mutator method:
    // Allows client to set model
    public void setModel( String newModel )
    {
      model = newModel;
    }

    // Mutator method:
    // Allows client to set value of milesDriven
    // prints an error message if new value is less than 0
    public void setMilesDriven( int newMilesDriven )
    {
      if ( newMilesDriven >= 0 )
         milesDriven = newMilesDriven;
      else
      {
         System.err.println( "Miles driven must be at least 0." );
         System.err.println( "Value not changed." );
      }
    }

    // Mutator method:
    // Allows client to set value of gallonsOfGas;
    // prints an error message if new value is less than 0.0
    public void setGallonsOfGas( double newGallonsOfGas )
    {
      if ( newGallonsOfGas >= 0.0 )
         gallonsOfGas = newGallonsOfGas;
      else
      {
         System.err.println( "Gallons of gas must be at least 0." );
         System.err.println( "Value not changed." );
      }
    }

    // Mutator method
    // Allows client to add miles driven and gallons of gas used
    //  to current values;
    // prints error messages if new values are less than 0
    public void addMileage( int newMilesDriven,
                            double newGallonsOfGas )
    {
      if ( newMilesDriven < 0 )
      {
         System.err.println( "Miles driven must be at least 0." );
         System.err.println( "Value not changed" );
         return;   // do not continue executing method
      }

      if ( newGallonsOfGas < 0.0 )
      {
         System.err.println( "Gallons of gas must positive." );
         System.err.println( "Value not changed" );
         return;  // do not continue executing method
      }

      // ok to change values
      milesDriven += newMilesDriven;  // add newMilesDriven
      gallonsOfGas += newGallonsOfGas; // add newGallonsOfGas
    }

    // Calculates mileage as miles per gallon.
    //  If no gallons of gas have been used, returns 0.0;
    //  Otherwise, returns miles per gallon
    //        as milesDriven / gallonsOfGas
    public double calculateMilesPerGallon( )
    {
      if ( gallonsOfGas != 0.0 )
         return milesDriven / gallonsOfGas;
      else
         return 0.0;
    }

    public static void clearNumberAutos( )
    {
        numberAutos = 0;
    }

    public String toString( )
    {
      DecimalFormat gallonsFormat = new DecimalFormat( "##.0" );
      return "Model: " + model
           + " Miles driven: " + milesDriven
           + " Gallons of gas: "
           + gallonsFormat.format( gallonsOfGas );
    }

    public void draw( Graphics g, int startX, int endX, int y)
    {}

     } // end Auto class definition

任何帮助都将不胜感激!谢谢大家!

您没有显示Auto类,但它可能有一个方法setModelString model?是的,它有。很抱歉,我是新来的,所以我不知道如何将其发布到公共void setModel字符串newModel{model=newModel;}