Write a program to determine, and output to the screen, the class of an earthqua
ID: 3842175 • Letter: W
Question
Write a program to determine, and output to the screen, the class of an earthquake, given its magnitude. A class of minor is given for those in the range of [0, 4), light if in [4, 5), moderate if in [5, 6), strong if in [6, 7), major if in [7, 8) and great if in [8, infinity). Write a program to guess a magic number m = 4, and output to a file, each of guesses that are made. A sequence of random numbers is generated, each in the range [1, 10]. If the number is equal to m, then the program terminates. Otherwise, it generates another number. Your submission should include the contents of the output file using a random number generator seed of 760.Explanation / Answer
The language to write is not specified.
I assumed this assignment to be in Java language.
Please find the codes for both as below.
---------------------------------------------------
Prog1.java
----------
import java.util.Scanner;
public class Prog1 {
public static void main(String[] args) {
double magnitudeValue = 0;
System.out.println("Please enter the magnitude of EarthQuake");
// used to read input.
Scanner sc = new Scanner(System.in);
magnitudeValue = sc.nextDouble();
sc.close();
System.out.print("Earthquake was ");
// Start with the largest value to compare i.e. 8 and above.
if(magnitudeValue >= 8){
System.out.print("Great");
}
else if(magnitudeValue >= 7 && magnitudeValue <8){
System.out.print("Major");
}
else if(magnitudeValue >= 6 && magnitudeValue <7){
System.out.print("Strong");
}
else if(magnitudeValue >= 5 && magnitudeValue < 6){
System.out.print("Moderate");
}
else if(magnitudeValue >= 4 && magnitudeValue < 5){
System.out.print("Light");
}
else if(magnitudeValue >= 0 && magnitudeValue < 4){
System.out.print("Minor");
}
}
}
OUTPUT:
Please enter the magnitude of EarthQuake
5
Earthquake was Moderate
---------------------------------------------------
Prog2.java
----------
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class Prog2 {
public static void main(String[] args) throws IOException {
int magicNumber = 4;
// Seed is specified here as 760
Random randomno = new Random(760);
// output file name specified here.
final String FILENAME = "output.txt";
// FileWriter class to write to file.
FileWriter fw = new FileWriter(FILENAME);
BufferedWriter bw = new BufferedWriter(fw);
while(true){
// Generate random numbers between 1 and 10.
int temp = randomno.nextInt(10 - 1) + 1;
// write the value to file.
bw.write(Integer.toString(temp));
bw.newLine();
// compare with magicNumber to end the loop.
if (temp == magicNumber)
break;
}
bw.close();
fw.close();
}
}
OUTPUT:
A new file output.txt will be created in the same folder as this java file is present
having the random values.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.