About the application program Write an application program that declares one One
ID: 3639619 • Letter: A
Question
About the application program
Write an application program that declares one OneNumber object and one TwoNumber object. (You will also need to declare other variables.) The program should ask the user to enter the input filename and the output filename (use the Scanner class or JOptionPane). The main method should contain a while loop that reads data from the input file. The loop should terminate when there is no more data in the file. The OneNumber object should call appropriate accessor/mutator methods and each of the additional methods required for this particular class. The TwoNumber object should call appropriate accessor/mutator methods and each of the additional methods required for this particular class. The program should output to a file the value(s) of the field(s) and the results of the unique class methods (isPrime, sumOfDigits, divisibleByNine or greatestCommonFactor, sumBetweenNumbers). Make sure that each part of the output is properly labeled and easy to read.
About the first class
Write a class named OneNumber that has one integer field. Use only while or for loops. You should only exit from a loop by a false loop condition —no exit or breaks allowed.
About the second class
Write a class named TwoNumber.that has two integer fields. Use only while or for loops. You should only exit from a loop by a false loop condition—no exit or breaks allowed.
Explanation / Answer
import java.io.*;
public class main {
public static void main(String[] args) {
String inputfile;
String outputfile;
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Enter Input Filename : ");
inputfile = in.next();
System.out.println("Enter Output Filename : ");
outputfile = in.next();
try {
BufferedReader fin = new BufferedReader(new FileReader(inputfile));
FileWriter fstream = new FileWriter(outputfile);
BufferedWriter out = new BufferedWriter(fstream);
String str;
while ((str = fin.readLine()) != null) {
int value = Integer.parseInt(str);
OneNumber num = new OneNumber(value);
out.write("Value : "+value+" Sum of digits "+num.sumofDigits()+" Divisible by 9 :"+num.isDivisibleByNine()+" Prime :"+num.isPrime()+" ");
}
fin.close();
out.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
in.close();
}
}
import java.io.*;
public class main {
public static void main(String[] args) {
String inputfile;
String outputfile;
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Enter Input Filename : ");
inputfile = in.next();
System.out.println("Enter Output Filename : ");
outputfile = in.next();
try {
BufferedReader fin = new BufferedReader(new FileReader(inputfile));
FileWriter fstream = new FileWriter(outputfile);
BufferedWriter out = new BufferedWriter(fstream);
String str;
while ((str = fin.readLine()) != null) {
int value = Integer.parseInt(str);
OneNumber num = new OneNumber(value);
out.write("Value : "+value+" Sum of digits "+num.sumofDigits()+" Divisible by 9 :"+num.isDivisibleByNine()+" Prime :"+num.isPrime()+" ");
}
fin.close();
out.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
in.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.