I'm having to design a program in java to get the radius from the user than print back the diameter, circumference and area using that radius I'm not sure where I'm going wrong in this

import java.util.Scanner;

public class Circle
{
	private static void circleInfo()
	{
		
		float radius;
	
		public float getSolvedDiam()
		{
			return radius * 2;
		}
		
		public float getSolvedCircum()
		{
			return Math.PI * (radius * 2);
		}
		
		public float getSolvedArea()
		{
			return Math.PI * Math.pow(radius, 2);
		}
	}
		Scanner inData = new Scanner(System.in);
		System.out.println("Enter the radius");
	   float radius = inData.nextFloat();
	}
	
	public static void main(String[] args)
	{
	   circleInfo();
		System.out.println(circleInfo.getSolvedDiam());
		System.out.println(circleInfo.getSolvedCircum());
		System.out.println(circleInfo.getSlovedArea());
	}
}

Thanks a lot

Dean

Recommended Answers

All 8 Replies

You are trying to define methods inside other methods, and that's not legal.
You probably want to have a constructor for Circle that takes radius as a parameter and stores it. You can then create a new Circle object using the radius value supplied by the user. Then all your getXXX methods can access that value to compute their answers.

You are trying to define methods inside other methods, and that's not legal.
You probably want to have a constructor for Circle that takes radius as a parameter and stores it. You can then create a new Circle object using the radius value supplied by the user. Then all your getXXX methods can access that value to compute their answers.

Which part? where I have float radius than radius = in.nextFloat()?? was that the example?

private static void circleInfo() // method
{
 
  float radius;
 
  public float getSolvedDiam() // method defined inside method
private static void circleInfo() // method
{
 
  float radius;
 
  public float getSolvedDiam() // method defined inside method

Than how do I make my calculations part of the the circleInfo() method?
just by defining the variables inside circleInfo() like

solvedArea = Math.pow(radius, 2) * Math.PI;

than later use circleInfo.getsolvedArea??

You need to move getSolvedDiam, getSolvedCircum, and getSolvedArea out of circleInfo and into the Circle class.

public class Circle {
  private static void circleInfo() {...}
  public float getSolvedDiam() {...} 
  public float getSolvedCircum() {...} 
  public float getSolvedArea() {...}
 
  public static void main(String[] args) {
    circleInfo();
    System.out.println(circleInfo.getSolvedDiam());
    System.out.println(circleInfo.getSolvedCircum());
    System.out.println(circleInfo.getSlovedArea());
  }
}

I'm somewhat of a newbie too (or maybe an advanced newbie if there is such a thing) so I may be wrong, but you can get declare 'float radius' (in the Circle class) globally. Then have getSolvedDiam, getSolvedCircum, and getSolvedArea take in a float parameter, for example getSolvedArea(float r). Then use 'r' in the methods instead of 'radius'. Lastly, feed the global 'radius' inside the method declarations in 'main'. You may have to play around with this info as I'm going by memory and I can't test it at work...

I hope this helps though.

Doesn't really seem to help me sorry lol would it be better to make another class to take care of the calculates that incorporate it in as like a driver?

Alright I made my Circle method into one .java file no I have to add a parameter-less constructor and use the method printCircleInfo() to display the results

import java.util.Scanner;
     
public class Circle
{

    public static void main(String[] args)
    {	
	 	Scanner in = new Scanner(System.in);
    	System.out.println("Enter the radius");
    	float radius = in.nextFloat();
		
   	System.out.println("Radius is equal to: " + radius);
		System.out.println("Diameter is equal to: " + radius * 2);
		System.out.println("Circumference is equal to: " + Math.PI * (radius * 2));
		System.out.println("Area is equal to: " + Math.PI * Math.pow(radius, 2));
	}
}

now is this done by declaring CircleInfo() as private static void and incorporate the values from the parameterless constructor like having

Circle()
radius;
diam;
circum;
area;

now would I draw those variables into CircleInfo() to do the calculations?

Thanks

Dean

Is this thread finished? You have started another one on exactly the same subject. That's going to confuse people and waste their time. At least mark this one "solved" so no one spends any more effort on it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.