cannot find symbol symbol: method location: class - java

Sorry, just learning Java; but, can someone tell me why I'm getting a "cannot find symbol" error?
My code is as follows:
public class NumberHolder {
public int anInt;
public float aFloat;
public NumberHolder(int setAnInt, float setAFloat) {
setAnInt = anInt;
setAFloat = aFloat;
}
public static void main(String[] args) {
NumberHolder newNumber = NumberHolder(12, 24F);
}
}

Looks like you're missing a new before the call to the constructor:
NumberHolder newNumber = new NumberHolder(12, 24F);
EDIT:
Also, as Tassos Bassoukos points out in his answer, you need to turn around the assignments in the constructor:
anInt = setAnInt;
aFloat = setAFloat;
Although personally, I like to write my constructors like this:
public NumberHolder(int anInt, float aFloat) {
this.anInt = anInt;
this.aFloat = aFloat;
}
This is a matter of style and personal preference, though.

Since
public NumberHolder(int anInt, float aFloat);
is a constructor and not an ordenary method, you need to use the keyword new in order to obtain the actual object. You are calling it like a method and you don't have any method named NumberHolder (but it would be valid if you'd have)

Beyond the new keyword that you're missing, the assignment in the constructor should be the other way around.

You need to instanciate new objects with the new keyword.
public class NumberHolder {
public int anInt;
public float aFloat;
public NumberHolder(int anInt, float aFloat) {
this.anInt = anInt;
this.aFloat = aFloat;
}
public static void main(String[] args) {
NumberHolder newNumber = new NumberHolder(12, 24F);
}
}

Related

How to use public method in another class

I have a class named PointOfSale which have a public method getTpid, but I failed when I want to call this method in my main class, I can only call "TPID_FIELD_NUMBER", how to solve this problem?
PointOfSale ps = new PointOfSale();
ps.TPID_FIELD_NUMBER; //correct
ps.getTpid();// error
public final class PointOfSale implements PointOfSaleOrBuilder {
public static final int TPID_FIELD_NUMBER = 1;
private int tpid_;
#java.lang.Override
public int getTpid() {
return tpid_;
}
}
public interface PointOfSaleOrBuilder {
int getTpid();
}
I think that you just need to add () at the end of your call.
ps.TPID_FIELD_NUMBER is a call for a constant.
If you want to call a method, you should call like this: ps.getTpid();
public static void main(String[] args) {
PointOfSale ps = new PointOfSale();
ps.getTpid();
}
The issue is with the line "ps.TPID_FIELD_NUMBER;". This will give "Not a statement" error. You should assign it to a variable for successful execution.
Your code should look like,
public static void main(String[] args) {
PointOfSale ps = new PointOfSale();
int num = ps.TPID_FIELD_NUMBER;
ps.getTpid();
}

Java, how to merge smaller classes via 1 big class?

Imagine having the following classes:
Head
Torso
Hands
Legs
that I would like to keep but link them via a new class Human. This is what their codes look like:
package myEntities;
public class Head {
private float headSize;
Head(float headSize) {
this.headSize = headSize;
}
public static void main(String[] args) { }
public float[] getHeadSize() {
return headSize;
}
}
repeated the same for the other 3 parts except for the name. If I wanted to make a human class, how would it look like? I'm thinking something like this but this seems too repetitive:
package myEntities;
public class Human {
private Head headObject = new Head(variable1);
private Torso torsoObject = new Torso(variable2);
private Leg rightLegObject = new Leg(variable3);
Human(float variable1, float variable2, float variable3) {
}
public static void main(String[] args) { }
And this way gives an error of not being able to resolve variable1 and the rest of the variables.
First, in your human class, the constructor should be a Human constructor (not a Zombie - Zombie could be a superclass, a subclass, or a completely separate class)
public class Human {
Human(float variable1, float variable2, float variable3) {
}
}
Second, you need to declare the object members like you are doing, but initialize them in your constructor
Human {
private Head headObject;
private Torso torsoObject;
private Leg rightLegObject;
public Human (float variable1, float variable2, float variable3) {
headObject = new Head(variable1);
torsoObject = new Torso(variable2);
rightLegObject = new Leg(variable3);
}
}

How can i access an object from another method in java?

I have the object numberlist that i created in create() method and i want to access it so i can use it in the question() method.
Is there another way to do this that I probably missed? Am I messing something up? If not, how should I do this to get the same functionality as below?
private static void create() {
Scanner input = new Scanner(System.in);
int length,offset;
System.out.print("Input the size of the numbers : ");
length = input.nextInt();
System.out.print("Input the Offset : ");
offset = input.nextInt();
NumberList numberlist= new NumberList(length, offset);
}
private static void question(){
Scanner input = new Scanner(System.in);
System.out.print("Please enter a command or type ?: ");
String c = input.nextLine();
if (c.equals("a")){
create();
}else if(c.equals("b")){
numberlist.flip(); \\ error
}else if(c.equals("c")){
numberlist.shuffle(); \\ error
}else if(c.equals("d")){
numberlist.printInfo(); \\ error
}
}
While interesting, both of the answers listed ignored that fact that the questioner is using static methods. Thus, any class or member variable will not be accessible to the method unless they are also declared static, or referenced statically.
This example:
public class MyClass {
public static String xThing;
private static void makeThing() {
String thing = "thing";
xThing = thing;
System.out.println(thing);
}
private static void makeOtherThing() {
String otherThing = "otherThing";
System.out.println(otherThing);
System.out.println(xThing);
}
public static void main(String args[]) {
makeThing();
makeOtherThing();
}
}
Will work, however, it would be better if it was more like this...
public class MyClass {
private String xThing;
public void makeThing() {
String thing = "thing";
xThing = thing;
System.out.println(thing);
}
public void makeOtherThing() {
String otherThing = "otherThing";
System.out.println(otherThing);
System.out.println(xThing);
}
public static void main(String args[]) {
MyClass myObject = new MyClass();
myObject.makeThing();
myObject.makeOtherThing();
}
}
You would have to make it a class variable. Instead of defining and initializing it in the create() function, define it in the class and initialize it in the create() function.
public class SomeClass {
NumberList numberlist; // Definition
....
Then in your create() function just say:
numberlist= new NumberList(length, offset); // Initialization
Declare numberList outside your methods like this:
NumberList numberList;
Then inside create() use this to initialise it:
numberList = new NumberList(length, offset);
This means you can access it from any methods in this class.

Unable to create new object

I created these two files in java and they don't compile. This error comes up:
cannot find symbol C02FootprintV1".
Why doesn't the program recognize the object? I am new to this.
How could I fix this problem?
public class CO2FootprintV1 {
private double myGallonsUsed;
private double myTonsCO2;
private double myPoundsCO2;
CO2FootprintV1(double gals) {
myGallonsUsed = gals;
}
public void calcTonsCO2() {
myTonsCO2 = myGallonsUsed * 0.878;
}
public double getTonsCO2() {
return myTonsCO2;
}
public void convertTonsToPoundsCO2() {
myPoundsCO2 = myTonsCO2 * 220462262;
}
public double getPoundsCO2() {
return myPoundsCO2;
}
}
public class CO2FootprintV1Tester {
public static void main(String[] args) {
double gals;
double tonsCO2, poundsCO2;
gals = 1300;
CO2FootprintV1 object = new C02FootprintV1(gals);
object.calcTonsCO2();
tonsCO2 = object.getTonsCO2();
object.convertTonsToPoundsCO2();
poundsCO2 = object.getPoundsCO2();
}
}
On the line
CO2FootprintV1 object = new C02FootprintV1(gals);
you have C02 (see zero two) on the right hand side, you meant for it to be
CO2FootprintV1 object = new CO2FootprintV1(gals);
or CO2 (see oh two). Also, you should consider that the error messages your tools give you might be correct.
Just change:
CO2FootprintV1 object = new C02FootprintV1(gals);
to:
CO2FootprintV1 object = new CO2FootprintV1(gals);
That's why it is important to have good naming practice.
You put a "0" (cero) instead of an "O" (letter):
CO2FootprintV1 object = new C02FootprintV1(gals);
Try this:
CO2FootprintV1 object = new CO2FootprintV1(gals);

How do I pass a constructor parameter to another object?

I hope this illustration will make my question clear:
class someThread extends Thread{
private int num;
public Testing tobj = new Testing(num); //How can I pass the value from the constructor here?
public someThread(int num){
this.num=num;
}
void someMethod(){
someThread st = new someThread(num);
st.tobj.print(); //so that I can do this
}
}
For one thing, having a public field is a bad idea to start with IMO. (Your names aren't ideal either...)
All you need to do is initialize it in the constructor instead of inline:
private int num;
private final Testing tobj;
public someThread(int num) {
this.num = num;
tobj = new Testing(num);
}
(You don't have to make it final - I just prefer to make variables final when I can...)
Of course, if you don't need num for anything else, you don't need it as a field at all:
private final Testing tobj;
public someThread(int num) {
tobj = new Testing(num);
}
Why not just initialize your object in the constructor ??
public Testing tobj ; //How can I pass the value from the constructor here?
public someThread(int num){
this.num=num;
tobj = new Testing(this.num);
}

Categories