Gotten easy code by proffessor that I don't understand, do you?
Hi everyone,
I'm very, very new to java, but since i'm a girl studying a master in technologies I've been assigned to create several computer codes to solve optimization problems in a course of applied optimization. My problem is, I don't even understand some of the code given to us... :/ Can you help me? The only help I have gotten is that I have to use the functions readGraph, readLine, readInteger and readString. Can you comment it and tell me what the functions do? The meaning of them is to read in a example file of the format below. This represents a network directed graph. The first row (8) is the number of nodes. The second row and down represents bows/arcs between nodes. Eg, from node 1 goes an arc to node 3. Thank you so very much, would really appreciate it! :)
8
1 3
1 4
1 7
1 8
3 2
3 7
4 3
5 1
5 4
6 1
6 5
7 2
7 8
8 6
Code :
private static MyDigraph readGraph(String fileName)
{
MyDigraph G = null;
int lineNum = 0;
String line = null;
StringTokenizer temp;
BufferedReader inFile = null;
try {
inFile = new BufferedReader(new FileReader(fileName));
}
catch (FileNotFoundException e) {
System.out.println("?? file not found");
return null;
}
if ((line = readLine(inFile)) == null) {
System.out.println("?? file is empty");
return null;
}
else {
lineNum++;
temp = new StringTokenizer(line);
try {
if (temp.countTokens() != 1) {
throw new IllegalArgumentException();
}
int n = Integer.parseInt(temp.nextToken());
G = new MyDigraph(n);
}
catch (IllegalArgumentException e) {
System.out.println("?? format error on line #" + lineNum);
return null;
}
}
while ((line = readLine(inFile)) != null) {
lineNum++;
temp = new StringTokenizer(line);
try {
if (temp.countTokens() != 2) {
throw new IllegalArgumentException();
}
int u = Integer.parseInt(temp.nextToken());
int v = Integer.parseInt(temp.nextToken());
G.insertEdge(u, v);
}
catch (IllegalArgumentException e) {
System.out.println("?? format error on line #" + lineNum);
}
}
return G;
}
private static int readInteger(String prompt)
{
int temp = 0;
while (true) {
try {
temp = Integer.parseInt(readString(prompt));
break;
}
catch (NumberFormatException e) {
System.out.println("?? format error");
}
}
return temp;
}
private static String readString(String prompt)
{
StringTokenizer temp = null;
BufferedReader stdin = new BufferedReader
(new InputStreamReader(System.in));
while (true) {
System.out.print(prompt);
System.out.flush();
temp = new StringTokenizer(readLine(stdin));
if (temp.countTokens() == 1) break;
System.out.println("?? format error");
}
return temp.nextToken();
}
private static String readLine(BufferedReader inFile)
{
String line = null;
try {
line = inFile.readLine();
}
catch (IOException e) {
System.err.println("?? beats me ...");
System.exit(0);
}
return line;
}
}
Re: Gotten easy code by proffessor that I don't understand, do you?
That isn't really how this works. How about you tell us what the functions do and we'll tell you how close you are. Have you stepped through this with a debugger? Have you stepped through it with a piece of paper and a pencil, manually tracing what it's doing? If not, those are your first steps.
Re: Gotten easy code by proffessor that I don't understand, do you?
Why it took such mental gymnastics to do what shouldn't need to be this complicated, though, I don't know.
Re: Gotten easy code by proffessor that I don't understand, do you?
Quote:
Originally Posted by
Fazan
Why it took such mental gymnastics to do what shouldn't need to be this complicated, though, I don't know.
This was the point of the assignment. I have edited your above post and removed the spoilers.
Re: Gotten easy code by proffessor that I don't understand, do you?
Fair enough. Apologies for breaking the rules. I didn't think I said too much, but I defer to your judgement.
Re: Gotten easy code by proffessor that I don't understand, do you?
I think I have managed to solve this one on my own now :) Tried to find where I can put this thread as [SOLVED] so that noone puts energy into it anymore. Where can I find it?
Re: Gotten easy code by proffessor that I don't understand, do you?
The announcements page describes how to mark your thread as solved as well as other forum related useful information.
Re: Gotten easy code by proffessor that I don't understand, do you?