//Design an exception class for index out of bound in an one-domensional array p
ID: 3659752 • Letter: #
Question
//Design an exception class for index out of bound in an one-domensional array public class IndexOutOfRange extends Exception { public IndexOutOfRange ( ) { super ("array index out of bound"); } public IndexOutOfRange (String msg) { super (msg); } } //Design a method to test the exception class above public void testException ( ) throws IndexOutOfRange { int capacity = 25; Integer [ ] x = new Integer [capacity]; for (int i = 0; i < 30; i++) { Integer num = new Integer (i); if (i >= capacity) throws new IndexOutOfRange ( ); else x[i] = num; } } //Design an exception handler for the exceptio above try { testException ( ); } catch (IndexOutOfRange ex) { } //Design a method to merge two existing text files, assume that they have been opened correctly public PrintWriter merge (Scanner file1, Scanner file2) { PrintWriter mergedFile = null; try { mergedFile = new PrintWriter ("file3.txt"); } catch (FileNotFoundException ex) { System.out.println ("fail to open file"); return mergedFile; } while (file1.hasNext ( )) { mergedFile.out.println (file1.nextLine ( )) } while (file2.hasNext ( )) { mergedFile.out.println (file2.nextLine ( )) } return mergedFile; } //Design a method to count the tokens in an existing and opened text file public int countTokens (Scanner inFile) { int count = 0; String inString = ""; while (inFile.hasNext ( )) inString += (inFile.nextLine ( )); int pos; while (true) { inString = inString.trim ( ); pos = inString.indexOf (" "); if (pos == -1) break; else { count++; inString = inString.substring (pos); } } return count; }Explanation / Answer
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class JavaApplication3
{
public static void main(String[] args)
{
try
{
testException ( );
}
catch (IndexOutOfRange ex)
{
System.out.println("Index out of bounds custom exception");
}
}
public static void testException() throws IndexOutOfRange
{
int capacity = 25;
Integer[] x = new Integer[capacity];
for (int i = 0; i < 30; i++)
{
Integer num = new Integer (i);
if (i >= capacity)
throw new IndexOutOfRange();
else
x[i] = num;
}
}
public static PrintWriter merge (Scanner file1, Scanner file2) throws java.io.FileNotFoundException
{
PrintWriter mergedFile = null;
try
{
mergedFile = new PrintWriter ("file3.txt");
}
catch (FileNotFoundException ex)
{
System.out.println ("fail to open file");
return mergedFile;
}
while (file1.hasNext ( ))
{
mergedFile.println(file1.nextLine ( ));
}
while (file2.hasNext ())
{
mergedFile.println (file2.nextLine ( ));
}
return mergedFile;
}
//Design a method to count the tokens in an existing and opened text file
public static int countTokens (Scanner inFile)
{
int count = 0; String inString = "";
while (inFile.hasNext ( ))
inString += (inFile.nextLine ( ));
int pos;
while (true)
{
inString = inString.trim ( );
pos = inString.indexOf (" ");
if (pos == -1)
break;
else
{
count++;
inString = inString.substring (pos);
}
}
return count;
}
}
public class IndexOutOfRange extends Exception
{
public IndexOutOfRange()
{
super ("array index out of bound");
}
public IndexOutOfRange (String msg)
{
super (msg);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.