In: Computer Science
Provide a comprehensive set of test cases for the two programs below. Do not provide actual test data, but rather describe each test. For example, if one of the programs was to search an array, you might include test cases such as search an array that is in ascending order, search an empty array, search for an element not in the array, etc. a. The program described in Question 4.
Question 4 is
A text file called coit20245.txt consists of N lines each containing a name and a family name as follows. N is largest digit of your student id number. Example of coit20245.txt file: Daniel Atkinson Rob Jackson Brian Lara Write a main() method that reads the rows of coit20245.txt and counts the lines which starts with a first letter of your name. You are to provide class comments that describe what the application does. You are also to provide comments that describe what each significant block of code does.
Note:Java programming language
/* sample data in coit20245.txt
Jack sparrow
Harry porter
Harmoiny Gringer
Albus Dumbledoor
Tony Stark
Peter Parker*/
//program for given task
//importing the scanner class to read file and input from the
user
import java.util.Scanner;
//importing the File class for File reading object
import java.io.File;
//importing java.io.* as it contains all the exceptions ;
import java.io.*;//FileNotFoundException; will be in java.io
//Scanner class will throws FileNotFoundException; while reading
the content
//file object so we throws FileNotFoundException; at main funtion
to skip the
//error showing while compilation and running of code
//creating the main class Name_Check
public class Name_check
{
//defining the main method
public static void main(String[] args) throws
Exception
{
//creating the scanner object to
take
//name of user as input
Scanner sc=new
Scanner(System.in);
//asking the user to enter name of
user
System.out.print("enter your
name:=");
//taking the input from user
String name=sc.next();
//creating the File object to read
the file coit20245.txt
File file=new
File("coit20245.txt");
//now we will pass the instance of
File above created to scanner
//class to read the content of the
file
Scanner file_reader=new
Scanner(file);
//taking the first letter from the
name we have taken from user
char
first_letter_from_name=name.charAt(0);
//initilizing the count variable to
count the no of lines starting
//with first letter of our
name
int count=0;
//now looping over the contents of
file using while loop
//file_reader.hasNextLine() will
return true if file has next line
//after reading some line
while
(file_reader.hasNextLine())
{
String
line=file_reader.nextLine();//reading the line from file
//taking the
first letter of the line to compare with first letter
//taken from our
name
//checking the
length of line if it has any character or onlu line
if
(line.length()>0)
{
char
first_letter_from_line=line.charAt(0);
//comparing the first letter of name and line
for checking
//whether the line is starting with first letter
of name or not
if
(first_letter_from_name==first_letter_from_line)
{
//if first letter of
name==first letter of line count will increase
count+=1;
}
}
}
//printing the total no of line
that is staring with first letter of name
System.out.println("No of lines
staring with first letter of name:--> "+count);
}
}
/*output of above code
enter your name:=HArry
No of lines staring with first letter of name:--> 2
*/
//code snippet
//importing the scanner class to read file and input from the user
import java.util.Scanner;
//importing the File class for File reading object
import java.io.File;
//importing java.io.* as it contains all the exceptions ;
import java.io.*;//FileNotFoundException; will be in java.io
//Scanner class will throws FileNotFoundException; while reading the content
//file object so we throws FileNotFoundException; at main funtion to skip the
//error showing while compilation and running of code
//creating the main class Name_Check
public class Name_check
{
//defining the main method
public static void main(String[] args) throws Exception
{
//creating the scanner object to take
//name of user as input
Scanner sc=new Scanner(System.in);
//asking the user to enter name of user
System.out.print("enter your name:=");
//taking the input from user
String name=sc.next();
//creating the File object to read the file coit20245.txt
File file=new File("coit20245.txt");
//now we will pass the instance of File above created to scanner
//class to read the content of the file
Scanner file_reader=new Scanner(file);
//taking the first letter from the name we have taken from user
char first_letter_from_name=name.charAt(0);
//initilizing the count variable to count the no of lines starting
//with first letter of our name
int count=0;
//now looping over the contents of file using while loop
//file_reader.hasNextLine() will return true if file has next line
//after reading some line
while (file_reader.hasNextLine())
{
String line=file_reader.nextLine();//reading the line from file
//taking the first letter of the line to compare with first letter
//taken from our name
//checking the length of line if it has any character or onlu line
if (line.length()>0)
{
char first_letter_from_line=line.charAt(0);
//comparing the first letter of name and line for checking
//whether the line is starting with first letter of name or not
if (first_letter_from_name==first_letter_from_line)
{
//if first letter of name==first letter of line count will increase
count+=1;
}
}
}
//printing the total no of line that is staring with first letter of name
System.out.println("No of lines staring with first letter of name:--> "+count);
}
}
//screenshots of code
#screenshot of output