So I have three static, overloaded methods that are used in my AreaClient class that are taking input from the user and passing what those inputs are as parameters to the methods below. For some reason though I can't seem to get the last area method to take in my hieght variable as a parameter. I keep getting an error saying "cannot find symbol". These are supposed to be overloaded methods, just what the assignment says. Sorry if this is real simple but I am pretty new to programming. Here is the code that I wrote.
import java.util.Scanner; // Needed for the Scanner class
public class AreaClient {
public static void main(String[] args) {
double circleRadius; //input for radius of circle
int width, length; //input for rectangle width and length
double cylinderRadius, height; //input for radius of a cylinder and hieght
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// gathering input for radius of circle
System.out.println("Enter radius of circle");
circleRadius = keyboard.nextDouble();
// input for width and length of rectangle
System.out.println("Enter width of rectangle");
width = keyboard.nextInt();
System.out.println("Enter length of rectangle");
length = keyboard.nextInt();
// input for radius and hieght of cylinder
System.out.println("Enter radius of cylinder");
cylinderRadius = keyboard.nextDouble();
System.out.println("Enter hieght of cylinder");
height = keyboard.nextDouble();
//returning area methods results and storing them in new variables
double circleArea = area(circleRadius);
int rectangleArea = area(width, length);
double cylinderArea = area(cylinderRadius, height);
//displaying results of methods
System.out.println("The area of your circle is: " + circleArea);
System.out.println("The area of your rectangle is: " + rectangleArea);
System.out.println("The area of your cylinger is: " + cylinderArea);
}
//overloaded methods that take different inputs
public static double area(double r)
{
return 3.14159265359 * Math.pow(r, 2);
}
public static int area(int w, int l)
{
return w * l;
}
//actual method that doesn't recognize h inside
public static double area(double r, double h)
{
return 2*3.14159265359 * Math.pow(r,2) + h (2*3.14159265359*r);
}
}
And the error msg I am getting
AreaClient.java:54: error: cannot find symbol
return 2*3.14159265359 * Math.pow(r,2) + h (2*3.14159265359*r);
^
symbol: method h(double)
location: class AreaClient
1 error
Thanks guys. Any help is much appreciated.
Notice in the error message:
symbol: method h(double)
Why it is looking for a method called h() which accepts a double? Because you're telling it to:
h (2*3.14159265359*r)
h isn't a method, it's just a value. You need to use an operator to connect it to that other value. I think you meant to do this:
h * (2*3.14159265359*r)
I think you mean: h * (2*3.14159265359*r). Without the operator, Java thinks you're trying to call a method named h(double)
return 2*3.14159265359 * Math.pow(r,2) + h * (2*3.14159265359*r);
Related
im trying to make a code block to compute the volume of a cylinder but im unfamiliar with java so this is what i had so far. Is there anyway to re-purpose it so i can compute the volume?
package javaapplication231;
import java.util.Scanner;
public class JavaApplication231 {
public static void main(String[] args) {
int a,b,i,product;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the values of a and b: ");
a = keyboard.nextInt(); //Line 6
b = keyboard.nextInt();
i=0;
product=0;
while (i<a){
i=i+1;
product= product +b;
}
System.out.println("Product=" + product);
}
}
V =PIr^2h
where r is the radius and h is the height
user input: r and h
PI= 3.1416
To calculate the volume of Cylinder simply evaluate the expression, why do you need loop and do the addition. As you mentioned use the formula to calculate the
Volume=PI*radius2*height
int radius, height;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the values of radius and Height: ");
radius = keyboard.nextInt(); // Line 6
height = keyboard.nextInt();
double volume = 3.14 * radius * radius * height;
System.out.println("Volume=" + volume);
The question that I am really stuck on is this:
Write a program that asks the user to enter the width and length of a rectangle, and then display the rectangle’s area. The program should call the following methods:
• getLength – This method should ask the user to enter the rectangle’s length, and then return that value as a double.
• getWidth – This method should ask the user to enter the rectangle’s width, and then return that value as a double.
• getArea – This method should accept the rectangle’s length and width as arguments, and return the rectangle’s area. The area is calculated by multiplying the length by width.
• displayArea – This method should accept the rectangle’s length, width, and area as arguments, and display them in an appropriate message to the screen.
I don't know how to complete this code because right now what I have is this:
import java.util.Scanner;
public class WidthLengthAreaMethods
{
public static void main(String[]args)
{
Scanner keyboard = new Scanner(System.in);
double length;
double width;
double area;
length = getLength();
width = getWidth();
area = getArea(double length, double width);
displayData(length, width, area);
}
public static double getLength()
{
System.out.println("Enter length. ");
length = keyboard.nextDouble();
System.out.println("The length is " + length);
}
public static double getWidth()
{
double width;
System.out.println("Enter width. ");
width = keyboard.nextDouble();
System.out.println("The width is " + width);
}
public static double getArea()
{
double length;
double width;
double area = length * width;
System.out.println("The area is: " + area);
}
public static void displayData(double length, double width, double area)
{
System.out.println(" The length is: \t" + length);
System.out.println(" The width is: \t" + width);
System.out.println(" The area is: \t" + area);
}
}
What am I screwing up on and how would I go about fixing it? I am a beginner in programming so please bear with me :D.
Thanks guys!!
Since your program is broken up into several methods, the data inside each method is local unless you store it inside the class itself.
For example, your helper functions for getLength() and getWidth() wouldn't be able to access your keyboard Scanner unless you declared it outside of the main method, as such:
import java.util.Scanner;
public class WidthLengthAreaMethods {
Scanner keyboard = new Scanner( System.in );
// Initialized within the class, but outside of any methods
public static void main( String[] args ) {
double length = getLength();
double width = getWidth();
double area = getArea( length, width );
displayData( length, width, area );
}
}
Another alternative would be to pass your Scanner to each of the helper methods in their function calls, e.g.
public static double getLength( Scanner keyboard ){}
While passing the Scanner to each function separately would allow your methods to work as intended, the first option is slightly more readable.
The other thing to consider is that when a method has a return value, such as a double in the case of getLength(), getWidth(), and getArea(), the piece of code calling the function is expecting some variable of that type to be returned. In the case of a void function, such as main() or displayData(), the method states that it will not return a variable of any specific type.
Therefore, when you set length to equal getLength(), what you're trying to do is set the value of your local length variable to equal the value coming back from your helper function. If that value will never be sent, the program will most likely be unable to compile - an error will be thrown stating something along the lines of "expected type double" when you try to call that function. To fix the compiler error, a return statement needs to be added in to the helper functions, such as:
Scanner keyboard = new Scanner( System.in );
public static double getWidth() {
System.out.println("Enter width.");
double width = keyboard.nextDouble(); // Sets the value to return to your main function
System.out.println("The width is " + width);
return width; // Returns the value to your main function
// Causes any code underneath the return statement to be ignored
}
Combining all of that should allow the compiler errors to stop, and make your program work correctly.
Here is the working solution
import java.util.Scanner;
public class WidthLengthAreaMethods {
public static void main(String[]args)
{
double length;
double width;
double area;
length = getLength();
width = getWidth();
area = getArea(length, width);
displayData(length, width, area);
}
public static double getLength()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter length. ");
double length = keyboard.nextDouble();
System.out.println("The length is " + length);
return length;
}
public static double getWidth()
{
Scanner keyboard = new Scanner(System.in);
double width;
System.out.println("Enter width. ");
width = keyboard.nextDouble();
System.out.println("The width is " + width);
return width;
}
public static double getArea(double length, double width)
{
double area = length * width;
System.out.println("The area is: " + area);
return area;
}
public static void displayData(double length, double width, double area)
{
System.out.println(" The length is: \t" + length);
System.out.println(" The width is: \t" + width);
System.out.println(" The area is: \t" + area);
}
}
I'm practicing some work from my java book and I'm having an issue with getting a method to use a variable for a calculation. Please note that this is a work in progress and I'm only trying to get it to use the circleArea method to calculate the area of a circle at the moment. Here is the necessary code:
public class Geometry
{
public static void printMenu()
{
System.out.println("This is a geometry calculator\nChoose what you would like to calculate" + "\n1. Find the area of a circle\n2. Find the area of a rectangle\n3."
+ " Find the area of a triangle\n4. Find the circumference of a circle."
+ "\n5. Find the perimeter of a rectangle\n6. Find the perimeter of a triangle"
+ "\nEnter the number of your choice:");
}
public static void circleArea(double area)
{
System.out.println(Math.PI*Math.pow(radius, 2));
}
public static void main(String[] args)
{
int choice; //the user's choice
double value = 0; //the value returned from the method
char letter; //the Y or N from the user's decision to exit
double radius; //the radius of the circle
double length; //the length of the rectangle
double width; //the width of the rectangle
double height; //the height of the triangle
double base; //the base of the triangle
double side1; //the first side of the triangle
double side2; //the second side of the triangle
double side3; //the third side of the triangle
}
}
Please declare a variable of class and call the function from it.
public class Geometry
{
int choice; //the user's choice
double value = 0; //the value returned from the method
char letter; //the Y or N from the user's decision to exit
double radius; //the radius of the circle
double length; //the length of the rectangle
double width; //the width of the rectangle
double height; //the height of the triangle
double base; //the base of the triangle
double side1; //the first side of the triangle
double side2; //the second side of the triangle
double side3; //the third side of the triangle
public static void printMenu()
{
System.out.println("This is a geometry calculator\nChoose what you would like to calculate"
+ "\n1. Find the area of a circle\n2. Find the area of a rectangle\n3."
+ " Find the area of a triangle\n4. Find the circumference of a circle."
+ "\n5. Find the perimeter of a rectangle\n6. Find the perimeter of a triangle"
+ "\nEnter the number of your choice:");
}
public static void circleArea(double area)
{
System.out.println(Math.PI*Math.pow(radius, 2));
}
public static void main(String[] args)
{
Geometry g = new Geometry();
g.printMenu();
}
}
This quiz is in two parts. First is
public class FixDebugBox {
private int width;
private int length;
private int height;
private double Volume;
public void FixDebugbox() {
length = 1;
width = 1;
height = 1;
}
public FixDebugBox(int width, int length, int height) {
width = width;
length = length;
height = height;
}
public void showData() {
System.out.println("Width: " + width + " Length: " +
length + " Height: " + height);
}
public double getVolume() {
double vol = length * width * height;
return Volume;
}
}
The code above is one half of the quiz, it have the code above complied correctly but the second part I can't
public class FixDebugFour3
// This class uses a FixDebugBox class to instantiate two Box objects
{
public static void main(String args[])
{
int width = 12;
int length = 10;
int height = 8;
FixDebugBox box1 = new FixDebugBox(width, length, height);
FixDebugBox box2 = new FixDebugBox(width, length, height);
System.out.println("The dimensions of the first box are");
box1.showData();
System.out.println("The volume of the first box is");
showVolume(box1);
System.out.println("The dimensions of the first box are");
box2.showData();
System.out.println("The volume of the second box is");
showVolume(box2);
}
public void showVolume() {
double vol = FixDebugBox.getVolume();
System.out.println(vol);
}
}
I keep getting an error with double vol = FixDebugBox.getVolume(); error: non-static method getVolume() cannot be referenced from a static context
FixDebugBox.getVolume();
getVolume is non static method you can not call this with class name, its a public method which you need object to call it.
public void showVolume(FixDebugBox box) {
double vol = box.getVolume();
System.out.println(vol);
}
Now give me the prize. :D
I think you already answered yourself. You need to hold a reference to an instance of FixDebugBox in order to call its non-static methods.
As the error message says, you can't call a non-static method from the static context that is the main method. While you could turn your showVolume() to be a static method and take a FixDebugBox instance as an argument, seeing how FixDebugBox objects already have a getVolume() method, just call it for each instance:
System.out.println(box1.getVolume());
...
System.out.println(box2.getVolume());
Also, don't change the name of your Volume variable to volume. You should use camelCase.
If you move
public void showVolume() {
double vol = FixDebugBox.getVolume();
System.out.println(vol);
}
to the class FixDebugBox
and remove the getVolume() method inclass FixDebugBox , and change the showVolume() method to:
public void showVolume() {
double vol = length * width * height;
Volume = vol;
System.out.println(Volume);
}
That would fix your program. Also boxVolume would be a better name instead of Volume, since variables are not supposed to be written with a capital letter.
I have to ask the user to input the dimensions of two boxes then calulate the surface area and volume of two boxes. I am done with everything but i keep getting the error: constructor Box in class Box cannot be applied to given types:
My main class BoxCalc:
package boxcalc;
import cs1.Keyboard;
public class BoxCalc
{
public static void main(String[] args)
{
int width, length, height, volume1, volume2, surfacearea1, surfacearea2;
Box b1 = new Box();
Box b2 = new Box();
b1.getClass();
b2.getClass();
// Ask the user the demensions for Box 1.
System.out.println ("Box 1");
System.out.print ("Enter a value for the height of Box 1: ");
height = Keyboard.readInt();
System.out.println ("Enter a value for the length of Box 1: ");
length = Keyboard.readInt();
System.out.println ("Enter a value for the width of Box 1: ");
width = Keyboard.readInt();
// Calulating the volume and the surface area of Box 1.
volume1 = width*height*length;
surfacearea2 = (2*(length * height))+(2 *(length * width))+
(2*(width*height));
// Ask the user the demensions for Box 2.
System.out.println ("Box 2");
System.out.print ("Enter a value for the height of Box 2: ");
height = Keyboard.readInt();
System.out.println ("Enter a value for the length of Box 2: ");
length = Keyboard.readInt();
System.out.println ("Enter a value for the width of Box 2: ");
width = Keyboard.readInt();
// Calulating the volume and the surface area of Box 2.
volume2 = width*height*length;
surfacearea2 = (2*(length * height))+(2 *(length * width))+
(2*(width*height));
// Determain which Box has greater volume.
if (volume1 > volume2)
{
System.out.println("Box 1's volume is greater!");
}
else
if(volume2 > volume1)
{
System.out.println("Box 2's volume is greater!");
}
if(volume1 == volume2)
{
System.out.println("Box 1's volume is same as Box 2's!");
}
// Display all the information.
System.out.println();
System.out.println(b1);
System.out.println();
System.out.println(b2);
}
}
My class Box:
package boxcalc;
public class Box
{
private int width, length, height, volume, surfacearea;
// Defining the width, length, and height.
public Box (int boxwidth, int boxlength, int boxheight)
{
width = boxwidth;
length = boxlength;
height = boxheight;
}
// Method to calulate the volume.
public void findVolume(int volume)
{
volume = length * height * width;
}
// Method to calulate the surface area.
public void findSurfaceArea(int surfacearea)
{
surfacearea = (2*(length * height))+(2 *(length * width))+
(2*(width*height));
}
// Use toString method to display all the info above.
public String toString()
{
return ("Length: " + length + "\n" + "Width: " + width + "\n" +
"Height: " + height + "\n" + "Volume: " + volume +"\n" +
"Surface area: " + surfacearea + "\n");
}
}
your box constructor:
public Box (int boxwidth, int boxlength, int boxheight)
{
width = boxwidth;
length = boxlength;
height = boxheight;
}
but you creating box object with no parameter:
Box b1 = new Box();
Box b2 = new Box();
it should be:
after you ask user for the dimension of box 1, put this:
Box b1 = new Box(width, length, height);
after you ask user for the dimension of box 2, put this:
Box b2 = new Box(width, length, height);
As it stands, your program isn't object-oriented in the slightest.
There was no point in posting your Box class, as your main program doesn't make use of it in any significant way.
You're determining which box is bigger in a structured programming manner, by declaring enough variables in main to do all of the logic.
You should get all the dimensions for box1, then do this:
Box b1 = new Box(width, height, length);
Then do the same for box2, and again:
Box b2 = new Box(width,height,length);
Then, in your if statements, instead of volume1 > volume2, you should instead be doing something more like this:
if(b1.getVolume() > b2.getVolume())
Where getVolume() should be a method of your Box class that looks like this:
public int getVolume() {
return volume;
}
(Although, best practices might say don't even include a volume variable in the Box class, and simple do return (width * height * length);
The same principles should be applied to other aspects of your program.
As a note, by doing it this way, your main program only needs 3 int variables: width, height, length. Once you send b1's dimension into the constructor, the b1 variables knows its dimensions, and you can reuse these variables for taking in b2's dimensions. You don't need volume1, volume2, surfacearea1, or surfacearea2 variables, because a complete Box class would allow you to get these Box properties via getVolume() and getSurfaceArea().
You are calling the Box constructor with no parameters: Box b1 = new Box();, and in the class itself it is defined as requiring to have multiple (three) numeric parameters: public Box (int boxwidth, int boxlength, int boxheight). So the compiler is rightfully complaining that something is amiss.
Possible Solutions:
When you call the constructor, pass in 3 numbers that make sense. Edit: and do this after the user has entered valid data.
Or give Box a default constructor that takes no parameters and sets its fields to some default values.