Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Computer Programming Help Write a class named CarPlate encapsulating the concept

ID: 3721555 • Letter: C

Question

Computer Programming Help

Write a class named CarPlate encapsulating the concept of a license plate, assuming that it has the following attributes: the plate number, the state and its color. Write a client program that creates three CarPlate objects, writes them to a file as objects, then reads them from a file as objects, outputs a description of the object using the toString() method (which the CarPlate class should override), and outputs the number of objects. When reading the objects, you should assume that you do not know the number of objects in the file.

Explanation / Answer

Please find my implementation.

CarPlate.java

package com.chegg.testing;

public class CarPlate {
   private int plateNumber;
   private String state;
   private String color;

   public CarPlate(int num, String stateName, String colour) {
       this.plateNumber = num;
       this.state = stateName;
       this.color = colour;
   }

   /**
   * @return the plateNumber
   */
   public int getPlateNumber() {
       return plateNumber;
   }

   /**
   * @param plateNumber
   * the plateNumber to set
   */
   public void setPlateNumber(int plateNumber) {
       this.plateNumber = plateNumber;
   }

   /**
   * @return the state
   */
   public String getState() {
       return state;
   }

   /**
   * @param state
   * the state to set
   */
   public void setState(String state) {
       this.state = state;
   }

   /**
   * @return the color
   */
   public String getColor() {
       return color;
   }

   /**
   * @param color
   * the color to set
   */
   public void setColor(String color) {
       this.color = color;
   }

   public String toString() {
       return "PlateNumber : " + plateNumber + "State : " + state
               + " Color : " + color;
   }
}

CarPlateText.java

package com.chegg.testing;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

public class CarPlateText {
   public static void main(String[] args) throws ClassNotFoundException {

       CarPlate c1 = new CarPlate(12345, "Ontario", "Red");
       CarPlate c2 = new CarPlate(67891, "Quebec", "Yellow");
       CarPlate c3 = new CarPlate(23456, "Alberta", "Blue");
       try {
           FileWriter fw = new FileWriter("output1.txt", true);
           fw.write(c1.toString() + " ");
           fw.write(c2.toString() + " ");
           fw.write(c3.toString() + " ");
           fw.close();

           // Put some bytes in a buffer so we can
           // write them. Usually this would be
           // image data or something. Or it might
           // be unicode text.
           String bytes = "Hello theren";
           byte[] buffer = bytes.getBytes();

           FileOutputStream outputStream = new FileOutputStream("output.bin");

           // write() writes as many bytes from the buffer
           // as the length of the buffer. You can also
           // use
           // write(buffer, offset, length)
           // if you want to write a specific number of
           // bytes, or only part of the buffer.
           outputStream.write(buffer);

           // Always close files.
           outputStream.close();

           System.out.println("Wrote " + buffer.length + " bytes");

           // Use this for reading the data.
           byte[] buff = new byte[1000];

           FileInputStream inputStream = new FileInputStream("output.bin");

           // read fills buffer with data and returns
           // the number of bytes read (which of course
           // may be less than the buffer size, but
           // it will never be more).
           int total = 0;
           int nRead = 0;
           FileWriter fwriter=null;
           while ((nRead = inputStream.read(buff)) != -1) {
               // Convert to String so we can display it.
               // Of course you wouldn't want to do this with
               // a 'real' binary file.
               fwriter = new FileWriter("output2.txt", true);
               fwriter.write(c1.toString() + " ");
               fwriter.write(c2.toString() + " ");
               fwriter.write(c3.toString() + " ");
               fwriter.close();
               //System.out.println(new String(buff));
               total += nRead;
           }

           // Always close files.
           inputStream.close();

           System.out.println("Read " + total + " bytes");
       } catch (IOException ioe) {
           System.out.println("Excepton while creating a file.");
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote