Java Program Circumference and Area - java

I got the assignment to write a Java program that gives the circumference and area of a circle of radius 1 through 50. This is what I've got:
public class Circles {
public static void main(String[] args) {
}
{
for (int i = 1; i <=50; i=i+1)
area = PI * (radius * radius);
System.out.println("The area is " + area);
double circumference= PI * 2*radius;
System.out.println( "The circumference "+circumference) ;
}
}
It says that radius, pi, and area cannot be resolved to a variable.

Because you never declared them as variables (nor, in the cases of PI and radius, even give them values). Notice that it did not complain about circumference, which you did declare.

You have to declare your variables - at the moment you're only declaring two of them, namely i and circumference. Also, Java has a PI constant which is found in the Math class, so you'll need Math.PI:
public static void main(String[] args) {
for (int i = 1; i <=50; i=i+1) {
double radius = i;
double area = Math.PI * (radius * radius);
System.out.println("The area is " + area);
double circumference = Math.PI * 2*radius;
System.out.println( "The circumference "+circumference) ;
}
}

public class Circles {
public static void main(String[] args) {
double PI = Math.PI;
for (int radius = 1; radius <=50; i=i+1) {
double area = PI * (radius * radius);
System.out.println("The area is " + area);
double circumference= PI * 2*radius;
System.out.println( "The circumference "+circumference) ;
}
}

Related

Can't display the proper value [closed]

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 5 years ago.
Improve this question
I have these classes:
interface Shape
{
public double getPerimeter();
public double getArea();
public void getDetails();
}
and
class Circle implements Shape
{
private double PI = 3.14;
private int radius;
double perimeter, area;
//constructor
public Circle(int radius)
{
perimeter = 2.0 * (PI * radius);
area = PI * (radius * radius);
}
public int getRadius()
{
return radius;
}
public double getPerimeter()
{
return perimeter;
}
public double getArea()
{
return area;
}
public void getDetails()
{
System.out.println("Shape Type: Circle " + "\n"
+ "Radius: " + getRadius() + "\n"
+ "Perimeter: " + getPerimeter() + "\n"
+ "Area: " + getArea() + "\n" );
}
}
and finally
public class TestShape
{
public static void main(String args[])
{
for(int i = 0; i < args.length; i++)
{
try
{
Integer.parseInt(args[i]);
}
catch(NumberFormatException nfe)
{
System.err.print("wrong");
}
catch(IllegalArgumentException iae)
{
System.err.print("wrong");
}
if(args.length==1)
{
i = Integer.parseInt(args[0]);
Circle r1 = new Circle(i);
r1.getDetails();
}
}
}
}
We were assigned to input 1,2 or 3 numbers on the command line of the terminal and the output would display what kind of shape it is depending on the array size. I managed to get the proper parameter and area when I input a number but the radius keeps displaying 0 instead of the actual number I inputted.
This is what the output looks like
So what do you guys think?
You are not assigning the radius in the constructor of Circle...
public Circle(int radius)
{
perimeter = 2.0 * (PI * radius);
area = PI * (radius * radius);
this.radius = radius // this statement is missing
}
Therefore, circle.getRadius() always returns the uninitialized default int value 0.
In your constructor:
public Circle(int radius)
{
perimeter = 2.0 * (PI * radius);
area = PI * (radius * radius);
}
radius refers to the parameter radius, not the radius field declared in Circle. You need to make them the same by assigning the radius parameter to the radius field. To refer to the radius field, use this.radius.
this.radius = radius;
Because you did not do this in your code, the radius returned by getRadius is 0, which is the default value of an unassigned int.
Instance variable are assigned values by default if not initialized. In case of int it's 0 by default. So to assign the value to your instance variable radius you need to add this line in your constructor.
this.radius = radius
We are using this because your instance variable and local variable are having the same name.

Program print keeps print 0.0 for Cylinder area, volume, range + inheritance java

I have tried this a couple of different ways. It is required I use the inheritance to extend these classes. Every time I run the program it just out puts 0.0 for volume and area. Radius displays correctly. Output at the bottom.
public class Base_HW04Q1
{
public double pi = 3.14, l, radius, height, area, volume;
public static class RoundShape extends Base_HW04Q1 {
public RoundShape(double radius) {
this.radius = radius;
}
public double calcArea () {
area = (radius * radius) * pi;
return area;
}
public String toString() {
return "A Round Shape of radius: " + radius + ", area " + area + ".";
}
}
public static class Cylinder extends Base_HW04Q1
{
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
public double calcArea() {
l = Math.sqrt((radius * radius) + (height * height));
area = 2 * pi * radius * height + 2 * pi * l;
return area;
}
public double calcVolume() {
volume = pi * (radius * radius) * height;
return volume;
}
public String toString() {
return "A Cylinder of radius: " + radius + ", area " + area + " and a volume of " + volume;
}
}
public static class Cone extends Base_HW04Q1 //TODO: This line is almost, but not quite, complete.
{
public Cone(double radius, double height) {
this.radius = radius;
this.height = height;
}
public double calcArea() {
l = Math.sqrt((radius * radius) + (height * height));
area = (pi * radius * l) + (pi * radius * radius);
return area;
}
public double calcVolume() {
volume = 0.333 * pi * radius * radius * height;
return volume;
}
public String toString() {
return "A Cone of radius: " + radius + ", area " + area + " and a volume of " + volume;
}
}
public static void main(String[] args)
{
//object creation
Cylinder Cylinder1 = new Cylinder(30, 10);
Cone Cone1 = new Cone(10, 20);
RoundShape RoundShape1 = new RoundShape(50);
//print for objects
System.out.println(Cylinder1);
System.out.println(RoundShape1);
System.out.println(Cone1);
}
}
Output:
A Cylinder of radius: 30.0, area 0.0 and a volume of 0.0 A Round Shape
of radius: 50.0, area 0.0. A Cone of radius: 10.0, area 0.0 and a
volume of 0.0
Your toString() never calls the methods that do the calculations and instead prints the default 0.0 field values. You will run this risk if toString() is ever called before the calcXxxx() methods are called, i.e., before the calculated fields have been given a decent value. The best solution is to prevent this problem from happening in the first place by entirely getting rid of fields for calculated values, for instance area and volume. Instead within toString(), call the methods to get these values.
e.g.,
public double pi = 3.14, l, radius, height; // , area, volume;
public static class RoundShape extends Base_HW04Q1 {
public RoundShape(double radius) {
this.radius = radius;
}
public double calcArea () {
return (radius * radius) * pi;
// return area;
}
public String toString() {
return "A Round Shape of radius: " + radius + ", area " + calcArea() + ".";
}
}
It's because you only instantiate the object and entered to constructor but not in the other method.
Cylinder Cylinder1 = new Cylinder(30, 10);
Cone Cone1 = new Cone(10, 20);
RoundShape RoundShape1 = new RoundShape(50);
no one called in these methods
public double calcArea() {
l = Math.sqrt((radius * radius) + (height * height));
area = (pi * radius * l) + (pi * radius * radius);
return area;
}
public double calcVolume() {
volume = 0.333 * pi * radius * radius * height;
return volume;
}
public String toString() {
return "A Cone of radius: " + radius + ", area " + area + " and a volume of " + volume;
}
and the rest methods. Call them from constructor or from main if you want to calculate through method like:
public static void main(String[] args)
{
//object creation
Cylinder Cylinder1 = new Cylinder(30, 10);
Cone Cone1 = new Cone(10, 20);
RoundShape RoundShape1 = new RoundShape(50);
double roundArea = RoundShape1.calcArea();//then use this
string roundMessage = RoundShape1.toString();//and this whatever you want.
//do it in others too
//print for objects
System.out.println(Cylinder1);
System.out.println(RoundShape1);
System.out.println(Cone1);
}

why i didnot get output for below code need explanation?

public class Triangle
{
float length; //declared length as float
float breadth; //declared breadth as float
public static void main(String[] args)
{
Triangle Triangle1 = new Triangle();
Triangle Triangle2 = new Triangle();
float area; //declared area as float
Triangle1.length = 11;
Triangle1.breadth = 22;
Triangle2.breadth = 15;
Triangle2.length = 20;
area = 0.5 * Triangle1.length * Triangle1.breadth;
System.out.println("The area of Triangle is" + " " + area);
area = 0.5 * Triangle2.length * Triangle2.breadth;
System.out.println("The area of Triangle is" + " " + area);
}
}
i have declared the area as float earlier and i got complie time error for that, when i changed it into double, i can able to get the below output.
need to know why...??
float area;
Error:(24, 49) java: incompatible types: possible lossy conversion from double to float
Error:(27, 49) java: incompatible types: possible lossy conversion from double to float
Your code has several mistakes.
Please keep the naming conventions. Class starts with the capitalized letter, variable don't. So.. Triangle triangle1 = new Triangle().
In order to keep all the data in float, you need to multiply all the numbers in float as well. 0,5 is double as default, having the bigger decimal precision than float. There are two ways to fix that:
Use F or f indicator (I recommend the capitalized one) to say that you work with float. Otherwise Java unserstands it as double:
area = 0.5F * triangle1.length * triangle1.breadth;
Or cast all the resutl to float:
area = (float) 0.5 * triangle1.length * triangle1.breadth;
In Java by default number like 2.553 are double so when you do
area = 0.5 * Triangle1.length * Triangle1.breadth;
you are doing double * float * float. If you want to use 0.5 as float you need to add letter f after number so your code will looks like:
area = 0.5f * Triangle1.length * Triangle1.breadth;
You have two options:
Add f after every initialisation of float variables, like Triangle1.length = 11f;
Thus the code becomes:
public class Triangle {
float length; // declared length as float
float breadth; // declared breadth as float
public static void main(String[] args) {
Triangle Triangle1 = new Triangle();
Triangle Triangle2 = new Triangle();
float area; // declared area as float
Triangle1.length = 11f;
Triangle1.breadth = 22f;
Triangle2.breadth = 15f;
Triangle2.length = 20f;
area = 0.5f * Triangle1.length * Triangle1.breadth;
System.out.println("The area of Triangle is" + " " + area);
area = 0.5f * Triangle2.length * Triangle2.breadth;
System.out.println("The area of Triangle is" + " " + area);
}
}
Make the area variable double type:
Your code thus becomes:
public class Triangle {
float length; // declared length as float
float breadth; // declared breadth as float
public static void main(String[] args) {
Triangle Triangle1 = new Triangle();
Triangle Triangle2 = new Triangle();
double area; // declared area as float
Triangle1.length = 11;
Triangle1.breadth = 22;
Triangle2.breadth = 15;
Triangle2.length = 20;
area = 0.5 * Triangle1.length * Triangle1.breadth;
System.out.println("The area of Triangle is" + " " + area);
area = 0.5 * Triangle2.length * Triangle2.breadth;
System.out.println("The area of Triangle is" + " " + area);
}
}
Hope it helps!
Do this.
public class Triangle
{
float length; //declared length as float
float breadth; //declared breadth as float
public static void main(String[] args)
{
Triangle Triangle1 = new Triangle();
Triangle Triangle2 = new Triangle();
float area; //declared area as float
Triangle1.length = 11;
Triangle1.breadth = 22;
Triangle2.breadth = 15;
Triangle2.length = 20;
area = (float)0.5 * Triangle1.length * Triangle1.breadth;
System.out.println("The area of Triangle is" + " " + area);
area = (float)0.5 * Triangle2.length * Triangle2.breadth;
System.out.println("The area of Triangle is" + " " + area);
}
}

Math from class isn't carrying in to object

I have to make a program that has the Circle class which calculates the area and circumference of a circle by accepting a parameter for r from a new circle object in main. I fed it the radius value of 14. This is what it should output:
***** Circle *****
radius 14.0
area 615.75164
circumference 87.96452
But instead I'm getting this
***** Circle *****
radius 14.0
area 0.0
circumference 0.0
Why is this happening and how can I fix it? I was given the overall shell of the code to make work in java and I have to keep it for the most part how it was presented to me, so while I appreciate the intention of giving recommendations on better ways to structure things and make this program in general, the teacher wants me to do it this way. The only thing I'm not sure of is what to do with setRadius(), and whether or not I should have declared r at the top of the class.
package shapedemo;
class Circle {
private double radius;
private double circumference;
private double area;
private double r;
// constructors
public Circle(){
radius = 1;
}
public Circle(double r) {
radius = r;
}
// setters and getters
public void setRadius(double radius){
this.radius = radius;
}
public double getRadius(){
return radius;
}
// other methods
public double calcArea() {
area = Math.PI * r * r;
return area;
}
public double calcCircumference(){
circumference = Math.PI * 2 * r;
return circumference;
}
// display method
public void display() {
/*blank line
***** Circle *****
radius nnnn.nn
area nnnn.nn
circumference nnnn.nn
blank line
*/
System.out.println("\n***** Circle *****\nradius " + radius + "\narea " + area + "\ncircumference " + circumference + "\n");
}
}
public class ShapeDemo {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Circle circle1 = new Circle(14);
circle1.display();
}
}
Your area and circumference variables are only being set if you call the appropriate calculation methods (calcArea and calcCircumference). You're not calling them, so they have the default values of 0.
You could fix this in your main method:
public static void main(String[] args) {
Circle circle = new Circle(14);
circle.calcArea();
circle.calcCircumference();
circle.display();
}
... although you'd also have to fix your methods to use radius instead of r, as you're never setting r.
Personally I think it would be better to either move the logic into your constructor or calculate it on demand with methods rather than having fields at all for them.
First approach, in the constructor:
final class Circle {
private final double radius;
private final double circumference;
private final double area;
public Circle() {
this(1); // Delegate to other constructor
}
public Circle(double r) {
radius = r;
area = Math.PI * radius * radius;
circumference = Math.PI * 2 * radius;
}
public void display() {
System.out.println(
"\n***** Circle *****" +
"\nradius " + radius +
"\narea " + area +
"\ncircumference " + circumference + "\n");
}
}
Second approach, making methods compute the values:
final class Circle {
private final double radius;
public Circle() {
this(1); // Delegate to other constructor
}
public Circle(double r) {
radius = r;
}
public double getRadius() {
return radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getCircumference() {
return Math.PI * 2 * radius;
}
public void display() {
System.out.println(
"\n***** Circle *****" +
"\nradius " + radius +
"\narea " + getArea() +
"\ncircumference " + getCircumference() + "\n");
}
}
In both cases, I've made this an immutable type - while you can keep it mutable, in the first case that would mean recomputing the area and circumference. In that case you'd probably want your constructor to just call setRadius() and make that method do all the computations.
For example:
final class Circle {
private double radius;
private double circumference;
private double area;
public Circle() {
this(1); // Delegate to other constructor
}
public Circle(double r) {
setRadius(r);
}
public void setRadius(double r) {
radius = r;
area = Math.PI * radius * radius;
circumference = Math.PI * 2 * radius;
}
public double getRadius() {
return radius;
}
public double getArea() {
return area;
}
public double getCircumference() {
return circumference;
}
public void display() {
System.out.println(
"\n***** Circle *****" +
"\nradius " + radius +
"\narea " + area +
"\ncircumference " + circumference + "\n");
}
}
You never call to calcArea() and calcCircumference(), so circumference and area have the intial 0 value.
Circle circle1 = new Circle(14);
circle1.calcArea();
circle1.calcCircumference();
circle1.display();
Or change your constructor to
public Circle(double r) {
radius = r;
calcArea();
alcCircumference();
}
You're not calling the calcArea and calcCircumference so they retain the default value 0.0d
this should be your public static void main()
public static void main(String[] args) {
Circle circle1 = new Circle(14);
circle1.calcArea();
circle1.calcCircumference();
circle1.display();
}
Also, change your return type of calcArea() and calcCircumference() to void

coneVolume Method returning zero

I can't seem to figure out why my coneVolume method is returning zero when all of my other methods are working properly.
import java.util.Scanner;
public class P56old{
public static double sphereVolume(double r){
double sphereVolume = (4/3)*(Math.PI)*(Math.pow(r, 3));
return sphereVolume;
}
public static double sphereSurface(double r){
double sphereSurface = 4 * (Math.PI) * Math.pow(r, 2);
return sphereSurface;
}
public static double cylinderVolume(double r, double h){
double cylinderVolume = (Math.PI) * (Math.pow(r, 2)) * h;
return cylinderVolume;
}
public static double cylinderSurface(double r, double h){
double cylinderSurface = 2 * (Math.PI) * (Math.pow(r, 2)) + 2 * Math.PI * r * h;
return cylinderSurface;
}
public static double coneVolume(double r, double h){
double coneVolume = (1/3) * Math.PI * (Math.pow(r,2)) * h;
return coneVolume;
}
public static double coneSurface(double r, double h){
double s = Math.sqrt(Math.pow(r,2) + Math.pow(h, 2));
double coneSurface = Math.PI * Math.pow(r,2) + Math.PI * r * s;
return coneSurface;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Please give the radius: ");
double r = in.nextDouble();
System.out.print("Please give the height: ");
double h = in.nextDouble();
double coneVolume = coneVolume(r,h);
double sphereVolume = sphereVolume(r);
double sphereSurface = sphereSurface(r);
double cylinderVolume = cylinderVolume(r,h);
double cylinderSurface = cylinderSurface(r,h);
double coneSurface = coneSurface(r,h);
System.out.println("The Sphere Volume is " + sphereVolume);
System.out.println("The Sphere Surface is " + sphereSurface);
System.out.println("The Cylinder volume is " + cylinderVolume);
System.out.println("The Cylinder Surface is " + cylinderSurface);
System.out.println("The Cone Volume is " + coneVolume);
System.out.println("The Cone Surface is " + coneSurface);
}
}
I'd appreciate any insight on the matter, and any critique is appreciated. I think it may have to do with all the public classes and maybe another method is affecting the coneVolume method but I just don't know enough about methods at the moment to fix the issue at hand.
When you do 1/3, it does integer division, resulting in 0 (the remainder is 1). Multiplying by 0 gives 0. Do 1.0/3.0 instead, and it will correctly compute an approximation to one third.

Categories

Resources