Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need .Java file and .Class file. Write a console program that: Uses a while loop

ID: 3861195 • Letter: N

Question

Need .Java file and .Class file.

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.

Explanation / Answer

Program 1

Create a file named Validate.java and paste given code into it (filename must be Validate.java):

import java.util.Scanner;
import java.lang.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;

public class Validate {
  
private static final String FILENAME = "output.txt";
public static void main(String[] args)
{
try
{
BufferedWriter bw = null;
FileWriter fw = null;
fw = new FileWriter(FILENAME);
bw = new BufferedWriter(fw);
int firstnum,secondnum;
Scanner scanner = new Scanner(System.in);
while(true)
{
System.out.println("Enter the first number!");
firstnum = scanner.nextInt();
System.out.println("Enter the second number!");
secondnum = scanner.nextInt();
if((secondnum - firstnum) >= 10 && (secondnum < 1000) && (secondnum > 0) && (firstnum >0) )
{
fw.write("Thanks, numbers you have entered are acceptable!");
fw.write(" ");
fw.write("List of odd numbers:");
fw.write(" ");
int sum = -(firstnum + secondnum);
for(int i = firstnum ; i <= secondnum; i++)
{
if(i%2 == 1)
{
fw.write(Integer.toString(i));
fw.write(" ");
}
sum = sum + i;
}
fw.write("sum of all numbers between firstNum and secondNum exclusive = " + sum);
fw.write(" ");
fw.close();
break;
}
else
{
fw.write("You entered firstnum = " + firstnum + " , secondnum = " + secondnum );
fw.write("You have entered invalid numbers,secondNum should be at least 10 greater than firstNum, both numbers must be positive integers, and secondNum must be less than 1000!");
fw.write("You need to re-enter the numbers!");

System.out.println("You entered firstnum = " + firstnum + " , secondnum = " + secondnum );
System.out.println("You have entered invalid numbers,secondNum should be at least 10 greater than firstNum, both numbers must be positive integers, and secondNum must be less than 1000!");
System.out.println("You need to re-enter the numbers!");
}

}   
}
catch (IOException e)
{
e.printStackTrace();
}
}
  
}

Sample Input:

Enter the first number!
1
Enter the second number!
5
You entered firstnum = 1 , secondnum = 5
You have entered invalid numbers,secondNum should be at least 10 greater than firstNum, both numbers must be positive integers, and secondNum must be less than 1000!
You need to re-enter the numbers!
Enter the first number!
1
Enter the second number!
11

Sample Output.txt:

You entered firstnum = 1 , secondnum = 5You have entered invalid numbers,secondNum should be at least 10 greater than firstNum, both numbers must be positive integers, and secondNum must be less than 1000!You need to re-enter the numbers!Thanks, numbers you have entered are acceptable!
List of odd numbers:
1
3
5
7
9
11
sum of all numbers between firstNum and secondNum exclusive = 54

Program 2)

Create a file named Program2.java and paste given code into it (filename must be Program2.java):

import java.util.Scanner;
import java.lang.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;

public class Program2 {
  
private static final String FILENAME = "output.txt";
public static void main(String[] args)
{
try
{
BufferedWriter bw = null;
FileWriter fw = null;
fw = new FileWriter(FILENAME);
bw = new BufferedWriter(fw);
int firstnum,secondnum;
Scanner scanner = new Scanner(System.in);
while(true)
{
System.out.println("Enter the first number!");
firstnum = scanner.nextInt();
System.out.println("Enter the second number!");
secondnum = scanner.nextInt();
if((secondnum - firstnum) >= 10 && (secondnum < 1000) && (secondnum > 0) && (firstnum >0) )
{
fw.write("Thanks, numbers you have entered are acceptable!");
fw.write(" ");
fw.write("Numbers from " + firstnum + " to " + secondnum + ":" );
fw.write(" ");
for(int i = firstnum ; i <= secondnum; i++)
{

fw.write(Integer.toString(i));
fw.write(",");

}
fw.write(" ");
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
fw.write(dateFormat.format(date));
  
fw.close();
break;
}
else
{
fw.write("You entered firstnum = " + firstnum + " , secondnum = " + secondnum );
fw.write("You have entered invalid numbers,secondNum should be at least 10 greater than firstNum, both numbers must be positive integers, and secondNum must be less than 1000!");
fw.write("You need to re-enter the numbers!");

System.out.println("You entered firstnum = " + firstnum + " , secondnum = " + secondnum );
System.out.println("You have entered invalid numbers,secondNum should be at least 10 greater than firstNum, both numbers must be positive integers, and secondNum must be less than 1000!");
System.out.println("You need to re-enter the numbers!");
}

}   
}
catch (IOException e)
{
e.printStackTrace();
}
}
  
}

Sample Input:

Enter the first number!
1
Enter the second number!
11

Sample Output.txt:

Thanks, numbers you have entered are acceptable!
Numbers from 1 to 11:
1,2,3,4,5,6,7,8,9,10,11,
2017/07/27 23:21:55

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote