Question

In java. You are provided an abstract class called MyAbstract. You can only modify the writeFile () class. You are provided another class called Finally you are provided the file you must read from in the MyAbstract class. You will need to extend MyAbstract to MyTestClass.
You should have the following outputs.

The content of myData
employee avg salary
number of employees
File that was created in myFile()

l. Servers ary 1 35 1.81 age) t.java ss.java MyAbstract.java MytestClass.java myData emport java.io. BufferedReader; import j

Expl... Servers Library JavaSt-1.a MyAbstract.java MyPestClass.jave myata NU BLY Icout TELUI SULALLA! 17 DO NOT Moly Buffere

Expl... - Servers Orary (avast 10 1846 0 ON: My bitrat Java b MyTea Na Java yoạta import java.10.IOException: Import java.uti

Servers myData waSE-1.8] MyAbstract.java MyTestClass.java 1 Bob, Smith,@1234, 120000 2 Katy Zahnson, @5625, 20000 3 Alex, Ki

Everything I was provided is listed already.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the answer for your question in Java Programming Language.

NOTE : As per the instructions given under the writeFile() method only Employees FirstName and Ids have been written to the output file. You need to create BestEmployees.txt file before writing to it.

As MyAbstract is an abstract class we cannot create object for it directly but we can use its properties by extending it to another class. Hence to simplify the code I have used strings and string arrays to hold file data.

Kindly upvote of you find teh answer helpful.

CODE :

MyAbstract.java


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public abstract class MyAbstract {
  
public static String eachLine;
public static String data = "";
//Implementing readFileMethod()
public static void readFile(String fileName) throws FileNotFoundException, IOException{
BufferedReader br = new BufferedReader(new FileReader(fileName));   
System.out.println("---------------------------------------------------------------");
System.out.println("First Name\tLastName\tEmployee Id\tSalary");
System.out.println("---------------------------------------------------------------");
//Reading each line to eachLine variable
while((eachLine = br.readLine())!= null){   
//Adding each line to the data variable by concatinating a new line
data += eachLine + "\n";
//Printing data
System.out.println(String.format("%-15s%-15s%-15s%s",eachLine.split(",")[0],eachLine.split(",")[1],eachLine.split(",")[2],eachLine.split(",")[3]));
}
  
}
public abstract void employeeAvgSalary();
  
public abstract int countNumberOfEmployees();
  
protected abstract String[] employeeInformation();
  
//Implementation of writeFile method
public void writeFile() throws IOException{
//You must define the path of the file to access it correctly
String path = "C:\\Users\\vysali\\Desktop\\";
//Accessing the file using FileWriter class
FileWriter writer = new FileWriter(path+"BestEmployees.txt");

//Getting data from employeeInformation method and storing it in a string array
String[] employeeInfo = employeeInformation();
//Getting number of employees by countNumberOfEmployees method
int numberOfEmployees = countNumberOfEmployees();
//Writing data to file
writer.write("----------------------------------------------\n");
writer.write("First Name\t\tEmployee Id\n");
writer.write("----------------------------------------------\n");
//Loops through the number of employees
for(int i=0;i<numberOfEmployees;i++){
//Each time writes employeeinfo i.e., first name and id followed by a next line character
writer.write(employeeInfo[i] + "\n");
}
//flushing and closing writer object to output data in the file
writer.flush();
writer.close();
}
}

MyTestClass.java

import java.io.IOException;
import java.text.DecimalFormat;

public class MyTestClass extends MyAbstract{
//Decimal format to display average to two decimal places
DecimalFormat d = new DecimalFormat("0.00");
//int variable to hold number of employees in the file
public int numberOfEmployees = countNumberOfEmployees();
//String array to hold each line in the file
public static String[] Lines;
public static void main(String[] args) throws IOException{
//Creating file namealong with the path
String path = "C:\\Users\\vysali\\Desktop\\";
String fileName = "myData.txt";
//Calling readFile method with the filename
MyTestClass.readFile(path+fileName);
//Creating an object of myTestClass
MyTestClass a = new MyTestClass();
//Initialising the Lines array with the size of number of employees
Lines = new String[a.countNumberOfEmployees()];
//Storing each Line in the array from the variable data
Lines = data.split("\n");
System.out.println("\n--------------------------------------------------------------");
//Displaying number of employees
System.out.println("Number of Employees : " + a.numberOfEmployees);
//Calling method employeeAvgSalary
a.employeeAvgSalary();   
//Writing into the file BestEmployees.txt
a.writeFile();
System.out.println("Output File has been written successfully");
System.out.println("--------------------------------------------------------------");
}
@Override
public void employeeAvgSalary() {
  
long sum = 0;
double average;
//Gets the number of employees
for(int i=0;i<numberOfEmployees;i++){
//Adding up the salary t sum variable
sum += Long.parseLong(Lines[i].split(",")[3]);
}
//Calculating average
average = sum/numberOfEmployees;
//Displaying average
System.out.println("The average of employee's salary is " + d.format(average));
}
@Override
public int countNumberOfEmployees() {
//Returns number of employees by using the length of data when seperated by new lines
//Actually returns number of lines in the string data
return data.split("\n").length;
}
@Override
protected String[] employeeInformation() {
//Initialises a string with size of numberOfEmployees
String[] info = new String[numberOfEmployees];
//Loops through number of employees
for(int i=0;i<numberOfEmployees;i++){
//Stores the first name and id of each employee to the array info
info[i] = String.format("%-20s%s",Lines[i].split(",")[0],Lines[i].split(",")[2]);
}
return info; }
}

myData.txt File

Bob,Smith,@1234,120000
Katy,Johnson,@5625,20000
Alex,King,@1234,121000
Chad,Pettigrew,@1234,100000
David,Smith,@1234,101000
Mark,Walburg,@124,200000
Jennifer,Campbell,@1234,102000

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

MyAbstract.java

11 9 import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWrite

public void writeFile() throws IOException //You must define the path of the file to access it correctly String path = C:\\U

MyTestClass.java

© import java.io.IOException; import java.text.Decimal Format; public class MyTestclass extends MyAbstract{ //Decimal format@Override public void employeeAvgSalary() { long sum = 0; double average; // Gets the number of employees for(int i=0;i<numbe

myData.txt

myData.txt > 1 Bob, Smith,@1234, 120000 2 Katy, Johnson, @5625, 20000 3 Alex, King,@1234, 121000 4 Chad, Pettigrew, @1234,100

OUTPUT :

Output - JavaExamples (run) run: First Name Last Name Employee Id Salary Smith @1234 OS Johnson Ở. @1234 b Bob Katy Alex Chad

BestEmployees.txt

Best Employees.txt x - - - - - - - - - - 2 First Name Employee Id - - - 4 Bob 5 Katy 6 Alex 7 Chad 8 David 9 Mark 10 Jennifer

Any doubts regarding this can be explained with pleasure :)

Add a comment
Know the answer?
Add Answer to:
In java. You are provided an abstract class called MyAbstract. You can only modify the writeFile...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

  • Java Project Draw a class diagram for the below class (using UML notation) import java.io.BufferedReader; import...

    Java Project Draw a class diagram for the below class (using UML notation) import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; public class myData { public static void main(String[] args) { String str; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter text (‘stop’ to quit)."); try (FileWriter fw = new FileWriter("test.txt")) { do { System.out.print(": "); str = br.readLine(); if (str.compareTo("stop") == 0) break; str = str + "\r\n"; // add newline fw.write(str); } while (str.compareTo("stop") != 0); } catch (IOException...

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • Using Java programming: Modify the delimiter class (provided below) so that it can "incorrectly" ...

    Using Java programming: Modify the delimiter class (provided below) so that it can "incorrectly" evaluate simple parenthesized math expressions . a) ask the user for the whole expression to solve b) Solve the expression only if there is no mismatch in the delimiters. If there is a mismatch, then output an error. If there is an error, allow the user to reenter a corrected/new expression. c) The program should be able to evaluate expressions that uses only the following basic...

  • All question base on Java Se 8 Consider the Top class i public abstract class Topí...

    All question base on Java Se 8 Consider the Top class i public abstract class Topí 2 private 3 protected String name; 4 public intx - 12; int age; e public Top(String name)[ this.namename age0; System.out.println(x); 10 12 public abstract void foo(String f); 13 Create a Middle class that extends the Top class. The class should have a single constructor that takes two input parameters: a String (for name) and int (for age). Your constructor should not have any redundant...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the...

    Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the Comparable and Cloneable interfaces. //GeometricObject.java: The abstract GeometricObject class public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; /** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); } /** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color;...

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

  • Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so...

    Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so they throw exceptions when the following errors occur. - The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. - The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. - The ProductionWorker class should thrown anexception named InvalidPayRate when it receives a negative number...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT