I have been working on this for days and can't get the program to run. I have to name 3 fields, and utilize setRadius() getRadius(). I also must set and compute all fields in setRadius(). At first I had no main, but kept getting error. I included main, program compiles, but won't run. Please help.
public class Circle
{
public static void main(String[] args)
{
Circle myCircle = new Circle();
}
double radius;
double diameter;
double area;
public void Circle()
{
radius= 1;
diameter = radius * radius;
area = Math.PI * radius * radius;
}
public void setRadius()
{
radius = 1;
diameter = radius * radius;
area = Math.PI * (radius * radius);
}
public double getRadius()
{
return radius;
}
public void display()
{
System.out.println("The radius is " + radius);
System.out.println("The diameter is " + diameter);
System.out.println("The area is " + area);
}
}
Like someone said in comments your constructor is not valid.
your setRadius shoud get an parameter
and call display after construction
public class Circle {
public static void main(String[] args) {
Circle myCircle = new Circle();
myCircle.display();
}
double radius;
double diameter;
double area;
public Circle() {
radius = 1;
diameter = radius * radius;
area = Math.PI * radius * radius;
}
public void setRadius(double radius) {
this.radius = radius;
diameter = radius * radius;
area = Math.PI * (radius * radius);
}
public double getRadius() {
return radius;
}
public void display() {
System.out.println("The radius is " + radius);
System.out.println("The diameter is " + diameter);
System.out.println("The area is " + area);
}
}
Main is the entry point of your program, whatever you include in it will run. Your program is running it's just not doing anything (except creating a circle). If you want the program to display those messages you need to call display() inside main.
public static void main(String[] args)
{
Circle myCircle = new Circle();
myCircle.display();
}
add myCircle.display(); to main.
It works. But only doing you make here is creting new object.
Add some more operations for your object.
public static void main(String[] args)
{
Circle myCircle = new Circle();
myCircle.setRadius();
myCircle.display();
}
UPD: Also you have some mistakes:
diametr = 2 * radius (not radius * radius)
public void Circle() - is a function, not constructor. It's bad practice to use methods with the same name as contructor. So if you want it be a constructor remove 'void'
It will be more flexible if you will use setRadius method like that:
public void setRadius(double r)
{
radius = r;
diameter = 2 * radius;
area = Math.PI * radius * radius;
}
Related
While making a circle class program. I faced an error in my driver section. The error is, Exception in thread "main" java.lang.Error: Unresolved compilation problems:
LastLab cannot be resolved to a type
LastLab cannot be resolved to a type
at Task1.Mainclass.main(Mainclass.java:18)
This is my method and data field code
public class Circle {
private final double PI = 3.14159;
private double radius;
public void Circle(double rad) {
radius = rad;
}
public void setRadius(double rad) {
radius = rad;
}
public double getRadius() {
return radius;
}
public double getArea() {
return PI * radius * radius;
}
public double getDiameter() {
return radius * 2;
}
public double getCircumference() {
return 2 * PI * radius;
}
}
This is my driver code
public class Mainclass {
public static void main(String... args) {
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the radius from user;
System.out.print("Enter the radius of your circle:");
double radius = keyboard.nextDouble();
// Create a circle object.
LastLab circle = new LastLab();
// Get data from circle and display it
System.out.println("The circle's area is: " + circle.getArea());
System.out.println("The circle's diameter is: " + circle.getDiameter());
System.out.println("The circle's circumference is: " + circle.getCircumference());
}
}
Circle circle = new Circle();
You have incorrect declaration: LastLab circle = new LastLab();
And you have incorrectly declared constructor for Circle. Check this out:
public class Circle {
private static final double PI = 3.14159;
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea() {
return PI * radius * radius;
}
public double getDiameter() {
return radius * 2;
}
public double getCircumference() {
return 2 * PI * radius;
}
}
public class Mainclass {
public static void main(String... args) {
Scanner sca = new Scanner(System.in);
System.out.print("Enter the radius of your circle: ");
Circle circle = new Circle(sca.nextDouble());
System.out.format(Locale.ENGLISH, "The circle's area is: %.2f\n", circle.getArea());
System.out.format(Locale.ENGLISH, "The circle's diameter is: %.2f\n", circle.getDiameter());
System.out.format(Locale.ENGLISH, "The circle's circumference is: %.2f\n", circle.getCircumference());
}
}
I am currently working on a project and i keep getting error messages. I am stuck and have contacted many people (including my instructor), and I have now turned to you guys.
Here is my code so far.
public class Circle
private int radius = getRadius();
private double area = getArea();
public Circle(int r)
{
r = radius;
}
public int getRadius()
{
return radius;
}
public double getArea(int r)
{
return area = Math.PI * r * r;
}
}
/
java.util.Scanner;
public class CircleTest
{
public CircleTest()
{
int radius = getRadius();
double area = getArea(r);
}
public static void main (String[] args)
{
Scanner kboard = new Scanner(System.in);
System.out.print("Give the radius of a circle. ");
String area = kboard.nextLine();
System.out.println("The area of the circle is... " +
area);
System.out.println();
kboard.close();
}
}
C:\Users\jthom\My Work\Circle\src\CircleTest.java:18: error: cannot find symbol
double area = getArea(r);
symbol: variable r
location: class CircleTest
.
C:\Users\jthom\My Work\Circle\src\CircleTest.java:33: error: cannot find symbol
System.out.println("The area of the circle is... " + area);
symbol: variable area
location: class CircleTest
2 errors
Let's start with class Circle. This is your original code:
public class Circle
private int radius = getRadius();
private double area = getArea();
public Circle(int r)
{
r = radius;
}
public int getRadius()
{
return radius;
}
public double getArea(int r)
{
return area = Math.PI * r * r;
}
}
For your variables, you don't need "area", as it is calculated. Also, you shouldn't be assigning radius to anything except in the constructor:
private int radius;
//private double area = getArea(); <-- don't need this variable at all
You pass in "r" to the constructor, but then incorrectly try to assign the "radius" value to it. This is backwards; you should be assigning the "r" value to "radius" instead:
public Circle(int r)
{
radius = r;
}
Finally, in getArea(), you don't need the radius passed in, or "area"; just return the calculated value (using the stored value in "radius", not "r"):
public double getArea()
{
return Math.PI * radius * radius;
}
Put all together, your Circle class should look more like:
public class Circle
private int radius;
public Circle(int r)
{
radius = r;
}
public int getRadius()
{
return radius;
}
public double getArea()
{
return Math.PI * radius * radius;
}
}
Over in CircleTest, you should first get the radius from the user, then pass that to the constructor of Circle. Finally, with your instance of Circle, call its getRadius() and getArea() methods.
You have not defined variable r and area ,to help solve this
replace double area = getArea(r); to double area = circle.getArea(radius);
and
System.out.println("The area of the circle is... " + area); with System.out.println("The area of the circle is... " + circle.getArea(Integer.parseInt(area)));
here the circle is an object of Class circle
Circle circle = new Circle(radius) //Note: circle cannot be accessed from main function create another object for class circle again in main
it should help
edit: here is link about the error What does a "Cannot find symbol" compilation error mean?
I am new to java and I have an assignment to write a circle calculator program that computes the area, circumference and diameter of a circle. The program will accept input number (radius) and that number will be used to get the area, circumference and diameter of a circle. This time you are going to use separate methods for each computation. For example, to get the value of area you need to write method area that has radius parameter and returns double value.
I'm not sure if my code is right, I am getting zero values.
import java.util.Scanner;
class Circle {
static Scanner sc = new Scanner(System.in);
private double radius;
public double getRadius() {
System.out.print("Enter the radius: ");
int radius = sc.nextInt();
return radius;
}
public void setRadius(double radius) {
}
public double diameter() {
double diameter = 2 * radius;
return diameter;
}
public double area() {
double area = Math.PI * (radius * radius);
return area;
}
public double circumference() {
double circumference = 2 * Math.PI * radius;
return circumference;
}
}
public class Methods {
public static void main(String[] args) {
Circle circleTest = new Circle();
System.out.printf("Radius: %.1f %n", circleTest.getRadius());
System.out.printf("Diameter: %.1f %n", circleTest.diameter());
System.out.printf("Area: %.1f %n", circleTest.area());
System.out.printf("Circumference: %.1f %n", circleTest.circumference());
}
}
Inside your getRadius method you are setting a local variable to the value entered by the user:
int radius = sc.nextInt();
When you calculate the diameter, area, circumference you're using an instance variable - for example:
double diameter = 2 * radius;
Since you never set the instance variable its value by default is 0.
I am trying to make a program with two methods which calculate and return the surface area and volume of a cylinder.
When i run the program the system outputs 0.00 for both and i'm unsure of what i'm doing wrong.
Here is my code:
public class Circle {
public static int height;
public static int radius;
public static double pi = 3.14;
public Circle(int height, int radius) {
height = 10;
radius = 5;
}
public static double getSurfaceArea() {
int radiusSquared = radius * radius;
double surfaceArea = 2 * pi * radius * height + 2 * pi * radiusSquared;
return surfaceArea;
}
public static double getVolume() {
int radiusSquared = radius * radius;
double volume = pi * radiusSquared * height;
return volume;
}
public static void main(String[] args) {
System.out.println("The volume of the soda can is: " + getVolume() + ".");
System.out.println("The surface area of the soda is: " + getSurfaceArea() + ".");
}
}
Thanks in advance.
You have to add this line of code to your main:
Circle c = new Circle(10,5);
so it would be like so:
public static void main(String[] args) {
Circle c = new Circle(10,5);
System.out.println("The volume of the soda can is: " + c.getVolume() + ".");
System.out.println("The surface area of the soda is: " + c.getSurfaceArea() + ".");
}
and change your circle constructor method to this:
public Circle(int height, int radius) {
this.height = height;
this.radius = radius;
}
I believe this is what you are looking for:
public class Circle {
public static int height;
public static int radius;
public static double pi = 3.14;
public Circle(int height, int radius) {
this.height = height;
this.radius = radius;
}
public static double getSurfaceArea() {
int radiusSquared = radius * radius;
double surfaceArea = 2 * pi * radius * height + 2 * pi * radiusSquared;
return surfaceArea;
}
public static double getVolume() {
int radiusSquared = radius * radius;
double volume = pi * radiusSquared * height;
return volume;
}
public static void main(String[] args) {
Circle circle = new Circle(10,5);
System.out.println("The volume of the soda can is: " + circle.getVolume() + ".");
System.out.println("The surface area of the soda is: " + cirlce.getSurfaceArea() + ".");
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Thank you for assisting me. I have written a Main class that I am able to compile and run. The problem I have is I can run the Test class using the display method.(not pulling correctly to new class) I have to create the Test class with 3 objects one with small value, large value, and retain value. I must use setRadius() method. Any guidance you can provide would be very helpful.
public class Circle
{
public static void main(String[] args)
{
//call constructor and method
Circle myCircle = new Circle();
myCircle.display();
}
//set fields
double radius;
double diameter;
double area;
//create constructor set radius & compute diameter and area.
public Circle()
{
radius= 1;
diameter = 2 * radius;
area = Math.PI * (radius * radius);
}
//set radius, diameter, and area
public void setRadius()
{
radius= 1;
diameter = radius * radius;
area = Math.PI * (radius * radius);
}
//return radius
public double getRadius()
{
return radius;
}
//write display
public void display()
{
System.out.println("The radius is " + radius);
System.out.println("The diameter is " + diameter);
System.out.println("The area is " + area);
}
}
public class TestCircle
{
public static void main(String[] args)
{
Circle circleA = new Circle();
circleA.setRadius();
System.out.println("Circle A: " + circleA.setRadius());
Circle circleB = new Circle();
circleB.setRadius();
System.out.println("Circle B: " + circlB.setRadius());
}
}
Here are some pointer:
Use the constructor to set the initial value of the instance variables when creating the object. Use set radius to change the value after creation, if needed. diameter and area are variables that can be derived from the radius. I would suggest to write methods that returns these.
Also just do what do method name says it does. In setRadius for example just set the radius, nothing else.
public class Circle
{
//set fields
private double radius;
//create constructor set radius & compute diameter and area.
public Circle(double radius)
{
this.radius=radius;
}
//set radius
public void setRadius(double radius)
{
this.radius = radius;
}
public double getDiameter()
{
return 2*radius;
}
//return radius
public double getRadius()
{
return radius;
}
public double getArea()
{
return Math.PI * (radius * radius);
}
//write display
public void display()
{
System.out.println("The radius is " + radius);
System.out.println("The diameter is " + getDiameter());
System.out.println("The area is " + getArea());
}
}
public class TestCircle
{
public static void main(String[] args)
{
Circle circleA = new Circle(1.0);
circleA.display();
Circle circleB = new Circle(2.0);
circleB.display();
}
}
I think you want to call display() on your Circle instances. That would look something like,
public class TestCircle
{
public static void main(String[] args)
{
Circle circleA = new Circle();
circleA.setRadius();
// System.out.println("Circle A: " + circleA.setRadius());
circleA.display();
Circle circleB = new Circle();
circleB.setRadius();
// System.out.println("Circle B: " + circlB.setRadius());
circleB.display();
}
}
Edit
You create a circleA and a circleB but your setRadius method doesn't take a radius argument, also your math appears to be off.
public void setRadius(int radius)
{
this.radius = radius;
diameter = radius + radius; // <-- 2*radius (not radius*radius)
area = Math.PI * (radius * radius);
}
Then you can call setRadius with different radii in your main() like
public static void main(String[] args)
{
Circle circleA = new Circle();
circleA.setRadius(2);
circleA.display();
Circle circleB = new Circle();
circleB.setRadius(4);
circleB.display();
}