Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
The given problem was to create two Java classes, one with constructor methods and the other with a main method, calling the first.
The second half won't compile due to a "Error, cannot find symbol" with symbols symbol: method getArea()
and location
location: variable radius of type Circle
Prof said to check there was something off with my usage of printf, but that's not what's causing the compile errors, is it?
I've done some quick searching for similar problems but a lot of issues people with the same errors were having were related to their class and methods being private.
// Class I'm trying to call from
public class Circle
{
double radius;
public Circle(double r)
{
radius = r;
}
public Circle()
{
radius = 0.0;
}
public void setRadius(double r)
{
radius = r;
}
public double getRadius()
{
return radius;
}
public double area()
{
return Math.PI * Math.pow(radius, 2);
}
public double diameter()
{
return radius * 2.0;
}
public double circumference()
{
return Math.PI * radius * 2.0;
}
}
// Seperate java program, used to call Circle.java
import java.util.Scanner;
public class CircleDemo
{
public static void main(String[] args)
{
String input;
double value;
Circle radius = new Circle();
System.out.printf("Enter the radius"+
"of a circle");
Scanner keyboard = new Scanner(System.in);
input = keyboard.nextLine();
value = Double.parseDouble(input);
radius.setRadius(value);
System.out.printf("Area: " + radius.getArea()
+ "\n Diameter: " + radius.getDiameter() +
"\nCircumference: " + radius.getCircumference());
System.exit(0);
}
}
Error output:
CircleDemo.java:18: error: cannot find symbol
area = radius.getArea();
^
symbol: method getArea()
location: variable radius of type Circle
CircleDemo.java:19: error: cannot find symbol
diameter = radius.getDiameter();
^
symbol: method getDiameter()
location: variable radius of type Circle
CircleDemo.java:20: error: cannot find symbol
circumference = radius.getCircumference();
^
symbol: method getCircumference()
location: variable radius of type Circle
3 errors
You're looking for the wrong method names.
You've defined the method circumference(), but are calling getCircumference(). You've define the method area(), but are calling getArea().
Either rename the methods in Circle, or change the methods called from main.
Right off the bat, it appears as if you're calling the wrong methods.
If you are trying to use getters and setters in Java you have to actually create a method for getting and a method for setting.
Java doesn't automatically give you the values if you just call getDiameter(). You need to actually create a method called getDiameter() then you can call it.
Let me know if that fixes your problem!
You're using methods that do not exist. getDiameter(), getArea() and getCircumference() are not declared in your class Circle. I think you're trying to call diameter(), area() and circumference(). Getters and setters must be declared in the class with its own name; they are not automatically created or something like that.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
First off, I am a BEGINNER in Java. I am finally taking my core classes in college. I am in Computer Science 1 and I'm correcting a code I got from a book as a practice so I can gain a better understanding on how to fix codes. It's a simple code, however, I keep running into 3 errors every single time. I need advice on how to correct them! I am new to all of this so it can be frustrating at times. Thanks!
public class Mistakes
{
public static void main(String[] args);
{
int y;
int x = 12;
y + x = y;
System.out.println("This is what y + x equals: "+y);
System.out.print("This is what t equals: "+t);
}
}
I keep running into 3 errors:
java:3: error: missing method body, or declare abstract
public void main(String[] args);
^
java:7: error: unexpected type
y + x = y;
^
required: variable
found: value
java:9: error: cannot find symbol
System.out.print("This is what t equals: "+t);
^
symbol: variable t
location: class Mistakes
Do I change t into x?
Do I change public class to public abstract?
Any advice will be greatly appreciated!
First, your main() method has a ; after it's declaration. This is allowed only if the method you are declaring is abstract and, thus, has no "body". In this case, you should remove the semicolon. Look below:
public static void main(String[] args) {
//Your code here
}
Second, your assignment operation is wrong. In Java (and in programming in general) you must first specify a variable and then the value it is going to receive (literally or, in your case, through an expression). You should do it as shown below:
y = x + y; //the value of y will be equal to x+y
In this case you could even use a shortcut, if you want to:
y += x; //this expression will have the same effect as the shown above
Finally, you are getting the last error because the variable t wasn't declared, so the method System.out.print() is trying to print a variable that doesn't exist. In this case, you should either remove the symbol t or declare a variable with this name, like I do below:
int t = 3;
System.out.print("This is what t is equals to " + t); //t will be 3
The error I'm getting is:
"Cannot invoke yearlyPay() on the primitive type double"
I'm trying to call the method: yearlyPay() on the variable calcPay.
I've created yearlyPay() and it looks as so:
public double yearlyPay(double pay)
{
double yearlyPay = hourlyRate * HOURS_YEAR;
System.out.println("public double yearlyPay(double pay): " + yearlyPay);
System.out.println("");
return yearlyPay;
}
then I have another method where calcPay is located
public double localTax(double calcPay)
{
double pay = calcPay;
double localTax;
if (pay.yearlyPay() < 45000)
{
localTax = (1.15 / 100) * pay;
}
else
localTax = (1.15 / 100) * 45000;
return localTax;
}
I also figured that having
double pay = calcPay
is kinda redundant, so I changed it to
public double localTax(double calcPay)
{ //removed double pay = calcPay
double localTax;
if (calcPay.yearlyPay() < 45000)
{
localTax = (1.15 / 100) * pay;
}
else
localTax = (1.15 / 100) * 45000;
return localTax;
}
But...same thing happened.
I googled the problem (with the error message) but I didn't find anything that helped.
There was a place that said to change double to Double, but I didn't know which ones where to change. So I tried each one, one by one...needless to say that didn't work either.
Any help would be appreciated. There is more to the code (which is homework). I didn't post it all cuz I don't think it's useful. Ask if you need specific though.
Also if this question is similar to this one, let me know, I'll delete this and look at that one. I probably missed it in the 3,000,000+ search results on google.
Only objects have methods.
A double is a primitive not an object.
Your yearlyPay() method is a method of the class you are writing, not of double.
So if yearlyPay() is defined in the same class as the call, you call it as:
double n = yearlyPay(calcPay);
... or if you wrote the method in another class (say, PayCalculator):
PayCalculator payCalculator = new PayCalculator();
...
double n = payCalculator.yearlyPay(calcPay);
... or if the method is in PayCalculator as a static method:
double n = PayCalculator.yearlyPay(calcPay);
Incidentally, it's not a good idea to use floating-point number types for money calculations. Google for reasons.
Hello I'm having a little trouble in understanding this homework. I just started Java a few weeks ago and we just started on building classes so here is the question.
The problem: I'm having a trouble with the first constructor and setRadius I generally don't know what to do in them. Do I input into setRadius and send that value to that constructor. What do I do with the constructor?
Write a Circle class that has the following fields:
radius: a double
PI: a final double initialized with the value 3.14159
The class should have the following methods:
Constructor: accepts the radius of the circle as an argument.
Constructor: a no-arg constructor that sets the radius field to 0.0.
setRadius: a mutator method for the radius field.
getRadius: an accessor method for the radius field.
getArea: returns the area of the circle, which is calculated as area = PI * radius * radius.
getDiameter: returns the diameter of the circle, which is calculated as diameter = radius * 2.
getCircumference: returns the circumference of the circle, which is calculated as circumference = 2 * PI * radius.
Write a program that demonstrates the Circle class by asking the user for the circle's radius, creating a Circle object, and then reporting the circle's area, diameter, and circumference.
So I wrote this code:
public class CircleClass {
final double PI = 3.14159;
double radius;
// this constructor allows the input of the user
public CircleClass (double rad){
radius = rad;
}
// this is the default constructor in case of no user input
public CircleClass (){
radius = 0;
}
// this method allows you to set the radius
public void setRadius (double input){
//CircleClass input;
radius = input;
}
// value returners //
// returns radius
public double getRadius (){
return radius;
}
// returns area
public double getArea (){
return PI * radius * radius;
}
// returns diameter
public double getDiameter(){
return radius * 2;
}
// returns circumference
public double getCircumference(){
return 2 * PI * radius;
}
}
So I'm confused as to the first method and also I may have made a mistake somewhere else in this program if you see something and you think I should know let me know please.
When you create an instance / object of a class, this basically calls the constructor (default / parameterized) constructor of the class.
CircleClass circleClass = new CircleClass(10);
This will call the parameterized constructor (first constructor in your code) of your class and will set the value of the radius as 10.
CircleClass circleClass = new CircleClass();
This will call the default constructor (no-arg, second constructor in your code) of your class and will set the value of the radius as 0.
If you want to set the value of the radius using the method setRadius(double), it should be done using an object of the class. Since, the methods of a class can be accessed using the objects of the class.
It can be done as follows:
Scanner in = new Scanner(System.in);
CircleClass circleClass = new CircleClass(); // calls the no-arg constructor
System.out.println("Enter the radius of the circle : ");
double radius = in.nextDouble();
circleClass.setRadius(radius); // this sets the radius of the circle using the method setRadius(double) of the class
Methods can also be called in java using class names if the method is defined as public static. Consider the code fragment:
public class Class1{
public static final void PrintMessage(){
System.out.println("Just prints a message");
}
}
The method PrintMessage(void) can be called using the class name as follows:
Class1.PrintMessage(); // Calls the method PrintMessage(void)
Moreover, I don't see any mistakes in your code given the problem statement.
You might however want to make use of the 'this' variable for your class variables. Many a times we might run out of meaningful names for variables (in your case you have used input as a variable name for radius in your setRadius(double) method which may not make much sense to another programmer). However, if you make use of the 'this' variable you can avoid this.
public class CircleClass{
double radius; // class variable
public CircleClass(double radius){
this.radius = radius; // this.radius -> refers to the class variable
}
public void setRadius(double radius){
this.radius = radius; // radius -> refers to the variable passed as argument
}
}
Just a small tip, works wonders.
By creating the main method (the method that actually does something with the program you've created), you might get a better understanding:
First include the below import at the top of your java document (outside the class). This will allow you to use built in java methods that will handle user inputs:
import java.util.Scanner;
Then in the main method:
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
CircleClass cc = new CircleClass(input.nextDouble());
System.out.println(cc.getArea());
}
Inside the main method, you first create a Scanner object, that we give the name "input", (from the Scanner class that we imported). This will allow you to take user inputs.
Then create a CircleClass object (that I named cc, you can name it whatever you want). This object is directly referencing the constructor in your class, as in the method with the same name as the class (CircleClass()). Now, since your constructor takes a parameter with the data type double, we also need to provide a "double" parameter when we create the object. Which we set to the user input (input.nextDouble(), <-- This is how you do that using the Scanner class).
Then at the bottom I simply just print out one of the return methods that you created, in this case getArea(). But note that you put cc.getArea(), since "cc" is the name of the CircleClass object. Creating the CircleClass object allows us to use the methods inside of the CircleClass class in that way.
Hope this didn't get too confusing.
You could create two instance of CircleClass class.
CircleClass uncircle = new CircleClass(); //Default constructor
CircleClass circle = new CircleClass(5.7);
The method uncircle.getRadius() would return 0. I suggest you try circle.getRadius() to check the output.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am getting this error message in eclipse java.lang.Error: Unresolved compilation problem. This is the code.
public class AreaOfSquare {
public static void main (String [] args){ }
int base;
int height;
int area;
base = 10;
height = 10;
area = base * height;
}
The error seems to be around declaring the last variable but i don't know what the problem is...
"java.lang.Error: Unresolved compilation problem"
This means that you're trying to run code that won't compile, something that you should never do. Instead, you should compile first and fix all compilation errors before trying to run your code.
As for your specific problem, your curly braces don't go around your main method body, and your class is missing a closing brace. You have code dangling out where it doesn't belong, neither in a method, nor constructor, nor initializer block.
So change this:
public class AreaOfSquare {
public static void main (String [] args){ }
int base;
int height;
int area;
base = 10;
height = 10;
area = base * height;
}
to this:
public class AreaOfSquare {
public static void main (String [] args){
int base;
int height;
int area;
base = 10;
height = 10;
area = base * height;
System.out.println("area is: " + area);
}
}
You will want to study the first few chapters of any good Java textbook which will show you how to construct simple Java classes that compile and work.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Attempting to write some code to convert feet to meters and meters to feet and achieve the table shown below. This is my first java program and I have no idea why I'm getting syntax errors involving "}".
Feet Meters| Meters Feet
1.0 0.305 | 20.0 65.574
2.0 0.61 | 25.0 81.967
…
9.0 2.745 | 60.0 196.721
10.0 3.05 | 65.0 213.115
Here's what I have...
public class Hmwk {
public static void main(String[] args){
public static double footToMeter(double foot){
return 0.305 * foot;
}
public static double meterToFoot(double meter){
return 3.279 * meter;
}
for (double i = 1.0; i <11; i++){
System.out.printf(i+footToMeter(i)+"|"+(i*5+15)+meterToFoot(i*5+15));}
}}
Any and all help is much appreciated.
Here's a small tip, if you don't like indenting code, then coding might not be for you.
This may sound harsh, but I sincerely believe that before one writes his/her first program, one needs to know what code structure is all about. Unfortunately, schools and courses tend to hide or ignore this fact completely. I know, I've seen this back at secondary school - telling your teacher to properly format code isn't nice believe me.
public class Hmwk {
public static double footToMeter(double foot){
return 0.305 * foot;
}
public static double meterToFoot(double meter){
return 3.279 * meter;
}
public static void main(String[] args){
for (double i = 1.0; i <11; i++){
System.out.printf(i + footToMeter(i) + "|" + (i*5+15) + meterToFoot(i*5+15));
}
}
}
In Java, you cannot have methods inside methods.
Indenting code means you can find your problems easily.
Brackets are meant to stay on separate lines, except the opening brackets (depends on taste)
You have a lot of braces in incorrect positions. Most notably, your footToMeter and meterToFoot are declared inside of your main method, which is incorrect. Here is your code with correct brace placement:
public class Hmwk
{
public static void main(String[] args)
{
for (double i = 1.0; i <11; i++)
{
System.out.printf(i+footToMeter(i)+"|"+(i*5+15)+meterToFoot(i*5+15));
}
}
public static double footToMeter(double foot)
{
return 0.305 * foot;
}
public static double meterToFoot(double meter)
{
return 3.279 * meter;
}
}
As a general rule, methods cannot be nested in Java. This means that no method can be declared inside of another.
No closing brace in the void main. Put '}' before 'public static double footToMeter' and delete closing bracket after closing bracket from for