MY CODE SOR FAR: /DictionaryException public class DictionaryException extends E
ID: 3545654 • Letter: M
Question
MY CODE SOR FAR:
/DictionaryException
public class DictionaryException extends Exception
{
public DictionaryException()
{
super();
}
public DictionaryException(String message)
{
super(message);
}
}
//Dictionary class
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileReader;
public class Dictionary implements Iterable<String>
{
ArrayList<String> words = new ArrayList<String>();
public Dictionary(String fileName, boolean sort)
throws DictionaryException
{
try
{
words = new ArrayList<String>();
FileReader file = new FileReader(fileName);
Scanner sc = new Scanner(file);
String str;
while(sc.hasNext())
{
str = sc.nextLine();
words.add(str);
}
file.close();
}
catch(DictionaryException e)
{
e.printStackTrace();
}
}
public Iterator<String> iterator()
{
return null;
}
boolean lookUp(String word)
{
return true;
}
public void write(String fileName)
{
}
private void sort()
{
for (int i = 0; i < this.words.size() - 1; i++)
{
int minPos = minimumPosition(i);
swap(minPos, i);
}
}
private int minimumPosition(int from)
{
int minPos = from;
for (int i = from + 1; i < this.words.size(); i++)
{
int temp = this.words.get(i).compareTo(this.words.get(minPos));
if (temp < 0)
{
minPos = i;
}
}
return minPos;
}
private void swap(int i, int j)
{
String temp = this.words.get(i);
this.words.set(i, this.words.get(j));
this.words.set(j, temp);
}
}
OK
I have my BinarySearch code and my selection sort
I need help where I have to throw the exception that I ve create and the writing the iterator methods
ny suggestions? thanks
Explanation / Answer
Please see the highlighted code below...
//Dictionary class
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileReader;
import java.util.Iterator;
import java.io.*;
public class DictionaryException extends Exception
{
public DictionaryException()
{
super();
}
// you need to catch the other type exceptions as well because you are dealing with files also
// FileNotFoundException or others.
catch (Exception e)
{
e.printStackTrace();
}
public DictionaryException(String message)
{
super(message);
}
}
class Dictionary implements Iterable<String>
{
ArrayList<String> words = new ArrayList<String>();
public Dictionary(String fileName, boolean sort) throws DictionaryException
{
try
{
words = new ArrayList<String>();
FileReader file = new FileReader(fileName);
Scanner sc = new Scanner(file);
String str;
while(sc.hasNext())
{
str = sc.nextLine();
words.add(str);
}
file.close();
// use the below line in the block which you want to throw the exception.
// when you use user defined exception, you need to throw it using the throw keyword.
// when you use throws, you should use throw keyword to throw an exception.
// throws - throw
// throws - try - throw - catch
throw new DictionaryException("Dictionary Exception");
}
catch(DictionaryException e)
{
e.printStackTrace();
}
}
public Iterator<String> iterator()
{
return null;
}
boolean lookUp(String word)
{
return true;
}
public void write(String fileName)
{
}
private void sort()
{
for (int i = 0; i < this.words.size() - 1; i++)
{
int minPos = minimumPosition(i);
swap(minPos, i);
}
}
private int minimumPosition(int from)
{
int minPos = from;
for (int i = from + 1; i < this.words.size(); i++)
{
int temp = this.words.get(i).compareTo(this.words.get(minPos));
if (temp < 0)
{
minPos = i;
}
}
return minPos;
}
private void swap(int i, int j)
{
String temp = this.words.get(i);
this.words.set(i, this.words.get(j));
this.words.set(j, temp);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.