Java code for circle assignment

Code compiles but get errors when I run it.

Page 1 of 1

3 Replies - 7474 Views - Last Post: 22 March 2009 - 07:37 PM Rate Topic: -----

#1 dsingram  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-March 09

Java code for circle assignment

Post icon  Posted 22 March 2009 - 12:51 PM

The below java code compiles and displays the entry for the user. Once the user enters a number I get the Diameter is: display but with an extension formatting error. I don't anyone to do the code I just need a few suggestions. I am taking the course online and online is not very user friendly when the professor is trying to get the point across by referencing the book page only. Any help will be greatly appreciated. -Dan-




// Ex. 2.28: Circle.java
// Take user input for radius of a circle and provide
// circumference, diameter and area of the circle.
import java.util.Scanner; // program uses class Scanner

public class Circle
{
// main method begins execution of Java application
public static void main( String arg[] )
{
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );

int radius; // first number to compare



System.out.print( "Enter radius of circle: " ); // prompt
radius = input.nextInt(); // read first number from user

System.out.printf( "Diameter is: %f\n", ( radius * 2 ) );

System.out.printf( "Circumfrence is: %f\n", ( 2 * Math.PI * radius ) );

System.out.printf( "Area is: %f\n", ( ( Math.PI * radius) * 2 ) );

} // end method main

} // end class Circle

Is This A Good Question/Topic? 0
  • +

Replies To: Java code for circle assignment

#2 Nat3TheGreat13  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 162
  • Joined: 09-February 09

Re: Java code for circle assignment

Posted 22 March 2009 - 01:23 PM

declare radius as a double not an int.
Was This Post Helpful? 0
  • +
  • -

#3 Nat3TheGreat13  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 162
  • Joined: 09-February 09

Re: Java code for circle assignment

Posted 22 March 2009 - 01:30 PM

Also, you might want to check your equation for the area of a circle.
To find the area of a circle it's PI r
Was This Post Helpful? 0
  • +
  • -

#4 priya_lorha  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 78
  • Joined: 02-August 08

Re: Java code for circle assignment

Posted 22 March 2009 - 07:37 PM

import java.util.Scanner;
class circle{
public static void mia(String args[])
{ 
Scanner input = new Scanner( System.in );
System.out.print( "Enter radius of circle: " ); // prompt
double radius = input.nextInt(); // read first number from user
double diameter=2*radius;
double circumference=2*3.14*radius;
double area=Math.pow(radius,2)*3.14;
System.err.printf("diameter="+ diameter);
System.err.printf("circumference="+ circumference);
System.err.printf("area="+ area);
}
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1