Knowee
Questions
Features
Study Tools

Create a Java program based on what you have learned so far. Your output MUST HAVE at least two (2) of the following concepts:Objects, Classes, MethodsInheritanceException HandlingJava File Input/Output

Question

Create a Java program based on what you have learned so far. Your output MUST HAVE at least two (2) of the following concepts:Objects, Classes, MethodsInheritanceException HandlingJava File Input/Output

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple Java program that uses Classes, Methods, Inheritance, Exception Handling and Java File Input/Output:

import java.io.*;

class Person {
    String name;
    int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    void display() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

class Employee extends Person {
    int salary;

    Employee(String name, int age, int salary) {
        super(name, age);
        this.salary = salary;
    }

    @Override
    void display() {
        super.display();
        System.out.println("Salary: " + salary);
    }
}

public class Main {
    public static void main(String[] args) {
        Employee emp = new Employee("John", 30, 5000);
        emp.display();

        try {
            FileWriter writer = new FileWriter("employee.txt");
            writer.write("Name: " + emp.name + "\n");
            writer.write("Age: " + emp.age + "\n");
            writer.write("Salary: " + emp.salary + "\n");
            writer.close();
            System.out.println("Successfully wrote to the file.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

This program creates a Person class with name and age as attributes and a display method to print these attributes. It also creates an Employee class that extends Person and adds a salary attribute. The Employee class also overrides the display method to include the salary.

In the main method, an Employee object is created and its details are displayed using the display method. Then, a FileWriter is used to write the employee's details to a file named employee.txt. Exception handling is used to catch and handle any IOException that might occur during file operations.

This problem has been solved

Similar Questions

e a Java program for the above with proper exception handling.Input-Output Form 1:

Handling Exceptions in File I/O Operations Instruction: 1. Create a Java program that reads data from a text file named "input.txt" and copies it to another text file named "output.txt". 2. Implement exception handling to deal with potential I/O errors, such as file not found, permission denied, or input/output errors. 3. Use FileReader, BufferedReader for reading from the input file, and FileWriter, BufferedWriter for writing to the output file. 4. Display informative error messages in case of exceptions.

Copy the code of this Java file: ExceptionSample.javaUsing JCreator, edit ExceptionSample so it can catch exceptions wherein text is entered as String input instead of integer data type input.Submit a copy of your completed Java program to the instructor before the start of the next class.

how to write java application

orrect answerWhat is the output of this program? (Assume 'inputoutput.java' file exists in the current directory)import java.io.*;class filesinputoutput { public static void main(String args[]) throws FileNotFoundException, IOException { InputStream obj = new FileInputStream("inputoutput.java"); System.out.print(obj.available()); obj.close(); }}

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.