Question

In: Computer Science

import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...

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?

Solutions

Expert Solution


//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");

   }

}


Related Solutions

My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // parse the number of strings int numStrings = Integer.parseInt(sc.nextLine());    // parse each string String[] stringsArray = new String[numStrings]; for (int i = 0; i < numStrings; i++) { stringsArray[i] = sc.nextLine(); }    // print whether there are duplicates System.out.println(hasDuplicates(stringsArray)); }    private static boolean hasDuplicates(String[] stringsArray) { // TODO fill this in and remove the below line...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {   ...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {    Scanner console = new Scanner(System.in);    double radius; double height; System.out.println("This program can calculate "+ "the area of a rectangle, the area "+ "of a circle, or volume of a cylinder."); System.out.println("To run the program enter: "); System.out.println("1: To find the area of rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the volume of a cylinder."); System.out.println("-1: To terminate the...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num=0; String s = ""; while (true) { System.out.println("Enter an integer greater than 1: "); try { // reading input s = sc.nextLine(); // converting into int num = Integer.parseInt(s); break; } catch (Exception e) { System.out.println(s + " is not valid input."); } } // Now we have a valid number // putting into square int square = num; int count...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test the smallest method System.out.print("smallest(1, 0, 2) -> "); System.out.println( smallest(1, 0, 2) ); // test the average method System.out.print("average(95, 85, 90) -> "); System.out.println( average(95, 84, 90) ); } // end main /* * smallest(double, double, double) -> double * * method is given 3 numbers, produces the smallest of the three * * examples: * smallest(1, 0, 2) -> 0.0 */ public static...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT