Diagram 81 1 Refer to class diagram 81 Write the code for th

Diagram 8-1:

1. (Refer to class diagram 8-1.) Write the code for the ColorConstants interface. This interface should declare constants for the colors red, white, and black and assign the int values 1, 2, and 3 to those constants.

2. (Refer to class diagram 8-1.) Write the code for the AutoReader interface. This interface should declare two methods. The first one, named getAuto, should accept an id and return an Automobile object for the automobile with that id. The second one, named getAutosByColor, should accept an integer that represents a color in the ColorConstant interface and return a string that contains the ids of all the automobiles that are that color.

3. (Refer to class diagram 8-1.) Write the code for the AutoTextFile class. Simulate the getAuto method by creating a new Automobile object with the id that’s passed to the method. Don’t worry about setting the values of the other instance variables. Simulate the getAutosByColor method by using a switch statement to return a string of numbers depending on which color is specified (you choose the numbers). If an invalid color number is specified, return a null.

4. (Refer to class diagram 8-1.) Write code for the AutoDisplayApp class that uses the getAuto method defined by the AutoTextFile class to get an Automobile object with a specified id and then prints a message with the id in this format:

Auto 1234 read successfully.

Assume that a long variable named id has already been declared and assigned a value.

5. (Refer to class diagram 8-1.) Write code that uses the getAutosByColor method defined by the AutoTextFile class to get the ids for all the autos with a specified color. If one or more autos are found, a message should be printed in this format:

Red automobiles: 1,4,7,10

Otherwise, this message should be printed:

There are no automobiles with that color.

Assume that an int variable named color has already been declared and assigned a color value. Use the constants in the ColorConstants interface in your code.

AutoDisplay App Auto TextFile Auto Reader ColorConstants Automobile

Solution

// AutoDisplayApp


public class AutoDisplayApp {

   public static void main(String[] args)
   {
       AutoTextFile at = new AutoTextFile();
       long id = 1234;
       Automobile auto = at.getAuto(id);
       System.out.println(\"Auto \" + auto.getId() + \" read successfully\");
      
       String redIds = at.getAutosByColor(ColorConstants.RED);
       if (redIds != null)
           System.out.println(\"Red automobiles: \" + redIds);
       else
           System.out.println(\"There are no automobiles with red color.\");
      
       String whiteIds = at.getAutosByColor(ColorConstants.WHITE);
       if (whiteIds != null)
           System.out.println(\"White automobiles: \" + whiteIds);
       else
           System.out.println(\"There are no automobiles with white color.\");
      
       String blackIds = at.getAutosByColor(ColorConstants.BLACK);
       if (blackIds != null)
           System.out.println(\"black automobiles: \" + blackIds);
       else
           System.out.println(\"There are no automobiles with black color.\");
      
       int color = 5;
       String colorIds = at.getAutosByColor(color);
       if (colorIds != null)
           System.out.println(\"some color automobiles: \" + colorIds);
       else
           System.out.println(\"There are no automobiles with this color.\");
   }
}

// ****************************************************************************************************************

// AutoTextFile.java


public class AutoTextFile implements AutoReader{

   @Override
   public Automobile getAuto(long id) {
       return new Automobile(id);
   }

   @Override
   public String getAutosByColor(int color) {
      
       switch (color) {
           case ColorConstants.RED:
               return \"1,2,3,4\";
           case ColorConstants.WHITE:
               return \"5,6,7\";
           case ColorConstants.BLACK:
               return \"9,10,34\";
           default:
               break;
       }
       return null;
   }

}

// ***********************************************************************************************

// Automobile.java


public class Automobile {

   private long id;
   public Automobile() {
   }
  
   public Automobile(long id)
   {
       this.id = id;
   }
  
   public long getId()
   {
       return id;
   }
  
   public void setId(long id)
   {
       this.id = id;
   }
}

// ***********************************************************************************************************

// AutoReader.java


public interface AutoReader{

   Automobile getAuto(long id);
   String getAutosByColor(int color);
}

// ************************************************************

// ColorConstants.java


public interface ColorConstants {

   final int RED = 1;
   final int WHITE = 2;
   final int BLACK = 3;
}

Diagram 8-1: 1. (Refer to class diagram 8-1.) Write the code for the ColorConstants interface. This interface should declare constants for the colors red, white
Diagram 8-1: 1. (Refer to class diagram 8-1.) Write the code for the ColorConstants interface. This interface should declare constants for the colors red, white
Diagram 8-1: 1. (Refer to class diagram 8-1.) Write the code for the ColorConstants interface. This interface should declare constants for the colors red, white

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site