This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 5 years ago.
I have not been able to get my methods working, as when I import classes from the same package it gives me an error. When I put them in the same package it gives me an error of "cannot find symbol", referring to the class/method I am trying to use in the second one. Like here for example, I can use variables from the other classes but it throws an error whenever I use a method. I have seen similar problems but none of them have helped me so far.
1st class:
package main;
/**
*
* #author Darias
*/
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
ball p;
p = new ball();
System.out.println("the ball weighs" +p.getlength);
}
}
2nd class:
package main;
public class ball {
float length;
float weight;
public ball()
{
length = 100;
weight = 250;
}
public ball(float length, float peso)
{
this.length = length;
this.weight = weight;
}
public float getlength()
{
return length;
}
public float getweight()
{
return weight;
}
public void kickball()
{
System.out.println("you kicked the ball");
}
public void atraparPelota()
{
System.out.println("you caught the ball");
}
}
Note: it's properly indented in the program, here I was just having trouble passing it to text
The getlength is a function so use this way: "the ball weighs" +p.getlength()
and please use visibility syntax and be object oriented: private, protected etc.
public class Ball {
private float length;
private float weight;
public Ball()
{
this.length = 100;
this.weight = 250;
}
public Ball(float length, float peso)
{
this.length = length;
this.weight = weight;
}
public float getLength()
{
return length;
}
public float getWeight()
{
return weight;
}
public void kickBall()
{
System.out.println("you kicked the ball");
}
public void atraparPelota()
{
System.out.println("you caught the ball");
}
You're missing the parentheses after p.getlength in Main.
System.out.println("the ball weighs" +p.getlength());
Related
I am trying to write a public instance method names move()
It takes two integer arguments which showing the amount that the objects needs to change the values of xPos and yPos.
I don't want the method the return a value.
I have done this below but I get the following error message?
Compilation failed (18/01/2020 15:16:31)
Error: line 1 - method move in class StickFigure cannot be applied to given types;
required: no arguments
found: int,int
reason: actual and formal argument lists differ in length
Could I get some guidance where I am going wrong.
/*Instance variables*/
private int xPos;
private int yPos;
private Circle head;
private Triangle body;
private Rectangle leg;
public person()
{
super();
this.head = new Circle(30, OUColour.PINK);
this.body = new Triangle (50, 50, OUColour.RED);
this.leg = new Rectangle (6, 50, OUColour.PINK);
this.setXPos(25);
this.setYPos(220);
this.alignAll();
}
public void setXPos(int newPos)
{
this.xPos = newPos;
this.body.setXPos(newPos);//part (b)(iii)
}
public int getXPos()
{
return this.xPos;
}
public void setYPos(int newPos)
{
this.yPos = newPos;
this.body.setYPos(newPos);//part (b)(iii)
}
public int getYPos()
{
return this.yPos;
}
public Circle getHead()
{
return this.head;
}
public Triangle getBody()
{
return this.body;
}
public Rectangle getLeg()
{
return this.leg;
}
public void alignHead()
{
this.head.setXPos(this.body.getXPos() + (this.body.getWidth() - this.head.getDiameter())/2);
this.head.setYPos(this.body.getYPos() - this.head.getDiameter());
}
public void alignBody()
{
this.body.setXPos(25);
this.body.setYPos(220);
}
public void alignLeg()
{
this.leg.setXPos(this.body.getXPos() + (this.body.getWidth() - this.leg.getWidth())/2);
this.leg.setYPos(this.body.getYPos() + this.leg.getHeight());
}
public void alignAll()
{
this.alignBody();
this.alignHead();
this.alignLeg();
}
public void move(int newxPos, int newyPos)
{
this.body.setXPos(xPos + newxPos);
this.body.setYPos(yPos + newyPos);
this.alignAll();
this.delay(20);
}
If you want method move to take arguments, you have to declare it:
public void move(int xPos, int yPos)
instead of
public void move()
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
The method that I am having trouble with is the "getTotalCost" method.
Each method is as follows:
DemoRoomCarpet
package cnmt.sec01.homeworkassign05;
import javax.swing.JOptionPane;
public class DemoRoomCarpet {
private static double length;
private static double width;
private static double cost;
public static void main(String[] args) {
length = Double.valueOf(JOptionPane.showInputDialog("What is the length of your carpet?"));
width = Double.valueOf(JOptionPane.showInputDialog("What is the width of your carpet?"));
cost = Double.valueOf(JOptionPane.showInputDialog("What is the cost per square foot of carpet?"));
RoomDimension myRoomDim =
new RoomDimension(length,width);
RoomCarpet myRoomCarpet =
new RoomCarpet(cost);
myRoomDim.setLength(length);
myRoomDim.setWidth(width);
myRoomCarpet.setCarpetCost(cost);
myRoomCarpet.getTotalCost();
myRoomCarpet.toString();
}
}
RoomDimension
package cnmt.sec01.homeworkassign05;
public class RoomDimension {
private double length,
width;
public RoomDimension(double length, double width)
{
this.length = length;
this.width = width;
}
public RoomDimension(RoomDimension object)
{
length = object.length;
width = object.width;
}
/**
* #return the length
*/
public double getLength() {
return length;
}
/**
* #param length the length to set
*/
public void setLength(double length) {
this.length = length;
}
/**
* #return the width
*/
public double getWidth() {
return width;
}
/**
* #param width the width to set
*/
public void setWidth(double width) {
this.width = width;
}
public double getArea()
{
double area = length * width;
return area;
}
public String toString() {
String str = "Length is: " + length + "\nWidth is: " + width + "\nArea is: " + this.getArea();
return str;
}
}
and RoomCarpet
package cnmt.sec01.homeworkassign05;
public class RoomCarpet {
private RoomDimension dim;
private double carpetCost;
public RoomCarpet(double carpetCost)
{
this.carpetCost = carpetCost;
}
public RoomCarpet(RoomCarpet object)
{
carpetCost = object.carpetCost;
}
/**
* #return the carpetCost
*/
public double getCarpetCost() {
return carpetCost;
}
/**
* #param carpetCost the carpetCost to set
*/
public void setCarpetCost(double carpetCost) {
this.carpetCost = carpetCost;
}
public double getTotalCost()
{
double total = dim.getArea() * carpetCost;
return total;
}
public String toString()
{
String str = "Your total cost is " + this.getTotalCost() + " dollars";
return str;
}
}
I feel like the issue is something minor that I am missing, but I could be wrong. Apologies for no comments, but I will add them once I get over this error. Passing the getTotalCost method gives a null pointer exception, but I believe that I passed the user input through it.
Thats because, RoomDimension dim is not been initialized. The field has to be initialized either using constructor or through setter.
In RoomCarpet class:
public RoomCarpet(double carpetCost, RoomDimension dim)
{
this.carpetCost = carpetCost;
this.dim = dim;
}
In main:
RoomCarpet myRoomCarpet =
new RoomCarpet(cost, myRoomDim);
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I'm working on a lab for programming principles II, and I have a class that makes a point, with methods for setting the point, and calculating the distance between other points. Calculating the distance worked fine when testing it with a runner for just the class, but when I make other classes that use it as an object, I'm getting an error with the distance formula.
import java.lang.Math;
public class MyPoint {
private double x;
private double y;
public MyPoint(double dubx, double duby)
{
x=dubx;
y=duby;
}
public void setX(double dub) {
x = dub;
}
public void setY(double dub) {
y = dub;
}
public double getX() {
return x;
}
public double getY()
{
return y;
}
public double distance (MyPoint otherPoint)
{
return Math.sqrt(Math.pow((otherPoint.getX()-getX()),2)+(Math.pow((otherPoint.getY()-getY()),2)));
}
public MyPoint midpoint(MyPoint otherPoint)
{
MyPoint point = new MyPoint((otherPoint.getX()+getX()/2),(otherPoint.getY()+getY())/2);
return point;
}
}
That's the class I'm getting the error on. The distance part is getting a null pointer exception.
Here's what I'm passing in:
import java.lang.Math;
public class MyTriangle
{
private MyPoint v1;
private MyPoint v2;
private MyPoint v3;
public MyPoint getPoint1()
{
return v1;
}
public MyPoint getPoint2()
{
return v2;
}
public MyPoint getPoint3()
{
return v3;
}
public void setPoint1(double x, double y)
{
v1= new MyPoint(x,y);
}
public void setPoint2(double x, double y)
{
v2 = new MyPoint(x,y);
}
public void setPoint3(double x, double y)
{
v2= new MyPoint(x,y);
}
public double getArea()
{
double a= v2.distance(v3);
double b= v1.distance(v3);
double c= v1.distance(v2);
double s= (a+b+c)/2;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
}
public class TestMyTriangle
{
public static void main(String [] args)
{
MyTriangle tr1 = new MyTriangle();
tr1.setPoint1(17,17);
tr1.setPoint2(5,30);
tr1.setPoint3(5,17);
System.out.println("Area:\t"+tr1.getArea());
}
}
And the error:
Exception in thread "main" java.lang.NullPointerException
at MyPoint.distance(MyPoint.java:34)
at MyTriangle.getArea(MyTriangle.java:37)
at TestMyTriangle.main(TestMyTriangle.java:9)
I can't seem to figure it out. Please help.
You get the Nullpointer because v3 is null:
fix with:
public void setPoint3(double x, double y)
{
v3= new MyPoint(x,y); // instead of v2
}
Another tipp: to calculate a square dont use Math.pow(x,2).
Altough it works.
The code is cleaner and faster if you use
x*x instead Math.pow(x,2);
This question already has answers here:
What is float in Java?
(4 answers)
Closed 7 years ago.
I cant figure out the errors! But while compiling it shows error. Please help me out.....
// This program is used to find the area of a circle and a rectangle
// through constructor overloading concept.
class area {
float radius;
int l , b;
public area(float r) {
radius=r;
}
public area(int a , int d) {
l=a;
b=d;
}
public void display() {
System.out.println("Area of Circle is = "+(3.14*radius*radius));
System.out.println("Area of Rectangle is = "+(l*b));
}
}
class constadd {
public static void main(String arr[]) {
area c = new area(4.5);
c.display();
area e=new area(4,5);
e.display();
}
}`
Use double instead of float.
import java.util.*;
import java.lang.*;
import java.io.*;
class area {
double radius;
int l , b;
public area(double r) {
radius=r;
}
public area(int a , int d) {
l=a;
b=d;
}
public void display() {
System.out.println("Area of Circle is = "+(3.14*radius*radius));
System.out.println("Area of Rectangle is = "+(l*b));
}
}
class Ideone {
public static void main(String arr[]) {
area c = new area(4.5);
c.display();
area e=new area(4,5);
e.display();
}
}
As Anik mentioned, either change the constructor to take double as a parameter instead of float or while calling this constructor use suffix 4.5 with 'f' to specify that you want to pass float i.e., new area(4.5f);
I have really straight forward code, I am hoping that my issue is that I have just been looking at it too long. I am testing some calculations to make sure I am doing it right before I throw in a huge list. All I want to do is create a new objects through a for loop and toss them in my constructed array.
Contents of public static void main(String args[]) in my Main class:
PositionHolder[] positions = new PositionHolder[8];
PositionHolder currPosition;
int currPos = 0;
for(int i = 0; i <= 7; i++){
/* For Random Points */
currPosition = new PositionHolder(i);
System.out.println("Resetting " + i);
positions[i] = currPosition;
//positions[i].setxPos(100 * Math.random()); // these get set in the
//positions[i].setyPos(100 * Math.random()); // PositionHolder constructor
for(int k = i; k >= 0; k--){
System.out.println(k + ": " + positions[k].getxPos() + ", " + positions[k].getyPos());
}
}
Just for clarification, my PositionHolder class is as follows:
public class PositionHolder {
private static double xPos;
private static double yPos;
private static int point;
private static boolean visited;
public PositionHolder(int pointNumber){
setxPos(100 * Math.random());
setyPos(-100 * Math.random());
setPoint(pointNumber);
setVisited(false);
}
public double getxPos() {
return xPos;
}
public void setxPos(double xPos) {
PositionHolder.xPos = xPos;
}
public double getyPos() {
return yPos;
}
public void setyPos(double yPos) {
PositionHolder.yPos = yPos;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
PositionHolder.point = point;
}
public boolean isVisited() {
return visited;
}
public void setVisited(boolean visited) {
PositionHolder.visited = visited;
}
}
The problem is that for some reason each time through the for loop override the previous PositionHolders I have put in my array. As a quick example, here is the first few lines of my system output from the System.println towards the end of the for loop:
Resetting 0
0: 60.697435147416186, -96.35236848097432
Resetting 1
1: 57.98340997157546, -52.56948459757237
0: 57.98340997157546, -52.56948459757237
Resetting 2
2: 45.75236962694197, -32.03840605394901
1: 45.75236962694197, -32.03840605394901
0: 45.75236962694197, -32.03840605394901
So where I want 0 to stay at 60.69743.... and 1 to stay at 57.98340.... they are all getting set to the same (most resent) value. I wish I could say it is more complex than that, but that is it. What is going on?
--- The Answer, given below by Logan Murphy, is correct ---
As a note, not only should you take a break from time to time to avoid silly mistakes from code you have looked at too much, but you REALLY shouldn't rely on eclipses "fix" solutions to make good code :P
Because you set your variables to be static (shared between instances of the class). They need to be non-static like so
public class PositionHolder {
private double xPos;
private double yPos;
private int point;
private boolean visited;
public PositionHolder(int pointNumber){
setxPos(100 * Math.random());
setyPos(-100 * Math.random());
setPoint(pointNumber);
setVisited(false);
}
public double getxPos() {
return xPos;
}
public void setxPos(double xPos) {
this.xPos = xPos;
}
public double getyPos() {
return yPos;
}
public void setyPos(double yPos) {
this.yPos = yPos;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
public boolean isVisited() {
return visited;
}
public void setVisited(boolean visited) {
this.visited = visited;
}
}
This way each instance of the class PositionHolder has its own variables (instance variables are globally declared variables that are non-static)