Question

1. Write a program to open the input filename “input01.txt”. Read and display all integers (and integers only) in the file to the output file named “output01.txt”. Note that the file may contain something that is not an integer.

Example:
“input01.txt”10 a 12 15.4 13.2 16 7 4.9
“output01.txt”
10 12 16 7


2. Write a method to open an input file for reading double numbers and returning the average those numbers (MAX = 100 numbers). Name this method findAvgFromFile. This method takes the input filename as its parameter. Specify the method declaration and the caller to this method with the input filename “input02.txt”.

Example:
“input02.txt”
3.1 3.2 3.3
Then the call
double avg = findAvgFromFile(“input02.txt”);
should make avg have value 3.3;

3. Write a method to open an input file for reading double numbers and returning an array containing those numbers (MAX = 100 numbers). Name this method findArrFromFile.This method should take the input filename as its parameter, and should return an array containing the read numbers. Specify the method declaration and the caller to this method with the input filename “input03.txt”.
Example:
“input03.txt”
3.4 4.5 5.6 6.7
Then the call
double arrList = findArrFromFile(“input03.txt”);
should make arrList[0] = 3.4, arrList[1] = 4.5, arrList[2] = 5.6 and arrList[3]
= 6.7;


4. Write a method to open an input file for reading an integer N. Name this method readCapFromFile. This method should take the input filename as its parameter and should return the read integer. Write another method named writePrimeToFile taking into account the followings as parameters (1) an integer P and (2) the output filename (type String) fileName. This method will not return any value and will only write to the output file the prime number closest to (but not exceeding) P.
For example:
The call (i)
int N = readCapFromFile(“input04.txt”);
with file “input04.txt” contains the value 20 should make N having value 20. The call (ii) ( following (i) ) writePrimeToFile(N, “output04.txt”); // here N==20 from the call (i)should create the file “output04.txt” containing the number 17 (since 17 is the prime closet to but not exceeding 20)

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

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

public class Problem1 {

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException
{
Scanner input = new Scanner(new File("input01.txt"));
PrintWriter writer = new PrintWriter("output01.txt", "UTF-8");
String read;

while(input.hasNext())
{
read = input.next();
try
{
int test = Integer.parseInt(read);
writer.print(test + " ");
} catch(NumberFormatException e){}
}
writer.close();
input.close();
}
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Cylinder Containers in Java
Homework Solution
$38.00
Java
Programming
Codes
Algorithms
Computer Science
Statements
Variables
Loops
Input
Output
Integers
Strings
Geometry
Mathematics
Cylinder
Radius
Finance
Costs
Containers
Companies
Functions
Multiplication Table in Java
Homework Solution
$40.00
Java
Programming
Codes
Algorithms
Computer Science
Statements
Variables
Loops
Rows
Columns
Input
Output
Integers
Strings
Multiplication
Tables
Operations
Conditions
Money Problems in Java
Homework Solution
$20.00
Java
Programming
Codes
Algorithms
Money
Dimes
Nickels
Pennies
Input
Output
Integers
Statements
Variables
Accounting
Recursion Methods
Finance
Java Programming Assignment
Homework Solution
$20.00
Java Test Application
Java Programming
Program
3-D Geometric Shape
Java Class
Constructor
Get help from a qualified tutor
Live Chats