In: Computer Science
Here is your required code in JAVA:
A)
import java.util.Scanner;
public class TempConvertor
{
// converts Celsius to Fahrenheit scale
static float convert(float n)
{
return ((n * 9.0f / 5.0f) + 32.0f);
}
// Driver code
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//number of test cases
//chnage accordingly
int t=4;
for(int i=0;i<t;i++)
{
try
{
float c;
System.out.println("\nTest Case "+i+": ");
System.out.print("Enter temperature in Celsius: ");
//taking input
c = sc.nextFloat();
//displaying output
System.out.println("Temperature in Fahrenheit: " +
convert(c));
}
catch(Exception e)
{
//displaying error
System.out.println("Error: Bad Input!");
}
}
}
}
B)
import java.util.Scanner;
public class FuelCosts
{
// calculate maximum distance that can betraveled
static double max_dis(double fuel, double efficiency)
{
return efficiency * fuel;
}
// Driver code
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//number of test cases
//chnage accordingly
int t=4;
for(int i=0;i<t;i++)
{
try
{
float f, e;
Exception ex = new Exception();
System.out.println("\nTest Case "+i+": ");
System.out.print("Enter gallons of Fuel in tank: ");
//taking input
f = sc.nextFloat();
if(f<0)
throw ex;
System.out.print("Enter efficiency in miles per gallon: ");
//taking input
e = sc.nextFloat();
if(e<0)
throw ex;
//displaying output
System.out.println("Maximum Distance that can be covered: " +
max_dis(f, e));
}
catch(Exception e)
{
//displaying error
System.out.println("Error: Bad Input!");
}
}
}
}
Please refer to
the screenshot of the code to understand the indentation of the
code:
A)
B)
Here is the Output
for the sample Test Cases:
A)
B)