Write a console program that: Uses a while loop to perform the following steps:
ID: 3869610 • Letter: W
Question
Write a console program that:
Uses a while loop to perform the following steps:
1. 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.
2. Verify that the user entered acceptable numbers, and if not, provide error
feedback and prompt them again.
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.
4. Output all odd numbers between firstNum and secondNum inclusive, one
number per line.
5. Output the sum of all numbers between firstNum and secondNum
exclusive.
Uses a for loop to perform the following steps:
1. Continue writing to the same file as before.
2. Write a label as before.
3. Output all numbers from secondNum to firstNum in a single line with
commas separating the numbers.
Write the date and time as the last line in the file in the format
yyyy-mm-dd hh:mm:ss.
Must have .JAVA and .Class in notepad function.
Explanation / Answer
NumberTester.java
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<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();
}
}
}
Sample output:
Enter a positive number :
15
Enter another positive number (at least 10 greater than first Num, and is less than 1000.) :
24
Second number is not 10 greater than first
Enter a positive number :
1
Enter another positive number (at least 10 greater than first Num, and is less than 1000.) :
1009
Second number is greater than 1000
Enter a positive number :
15
Enter another positive number (at least 10 greater than first Num, and is less than 1000.) :
100
output :
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
output : 4830
file_out.txt
4830
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.