Recieved error with this: Error: Main method not found in class course_grade.Cou
ID: 666521 • Letter: R
Question
Recieved error with this: Error: Main method not found in class course_grade.Course_Grade, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Java Result: 1
Someone please write the corrected code.
package carplate;
import java.io.*;
//@author Daniela Lopez
public class CarPlate implements Serializable {
{
String plateNumber;
private String state;
private String color;
public CarPlate() {} // default constructor
public static void main(String argsp[])
{
new CarPlate();
}
public CarPlate(String pNumber, String st, String clr) {
plateNumber = pNumber;
state = st;
color = clr;
} // end overloaded constructor
@Override
public String toString() {
String s = "plate number: "+ plateNumber;
s += " state: "+state;
s += " color: "+color;
return s;
} // end toString()
} // end class CarPlate
class TestCarPlate {
public static void main(String[] args) throws IOException {
CarPlate plate0 = new CarPlate("ABC 123", "New Jersey", "Silver");
CarPlate plate1 = new CarPlate("DEF 1000", "Ohio", "Red");
CarPlate plate2 = new CarPlate("GH 3", "Kentucky", "Blue");
File f = new File("cardata.tmp");
ObjectOutputStream oos = null;
try {
FileOutputStream fos = new FileOutputStream(f);
oos = new ObjectOutputStream(fos);
oos.writeObject(plate0);
oos.writeObject(plate1);
oos.writeObject(plate2);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) oos.close();
} // end try
int c = 0;
CarPlate cp;
ObjectInputStream ois = null;
try {
FileInputStream fis = new FileInputStream(f);
ois = new ObjectInputStream(fis);
while (true) {
cp = (CarPlate) ois.readObject();
System.out.println(cp.toString());
c++;
} // end while
} catch (EOFException e) {
System.out.println("Number of objects = "+c);
} catch (ClassNotFoundException e) {
System.out.println("Class not found");
} finally {
if (ois != null) ois.close();
} // end try
} // end main()
} // end class TestCarPlate
Explanation / Answer
//CarPlate.java
/*A class carplate to create an instance of CarPlate with plate number,state and color. The method toString
that returns the string description of CarPlate object .
Create this package in eclipse project
before create classes*/
package carplate; //package name ,create this packate before creating classes
import java.io.Serializable;
//The class implements the interface Serilizable
//Serialization is used in communication of two machines over a network
//to convert an object into byte array and re-assemble the byte array to
//back to object and vice versa between communicating devices.
public class CarPlate implements Serializable
{
String plateNumber;
private String state;
private String color;
// default constructor
public CarPlate()
{
plateNumber="unknown";
state="unknown";
color="unknown";
}
public CarPlate(String pNumber, String st, String clr)
{
plateNumber = pNumber;
state = st;
color = clr;
} // end overloaded constructor
@Override
public String toString()
{
String s = "plate number: "+ plateNumber;
s += " state: "+state;
s += " color: "+color;
return s;
} // end toString()
} // end class CarPlate
------------------------------------------------------------------------------------------------------
/**Driver program that creates three objects of CarPlate class
and write to a binary file called cardata.tmp(Since it is binary file
data is not readable and it should read throught the program only)
and read the binary file and display the objects of the CarPlate
on console.*/
//TestCarPlate.java
//Create this package in eclipse project
//before create classes
package carplate;//package name
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
class TestCarPlate
{
public static void main(String[] args) throws IOException
{
//create three instances of CarPlate class
CarPlate plate0 = new CarPlate("ABC 123", "New Jersey", "Silver");
CarPlate plate1 = new CarPlate("DEF 1000", "Ohio", "Red");
CarPlate plate2 = new CarPlate("GH 3", "Kentucky", "Blue");
//create a File class object using file name
File f = new File("cardata.tmp");
//declare a class variable ObjectOutputStream as null
ObjectOutputStream oos = null;
try
{
//Create a class FileOutputSteram object to write
//the three objects of class CarPlate to file, f
FileOutputStream fos = new FileOutputStream(f);
//create an instance of ObjectOutputStream
oos = new ObjectOutputStream(fos);
//write object using writeObject method
oos.writeObject(plate0);
//write object using writeObject method
oos.writeObject(plate1);
//write object using writeObject method
oos.writeObject(plate2);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (oos != null)
oos.close();
} // end try
int c = 0;
CarPlate cp;
ObjectInputStream ois = null;
//reading binary file ,cardata.tmp using the class ObjectInputStream object,ois
try
{
//open input file using FileInputStream class passing
//file,f as argument
FileInputStream fis = new FileInputStream(f);
//create an instance of class ObjectInputStream
ois = new ObjectInputStream(fis);
//read object data from the file using readObject
//(since file objects wrote as objects to file cardata.tmp)
while (true)
{
//read objects using readObject method
cp = (CarPlate) ois.readObject();
//print the car object
System.out.println(cp.toString());
//increment c by one to count number of objects that read from file,cardata.tmp
c++;
} // end while
}
catch (EOFException e)
{
//Display the number of objects are read
//print c vlaue
System.out.println("Number of objects = "+c);
}
catch (ClassNotFoundException e)
{
System.out.println("Class not found");
}
finally
{
if (ois != null) ois.close();
} // end try
} // end main
}//end of tester class
-----------------------------------------------------------------------------------------------
Sample output:
plate number: ABC 123 state: New Jersey color: Silver
plate number: DEF 1000 state: Ohio color: Red
plate number: GH 3 state: Kentucky color: Blue
Number of objects = 3
Note: As you said, TestCarPlate class must have main method to run the program. No need to have main method in the class CarPlate class.
Hope this helps you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.