In: Computer Science
import java.util.Random;
class Conversions {
public static void main(String[] args) {
public static double quartsToGallons(double quarts) {
return quarts * 0.25;
}
public static double milesToFeet(double miles) {
return miles * 5280;
}
public static double milesToInches(double miles) {
return miles * 63360;
}
public static double milesToYards(double miles) {
return miles * 1760;
}
public static double milesToMeters(double miles) {
return miles * 1609.34;
}
public static double milesToKilometer(double miles) {
return milesToMeters(miles) / 1000.0;
}
public static double inchesToFeet(double inches) {
return inches * 0.0833333;
}
public static int minutesToSeconds(int minutes) {
return minutes * 60;
}
public static double minutesToHours(int minutes) {
return minutes / 60.0;
}
public static double minutesToDays(int minutes) {
return minutes / (24 * 60);
}
public static int randRange(int low, int high) {
Random random = new Random();
// returns inclusive of low and high
return low + random.nextInt(1 + high - low);
}
public static double percentage(double x, double y) {
return x * 100 / y;
}
}
class Driver {
public static void main(String[] args) {
System.out.println("1000 Quarts = " +
Conversions.quartsToGallons(1000) + " gallons");
System.out.println("12 Miles = " + Conversions.milesToFeet(12) +
" feet");
System.out.println("12 Miles = " + Conversions.milesToInches(12) +
" inches");
System.out.println("12 Miles = " + Conversions.milesToYards(12) + "
yards");
System.out.println("12 Miles = " + Conversions.milesToMeters(12) +
" meters");
System.out.println("12 Miles = " + Conversions.milesToKilometer(12)
+ " kilometers");
System.out.println("50 inches = " + Conversions.inchesToFeet(50) + " feet");
System.out.println("1200 minutes = " +
Conversions.minutesToSeconds(1200) + " seconds");
System.out.println("1200 minutes = " +
Conversions.minutesToHours(1200) + " hours");
System.out.println("1200 minutes = " +
Conversions.minutesToDays(1200) + " days");
System.out.println("Random number between [1-20] = " + Conversions.randRange(1, 20));
System.out.println("2 is " + Conversions.percentage(2, 5) + "% of 5");
}
}
what is wrong with my code?
//You have defined functions in main() method
//and called the functions defined inside in Driver class
//just remove the main method definition in Conversions
//and then execute the Driver class . you will get the
output.
//--------- Conversions.java ------------
import java.util.Random;
class Conversions {
public static double quartsToGallons(double quarts) {
return quarts * 0.25;
}
public static double milesToFeet(double miles)
{
return miles * 5280;
}
public static double milesToInches(double miles)
{
return miles * 63360;
}
public static double milesToYards(double miles)
{
return miles * 1760;
}
public static double milesToMeters(double miles)
{
return miles * 1609.34;
}
public static double milesToKilometer(double miles)
{
return milesToMeters(miles) /
1000.0;
}
public static double inchesToFeet(double inches)
{
return inches * 0.0833333;
}
public static int minutesToSeconds(int minutes)
{
return minutes * 60;
}
public static double minutesToHours(int minutes)
{
return minutes / 60.0;
}
public static double minutesToDays(int minutes)
{
return minutes / (24 * 60);
}
public static int randRange(int low, int high)
{
Random random = new Random();
// returns inclusive of low and
high
return low + random.nextInt(1 +
high - low);
}
public static double percentage(double x, double y)
{
return x * 100 / y;
}
}
//----------- Driver.java ---------
class Driver {
public static void main(String[] args) {
System.out.println("1000 Quarts = "
+ Conversions.quartsToGallons(1000) + " gallons");
System.out.println("12 Miles = "
+ Conversions.milesToFeet(12) + " feet");
System.out.println("12 Miles = " +
Conversions.milesToInches(12) + " inches");
System.out.println("12 Miles = " +
Conversions.milesToYards(12) + " yards");
System.out.println("12 Miles = " +
Conversions.milesToMeters(12) + " meters");
System.out.println("12 Miles = " +
Conversions.milesToKilometer(12) + " kilometers");
System.out.println("50 inches = " + Conversions.inchesToFeet(50) + " feet");
System.out.println("1200 minutes
= " + Conversions.minutesToSeconds(1200) + " seconds");
System.out.println("1200 minutes =
" + Conversions.minutesToHours(1200) + " hours");
System.out.println("1200 minutes =
" + Conversions.minutesToDays(1200) + " days");
System.out.println("Random number between [1-20] = " + Conversions.randRange(1, 20));
System.out.println("2 is " + Conversions.percentage(2, 5) + "% of 5");
}
}