# Answered! Review Exercise 4 from Lab 2. a) Write a method called convertDistance that takes a double and a Boolean…

ex2 a and b

Review Exercise 4 from Lab 2. a) Write a method called convertDistance that takes a double and a Boolean parameter and returns a double value that is calculated by converting between kilometres and miles. The method should convert the double parameter into either kilometres or miles, depending on the value of the Boolean parameter. Your method should convert from kilometres into miles if the Boolean parameter is true, and should convert from miles into kilometres otherwise. b) Write a main method that prints to output two prompts for the user, and after each prompt pauses to collect input from the user using the Scanner class. Prompt the user once for a number of kilometres, and convert this into miles, and also prompt the user for a number of miles, which should be converted into miles. Use nicely formatted output that makes clear both the input and output. For instance, your main method could print out something like: There are 6.21371 miles in 10.0 kilometres! There are 8.04672 kilometres in 5.0 miles! Review Exercise 5 from Lab 3. a) Write a method called circleCirc that takes a single double parameter and returns a double value that is equal to the circumference of a circle. The method should calculate the circumference using the absolute value of the double parameter as the circles radius. b) Write a method called circleArea that takes one double parameter and returns a double value that is equal to area of a circle. The method should calculate the area using the absolute value of the double parameter as the circles radius.

Hi,

Don't use plagiarized sources. Get Your Custom Essay on
Answered! Review Exercise 4 from Lab 2. a) Write a method called convertDistance that takes a double and a Boolean…
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS \$13/PAGE

Ans 2 a)

import java.text.DecimalFormat;
import java.util.Scanner;
public class HelloWorld{
private Double convertdistance(int destination, boolean isMiles)
throws IllegalArgumentException {
if (destination < 0) {
throw new IllegalArgumentException();
}
//return String.format(destination + ” ” +new DecimalFormat(“#.###”).format(isMiles ? destination / 0.621371192 : destination / 1.609344));
return isMiles ? destination / 0.621371192 : destination / 1.609344;
}
public static void main(String []args){
HelloWorld h1=new HelloWorld();
try {
System.out.println(h1.convertdistance(10, false)); // 10 km
System.out.println(h1.convertdistance(10, true)); // 10 m
} catch (IllegalArgumentException e) {
System.out.println(“You pass wrong arguments to the method!”);
}
}
}

Screenshot-

Ans 2 b)

Java code-

import java.text.DecimalFormat;
import java.util.Scanner;
public class HelloWorld{
private Double convertdistance(double destination, boolean isMiles)
throws IllegalArgumentException {
if (destination < 0) {
throw new IllegalArgumentException();
}
return isMiles ? destination / 0.621371192 : destination / 1.609344;
}
public static void main(String []args){
double kms,miles;
HelloWorld h1=new HelloWorld();
Scanner s=new Scanner(System.in);
System.out.println(“Please enter the distance in Kilometers”);
kms=s.nextDouble();
try {
System.out.println(“There are “+h1.convertdistance(kms, false)+” miles in “+kms+” kilometers”);
} catch (IllegalArgumentException e) {
System.out.println(“Wrong arguments!”);
}
System.out.println(“Please enter the distance in miles”);
miles=s.nextDouble();
try {
System.out.println(“There are “+h1.convertdistance(miles, true)+” kilometers in “+miles+” miles”);
} catch (IllegalArgumentException e) {
System.out.println(“Wrong arguments!”);
}

}
}

Screenshot-

Regards,

Vinay Singh