My code compiles, but the output won't show. I think my methods are all correct, but I have no idea why my output will not display. (NB: beginner in Java)
import java.util.Scanner;
import java.lang.Math;
public class Lab8 {
public static void main(String[] args) {
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius = in.nextDouble();
System.out.print("Enter height: ");
height = in.nextDouble();
}
public static double Calculations(double radius, double height) {
double surfaceArea = (2 * Math.PI * radius * radius) + (2 * Math.PI * radius * height);
return surfaceArea;
}
public static double calculations(double radius, double height) {
double volume = Math.PI * radius * radius * height;
return volume;
}
public static void output(double surfaceArea, double volume) {
System.out.println("Surface Area of Cylinder: " + surfaceArea);
System.out.println("Voulme of Cylinder: " + volume);
}
}
You need to call your method inside main to print the value.
System.out.println("surfacearea " + Calculations(radius,height));
System.out.println("volume " +calculations(radius,height));
System.out.println("output " + output(radius,height));
Fixed Code:
import java.util.Scanner;
import java.lang.Math;
public class Lab8
{
public static void main(String[] args)
{
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius= in.nextDouble();
System.out.print("Enter height: ");
height= in.nextDouble();
System.out.println("surfacearea " + Calculations(radius,height));
System.out.println("volume " +calculations(radius,height));
System.out.println("output " + output(radius,height));
}
public static double Calculations(double radius, double height)
{
double surfaceArea= (2 * Math.PI * radius * radius) + (2 * Math.PI *radius* height);
return surfaceArea;
}
public static double calculations(double radius,double height)
{
double volume= Math.PI * radius * radius * height;
return volume;
}
public static void output(double surfaceArea, double volume)
{
System.out.println("Surface Area of Cylinder: " + surfaceArea);
System.out.println("Voulme of Cylinder: " + volume);
}
}
Try calling one of your Calculations method in your main method, post taking the input such as:
System.out.println(Lab8.Calculations(radius,height));
In main you miss the calculate and output method call
public static void main(String[] args)
{
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius= in.nextDouble();
System.out.print("Enter height: ");
height= in.nextDouble();
// call this
System.out.println("area " + output(Calculations(radius,height)));
}
You don't actually call your methods.
In main, after you get the radius and height, call Calculations and calculations respectively, with the appropriate arguments.
Here's an example:
surfaceArea = Calculations(radius, height);
volume = calculations(radius, height);
You didn't call any of your helper functions within main(...). Try adding a call to output or calculations within your main function.
In particular, something like
output(Calculations(radius, height), calculations(radius, height));
will probably suffice.
As you learn Java, it may be helpful to begin applying descriptive names to your functions, such as CylinderSurfaceArea to help keep confusion to a minimum both for you and for anyone who might read code you have written.
Related
I am having a problem I have not been able to solve searching the internet, basically I need the user to input first the diameter of a sphere then get back the radius and from there use the formula (4 over 3 multiplied by pi (3.14) multiplied by radius (calculated from diameter - user inputted) to the power of 3.
and the other one is very similar..... 4 multiplies by pi (3.14) multiplied by radius to the power of 2.
Now the thing is every time I try to compile I get errors that the method is lossy double not int or the symbol is not recognized.
guys any help would be helpful since I cant find a solution for this.
the code is below :
import static java.lang.Math.sqrt;
import static java.lang.Math.abs;
import java.lang.*;
import java.util.Scanner;
import java.util.*;
public class RadiusConverter {
public class Pi{
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int FirstStep, Diameter, Radius, SecondStep, ThirdStep;
System.out.println ("Please enter the diameter of the Sphere: ");
int diameter =scan.nextInt();
System.out.println ("Your Shpere has a radius of: " + FirstStep);
int radius = scan.nextInt();
System.out.println ("The Volume of your Sphere is: " + SecondStep);
System.out.println("The Surface Area of your Sphere is: " + ThirdStep);
FirstStep = Diameter / 2;
SecondStep = 4 / 3 * 3.14 * (int) FirstStep * 3;
ThirdStep = 4 * 3.14 * (int) Radius * 2;
Diameter = diameter;
Radius = radius;
//------------------------------------------------------------------------
//
//
//-------------------------------------------------------------------------
scan.close();
}
}
This is the older source code I was using … which also did not work.
import static java.lang.Math.sqrt;
import static java.lang.Math.abs;
import java.lang.*;
import java.util.Scanner;
import java.util.*;
public class RadiusConverter
{
public class Pi{
}
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
double FirstStep, Diameter, Radius, SecondStep, ThirdStep;
System.out.println ("Please enter the diameter of the Sphere: ");
double diameter =scan.nextDouble();
System.out.println ("Your Shpere has a radius of: " + FirstStep);
double radius =scan.nextDouble();
System.out.println ("The Volume of your Sphere is: " + SecondStep);
System.out.println("The Surface Area of your Sphere is: " + ThirdStep);
FirstStep = diameter / 2;
SecondStep = 4 / 3 * Math.PI* Math.pow (FirstStep, 3);
ThirdStep = 4 * Math.PI * radius * Math.pow (2);
//------------------------------------------------------------------------
//
//
//-------------------------------------------------------------------------
scan.close();
}
}
try out this one
import java.util.Scanner;
public class RadiusConverter {
public class Pi {
}
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
int firstStep = 0, diameter = 0, radius = 0, secondStep = 0, thirdStep = 0;
System.out.print("Please enter the diameter of the Sphere: ");
diameter = scan.nextInt();
System.out.print("Please enter the radius of the Sphere: ");
radius = scan.nextInt();
firstStep = diameter / 2;
secondStep = (int) (4 / 3 * 3.14 * firstStep * 3);
thirdStep = (int) (4 * 3.14 * radius * 2);
System.out.println("Your Shpere has a radius of: " + firstStep);
System.out.println("The Volume of your Sphere is: " + secondStep);
System.out.println("The Surface Area of your Sphere is: " + thirdStep);
}
}
}
Output
Please enter the diameter of the Sphere: 250
Please enter the radius of the Sphere: 25
Your Shpere has a radius of: 125
The Volume of your Sphere is: 1177
The Surface Area of your Sphere is: 628
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() + ".");
}
}
The first bit of code I am having problems with is DemoSquare - it will crash when I run it. The error is:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: variable area
location: class Square
at Square.computeSurfaceArea(DemoSquare.java:57)
at DemoSquare.main(DemoSquare.java:23)
Java Result: 1
My code is -
// package demosquare;
import java.util.*;
public class DemoSquare {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the height of the square/rectangle");
double sqHeight = input.nextDouble();
System.out.println("Please enter the width of the square/rectangle");
double sqWidth = input.nextDouble();
System.out.println("Please enter the height of the Cube/Rectangular Prism");
double cuHeight = input.nextDouble();
System.out.println("Please enter the width of the Cube/Rectangular Prism");
double cuWidth = input.nextDouble();
System.out.println("Please enter the depth of the Cube/Rectangular Prism");
double cuDepth = input.nextDouble();
// Create a square and print out the information about it
Square square = new Square(sqHeight, sqWidth);
System.out.println("A Square with dimensions " + square.getHeight()
+ " by " + square.getWidth() + " has a surface area of "
+ square.computeSurfaceArea());
// Create a cube and print out the information about it.
Cube cube = new Cube(cuHeight, cuWidth, cuDepth);
System.out.println("A Cube with dimensions " + cube.getHeight()
+ " by " + cube.getWidth() + " by " + cube.getDepth() + " has a surface area of "
+ cube.computeSurfaceArea());
} // end main method
} //end class DemoSquare
class Square {
// enter the code to create the square class here
double sqHeight = 0;
double sqWidth = 0;
public Square(double height, double width) {
sqHeight = height;
sqWidth = width;
}
public double getWidth() {
return sqWidth;
}
public double getHeight() {
return sqHeight;
}
public double computeSurfaceArea() {
double surfaceArea = sqHeight * sqWidth;
surfaceArea = (getHeight() * getWidth());
return area;
}
}
class Cube extends Square {
double sqDepth = 0.00;
// enter the cube class code here
public Cube(double height, double width, double depth) {
super(height, width);
sqDepth = depth;
}
#Override
public double getWidth() {
return sqWidth;
}
#Override
public double getHeight() {
return sqHeight;
}
public double getDepth() {
return sqDepth;
}
#Override
public double computeSurfaceArea() {
//Surface Area = 2hw + 2wd + 2dh
double tsa = (2 * sqHeight * sqWidth) + (2 * sqWidth * sqDepth) + (2 * sqDepth * sqHeight);
return tsa;
}
}
What am I doing wrong in this code?
You can tell by the stacktrace where the error lies. Let's review it.
The reason why the program didn't work:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
This means your code is currently broken and will not execute by any means until at least compiles.
Uncompilable source code - cannot find symbol
symbol: variable area
This means there's an unknown variable with name area that's been used in your code but is never declared.
Then it comes the location of the error:
location: class Square
at Square.computeSurfaceArea(DemoSquare.java:57)
Now we have where to look. Let's go to the Square class, inside the method computeSurfaceArea, more specifically, at line 57 in the file DemoSquare.java.
//54. public double computeSurfaceArea() {
//55. double surfaceArea = sqHeight * sqWidth;
//56. surfaceArea = (getHeight() * getWidth());
//57. return area;
//58. }
Now we found the culprit: return area;. Note that area here is an undeclared variable. As noted in other answers, you probably meant to use surfaceArea instead.
//54. public double computeSurfaceArea() {
//55. double surfaceArea = sqHeight * sqWidth;
//56. surfaceArea = (getHeight() * getWidth());
//57. return surfaceArea;
//58. }
Do this change and the code will be fixed until now.
Do the same process to fix other problems in your code.
Do a similar process when you encounter other exceptions at runtime like NullPointerException.
Here's what you're doing wrong:
public double computeSurfaceArea() {
double surfaceArea = sqHeight * sqWidth;
surfaceArea = (getHeight() * getWidth());
return area; // no such variable - it's called surfaceArea...
}
The variable is called surface area, not area.
I.e., you should be doing:
public double computeSurfaceArea() {
double surfaceArea = sqHeight * sqWidth;
surfaceArea = (getHeight() * getWidth());
return surfaceArea;
}
double surfaceArea = sqHeight * sqWidth;
surfaceArea = (getHeight() * getWidth());
return area;
I think you meant
return surfaceArea ;
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I was given an assignment to perform method overloading in inheritence in java by designing a program that calculates the perimeter of different shapes, i designed code as shown below but when i try to compile, there are errors.
import java.io.*;
import java.util.*;
public class Perimeter {
public double getperimeter(int constant,double pi,double radius){
return(constant*pi*radius);
}
public double getperimeter(int sconstant,double length){
return(sconstant*length);
}
public double getperimeter(int rconstant,double rlength,double widith){
return(rconstant*(rlength+widith));
}
public double getperimeter(double base,double height,double hypotenuse){
return(base+height+hypotenuse);
}
public void main (String args []){
final double pi=22/7;
final int constant=2;
double raduius;
final int sconstant=4;
double length;
final int rconstant=2;
double rlength;
double widith;
double base;
double height;
double hypotenuse;
Perimeter g= new Perimeter();
Scanner s=new scanner(System.in);
System.out.println("Enter The Radius");
g.radius=s.nextDouble();
System.out.println("Enter The Square Length");
g.lenght=s.nextInt();
System.out.println("Enter The Rectangle Lenght");
g.rlength=s.nextInt();
System.out.println("Enter The Rectangle widith");
g.widith=s.nextInt();
System.out.println("Enter The Triangle Base");
g.base=s.nextInt();
System.out.println("Enter The Triangle height");
g.height=s.nextInt();
System.out.println("Enter The Triangle hypotenuse");
g.hypotenuse=s.nextInt();
System.out.println("Perimeter = " + g.getperimeter(constant,pi,radius));
System.out.println("Perimeter = " + g.getperimeter(sconstant,length));
System.out.println("Perimeter = " + g.getperimeter(rconstant,rlength,widith));
System.out.println("Perimeter = " + g.getperimeter(base,height,hypotenuse));
This is a "classic" overriding problem that academics love. (Others involve animals or vehicles.)
Start with a Shape interface:
public interface Shape {
double getPerimeter();
}
Then have subclasses implement it, each in their own way:
public class Rectangle implements Shape {
private double height;
private double width;
public Rectangle(double w, double h) {
this.width = w;
this.height = h;
}
public double getPerimeter() { return 2.0*(this.width + this.height); }
}
public class Circle implements Shape {
private double radius;
public Circle(double r) {
this.radius = r;
}
public double getPerimeter() { return 2.0*Math.PI*this.radius; }
}
You'll be able to do things like this:
List<Shape> shapes = new ArrayList<Shape>();
shapes.add(new Rectangle(1.0, 2.0));
shapes.add(new Circle(10.0));
for (Shape shape : shapes) {
System.out.println(shape.getPerimeter()); // every Shape will do it their own way
}
Add a new Shape implementation and your original code still just works. It's the essence of polymorphism and dynamic binding.
You are defining two methods with same signature within a single class. This is an error. Your methods :
double getperimeter(int constant,double pi,double radius);
double getperimeter(int rconstant,double rlength,double widith);
Also your main method must be declared as static
Hi you should have a the class Perimeter in a different file.
I see missing brackets closing the class Perimeter.
Also like Xavier mentioned you have two methods with the same signature.
You are also reading values from Scanner into Perimeter instance variables and then passing the uninitialized values to the Perimeter methods.
So i would separate the Perimeter class and in the main read the values to local variables and pass them to Perimeter methods.
public void main (String args []){
final double pi=22/7;
final int constant=2;
double raduius;
final int sconstant=4;
double length;
final int rconstant=2;
double rlength;
double widith;
double base;
double height;
double hypotenuse;
Perimeter g= new Perimeter();
Scanner s=new scanner(System.in);
System.out.println("Enter The Radius");
radius=s.nextDouble();
System.out.println("Enter The Square Length");
lenght=s.nextInt();
System.out.println("Enter The Rectangle Lenght");
rlength=s.nextInt();
System.out.println("Enter The Rectangle widith");
widith=s.nextInt();
System.out.println("Enter The Triangle Base");
base=s.nextInt();
System.out.println("Enter The Triangle height");
height=s.nextInt();
System.out.println("Enter The Triangle hypotenuse");
hypotenuse=s.nextInt();
System.out.println("Perimeter = " + g.getperimeter(constant,pi,radius));
System.out.println("Perimeter = " + g.getperimeter(sconstant,length));
System.out.println("Perimeter = " + g.getperimeter(rconstant,rlength,widith));
System.out.println("Perimeter = " + g.getperimeter(base,height,hypotenuse));
You are still unfamiliar with java it seems, hence start help:
The main method is static, that is executed first.
You are using overloaded methods getperimeter, where you can easily mix up int and double. Maybe pick a unique name. Java convention is to use funny camel case: getPerimeter.
import java.util.*;
public class Perimeter {
public static void main(String args[]) {
new Perimeter().execute();
}
public double getperimeter(int constant, double pi, double radius) {
return (constant * pi * radius);
}
public double getperimeter(int sconstant, double length) {
return (sconstant * length);
}
public double getperimeterRLenghtWidith(int rconstant, double rlength, double widith) {
return (rconstant * (rlength + widith));
}
public double getperimeter(double base, double height, double hypotenuse) {
return (base + height + hypotenuse);
}
private void execute() {
final double pi = Math.PI; //22 / 7;
final int constant = 2;
double radius;
final int sconstant = 4;
double length;
final int rconstant = 2;
double rlength;
double widith;
double base;
double height;
double hypotenuse;
Scanner s = new Scanner(System.in);
System.out.println("Enter The Radius");
radius = s.nextDouble();
System.out.println("Enter The Square Length");
length = s.nextInt();
System.out.println("Enter The Rectangle Lenght");
rlength = s.nextInt();
System.out.println("Ener The Rectangle widith");
widith = s.nextInt();
System.out.println("Enter The Triangle Base");
base = s.nextInt();
System.out.println("Enter The Triangle height");
height = s.nextInt();
System.out.println("Enter The Triangle hypotenuse");
hypotenuse = s.nextInt();
System.out.println("Perimeter = " + getperimeter(constant, pi, radius));
System.out.println("Perimeter = " + getperimeter(sconstant, length));
System.out.println("Perimeter = " + getperimeterRLenghtWidith(rconstant, rlength, widith));
System.out.println("Perimeter = " + getperimeter(base, height, hypotenuse));
}
}