Write a Java application that prompts and reads a value representing the radius, of a circle, then prints the circle's circumference and area.

I do not know where to start

Edited 2 Years Ago by pritaeas: Moved to Java

You start by learning the fundamental math of circles (click here).

Then you read your textbook or online tutorial about how to create a simple java program.

No one here will do your homework for you, so you might as well start studying.

whether it is homework, just work, a promise to your mum or just some doodlin' about, trying to learn how to write code, you won't learn anything by copy pasting code we write.

just try and write it, and if (or when) it would fail, post your code here, with the relevant information (error message, input data, what is expected, what does(n't) happen, ... )

you have a community of thousands of developers here, willing to help you improve your skills. but do understand we are volunteers. we are volunteering our time to help you out, we do expect (so do the community rules, btw) some effort from your side.

import javax.swing.*;
import java.util.Scanner;

class RadiusofCircle { 



public static void main(String[] args) {

Scanner sc = new Scanner(System.in); // read the keyboard
System.out.println("Enter radius:"); // print to screen
double radius = sc.nextDouble(); // read in a double from keyboard

// use the formulas for circle properties
double area = Math.PI * radius * radius; // d/2
double circ = Math.PI * radius * 2; // c = pi*d = 2 * pi * r

// construct a string to show on the screen
String output = "Radius: " + d/2 + "\n";
output = output + "Area: " + area + "\n";
output = output + "Circumference: " + circ;

// give it a title
String title = "Here's the result!";

// the type
int type = JOptionPane.PLAIN_MESSAGE;

JOptionPane.showMessageDialog(null, output, title, type);

} // end main 




} // end class

This is what I came up with. How do I fix the errors.

We can start by explaining how to post code in this forum correctly, and while we're at it, how to format Java code according to the usual conventions. The forum software does not, by default, retain indentation; in order to post code samples, you need to indent the code by four spaces. Fortunately, the forum software includes a 'Code' button at the top of the editing menu, which makes it easier to paste code in at one go:

import javax.swing.*;
import java.util.Scanner;

class RadiusOfCircle {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in); // read the keyboard
        System.out.println("Enter radius:"); // print to screen
        double radius = sc.nextDouble(); // read in a double from keyboard

        // use the formulas for circle properties
        double area = Math.PI * radius * radius; // d/2
        double circ = Math.PI * radius * 2; // c = pi*d = 2 * pi * r

        // construct a string to show on the screen
        String output = "Radius: " + d/2 + "\n";
        output += "Area: " + area + "\n";
        output += "Circumference: " + circ;

        // give it a title
        String title = "Here's the result!";

        // the type
        int type = JOptionPane.PLAIN_MESSAGE;

        JOptionPane.showMessageDialog(null, output, title, type);
    } // end main
} // end class

(Note that I made a couple of minor changes to the code, starting with the class name. HTH.)

Now that we can read your code, can you tell us what errors you are getting?

Edited 2 Years Ago by Schol-R-LEA

It's taking forever to run again so did I mess something up? I do recall it saying I have to enter the radius but I already entered it. My teacher told me at the end, a small box should pop up that displays it. It'll say build successful but nothing appeared.

just took a closer look at your code, I think I'm closer at understanding your issue. you are not running this code.
fact is, you haven't compiled it. you are using a variable d, which you never declared, hence, doesn't exist.

this code won't compile, so naturally, it won't return a correct result.

for your JOptionPane problem: this might help you out:

JOptionPane op = new JOptionPane(output,JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = op.createDialog(title);
dialog.setAlwaysOnTop(true);
dialog.setModal(true);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);      
dialog.setVisible(true);

Edited 2 Years Ago by stultuske

I'm sorry but I do not understand the code. I'm an Art major and this class is mandatory. So basically my whole code is wrong ?

no.
I'm not really sure why your code doesn't work, haven't really looked that hard into it, for lack of time, but if you replace your line with that, it will work.

but what I said was: you are using a variable that you don't declare, so I know that code won't compile.

the line:

String output = "Radius: " + d/2 + "\n";

will prevent your code from compiling, because you have no variable called d, so you can't divide it's value.

Its not homework, its a sample problem.

it's still homework, just self imposed homework...
Same logic applies, if you wait for others to do it for you you won't learn anything.

This article has been dead for over six months. Start a new discussion instead.