Question

In: Computer Science

Create a class called FileSumWrapper with a method that has the signature public static void handle(String...

Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound).

Make this method call FileSum.read and make your method catch all the errors.

FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound.

I'm unsure at how to incorporate the FileSum.read() method to even start anything. I also don't know what the whole "lower bound" means or is suppose to do. Please attach comments/notes as to why something is there/ how it works. I'm also not very great on how to import files. Thank you if you can help even just a bit.

Given:

FileSum.java

import java.io.File;
import java.rmi.UnexpectedException;
import java.util.Scanner;

public class FileSum {

    public static int read(String filename, int lowerBound) throws Exception 
{
        Scanner inputFile = new Scanner(new File(filename));

        int acc = 0;
        boolean atLeastOneFound = false;

        while (inputFile.hasNext()) {
            int data = inputFile.nextInt();
            if (data >= lowerBound) {
                acc += data;
                atLeastOneFound = true;
            }
        }

        if (!atLeastOneFound) {
            throw new UnexpectedException("");
        }
        return acc;
    }
}

---------------------------------------------------------------------------------------------------------

Question1.java

import java.util.Scanner;

public class Question1 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter a filename");
        String filename = keyboard.nextLine();
        System.out.println("Enter a lower bound");
        int lowerBound = keyboard.nextInt();

        FileSumWrapper.handle(filename, lowerBound);
    }
}

---------------------------------------------------------------------------------------------------------

err.txt

50 40 30
90
85
23
06
30x
54
675
875
34
2323
423
423
5
5
79
97
90y
7986
5
46
64656
66
6





333 93 9 300 20 2 9 209 290 39 48 85 7847 578048

---------------------------------------------------------------------------------------------------------

t1.txt

50 40 30
90
85
23
06
30
54
675
875
34
2323
423
423
5
5
79
97
90
7986
5
46
64656
66
6





333 93 9 300 20 2 9 209 290 39 48 85 7847 578048

---------------------------------------------------------------------------------------------------------

Input                 Output
t1.txtENTER
50

Enter a filename\n

Enter a lower bound\n

Sum of all numbers in t1.txt is 665177\n

Input                 Output
t1.txtENTER
500

Enter a filename\n

Enter a lower bound\n

Sum of all numbers in t1.txt is 662410\n

Input                 Output
t1.txtENTER
5000000

Enter a filename\n

Enter a lower bound\n

CS112 error: Found no numbers above 5000000\n   

Input                 Output
t1.txtENTER
6000000

Enter a filename\n

Enter a lower bound\n

CS112 error: Found no numbers above 6000000\n   

Input                 Output
t1e.txtENTER
50

Enter a filename\n

Enter a lower bound\n

CS112 error: Couldn't open t1e.txt\n

Input                 Output
t11e.txtENTER
50

Enter a filename\n

Enter a lower bound\n

CS112 error: Couldn't open t11e.txt\n

Input                 Output
err.txtENTER
50

Enter a filename\n

Enter a lower bound\n

CS112 error: Was reading integers but found something else\n

Solutions

Expert Solution

FileSum.java

import java.io.File;
import java.rmi.UnexpectedException;
import java.util.Scanner;

public class FileSum {
  
public static int read(String filename, int lowerBound) throws Exception
{
Scanner inputFile = new Scanner(new File(filename));

int acc = 0;
boolean atLeastOneFound = false;
  
while (inputFile.hasNext()) {
int data = inputFile.nextInt();
if (data >= lowerBound) {
acc += data;
atLeastOneFound = true;
}
}

if (!atLeastOneFound) {
throw new UnexpectedException("");
}
return acc;
}
}

FileSumWrapper.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FileSumWrapper {
  
public static void handle(String fileName, int lowerBound)
{
// section to check:
// 1. If the file exists or not, and
// 2. If only integers are being read from the file or not
Scanner fileReader;
try
{
fileReader = new Scanner(new File(fileName));
while(fileReader.hasNext())
{
if(!isDigit(fileReader.next()))
System.exit(0);
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println("Error: Couldn't open " + fileName);
System.exit(0);
}
  
// section for calling FileSum.read() method
try
{
int sum = FileSum.read(fileName, lowerBound);
System.out.println("Sum of all numbers in " + fileName + " is " + sum);
  
}catch(Exception ex){
System.out.println("Error: Found no numbers above " + lowerBound);
System.exit(0);
}
}
  
// this method returns true if the String s in an integer, else returns false
private static boolean isDigit(String s)
{
try
{
Integer.parseInt(s);
return true;
}catch(NumberFormatException nfe){
System.out.println("Error: Was reading integers but found something else");
return false;
}
}
}

Question1.java (Main class)

import java.util.Scanner;

public class Question1 {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter a filename");
String filename = keyboard.nextLine();

System.out.println("Enter a lower bound");
int lowerBound = keyboard.nextInt();

FileSumWrapper.handle(filename, lowerBound);
}
}

**************************************************************** SCREENSHOT *******************************************************


Related Solutions

In Java. Create a class called FileSumWrapper with a method that has the signature public static...
In Java. Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound) Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. FileSum : import java.io.File; import java.rmi.UnexpectedException; import java.util.Scanner; public class FileSum { public static int...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
Consider a class ‘A’ with a method public static void show( ). A class ‘Derived’ is...
Consider a class ‘A’ with a method public static void show( ). A class ‘Derived’ is the subclass of ‘A’. Is it possible to define the same method public static void show( ) in the Derived class. Why give reasons? Is it run time polymorphism? (0.5 Marks) Is it possible to define the method public void show( ) (non static method) in the Derived class. What will happen and why give reasons?
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
public class OOPExercises {     public static void main(String[] args) {         A objA = new...
public class OOPExercises {     public static void main(String[] args) {         A objA = new A();         B objB = new B();         System.out.println("in main(): ");         System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         objA.setA (222);         objB.setB (333.33);       System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } Output: public class A {     int a = 100;     public A() {         System.out.println("in the constructor of class A: ");         System.out.println("a = "+a);         a =...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // You must use Java's built-in Arrays. You are welcome to use the Math and/or Random class if necessary. // You MUST put your code directly beneath the comment for each item indicated. String[] myData; // 1. Instantiate the given...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
public class Q8 { public static void main(String[] args) { //Nancy did an online shopping at...
public class Q8 { public static void main(String[] args) { //Nancy did an online shopping at Macy's.com on a week end sale. //She purchased a bedding collection on a 60 % sale. It is original price was $ 450 //She added this item in the shopping cart and then during check out, the web portal // notified Nancy that she was qualified // to get an extra offer of 20 % off from the previous discounted price. //The Shipping charge...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT