Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

java - How to print the array index of an object class?

I have a somewhat big code and I'm triying to print the slot number of an array from an object class that resulted to be the one with the highest weight.

On my main I have declared my array like this:

Car x[] = new car[2];

Now when triying to get which car has the highest weight I've made this validation inside a for loop

//.getweight obtains the weight of each car

if(x[i].getWeight() > max){

  max = x[i].getWeight();

 }

That code gives me the actual weight number of the heaviest car, but what I want to print is the actual car number. The comparing works but I can't find a way to just print the slot that corresponds to the heaviest car.

UPDATE with full code:

My object car:

public class Car
{

private String color;
private int weight;
private int state;
private int fuel;
private int Maxspeed ;
private int engine;



public Car(){
 this.color = "White";
 this.weight = 1000;
 this.state = 0;
 this.fuel =0;
 this.Maxspeed = 0;
 this.engine = 0;
}

public Car( String color, int weight,
            int state, int fuel, int Maxspeed 
            ){
   this.color = color;
   this.weight = weight;
   this.state = state;
   this.fuel = fuel;
   this.Maxspeed = Maxspeed;
}




public String getColor(){
  return this.color;
}

public int getweight(){
  return this.weight;
} 

public int getstate(){

   return this.state;
}

public int getfuel(){
  return this.fuel;
}

public int getMaxspeed(){
  return this.Maxspeed;
}

public int getengine(){
    return this.engine;
}


public void setColor( String color ){
  this.color = color;

}

public void setweight( int weight ){
   this.weight = weight;
}

public void setstate( int state ){

   this.state = state;

}

public void setfuel( int fuel ){
  this.fuel = fuel;
}

public void setMaxspeed( int Maxspeed ){
  this.Maxspeed = Maxspeed;
}

public void setengine(int engine){
   this.engine = engine;
}




public void showdata(){
  System.out.println( "
Car's color is: " + this.getColor() );
  System.out.println( "Car's weight is: " + this.getweight() );
  System.out.println( "State: " + this.getstate() );
  System.out.println( "Fuel: " + this.getfuel());
  System.out.println( "Max speed: "  + this.getMaxspeed());


}

public void accelerate( int speed ){
  if( this.getstate() == 0 || 
      this.getstate() == 3 || 
      this.getstate() == 4 || 
      this.getMaxspeed() < speed )
   {
    System.out.println("
Car cannot accelerate...");
   }
   else{
      System.out.println("
Car is accelerating...");
      this.setfuel(this.getfuel()-2);
      this.setstate(2);
        if( this.getfuel() <= 0 ){
           this.setstate(4);
        }
    }


}

   public void crash(){
      this.setstate(3);
      System.out.println("
Crash!!!");
    }
   public void stop(){
      this.setstate(1);
      System.out.println("
Car has stopped.");
    }

   public void addfuel(int fuel){

       if(this.getstate() == 0 || this.getstate() == 4){
           this.setfuel(this.getfuel()+ fuel);
       }
       else{
           System.out.println("You can't add fuel.");
        }


    }

   public void repair(){

       if(this.getstate() == 3){
           this.setstate(1);
           System.out.println("The car has been repaired");
        }
       else{
           System.out.println("The car is not broken");
        } 
    }


}

My Main:

import java.util.Scanner;
public class aaa{
public static void main (String args []){    
    Car x[] = new Car[2];
        int keep=1;
        int counter = 0;
        int counter_stopped = 0;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        int maxIndex = 0;
        int maxweight = 0;
        int index_engine = 0;
        int min_engine = 0;

        Scanner input = new Scanner(System.in);

        for(int i = 0; i < x.length; i++){
            String color;
            int weight;
            int fuel;
            int Maxspeed;
            int engine;

            x[i] = new Car();


            System.out.print("
Enter car color " + (i + 1) + ": ");
            color = input.next();

            System.out.print("Enter car weight " + (i + 1) + ": ");
            weight = input.nextInt();

            System.out.print("Enter car fuel " + (i + 1) + ": ");
            fuel = input.nextInt();

            System.out.print("Enter car max speed " + (i + 1) + ": ");
            Maxspeed = input.nextInt();

            System.out.print("Enter car engine weight " + (i + 1) + ": ");
            engine = input.nextInt();


            x[i].setColor(color);
            x[i].setweight(weight);
            x[i].getstate();
            x[i].setfuel(fuel);
            x[i].setMaxspeed(Maxspeed);
            x[i].setengine(engine);


        }        



        for(int i = 0; i < x.length; i++){


                int state;

                System.out.print("
Enter car state " + (i + 1) + ": ");
                state = input.nextInt();
                x[i].setstate(state);

                while(state > 4 || state < 0){
                    System.out.print("state not valid.
Try again: ");
                    state = input.nextInt();
                    x[i].setstate(state);
                }


                do {

                    keep = menu();

                    switch( keep ){
                     case 1:
                        accelerate(x[i]);
                        break;

                     case 2:
                        stop(x[i]);
                        break;

                     case 3:
                           crash(x[i]);
                           break;

                     case 4:
                           addfuel(x[i]);
                           break;

                     case 5:
                           repair(x[i]);
                           break;      

                     case 6:
                           x[i].showdata();

                }

            }   while(keep != 7);   



                if(x[i].getstate() == 4 || x[i].getfuel() <= 0){
                    counter += 1;
                }

                if(x[i].getstate() == 1){
                    counter_stopped += 1;
                }

                int weight = x[i].getweight();
                if(weight > max){
                     maxweight = weight;
                     maxIndex = i;
                 }


                int weightengine = x[i].getengine();
                 if(weightengine < min){

                  min_engine = weightengine;
                  index_engine = i;


                }


        }

        System.out.println("
SUMMARY");
        System.out.println("Amount of cars with no fuel: " + counter);
        System.out.println("Amount of stopped cars: " + counter_stopped);
        System.out.println("Heaviest car: " + maxIndex);
        System.out.println("Car with the smallest engine: " + index_engine);
        System.out.println("=============================================");
    }

  public static int menu(){
      int option = 0;
      Scanner s = new Scanner(System.in);

      System.out.println("
1. Accelerate Car ");
      System.out.println("2. Stop Car ");
      System.out.println("3. Crash Car ");
      System.out.println("4. Add fuel ");
      System.out.println("5. Repair ");
      System.out.println("6. Show data ");
      System.out.println("7. Exit ");
      System.out.println("=============================================");
      System.out.print("Choose an option : ");
      option = s.nextInt();
      System.out.println("=============================================");
      return option;

    } 

  public static void accelerate(Car myCar){

  Scanner input = new Scanner(System.in);  
  int s;

  System.out.print("Enter speed: ");
  s =  input.nextInt();
  myCar.accelerate(s);
  //myCar.showdata();

}

public static void stop(Car myCar){
    myCar.stop();
}

public static void crash(Car myCar){
    myCar.crash();
}

public static void addfuel(Car myCar){
    int fuel;
    Scanner input = new Scanner(System.in);  
    System.out.print("Amount to add: ");
    fuel = input.nextInt();
    myCar.addfuel(fuel);
}

public static void repair(Car myCar){
    myCar.repair();


}


}

Now, when I compile and test which engine or car is smaller or heaviest I get the number 1 as result.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The array index would be i

The actual car would be x[i]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...