Question

In: Computer Science

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 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:

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
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

Here is the input :

t1.txt
50

output:

Enter a filename\n
Enter a lower bound\n
Sum of all numbers in t1.txt is 665177\n

Solutions

Expert Solution

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

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;
}

}
//created wrapper class
class FileSumWrapper {
   //create a hanlde method
   // which calles read method in FileSum class
   public static void handle(String filename, int lowerBound){
           try {
               int sum=FileSum.read(filename, lowerBound);
               System.out.println("Sum of all numbers in "+filename+" is "+sum);
           } catch (Exception e) {
               e.printStackTrace();
           }
   }
}
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);
}

}

Note : If you like my answer please rate and help me it is very Imp for me


Related Solutions

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...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static)...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static) method named firstOne. firstOne accepts a String array and returns a map from Strings to Integer. The map must count the number of passed Strings based on the FIRST letter. For example, with the String array {“banana”, “apples”, “blueberry”, “orange”}, the map should return {“b”:2, “a”:1, “o”:1}. Disregard empty Strings and zero counts. Retrieve first character of String as a char using charAt, or,...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and...
Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds: 00:00   00:01...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate,...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also...
Create a new Java file, containing this code public class DataStatsUser { public static void main...
Create a new Java file, containing this code public class DataStatsUser { public static void main (String[] args) { DataStats d = new DataStats(6); d.append(1.1); d.append(2.1); d.append(3.1); System.out.println("final so far is: " + d.mean()); d.append(4.1); d.append(5.1); d.append(6.1); System.out.println("final mean is: " + d.mean()); } } This code depends on a class called DataStats, with the following API: public class DataStats { public DataStats(int N) { } // set up an array (to accept up to N doubles) and other member...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
Make a class called CashRegister that has the method public static double getTotalCostOfOfferings(ArrayList<Offering> o) Make this...
Make a class called CashRegister that has the method public static double getTotalCostOfOfferings(ArrayList<Offering> o) Make this method calculate the total cost of all Offering objects in the ArrayList. Submit Product, Service, and CashRegister. Given Files: import java.util.ArrayList; public class Demo3 { public static void crunch(ArrayList<Offering> o) { System.out.println("Adding up the following offerings:"); for (Offering current : o) { System.out.println(current); } System.out.printf("Total for all: $%,.2f\n", CashRegister.getTotalCostOfOfferings(o)); System.out.println("---------------------------------\n"); } public static void main(String[] args) { ArrayList<Offering> offeringList = new ArrayList<>(); offeringList.add(new Product("iPhone",...
Write a method with the signature public static NaturalNumber divRemainder(NaturalNumber m, NaturalNumber n) The method takes...
Write a method with the signature public static NaturalNumber divRemainder(NaturalNumber m, NaturalNumber n) The method takes two NaturalNumbers as arguments and should return a new NaturalNumber whose value is the sum of m divided by n and the remainder of m divided by n. For example, if m = 23 and n = 6, the method should return a NaturalNumber with the value 8, since 23/6 + 23 rem 6 = 3 + 5 = 8. Do not use toInt()...
Java Programming Using the class below, please ), write a static method called parse that parses...
Java Programming Using the class below, please ), write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1 parse(“{ { 4*X}”)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT