I'm trying to output some data from within an object to the console. Seems to be working, but below the data I'm getting a strange message.
Below is the code:
import java.text.DecimalFormat;
/**
* The Circle class calculates area, diameter, and circumference
*/
public class Circle {
DecimalFormat fmt = new DecimalFormat("#0.00");
// Fields
private double radius; // Holds the circle radius
private final double PI = 3.14159; // The formula for PI
/**
* Start Constructors
*/
public Circle(double rad) {
radius = rad;
System.out.println("The circle's radius is " + (fmt.format(radius)));
System.out.println("The circle's area is " + (fmt.format(PI * radius * radius)));
System.out.println("The circle's diameter is " + (fmt.format(radius * 2)));
System.out.println("The circle's circumference is " + (fmt.format(2 * PI * radius)));
}
}
The console output is:
The circle's radius is 2.00
The circle's area is 12.57
The circle's diameter is 4.00
The circle's circumference is 12.57
Circle#5115a298
What is the circle message?
Give your Circle class a decent toString() method override. You're seeing the return from Object's default version of this method.
#Override
public String toString() {
/* return a String that shows the "state" of this object */
}
You must have a System.out somewhere else that only has a Circle object as output. Then Hovercraft Full Of Eels' answer applies.
Btw: Math.PI already gives you Pi!
Related
When I run my java code on one computer it works fine but on the other nothing displays.
When I type java CircleComputation it just returns to command line with nothing displaying but when I copy the file to another computer it works. Both have java installed and both have Path set. I can't find any difference in the computers.
Here is the code (not that it matters as I tried multiple programs):
/*
* Print the area and circumferencee of a circle, given its radius double.
*/
public class CircleComputation { // Saved as "CircleComputation.java"
public static void main(String[] args) {
// Declare variables
double radius, area, circumference;
final double PI = 3.14159265;
// Assign a value to radius and height
radius = 1.2;
// Compute area and circumference
area = radius * radius * PI;
circumference = 2.0 * radius * PI;
// Print results
System.out.print("The radius is "); // Print description
System.out.println(radius); // Print the value stored in the variable
System.out.print("The area is ");
System.out.println(area);
System.out.print("The circumference is ");
System.out.println(circumference);
}
}
In the following program I was jus t trying to create a simple class for as Circle circ and then tried to inherit a Cylinder from it cylind. I defined several methods to find the perimeter, area and volume of these geometrical objects but I am constantly getting wrong answer. I had spent several painstaking our hour to find the error but I am unable to find it. I am a beginner so I think there might be something I may have missed. Please help.
package com.company;
class circ {
int radius;
public circ(int radius) {
this.radius = radius;
}
}
class cylind extends circ{
int height;
double area = (2*Math.PI * radius * ( radius + height ));
double volume = (Math.PI * radius *radius * height );
public cylind(int radius, int height) {
super(radius);
this.height = height;
}
public void area() {
System.out.println("Total Surface Area = " + this.area);
}
public void volume(){
System.out.println("Volume = " + this.volume);
}
}
class Trial_and_error_2 {
public static void main(String[] args) {
cylind b =new cylind(10,45);
System.out.println(b.radius);
System.out.println(b.height);
System.out.println(2*Math.PI * b.radius * ( b.radius + b.height ));
System.out.println(Math.PI * b.radius *b.radius * b.height );
b.area();
b.volume();
}
}
Output: I have used ----> to explain the output and the problem.
10 ---> Radius
45 ---> height
3455.7519189487725 ---> Surface area calculated in the main block
14137.16694115407 ---> Volume calculated in the main block
Total Surface Area = 628.3185307179587 ---> Wrong values of Surface area printed by the method of class THIS IS THE PROBLEM.
Volume = 0.0 ---> Wrong values of volume printed by the method of class THIS IS THE PROBLEM.
Instance variables "area" and "volume" are initialized BEFORE the constructor is called. That's why you get volume = 0, because height is 0 when that happens.
Your field calculations will be executed before the constructor.
Solution:
Just move calculations to constructor:
public Cylinder(int radius, int height) {
super(radius);
this.height = height;
area = 2*Math.PI * radius * (radius + height);
volume = Math.PI * radius *radius * height;
}
Also, I will recommend using full class names with a first capital.
this is a Class about calculating diameter,circumference and area of circle that user enter radius value and it gives him diameter,cirucumf... ,
this is the class code:
package circle;
import java.util.Scanner;
public class Circle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int radius=0;
int diameter;
int circumference ;
int area;
int Pi;
Pi=(int) 3.14;
area = (int) (radius*radius*Pi);
circumference =(int)(radius*2*Pi);
diameter = (int)(radius*2);
System.out.print("Enter radius value:");
radius=input.nextInt();
System.out.printf("area is %d%n" , area);
System.out.printf("diameter is %d%n", diameter);
System.out.printf("circumference is %d%n", environment);
}
}
this is what output gives me :
Enter radius value: (for exmaple) 4
area is 0 // (real value is 50.24)
diameter is 0 // (8)
circumference is 0 //(25.12)
what is the code problem?
or how can i fix it?
You read the radius AFTER computing area/environment(?)/diameter. Furthermore, your values are int variables, which also means that your value for pi is just 3. I suggest you correct the order of the statements, and start using double instead of int.
"environment" will be replaced by "circumference". As your excepted output is decimal value. So use float/double instead of int. In your program you are calculating diameter,circumference and area after initialising the value of radius(radius=0) but before getting the value of radius(radius=4). I have modified your code. It seem help you.
package circle;
import java.util.Scanner;
public class Circle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int radius=0;
float diameter;
double circumference ;
double area;
double Pi;
Pi= 3.14;
System.out.print("Enter radius value:");
radius=input.nextInt();
area = (radius*radius*Pi);
circumference =(radius*2*Pi);
diameter = (radius*2);
System.out.printf("area is " + area);
System.out.printf("\ndiameter is "+ diameter);
System.out.printf("\ncircumference is "+ circumference);
}
}
you are calculating the values area/environment(?)/diameter before getting and initializing the radius input.And at that time the default value for radius is set which is 0. Hence it is giving the results of all the parameters as 0.So you will have to re order your code as below:
System.out.print("Enter radius value:");
radius=input.nextInt();
area = (int) (radius*radius*Pi);
environment=(int)(radius*2*Pi);
diameter = (int)(radius*2);
System.out.printf("area is %d%n" , area);
System.out.printf("diameter is %d%n", diameter);
System.out.printf("environment is %d%n", environment);
I was trying to write a code to take the radius of a pentagon and turn it into a side, then output the area. Somewhere along the way my use of the math class is incorrect. (New to this site but tried to format everything so you could understand my program)
Class: CS 1301/16
// Term: Fall
// Name: Gabriel Tomasetto
// Intsructor: Ms. Tulin
// Assignment 1
import java.util.*;
public class Pentagon {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double radius, side, area;
System.out.println("Please enter the length of the Pentagon from the center to the vertex: ");
radius = scan.nextDouble();
side = (2 * radius) * (Math.sin(Math.PI/5)); //I'm assuming I'm not correctly using the Math class to represent the equation? Thoughts.
area = (5 * Math.pow(radius, 2)) / (4 * Math.tan(Math.PI /5));
System.out.println("The area of the Pentagon is:\t" + area);
}
}
Shouldn't it be side = (2 * radius) * (Math.sin(Math.PI/2/5));?
What's the error you're getting? Just the wrong answer?
Just a simple tpyo I think, try this.
area = (5 * Math.pow(side, 2)) / (4 * Math.tan(Math.PI /5));
...assuming this is the right formula using side length and not radius
http://www.mathopenref.com/polygonregulararea.html
I'm trying to print out the radius, circumference and area of a circle, and it is not working. I'm a complete beginner at java and coding so thank you for the help.
public class program54c {
public static void main (String args[]) {
double pi = 3.14159;
double radius = 15.337;
double circumference = (2*pi*radius);
double area = (pi*(radius*2));
System.out.println("The radius of the circle" = radius);
System.out.println("The circumference of the circle" = circumference);
System.out.println("The area of the circle" = area);
}
}
Your code seems correct except for one thing (assuming you know your mathematical calculations).
System.out.println("The radius of the circle" = radius);
You are not using the correct string concatenation for your output.
Replace = with + and try. For example,
System.out.println("The radius of the circle " + radius);
In your print statements, you need to use the + operator instead of = to append the variables value to your string. For example
"The area of the circle " = area
should be
"The area of the circle " + area
this will concatenate (append) the value of area to the string "The area of the circle "
Full example below:
public class program54c
{
public static void main (String args[])
{
double pi = 3.14159;
double radius = 15.337;
double circumference = (2*pi*radius);
double area = (pi*(radius*2));
System.out.println("The radius of the circle " + radius);
System.out.println("The circumference of the circle + circumference);
System.out.println("The area of the circle " + area);
}
}
I spot two errors in your code:
The string concatenation, as precised in the others answers, must be done with + and not =.
radius * 2 is not radiusĀ² ! You should use Math.pow(radius, 2) or radius*radius.
On a side note, you can use Math.PI instead of your own pi.