Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java, using Methods & Read a File only; (No Arrays). Please read through. CSc

ID: 3759363 • Letter: I

Question

In Java, using Methods & Read a File only; (No Arrays). Please read through.

CScourses.txt: [http://pastebin.ca/3232424]

Write a program that displays information about the Computer Science semester schedule. Your program will require two Java files: your main program and a Course class. The Course class must contain the following class instance data: data Course number CRN Instructor's last name Days the course is taught Start time of the class Room where the class will be held Title of the course example GEEN16.3 20781 Williams MWF 1000 Graham210 Intro to Computer Programming All of the data should be represented as strings. Your class must include a constructor method that initializes all of the data values. This class should not have a main method. The only additional method required is. public void printCourse () which should display the information about the course. You may format the output in any way that works In a separate class, the main program should read the file CScourses.txt and create a Course object for each course in the file, Each line of data in the file has the course information in the same order as shown above. Except for the title of the class, all of the data is one word separated by white space. Your program should read the data values using the Scanner method next () except for the title where it should use nextLine ( As your program reads each line of the file, the Course object should be put into a hash table. A hash table is a data structure that allows you to retrieve information based on a key. Your program will put the Course objects in the table using the course number as the key. You will need to create a hash table obiect at the beginning of vour program with java.util . HashMapString, Course> hashTable = new java.util. HashMap(); You can put data into the hash table using the put method and retrieve data using the get method. The put method has two parameters, the key and the object to be put in the hash table. The get method has only one parameter, the kev to be used to find the corresponding obiect. To put the data in the hash table use Course csCourse = new Course ( courseNumber, crn, other,arameters) ; hashTable.put ( courseNumber, csCourse); After vour program has read all of the data from the file, it should ask the user to enter a course number (such as GEEN163) from the keyboard. It should then use the get method to find the data in the hash table and display the information. The get method will return the Course object with the given course number Call the printCourse method on the returned object to display the course information. If the get method does not find a class in the hash table with that number, it will return null, Your program must check if the returned value is equal to null and, if it is, display a message indicating there is no such class EnteredNumber keyboard. next() Course found = hashTable.get( EnteredNumber ); if (found == null ; //if course is not in the table The program should continue asking the user for course numbers until END is entered Example output Enter a course number COMP375 CRN: 10070 Instructor: Williams Comp Architecture & Org days: MWF starts:1300 GRAHA210 Enter a course number >GEEN1 65 CRN: 11015 Instructor: Bryant Computer Program Desiqn days: MWF starts:1200 GRAHA210 Enter a course number >COMPO00 No such course Enter a course number >END

Explanation / Answer

In this Java program, we have used java.util.Scanner to read file line by line in Java. We have first created a File instance to represent a text file in Java and than we passed this File instance to java.util.Scanner for scanning. Scanner provides methods like hasNextLine() and readNextLine() which can be used to read file line by line. It's advised to check for next line before reading next line to avoid NoSuchElementException in Java.

Reader is the abstract class for reading character streams. It implements the following fundamental methods:

InputStreamReader is a bridge from byte streams to character streams. It converts bytes into characters using a specified charset. The charset can be default character encoding of the operating system, or can be specified explicitly when creating an InputStreamReader.

FileReader is a convenient class for reading text files using the default character encoding of the operating system.

BufferedReader reads text from a character stream with efficiency (characters are buffered to avoid frequently reading from the underlying stream) and provides a convenient method for reading a line of text readLine().

Program :

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
*
* Java program to read file using Scanner class in Java.
* java.util.Scanner is added on Java 5 and offer convenient method to read data
*
* @author
*/
public class ScannerExample {

public static void main(String args[]) throws FileNotFoundException {

//creating File instance to reference text file in Java
File text = new File("C:/temp/test.txt");

//Creating Scanner instnace to read File in Java
Scanner scnr = new Scanner(text);

//Reading each line of file using Scanner class
int lineNumber = 1;
while(scnr.hasNextLine()){
String line = scnr.nextLine();
System.out.println("line " + lineNumber + " :" + line);
lineNumber++;
}

}

}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote