Create a system with three classes (one of which is a test driver): 1. Create a
ID: 3909779 • Letter: C
Question
Create a system with three classes (one of which is a test driver):
1. Create a CodeBot class (CodeBot.java) that has two private fields: className and code (both are of type String). Create a no-arg constructor that hard-codes the class name (e.g., Hello) and initializes the code field with a string that represents a simple Java class that displays something (e.g, “hello, world”) to standard output (e.g.,"public class Hello { public static void main(String[] args) { System.out.println("hello, world");}}"). Create another constructor that requires that the class name and code are provided by the caller (i.e., a constructor with two String parameters). Both constructors should call the private “saveCode” method (see below) to save the code to the file system as part of constructing a CodeBot object. Create a private helper method named saveCode that is based on Listing 12.14 on page 477 (Liang, 10th edition). This method should have a void return type and result in a file being written in your project’s root directory. Create a public compile method that accepts no input arguments. If the class name is not found in the code, the method will throw a CompilationExceptionwith the message “Class name must be found in code.” Otherwise, it will print out a message: “The program className.java has compiled successfully.”
2. Create a CompilationException class ( CompilationException.java) that is a subclass of Exception (i.e., a custom checked exception).
3. Create a CodeBotTest class (CodeBotTest.java) that instantiates a CodeBot object using the no-arg constructor and then call the compile method. It should then create another CodeBot object using the other constructor with className = “Hello2” and code = "public class { public static void main(String[] args) {System.out.println("hello, world");}}" and then call the compile method.
Here is a sample run:
Program Hello.java has compiled successfully.
Exception in thread "main" CompilationException: Class name must be found in code.
at CodeBot.compile(CodeBot.java:33)
at CodeBotTest.main(CodeBotTest.java:7)
The second time you run your program, it should print out the following message:
File Hello.java already exists.
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
CodeBot.java
======
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class CodeBot {
private String className, code;
public CodeBot(){
className = "Hello";
code = "public class Hello { public static void main(String[] args) { System.out.println("hello, world");}}";
saveCode();
}
public CodeBot(String className, String code){
this.className = className;
this.code =code;
saveCode();
}
private void saveCode(){
try {
File f = new File(className + ".java");
if(f.exists())
{
System.out.println("File " + f.getName() + " already exists");
System.exit(0);
}
PrintWriter pw = new PrintWriter(f);
pw.println(code);
pw.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
public void compile() throws CompilationException{
if(code.indexOf(className) == -1)
throw new CompilationException("Class name must be found in code.");
System.out.println("The program " + className + ".java has compiled successfully");
}
}
CompilationException.java
------
public class CompilationException extends Exception {
public CompilationException() {
}
public CompilationException(String message) {
super(message);
}
}
CodeBotTest.java
--------
public class CodeBotTest {
public static void main(String[] args) throws CompilationException {
CodeBot b1 = new CodeBot();
b1.compile();
CodeBot b2 = new CodeBot("Hello2", "public class { public static void main(String[] args) {System.out.println("hello, world");}}");
b2.compile();
}
}
output
=======
The program Hello.java has compiled successfully
Exception in thread "main" CompilationException: Class name must be found in code.
at CodeBot.compile(CodeBot.java:32)
at CodeBotTest.main(CodeBotTest.java:8)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.