Lab 03 Solutions
Lab 03 Solutions
Example :1 DataSetTesterApp.java
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
class DataSetReader {
private double[] data;
public double[] readFile(String filename) throws IOException {
FileReader reader = new FileReader(filename);
try {
Scanner in = new Scanner(reader);
readData(in);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
reader.close();
}
return data;
}
}
public class DataSetTesterApp {
import java.util.Scanner;
public class ParseInts {
public static void main(String[] args) {
int val = 0, sum = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a line of text");
String line = scan.nextLine();
Scanner scanLine = new Scanner(line);
while (scanLine.hasNext()) {
try {
val = Integer.parseInt(scanLine.next());
} catch (NumberFormatException x) {
val = 0;
}
sum += val;
}
System.out.println("The sum of the integers on this line is " + sum);
}
}
Example :3
Factorials.java
import java.util.Scanner;
class MathUtils {
if (n > 0) {
if (n < 16) {
int fac = 1;
fac *= i;
return fac;
} else {
} else {
try {
System.out.println("Factorial(" + val + ") = "
+ MathUtils.factorial(val));
System.out.println(ex.getMessage());
keepGoing = scan.next();