1. Write a Java program that asks the user to enter a distance in meters. The pr
ID: 3571841 • Letter: 1
Question
1. Write a Java program that asks the user to enter a distance in meters. The program will then present the following menu of selections:
Convert to kilometers
Convert to inches
Convert to feet
Quit the program
The program will convert the distance to kilometers (=meters*0.001), inches (=meters*39.37) or feet (=meters*3.281), depending on the user’s selection. Each conversion should be handled by a separate method that takes the distance in meters as an argument.
2. A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided (that is with a remainder of zero) by 1 and 5. The number 6 is not prime because it can be evenly divided by 1, 2, 3, and 6. Write a Java program that ask the user for an integer number a writes to a file the prime numbers between 1 and the number entered by the user. The program should include a method named isPrime that takes an integer number as an argument and returns true if the argument is prime, and false if the argument is not prime.
Explanation / Answer
Question 1:
DistanceTest.java
import java.util.Scanner;
public class DistanceTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a distance in meters: ");
double meters = scan.nextDouble();
while(true){
System.out.println("Choose your chice: 1.Convert to kilometers 2.Convert to inches 3.Convert to feet 4.Quit Enter your choice:");
int choice = scan.nextInt();
switch(choice){
case 1: convertKilometer(meters); break;
case 2: convertInches(meters); break;
case 3: convertFeet(meters); break;
case 4: System.exit(0);
}
}
}
public static void convertKilometer(double meters){
double kilometers =meters*0.001;
System.out.println("Distance in kilometer: "+kilometers);
}
public static void convertInches(double meters){
double inches =meters*39.37;
System.out.println("Distance in inches : "+inches );
}public static void convertFeet(double meters){
double feet =meters*3.281;
System.out.println("Distance in feet: "+feet);
}
}
Output:
Enter a distance in meters: 10
Choose your chice:
1.Convert to kilometers
2.Convert to inches
3.Convert to feet
4.Quit
Enter your choice:
1
Distance in kilometer: 0.01
Choose your chice:
1.Convert to kilometers
2.Convert to inches
3.Convert to feet
4.Quit
Enter your choice:
2
Distance in inches : 393.7
Choose your chice:
1.Convert to kilometers
2.Convert to inches
3.Convert to feet
4.Quit
Enter your choice:
3
Distance in feet: 32.81
Choose your chice:
1.Convert to kilometers
2.Convert to inches
3.Convert to feet
4.Quit
Enter your choice:
4
Question 2:
Primes.java
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Primes {
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer number: ");
int n = scan.nextInt();
PrintWriter pw = new PrintWriter("primes.txt");
for(int i=1; i<=n; i++){
if(isPrime(i)){
System.out.print(i+" ");
pw.write(i+" ");
}
}
System.out.println();
pw.flush();
pw.close();
System.out.println("File has been generated");
}
public static boolean isPrime(int n){
for (int f = 2; f < n / 2; f++) {
if (n % f == 0) {
return false;
}
}
return true;
}
}
Output:
Enter an integer number: 100
1 2 3 4 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
File has been generated
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.