I have been trying and trying for a while with this and I just seem to cannot solve it.
I am supposed to extract classes from a Java-file and print in a shape UML-diagram, in the IDE or writing on a file
e.g the program
public class Complex {
private int re;
private int im;
public Complex(int re, int im) {
this.re = re;
this.im = im;
}
public Complex add(Complex h) {
return new Complex(this.re + h.re, this.im + h.im);
}
public Complex sub(Complex h) {
return new Complex(this.re - h.re, this.im - h.im);
}
public Complex mul(Complex h) {
int a = re;
int b = im;
int c = h.re;
int d = h.im;
return new Complex(a * c - b * d, b * c + a * d);
}
public Complex div(Complex h) {
int a = re;
int b = im;
int c = h.re;
int d = h.im;
return new Complex((a * c + b * d) / (c * c + d * d), (b * c - a * d)
/ (c * c + d * d));
}
public String toString() {
if (im >= 0) {
return re + " + " + im + "i";
} else
return re + " " + im + "i";
}
}
Should generate something like:
a Complex
b int re
b int re
a Complex(re:int, im:int)
a add():Complex
a sub():Complex
a mul():Complex
a div():Complex
a toString():String
a main()
I have started with stripping the first parenthesis from the file;
import java.io.*;
public class ClassExtract {
public static void main(String[] args) {
ClassExtract obj = new ClassExtract();
obj.removeBracket("Complexx.txt");
}
public void removeBracket(String filnamn) {
try {
File f = new File(filnamn);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("noparanthesis_" + filnamn);
BufferedWriter bw = new BufferedWriter(fw);
String rad = br.readLine();
while (rad != null) {
rad = rad.replaceAll("\\(", " ");
bw.write(rad);
bw.newLine();
rad = br.readLine();
}
bw.close();
br.close();
} catch (FileNotFoundException e) {
System.out.printf("The file " + filnamn + " was not found.");
} catch (IOException e) {
System.out.printf("Writing error.");
}
}
}
I have thought of different ways of approaching this problem. The way that I think would be easiest would be to strip everything after collected the head public class Complex, which would mean the rest of the file would look something like:
public Complex int re, int im
public Complex add Complex h
public Complex sub Complex h
etc and do the same and read the index of the lines.
I actually feel very lost and I have hard to tackle this problem, any help would really be appreciated.