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

1) Create a Java NetBeans project named AddLineNumbers with the main class named

ID: 3560110 • Letter: 1

Question

1) Create a Java NetBeans project named AddLineNumbers with the main class named AddLineNumbers . This program will read data from a text file "dataInput.txt" and while there are more records, prepend a line number to the text data and output the concatenated data to "dataOutput.txt" implementing a try/catch block to catch any file errors thrown by the JVM.

Follow this pattern.

package addlinenumbers;

/* Import the Scanner class and all classes from java.io */

// Your code here

public class AddLineNumbers

{

public static void main( String [] args )

{

/* Declare variable identifiers to handle text data input from the file and count/increment the linenumber */

// Your code here

/* Start try block */

// Your code here

/* Instantiate Scanner object for data input from file "dataInput.txt". There should be no path specification as your input file should be located in your AddLineNumbers project folder */

// Your code here

/* Instantiate a FileOutputStream object for file "dataOutput.txt". Do not specify a path. Your file will be created in the project folder by NetBeans */

// Your code here

/* Instantiate a PrintWriter object for writing data to your FileOutputStram object */

// Your code here

/* Using a while loop control structure, read one line of data from input file, prepend the line number to data read from the file and output concatenated data to output file */

// Your code here

/* Close files and output message indicating the file has been written */

// Your code here

/* End try block */

/* Code FileNotFoundException and IOException catch blocks. Call getMessage(), toString() and printStackTrace() methods on exception object thrown */

// Your code here

}

}

2) Write a class named CarPlate encapsulating the concept of a car 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 and writes them to a file as objects, then reads them from the file as objects, outputs a description of each of the objects 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 and detect the end of file when an EOFException is thrown by the readObject method.

Work this problem similar to the last problem and use those methods.

3) Write an abstract superclass encapsulating a vehicle: A vehicle has two attributes: its owner's name and its number of wheels. This class has two non-abstract subclasses: one encapsulating a bicycle, ant the other encapsulating a motorized vehicle. A motorized vehicle has the following additional attributes: its engine volume displacement, in liters; and a method computing and returning a measure of horsepower which is the number of liters times the number of wheels. You also need to include a client class to test these two classes.

Please show all steps and the output for all problems.

Explanation / Answer

1).

import java.io.*;
import java.util.*;

class CarPlate implements Serializable{
private int plateno;
private String color;
  
public CarPlate( int plateno, String color ){
this.plateno = plateno;
this.color = color;
}
@Override
public String toString() {
return String.format("Plate No: " + plateno + "Color: " + color);
}
}

public class CarPlateDemo{
public static void main( String [] args ) throws Exception{
  
List<CarPlate> CarPlateList = new ArrayList<CarPlate>();
  
CarPlate c1 = new CarPlate( 1111, "White");
CarPlate c2 = new CarPlate( 2222, "White");
CarPlate c3 = new CarPlate( 3333, "Balck");
  
CarPlateList.add(c1);
CarPlateList.add(c2);
CarPlateList.add(c3);
  
try {

// create a new file with an ObjectOutputStream
FileOutputStream file = new FileOutputStream("carplate_data.obj");
ObjectOutputStream out = new ObjectOutputStream(file);

// write something in the file
out.writeObject(CarPlateList);
out.flush();
out.close();
}catch( IOException e ) {
System.out.println(e.getMessage());
}
  
ObjectInputStream in = null;
  
try{
int i = 0;
//List<CarPlate> carplate_list = null;
in = new ObjectInputStream(new FileInputStream("carplate_data.obj"));
System.out.println(in.readObject());
CarPlate carplate = null;
//carplate_list = new ArrayList<CarPlate>();
do{
carplate = (CarPlate) in.readObject();
if(carplate != null){
System.out.println(carplate);
}
}while (carplate != null);
}catch( EOFException e ){
System.out.println("End of file.");
}catch( IOException e ){
System.out.println(e.getMessage());
}finally{
in.close();
}
}
}

2)

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

import java.io.*;
import java.util.*;

/**
*
* @author
*/
public class AddLineNumbers {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException{

try {
FileReader fr = new FileReader("dataInput.txt");
BufferedReader br = new BufferedReader(fr);

long lineno = 1;
String text = "";
File outputfile = new File("dataOutput.txt");

// if file doesnt exists, then create it
if (outputfile.exists()) {
System.out.println("File already exists. New file will be created.");
outputfile.createNewFile();
}
System.out.println();
String line = "";
while ((line = br.readLine()) != null) {
text+= lineno + " " + line + " ";
lineno++;
}
FileWriter fw = new FileWriter(outputfile.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(text);
bw.close();
br.close();
System.out.println("Text written successfully.");

} catch (IOException e) {
e.getMessage();
}
}
}