I need this .java file converted to a .class file? import java.io.FileWriter; im
ID: 3865747 • Letter: I
Question
I need this .java file converted to a .class file?
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class NumberTester {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Prompt the user to input two integers: firstNum and secondNum where
// secondNum is at least 10 greater than firstNum, both numbers are positive
// integers, and secondNum is less than 1000.
boolean flag = true;
while(flag){
System.out.println("Enter a positive number :");
int firstNum = Integer.valueOf(scan.nextLine());
System.out.println("Enter another positive number (at least 10 greater than first Num, and is less than 1000.) :");
int secondNum = Integer.valueOf(scan.nextLine());
// Verify that the user entered acceptable numbers, and if not, provide error
// feedback and prompt them again.
if(firstNum <= 0 || secondNum <=0 ){
System.out.println("Invalid number ");
writeToFile("Invalid number ");
continue;
}
else if(secondNum < firstNum +10){
System.out.println("Second number is not 10 greater than first");
writeToFile("Second number is not 10 greater than first");
continue;
}
else if(secondNum > 1000){
System.out.println("Second number is greater than 1000");
writeToFile("Second number is greater than 1000");
continue;
}
else{
//4. Output all odd numbers between firstNum and secondNum inclusive, one
//number per line.
int firstNumNew = 0,secondNumNew = 0;
String output = "";
if(firstNum % 2 == 0){
firstNumNew = firstNum +1;
}
if(secondNum % 2 == 0){
secondNumNew = secondNum - 1;
}
for(int i=firstNumNew;i<=secondNumNew;i++){
output = output +" " +i;
}
System.out.println("output : "+output);
writeToFile(output);
// 5. Output the sum of all numbers between firstNum and secondNum
// exclusive.
int sum = 0;
for(int i=firstNum+1;i
sum = sum + i;
}
output=String.valueOf(sum);
writeToFile(output);
System.out.println("output : "+output);
break;
}
}
}
/**
* 3. Output all results to a file in the same directory as the program, placing an
* appropriate label between each section of output. Note that your program
* must be able to run repeatedly overwriting the file from the previous run.
* @param content
*/
public static void writeToFile(String content)
{
// creating FileWriter Object using file2
FileWriter filewriter;
try {
filewriter = new FileWriter("file_out.txt");
// Writing the content
filewriter.write(content);
filewriter.flush();
filewriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Explanation / Answer
// I have changed the Code , Please store it in to the NumberTester.java file.
//Compile it using javac NumberTester.java // it will generate NumberTester.class file
// Below is the changed code
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class NumberTester {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Prompt the user to input two integers: firstNum and secondNum where
// secondNum is at least 10 greater than firstNum, both numbers are positive
// integers, and secondNum is less than 1000.
boolean flag = true;
while(flag){
System.out.println("Enter a positive number :");
int firstNum = Integer.valueOf(scan.nextLine());
System.out.println("Enter another positive number (at least 10 greater than first Num, and is less than 1000.) :");
int secondNum = Integer.valueOf(scan.nextLine());
// Verify that the user entered acceptable numbers, and if not, provide error
// feedback and prompt them again.
if(firstNum <= 0 || secondNum <=0 ){
System.out.println("Invalid number ");
writeToFile("Invalid number ");
continue;
}
else if(secondNum < firstNum +10){
System.out.println("Second number is not 10 greater than first");
writeToFile("Second number is not 10 greater than first");
continue;
}
else if(secondNum > 1000){
System.out.println("Second number is greater than 1000");
writeToFile("Second number is greater than 1000");
continue;
}
else{
//4. Output all odd numbers between firstNum and secondNum inclusive, one
//number per line.
int firstNumNew = firstNum;
String output = "";
while(firstNumNew <= secondNum){
if(firstNumNew % 2 == 1){
output = output +" " +firstNumNew;
firstNumNew++;
}
}
System.out.println("output : "+output);
writeToFile(output);
// 5. Output the sum of all numbers between firstNum and secondNum
// exclusive.
int sum = 0;
for(int i=firstNum+1;i<secondNum;i++){
sum = sum + i;
}
output=String.valueOf(sum);
writeToFile(output);
System.out.println("output : "+output);
break;
}
}
}
/**
* 3. Output all results to a file in the same directory as the program, placing an
* appropriate label between each section of output. Note that your program
* must be able to run repeatedly overwriting the file from the previous run.
* @param content
*/
public static void writeToFile(String content)
{
// creating FileWriter Object using file2
FileWriter filewriter;
try {
filewriter = new FileWriter("file_out.txt");
// Writing the content
filewriter.write(content);
filewriter.flush();
filewriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.