Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: [Easy] Finding area of triangle, rectangle, and circle.

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default [Easy] Finding area of triangle, rectangle, and circle.

    User inputs information to find the area of one of the three shapes. It works fine, but I need it to round to the second decimal. For example when you enter 2.5 for the radius of the circle, it outputs "Area of the circle is 19", rather than what I want it to, "Area of the circle is 19.63". Here is all of the code first:
    package areas;
     
    import java.util.Scanner;
     
    public class triangle_rectangle_circle {
     
    	public static void main(String[] args) {
     
    		System.out.println("This program was written by Dan");
    		System.out.println("-----------------------------");
     
    		String varName;
            System.out.print("Enter your name: ");
            Scanner stringScanner = new Scanner(System.in); //string scanner
            varName = stringScanner.nextLine(); 
     
    		String transChar;
    		System.out.print("Please select a shape: (R)ectangle, (T)riangle, or (C)ircle: ");
    		transChar = stringScanner.nextLine(); //transChar = transition to char
    		char caseSelector = transChar.charAt(0);
    		System.out.println("-----------------------------");
     
    		Scanner numScanner = new Scanner(System.in); //number scanner
     
    		switch (caseSelector){
     
    			//Case: Rectangle
     
    			case 'r':
    			case 'R':
     
    				double rLength;
    				double rWidth;
     
    				System.out.print("Please enter width: ");
    				rLength = numScanner.nextDouble();
    				System.out.println("Please enter height: ");
    				rWidth = numScanner.nextDouble();
    				double rArea = (rLength * rWidth);
    				System.out.print("Area of the rectangle is " + Math.round(rArea * 100 / 100));
    				System.out.println();
    				break;
     
     
    			//Case: Triangle
     
    			case 't':
    			case 'T':
     
    				double tBase;
    				double tHeight;
     
    				System.out.println("Please enter base:");
    				tBase = numScanner.nextDouble();
    				System.out.println("Please enter height: ");
    				tHeight = numScanner.nextDouble();
    				double tArea = (.5 * tBase * tHeight);
    				System.out.println("Area of the triangle is " + Math.round(tArea * 100) / 100);
    				System.out.println();
    				break;
     
     
    			//Case: Circle
     
    			case 'c':
    			case 'C':
     
    				double cRadius;
     
    				System.out.println("Please enter radius: ");
    				cRadius = numScanner.nextDouble();
    				double cArea = (Math.pow (cRadius,2) * Math.PI);
    				System.out.println("Area of the circle is " + Math.round(cArea * 100) / 100);
    				System.out.println();
    				break;
     
    			default:
     
    				System.out.println("Please restart the program and enter a valid option");
    				break;
     
    		} //switch (caseSelector)
     
    		System.out.println("-----------------------------");
    		System.out.println("This program was run by: " + varName);
    		stringScanner.close();
    		numScanner.close();
     
    	} //main method
     
    } //public class triangle_rectangle_circle

    This is the part where I believe I'm going wrong:
    case 'c':
    			case 'C':
     
    				double cRadius;
     
    				System.out.println("Please enter radius: ");
    				cRadius = numScanner.nextDouble();
    				double cArea = (Math.pow (cRadius,2) * Math.PI);
    				System.out.println("Area of the circle is " + Math.round(cArea * 100) / 100);
    				System.out.println();
    				break;
    double cArea = (Math.pow (cRadius,2) * Math.PI);
    calculates the area
    System.out.println("Area of the circle is " + Math.round(cArea * 100) / 100);
    is supposed to round the area to the second decimal by using
     Math.round(cArea * 100) / 100);

    I'm not sure where I'm going wrong, I just need the 2 decimal places to be there when I enter a decimal, and it's not a integer. Ty!


  2. #2
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: [Easy] Finding area of triangle, rectangle, and circle.

    Instead of using Math.round. Look at using String.format(format, args)
    Formatter (Java Platform SE 7 )

    Alternatively you can take your original 'double' value. Multiply it by 100 and cast to an 'int'. Take the new value divide it by 100 again (casting back to double) and you end up with a 2 decimal place floating point. The problem with this method is if you are not familiar/comfortable with casting.

    --- Update ---

    Or you can cast the Math.round result to a double value. Seeing how you are multiplying and dividing by 100 already. Remember that Math.round returns an int/long, but you can cast that into another data type.
    Last edited by Kewish; October 3rd, 2013 at 06:16 PM. Reason: Posted the wrong formatter API

Similar Threads

  1. Replies: 5
    Last Post: August 10th, 2013, 03:21 PM
  2. Replies: 2
    Last Post: October 16th, 2012, 10:13 PM
  3. java (circle)
    By coder.freak in forum Member Introductions
    Replies: 3
    Last Post: March 26th, 2011, 12:01 PM
  4. Area of a triangle (using 2 extra methods) error help
    By SilentPirate in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 12th, 2010, 06:08 PM

Tags for this Thread