//February 26,2016 import java.util.Scanner; class Project_2 { public static voi
ID: 3673364 • Letter: #
Question
//February 26,2016 import java.util.Scanner; class Project_2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter first name"); // entering the first name String first = sc.next(); // entering the last name System.out.println("Enter Last name"); String last = sc.next(); // entering the radius System.out.println("Enter Radius"); float rad = sc.nextFloat(); // Enter Height System.out.println("Enter height"); float height = sc.nextFloat(); // entering binary number System.out.println("Enter binary number"); String binaryString = sc.next(); // converting given binary to decimal int bd = Integer.parseInt(binaryString, 2); // generating random radious between 1 and given binary num double randrad = (Math.random()) * bd; // generating random height between 1 and 10 double randht = (Math.random()) * 10; // Caluclating volume for both cylinders double vol1 = 3.14 * rad * rad * height; double vol2 = 3.14 * randrad * randrad * randht; System.out.println("Hello " + first + " " + last); System.out.println("volume of Cylinder with radious " + rad + " height " + height + "is" + vol1); System.out.println("Random Cylinder volume with radious " + randrad + " height " + randht + "is" + vol2); System.out.println("the binary value of" + binaryString + " is " + bd); } }
This works fine but how do i get it to where my output would be smaller decimal form? like for example:2.8435560340752897 is smaller like 2.84?
Explanation / Answer
Hi, I have formatted double printing.
import java.text.DecimalFormat;
//February 26,2016
import java.util.Scanner;
class Project_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//Decimal formatter
//Note the "00", meaning exactly two decimal places.
DecimalFormat f = new DecimalFormat("##.00");
System.out.println("Enter first name");
// entering the first name
String first = sc.next();
// entering the last name
System.out.println("Enter Last name");
String last = sc.next();
// entering the radius
System.out.println("Enter Radius");
float rad = sc.nextFloat();
// Enter Height
System.out.println("Enter height");
float height = sc.nextFloat();
// entering binary number
System.out.println("Enter binary number");
String binaryString = sc.next();
// converting given binary to decimal
int bd = Integer.parseInt(binaryString, 2);
// generating random radious between 1 and given binary num
double randrad = (Math.random()) * bd;
// generating random height between 1 and 10
double randht = (Math.random()) * 10;
// Caluclating volume for both cylinders
double vol1 = 3.14 * rad * rad * height;
double vol2 = 3.14 * randrad * randrad * randht;
System.out.println("Hello " + first + " " + last);
System.out.println("volume of Cylinder with radious " +
f.format(rad) + " height " + f.format(height) + " is" + f.format(vol1));
System.out.println("Random Cylinder volume with radious " +
f.format(randrad) + " height " + f.format(randht) + "is" + f.format(vol2));
System.out.println("the binary value of" + binaryString + " is " + bd);
}
}
/*
Output:
Enter first name
Alex
Enter Last name
Bob
Enter Radius
12
Enter height
23
Enter binary number
100001010
Hello Alex Bob
volume of Cylinder with radious 12.00 height 23.00 is10399.68
Random Cylinder volume with radious 10.96 height 3.92is1476.06
the binary value of100001010 is 266
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.