Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Use the Java class prodived to construct a class that implements a collection of objects. You will write the Java definition of a collection class
Use the Java class prodived to construct a class that implements a collection of objects. You will write the Java definition of a collection class that has as a member an array or ArrayList of instances of the class provided Enable the user to manage this collection, stored in a disk file, by reading from a file and writing to the file. Write a driver method to instantiate and test your collection class.
class:
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Conference { private String conferenceAttendees; private String workshops; private String homeBuyers; private String cd; public Conference() { conferenceAttendees = ""; workshops = ""; homeBuyers = ""; cd = ""; } // accessor public String getConferenceAttendees() { if ("Manish".toString().equals(conferenceAttendees)) { return "VIP"; } else { return conferenceAttendees; } } // mutator public void setConferenceAttendees(String conferenceAttendees) { this.conferenceAttendees = conferenceAttendees; } public String getWorkshops() { return workshops; } public void setWorkshops(String workshops) { this.workshops = workshops; } public String getHomeBuyers() { return homeBuyers; } public void setHomeBuyers(String homeBuyers) { this.homeBuyers = homeBuyers; } public String getCd() { return cd; } public void setCd(String cd) { this.cd = cd; } public static void write(String conferenceAttendees, String workshops, String homeBuyers, String cd) throws IOException { int ch; // check if File exists or not FileReader fr = null; try { fr = new FileReader("text"); } catch (FileNotFoundException fe) { System.out.println("File not found"); } // read from FileReader till the end of file while ((ch = fr.read()) != -1) System.out.print((char) ch); // close the file fr.close(); // Accept a string String str = conferenceAttendees + workshops + homeBuyers + cd; // attach a file to FileWriter FileWriter fw = new FileWriter("output.txt"); // read character wise from string and write // into FileWriter for (int i = 0; i < str.length(); i++) fw.write(str.charAt(i)); System.out.println("Writing successful"); // close the file fw.close(); } }
input file name: conference.txt
input: conference1 conference2 conference3 conference4 conference5 conference6 }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started