Java - troubleshooting issues with DemoSquare - java

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 ;

Related

Why am I getting a "void could not be converted to int" error with JOptionPane.showInputDialog?

I have this assignment for school where I have to use JOptionPane to calculate the area of different shapes, but when I use JOptionPane.showMessageDialog, it turns red and the error code says that void cannot be converted to int. Can someone help me figure out how to use JOptionPane.showMessageDialog?
public static void areaTriangle(int base, int height)
{
System.out.println(0.5 * base * height);
}
public static void areaCircle(int radius, double area)
{
area = Math.PI * (radius * radius);
System.out.println(area);
}
public static void areaRectangle(int length, int width)
{
System.out.println(length * width);
}
public static void calcArea()
{
System.out.println("What type of area do you want to calculate?");
inputStr = JOptionPane.showInputDialog("Type 1 for triangle, 2 for circle, 3 for rectangle, and 0 for none, then press ENTER.");
int type = Integer.parseInt(inputStr);
if (type == 0)
{
return;
}
else if (type == 1)
{
inputStr = JOptionPane.showInputDialog("Enter the measurement of the base, then press ENTER.");
int base = Integer.parseInt(inputStr);
inputStr = JOptionPane.showInputDialog("Enter the measurement of the height, then press ENTER.");
int height = Integer.parseInt(inputStr);
int areaT = areaTriangle (base, height);
JOptionPane.showMessageDialog(null, "The area of the triangle is: " + areaT);
}
else if (type == 2)
{
inputStr = JOptionPane.showInputDialog("Enter the measurement of the radius, then press ENTER.");
int radius = Integer.parseInt(inputStr);
double area = Math.PI * (radius * radius);
int areaC = areaCircle (radius, area);
JOptionPane.showMessageDialog(null, "The area of the cicle is: " + areaC);
}
else if (type == 3)
{
inputStr = JOptionPane.showInputDialog("Enter the measurement of the length, then press ENTER.");
int length = Integer.parseInt(inputStr);
inputStr = JOptionPane.showInputDialog("Enter the measurement of the width, then press ENTER.");
int width = Integer.parseInt(inputStr);
int areaR = areaRectangle(length, width);
JOptionPane.showMessageDialog(null, "The area of the rectangle is: " + areaR);
}
}
public static void main(String[] args)
{
calcArea();
}
Your problem has nothing to do with the JOptionPane and everything to do with your area* methods
First you declare all your methods as void...
public static void areaTriangle(int base, int height) {
System.out.println(0.5 * base * height);
}
public static void areaCircle(int radius, double area) {
area = Math.PI * (radius * radius);
System.out.println(area);
}
public static void areaRectangle(int length, int width) {
System.out.println(length * width);
}
But then you try and assign a return result to a variable when you call them...
int areaT = areaTriangle(base, height);
//...
int areaC = areaCircle(radius, area);
//...
int areaR = areaRectangle(length, width);
Which simply makes no sense.
Based on what you seem to be trying to do, you need to change your area* methods to return a value of some kind...
public static double areaTriangle(int base, int height) {
return 0.5 * base * height;
}
public static double areaCircle(int radius, double area) {
return Math.PI * (radius * radius);
}
public static int areaRectangle(int length, int width) {
return length * width;
}
Then you can assign the return values from the methods to some variables...
double areaT = areaTriangle(base, height);
//...
double areaC = areaCircle(radius, area);
//...
int areaR = areaRectangle(length, width);

Java: Creating a program to find surface area and volume of a cylinder

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() + ".");
}
}

Finding length and angles of a triangle

My job is to compute the following properties of a given triangle: lengths of all sides, angles at all corners, the perimeter and the area. I have a Triangle class and a Triangle tester class. I THINK I have coded the perimeter and area correctly? But I am beginning to think instead of setting a constant variable side for my perimeter I should be using the lengths of all sides to find my perimeter as well. What I am stuck on is finding the lengths and the angles. For some reason when I run my tester class they all come out as 1.0. Any advice would be appreciated! Thank you!
import java.util.Scanner;
public class Triangle
{
Scanner in = new Scanner(System.in);
/**
* Variables
*/
double x1;
double x2;
double x3;
double y1;
double y2;
double y3;
double lengthA;
double lengthB;
double lengthC;
double angleA;
double angleB;
double angleC;
double area;
double perimeter;
double base;
double height;
double p;
/**
Constructs x and y coordinates
#param x1, x2, x3 to x1, x2, x3
#param y1, y2, y3 to y1, y2, y3
*/
public Triangle(double x1, double x2, double x3, double y1, double y2, double y3)
{
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
this.y1 = y1;
this.y2 = y2;
this.y3 = y3;
}
/**
*Find lengths of all sides
*/
public double getLengthA()
{
lengthA = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
return lengthA;
}
public double getLengthB()
{
lengthB = Math.sqrt(Math.pow((x3 - x2), 2) + Math.pow((y3 - y2), 2));
return lengthB;
}
public double getLengthC()
{
lengthC = Math.sqrt(Math.pow((x1 - x3), 2) + Math.pow((y1 - y3), 2));
return lengthC;
}
/**
* Find angles at all corners
#return angles at all corners
*/
public double getAngleA()
{
angleA = lengthA + lengthB + lengthC - (lengthB * lengthC);
return angleA;
}
public double getAngleB()
{
angleB = lengthB + lengthA + lengthC - (lengthA * lengthC);
return angleB;
}
public double getAngleC()
{
angleC = lengthC + lengthA + lengthB - (lengthA * lengthB);
return angleC;
}
/**
* Constant Variables
*/
public Triangle()
{
base = 5;
height = 15;
}
/**
* Find perimeter of triangle
*/
public double getPerimeter()
{
perimeter = lengthA + lengthB + lengthC;
return perimeter;
}
public double getHalfPerimeter()
{
p = perimeter / 2;
return p;
}
/**
* Find area of triangle
*/
public double getArea()
{
double area = Math.sqrt(p * (p - lengthA) * (p - lengthB) * (p - lengthC));
return area;
}
}
Here is my tester class:
import java.util.Scanner;
import javax.swing.JOptionPane;
public class TriangleSimulator
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
String input = JOptionPane.showInputDialog("Enter X coordinate for the first corner of the triangle: ");
double x1 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter Y coordinate for the first corner of the triangle: ");
double y1 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter X coordinate for the second corner of the triangle: ");
double x2 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter Y coordinate for the second corner of the triangle: ");
double y2 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter X coordinate for the third corner of the triangle: ");
double x3 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter Y coordinate for the third corner of the triangle: ");
double y3 = Double.parseDouble(input);
Triangle t = new Triangle(x1, x2, x3, y1, y2, y3);
System.out.println("Length A is: " + t.getLengthA());
System.out.println("Length B is: " + t.getLengthB());
System.out.println("Length C is: " + t.getLengthC());
System.out.println("Angle A is: " + t.getAngleA());
System.out.println("Angle B is: " + t.getAngleB());
System.out.println("Angle C is: " + t.getAngleC());
System.out.println("Area: " + t.getArea());
System.out.println("Perimeter: " + t.getPerimeter());
in.close();
}
}
You're showing JOptionPanes that prompt the user for input, but you're not getting the input and you're thus not using the input to set the state of your Triangle, creating a default Triangle object using its parameterless constructor. Understand that there's no magic in Java programming, and your Triangle object will not magically know what numbers the user has entered and change itself accordingly. Instead you must give that information into your Triangle.
What you must do is assign the results returned from the JOptionPanes, parse them into doubles, and then use those numbers to create a Triangle, using the constructor that takes numeric value parameters, not the default constructor. Do this, and you should be good.
e.g.
String input = JOptionPane.showInputDialog("Enter X coordinate for the first corner of the triangle: ");
double x1 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter Y coordinate for the first corner of the triangle:
double y1 = Double.parseDouble(input);
//.... etc repeat...
Triangle triangle = new Triangle(x1, x2, x3, y1, y2, y3);
Edit
This constructor ignores the values being passed in:
public Triangle(double x1, double x2, double x3, double y1, double y2, double y3)
{
x1 = 0;
x2 = 0;
x3 = 0;
y1 = 0;
y2 = 0;
y3 = 0;
}
A constructor like the one you'll need, should take the values passed in, and use those values to set class fields. For instance here:
public class Foo {
private int bar; // the bar field
public Foo(int bar) {
this.bar = bar;
}
}
Note that I use this.bar above so that Java knows that I want to set the bar field with the value held by the bar parameter (the bar without the this). You will need to do something similar only with 6 parameters, not one.
Then you do all your calculations in a separate block of code called an initializer block:
{
/**
* Find lengths of all sides
*/
lengthA = Math.pow(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2) * .05, lengthA);
lengthB = Math.pow(Math.pow((x3 - x2), 2) + Math.pow((y3 - y2), 2) * .05, lengthB);
lengthC = Math.pow(Math.pow((x1 - x3), 2) + Math.pow((y1 - y3), 2) * .05, lengthC);
}
This code gets called before your constructor, and so even if you set your Triangles fields correctly, this code wouldn't work. You don't want to use initializer blocks, and you can forget I even mentioned them other than to tell you not to use them. Instead do your calculations inside your constructor, and do so after setting all your fields.
Note that I've purposely not posted a solution to your problem because I firmly believe that what most of us need is to understand the concepts underlying any problems we are having, and then use that understanding to create our own code solutions.
Most important, read your texts, no better, study your texts, because the mistakes you're making involve foundational concepts, and show that you don't yet understand these concepts and have resorted to guessing. This will never work, as you need to understand all this and understand it well if you're going to be able to progress in this course.
Good luck!
Edit 2
For Tom, run this:
public class TestInitializerBlock {
public TestInitializerBlock() {
System.out.println("Inside of constructor");
}
{
System.out.println("Inside of initializer block");
}
public static void main(String[] args) {
new TestInitializerBlock();
}
}

My program will not display any output

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.

how to use math.pi in java

I am having problems converting this formula V = 4/3 π r^3. I used Math.PI and Math.pow, but I get this error:
';' expected
Also, the diameter variable doesn't work. Is there an error there?
import java.util.Scanner;
import javax.swing.JOptionPane;
public class NumericTypes
{
public static void main (String [] args)
{
double radius;
double volume;
double diameter;
diameter = JOptionPane.showInputDialog("enter the diameter of a sphere.");
radius = diameter / 2;
volume = (4 / 3) Math.PI * Math.pow(radius, 3);
JOptionPane.showMessageDialog("The radius for the sphere is "+ radius
+ "and the volume of the sphere is ");
}
}
You're missing the multiplication operator. Also, you want to do 4/3 in floating point, not integer math.
volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);
^^ ^
Here is usage of Math.PI to find circumference of circle and Area
First we take Radius as a string in Message Box and convert it into integer
public class circle {
public static void main(String[] args) {
// TODO code application logic here
String rad;
float radius,area,circum;
rad = JOptionPane.showInputDialog("Enter the Radius of circle:");
radius = Integer.parseInt(rad);
area = (float) (Math.PI*radius*radius);
circum = (float) (2*Math.PI*radius);
JOptionPane.showMessageDialog(null, "Area: " + area,"AREA",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "circumference: " + circum, "Circumfernce",JOptionPane.INFORMATION_MESSAGE);
}
}
Replace
volume = (4 / 3) Math.PI * Math.pow(radius, 3);
With:
volume = (4 * Math.PI * Math.pow(radius, 3)) / 3;
Your diameter variable won't work because you're trying to store a String into a variable that will only accept a double. In order for it to work you will need to parse it
Ex:
diameter = Double.parseDouble(JOptionPane.showInputDialog("enter the diameter of a sphere.");

Categories