Question

In java Objectives: In this lab, the following topics will be covered: 1. Text Files 2....

In java

Objectives:

In this lab, the following topics will be covered:

1. Text Files

2. Binary Files

Exercise 1 Create an array of Course objects taken from the user. The class Course has the following attributes: code like "ICS201", name like "Introduction to Computing II", and credit hours like 4. Save the array to both a text file, and a binary file.

Exercise 2 Write a program that reads the text file created in Exercise 1 into an array of Course objects then it displays it to the screen.

Exercise 3 Write a program that reads the binary file Exercise 1 into an array of Course objects then it displays it to the screen.

I posted in a previous question and I haven't got the answer yet and I need it in 4 hours plzZ

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

/*******************************Course.java****************************/

import java.io.Serializable;

public class Course implements Serializable {

  
   private static final long serialVersionUID = 1L;
   private String code;
   private String name;

   public Course(String code, String name) {
       this.code = code;
       this.name = name;
   }

   public String getCode() {
       return code;
   }

   public String getName() {
       return name;
   }

   @Override
   public String toString() {
       return "Course [code=" + code + ", name=" + name + "]";
   }

}
/*******************************ReadFile.java*************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;

public class ReadFiles {

   public static void main(String[] args) {

       Course c1 = new Course("CSE303", "Into to c Programming");
       Course c2 = new Course("CSE501", "Networking with TCP/IP");

       Course[] courses = new Course[10];
       int index = 0;
       try {
           Scanner scan = new Scanner(new File("courses.txt"));
           while (scan.hasNextLine()) {

               String line = scan.nextLine();

               String code = line.split(",")[0];
               String name = line.split(",")[1];

               courses[index] = new Course(code, name);
               index++;
           }
           scan.close();
       } catch (FileNotFoundException e) {
           System.out.println("File not found");
       }

       try {
           FileOutputStream f = new FileOutputStream(new File("courses.dat"));
           ObjectOutputStream o = new ObjectOutputStream(f);

           // Write objects to file
           o.writeObject(c1);
           o.writeObject(c2);

           o.close();
           f.close();

           FileInputStream fi = new FileInputStream(new File("courses.dat"));
           ObjectInputStream oi = new ObjectInputStream(fi);

           courses[index] = (Course) oi.readObject();
           courses[index + 1] = (Course) oi.readObject();

           oi.close();
           fi.close();

       } catch (FileNotFoundException e) {
           System.out.println("File not found");
       } catch (IOException e) {
           System.out.println("Error initializing stream");
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       }

       int i = 0;
       while (courses[i] != null) {

           System.out.println(courses[i].toString());
           i++;
       }
   }
}
/********************************courses.txt*********************/

ICS201,Introduction to Computing
CSE102,Introduction to Java
CSE203,C Programming

/*******************courses.dat*********************/

/*******************output*****************/

Course [code=ICS201, name=Introduction to Computing]
Course [code=CSE102, name=Introduction to Java]
Course [code=CSE203, name=C Programming]
Course [code=CSE303, name=Into to c Programming]
Course [code=CSE501, name=Networking with TCP/IP]

Please let me know if you have any doubt or modify the answer, Thanks:)

Add a comment
Know the answer?
Add Answer to:
In java Objectives: In this lab, the following topics will be covered: 1. Text Files 2....
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
  • Main topics: Files Program Specification: For this assignment, you need only write a single-file ...

    C program Main topics: Files Program Specification: For this assignment, you need only write a single-file C program. Your program will: Define the following C structure sample typedef struct int sarray size; the number of elements in this sanple's array floatsarray the sample's array of values sample Each sample holds a variable number of values, all taken together constitute a Sample Point Write a separate function to Read a file of delimited Sample Points into an array of pointers to...

  • Using Java how would I write a program that reads and writes from binary or text...

    Using Java how would I write a program that reads and writes from binary or text files and gives me an output similar to this? Example Output: --------------------Configuration: <Default>-------------------- Enter the file name: kenb Choose binary or text file(b/t): b Choose read or write(r/w): w Enter a line of information to write to the file: lasdklj Would you like to enter another line? Y/N only n Continue? (y/n)y Enter the file name: kenb Choose binary or text file(b/t): b Choose...

  • I've done all questions except question 6. how do I do this? please help. code needs...

    I've done all questions except question 6. how do I do this? please help. code needs to be java 1. You need to be able to open and read the text files that have been provided for the project. Write a Java program that reads the driver.txt file and displays each data value for each line from the file to the screen in the following format: Licence Number: Licence Class: First Name: Last Name: Address: Suburb: Postcode: Demerit Points: Licence...

  • Lab 10 Write a Java program that reads the integer numbers in a text file (numbers.txt)...

    Lab 10 Write a Java program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat).The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0

  • C++ please Write a program that reads the following sentences from a file: I am Sam...

    C++ please Write a program that reads the following sentences from a file: I am Sam Sam I am That Sam I am That Sam I am I do not like that Sam I am Do you like green eggs and ham I do not like them But I do like spam! You will first have to create the text file containing the input, separate from your program. Your program should produce two output files: (i) (ii) one with the...

  • JAVA Write a program that 1. opens a text file 2. reads lines of text from...

    JAVA Write a program that 1. opens a text file 2. reads lines of text from the file 3. Converts each line to uppercase 4. saves them in an ArrayList 5. Writes the list of Strings to a new file named "CAPSTEXT.txt" Mainjavo ext.txt Instructions from your teacher 1 My mistress' eyes are nothing like the sun; 2 Coral is far more red than her lips' red; 3 If snow be white, why then her breasts are dun; 4 If...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

  • Programming Assignment 6: A Python Class, Attributes, Methods, and Objects Obiectives .Be able to...

    I need some help with programming this assignment. Programming Assignment 6: A Python Class, Attributes, Methods, and Objects Obiectives .Be able to write a Python class Be able to define class attributes Be able to define class methods .Be able to process input from a text file .Be able to write an application using objects The goal of this programming assignment is to develop a simple image processing application. The application will import a class that creates image objects with...

  • please read directions first. this is supposed to be a hybrid programe add comments please JAVA...

    please read directions first. this is supposed to be a hybrid programe add comments please JAVA please add comments Programming Project 1 and Programming Project 2 "Hybrid" Your goal is to write a program that combines the functionality of both Programming Project 1andProgramming Project 2 into a single program. That is, your program should read a file ( Numbers.txt) of numbers of type double that contains some duplicates and is ordered smallest to largest. Your program should ignore the duplicate...

  • Please solve using c++ plus There are two text files with the following information stored in...

    Please solve using c++ plus There are two text files with the following information stored in them: The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma The department.txt file where each line stores the name, location and budget of the department separated by a comma You need to write a C++ program that reads these text files and provides user with the following menu: 1. Enter the instructor ID...

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