cannot find symbol in java - java

This is in DFM.java
This part is in the main class
Algebra.vect dx = new Algebra.vect(new double[] {2.0,3.0,4.0});
Algebra.matrix eij = new Algebra.matrix();
System.out.println(eij.get(1,1));
dx.set(1,4.0);
System.out.println(dx.get(1));
This is in Algebra.java
class Algebra {
public static class vect
{
double[] v = new double[3];
public vect()
{
v[0]=v[1]=v[2]=0;
}
public vect(double[] v)
{
this.v=v;
}
int tamanho()
{
return v.length;
}
double get(int i)
{
return v[i];
}
void set(double[] v)
{
this.v=v;
}
void set(int i, double n)
{
v[i]=n;
}
void print()
{
for(int i=0; i < v.length; i = i + 1)
System.out.print(v[i] + " ");
System.out.print("\n");
}
}
public static class operacoes
{
double prodInt(vect v1, vect v2)
{
return v1.get(0)*v2.get(0)+v1.get(1)*v2.get(1)+v1.get(2)*v2.get(2);
}
double[] somaVV(vect v1, vect v2)
{
return new double[] {v1.get(0)+v2.get(0), v1.get(1)+v2.get(1), v1.get(2)+v2.get(2) };
}
double[] prodMV(matrix m, vect v)
{
double[] Soma = new double[3];
Soma[0]=Soma[1]=Soma[2]=0;
for(int i=0;i< v.tamanho();i=i+1)
{
for(int j=0;i< v.tamanho();j=j+1)
{
Soma[i]=m[i][j]*v[j];
}
}
return Soma;
}
}
public static class matrix
{
double[][] m = new double[3][3];
public matrix()
{
for(int i=0;i< v.tamanho();i=i+1)
{
for(int j=0;i< v.tamanho();j=j+1)
{
m[i][j]=0;
}
}
}
public matrix(double[][] m )
{
this.m=m;
}
double get(int i,int j)
{
return m[i][j];
}
void set(double [][] m)
{
this.m=m;
}
void set(int i,int j, double n)
{
m[i][j]=n;
}
void print()
{
for(int i=0;i< v.tamanho();i=i+1)
{
for(int j=0;i< v.tamanho();j=j+1)
{
System.out.print(m[i][j] + " ");
}
System.out.print("\n");
}
System.out.print("\n");
}
}
Error
DFM.java:29: error: cannot find symbol
System.out.println(eij.get(1,1));
^
symbol: method get(int,int)
location: variable eij of type matrix
1 error
But when I ran with calls of eij method in commentary
dx.set(1,4.0);
System.out.println(dx.get(1));
This part where dx is of the vect class,
worked well and is the code is similar to the matrix class
Can anyone help please?

Looks like the signature of your matrix-class's get-method is missing the modifier public:
double get(int i,int j)
so it has "default" (package) visibility. Change it to
public double get(int i,int j)
and it should work.

Related

Cannot load from byte/boolean array because "BlackWhite.marked" is null

When I run my code below, I keep getting the error from the title of this post, although I don't know why. I tried: Arrays.fill(marked, false);, I am trying to figure out how many white nodes and black nodes, connect that do not exist in the blacknodes[].
public class BlackWhite {
private static boolean[] marked;
public BlackWhite(Graph G, int s) {
marked = new boolean[G.V()];
dfs(G, s);
}
public static int count(Graph G, int[] blacknodes) {
int rw_count = 0;
for (int w : G.adj(blacknodes.length)) {
if (marked(w)) {
rw_count++;
}
}
return rw_count;
}
private void dfs(Graph G, int v) {
marked[v] = true;
for (int w : G.adj(v)) {
if (!marked[w]) {
dfs(G, w);
}
}
}
public static boolean marked(int v) {
return marked[v];
}
public static void main(String[] args) {
Graph G = new Graph(3);
G.addEdge(1, 2);
G.addEdge(4, 1);
G.addEdge(1, 8);
System.out.println(BlackWhite.count(G, new int[] { 1 })); // should print 3
}
}
You are initializing the marked array at the Constuctor of BlackWhite class. But in your main method you never called new BlackWhite(). So your marked array is still null when you are calling the static count method.
You should change your count method like this.
public static int count(Graph G, int[] blacknodes) {
int rw_count = 0;
if(marked == null) marked = new boolean[G.V()];
for (int w : G.adj(blacknodes.length)) {
if (marked(w)) {
rw_count++;
}
}
return rw_count;
}

Cannot find what I think is a memory leak in predator prey program

I have six classes for my program and I believe most of my code is correct, but when I go to run it no print statements happen and instead the program continues running forever without output. This causes a massive memory leak if I run the simulation for larger numbers (100+). I can't seem to find what the problem is but the code just won't properly execute.
**** (where I execute the program from and where I think the error is)
import java.util.*;
public class {
public static void main(String[]args){
SubSim simulate=new SubSim();
ArrayList<Wolf>WolfArrayList=new ArrayList<>();
ArrayList<Moose>MooseArrayList=new ArrayList<>();
ArrayList<Grass>GrassArrayList=new ArrayList<>();
for (int i=0;i<simulate.getInitialWolves();i++){
WolfArrayList.add(new Wolf(simulate));
}
for (int i=0;i<simulate.getInitialMoose();i++){
MooseArrayList.add(new Moose(simulate));
}
for (int i=0;i<simulate.getMaxX();i++){
for (int j=0;i<simulate.getMaxY();j++){
int grassroll=(int)(Math.random()*5);
if (grassroll==1||grassroll==2||grassroll==3||grassroll==4){
GrassArrayList.add(new Grass(simulate,i,j,2));
}
else{
GrassArrayList.add(new Grass(simulate,i,j,0));
}
}
}
simulate.setGrassArrayList(GrassArrayList);
simulate.setMooseArrayList(MooseArrayList);
simulate.setWolfArrayList(WolfArrayList);
System.out.println("Ticks Wolves Moose");
for(int i=1;i<=20;i++){
System.out.printf("%d %d %d\n",(i-1),simulate.getWolfPop(),simulate.getMoosePop());
simulate.run();
}
}
}
** Class**
import java.util.*;
public class extends Simulator{
//making private variables
private ArrayList<Grass> GrassArrayList;
private ArrayList<Moose> MooseArrayList;
private ArrayList<Wolf> WolfArrayList;
//using the animals move method
#Override
public void animalsMove() {
for (int i = 0; i < WolfArrayList.size(); i++) {
WolfArrayList.get(i).move();
WolfArrayList.get(i).setEnergy(WolfArrayList.get(i).getEnergy()-getMovementCost());
}
for (int i = 0; i < MooseArrayList.size(); i++) {
MooseArrayList.get(i).move();
MooseArrayList.get(i).setEnergy(MooseArrayList.get(i).getEnergy()-getMovementCost());
}
}
//using the animals die method
#Override
public void animalsDie(){
int WolfArraySize=WolfArrayList.size();
for (int i=0;i<WolfArraySize;i++) {
if (WolfArrayList.get(i).getEnergy() <= 0){
WolfArrayList.remove(i);
WolfArraySize = WolfArrayList.size();
}
}
int MooseArraySize=MooseArrayList.size();
for (int i=0;i<WolfArraySize;i++){
if (MooseArrayList.get(i).getEnergy() <= 0){
MooseArrayList.remove(i);
MooseArraySize = MooseArrayList.size();
}
}
}
//using animals eat method
#Override
public void animalsEat() {
for (int i = 0; i < WolfArrayList.size(); i++) {
for (int j = 0; j < MooseArrayList.size(); j++) {
if (MooseArrayList.get(j).getLocationX() == WolfArrayList.get(i).getLocationX() && MooseArrayList.get(j).getLocationY() == WolfArrayList.get(i).getLocationY()) {
MooseArrayList.remove(j);
WolfArrayList.get(i).eat();
}
}
}
for (int i = 0; i < MooseArrayList.size(); i++) {
for (int j = 0; j < GrassArrayList.size(); j++) {
if (MooseArrayList.get(i).getLocationX() == GrassArrayList.get(j).getXcord() && MooseArrayList.get(i).getLocationY() == GrassArrayList.get(j).getYcord()) {
MooseArrayList.remove(j);
WolfArrayList.get(i).eat();
}
}
}
}
//using animals reproduce method
#Override
public void animalsReproduce(){
for (int i=0;i<WolfArrayList.size();i++){
if (WolfArrayList.get(i).getEnergy() >= 100) {
Wolf puppy = (Wolf) WolfArrayList.get(i).reproduce();
WolfArrayList.add(puppy);
WolfArrayList.get(i).setEnergy(getInitialEnergy());
}
}
for (int i=0;i<MooseArrayList.size();i++) {
if (MooseArrayList.get(i).getEnergy() >= 100) {
Moose calf = (Moose) MooseArrayList.get(i).reproduce();
MooseArrayList.add(calf);
MooseArrayList.get(i).setEnergy(getInitialEnergy());
}
}
}
//using grass grows method
#Override
public void grassGrows(){
for(int i=0;i<GrassArrayList.size();i++){
if (GrassArrayList.get(i).getLength()>0){
GrassArrayList.get(i).setLength((int)(GrassArrayList.get(i).getLength()+getGrassGrowthRate()));
}
//making shorthand instanced variables
int xcord=GrassArrayList.get(i).getXcord();
int ycord=GrassArrayList.get(i).getYcord();
if(GrassArrayList.get(i).getLength()>=10){
for (int j=0;j<GrassArrayList.size();j++){
//checking area around tall grass to place seeds
if(xcord==GrassArrayList.get(j).getXcord() && ycord+1==GrassArrayList.get(j).getYcord() || xcord-1==GrassArrayList.get(j).getXcord() && ycord+1==GrassArrayList.get(j).getYcord() || xcord-1==GrassArrayList.get(j).getXcord() && ycord==GrassArrayList.get(j).getYcord() || xcord-1==GrassArrayList.get(j).getXcord() && ycord-1==GrassArrayList.get(j).getYcord() || xcord==GrassArrayList.get(j).getXcord() && ycord-1==GrassArrayList.get(j).getYcord() || xcord+1==GrassArrayList.get(j).getXcord() && ycord-1==GrassArrayList.get(j).getYcord() || xcord+1==GrassArrayList.get(j).getXcord() && ycord==GrassArrayList.get(j).getYcord() || xcord+1==GrassArrayList.get(j).getXcord() && ycord+1==GrassArrayList.get(j).getYcord()){
GrassArrayList.get(j).setLength((int)(GrassArrayList.get(j).getLength()+getGrassGrowthRate()));
}
}
}
}
}
//using the run method
#Override
public void run(){
tick();
}
public int getMoosePop(){
return MooseArrayList.size();
}
public int getWolfPop(){
return WolfArrayList.size();
}
public ArrayList<Grass> getGrassArrayList() {
return GrassArrayList;
}
public void setGrassArrayList(ArrayList<Grass> grassArrayList) {
GrassArrayList = grassArrayList;
}
public ArrayList<Moose> getMooseArrayList() {
return MooseArrayList;
}
public void setMooseArrayList(ArrayList<Moose> mooseArrayList) {
MooseArrayList = mooseArrayList;
}
public ArrayList<Wolf> getWolfArrayList() {
return WolfArrayList;
}
public void setWolfArrayList(ArrayList<Wolf> wolfArrayList) {
WolfArrayList = wolfArrayList;
}
}
** Class**
public class extends {
private int movewolf;
public Wolf(Simulator simulator){
super(simulator);
}
#Override
public void eat(){
setEnergy((getEnergy()+getSimulator().getEnergyGainFromEatingMoose()));
}
#Override
public Animal reproduce(){
Wolf wolf=new Wolf(getSimulator());
wolf.setLocationX(getSimulator().randomInt(getSimulator().getMaxX()));
wolf.setLocationY(getSimulator().randomInt(getSimulator().getMaxY()));
wolf.setEnergy(getSimulator().getInitialEnergy());
return wolf;
}
#Override
public Animal die(){
return null;
}
#Override
public void move(){
int moveX=(int)(Math.random()*2);
int moveY=(int)(Math.random()*2);
if (getLocationX()<getSimulator().getMaxX()&& getLocationX()>0 && getLocationY()<getSimulator().getMaxY()&& getLocationY()>0){
if (moveX==1){
this.movewolf=getLocationX()+1;
setLocationX(movewolf);
}
else{
this.movewolf=getLocationX()-1;
setLocationX(movewolf);
}
if (moveY==1){
this.movewolf=getLocationY()+1;
setLocationY(movewolf);
}
else{
this.movewolf=getLocationY()-1;
setLocationY(movewolf);
}
}
else if (getLocationX()==getSimulator().getMaxX()){
this.movewolf=getLocationX()-1;
setLocationX(movewolf);
}
else if(getLocationX()==0){
this.movewolf=getLocationX()+1;
setLocationX(movewolf);
}
else if (getLocationY()==getSimulator().getMaxY()){
this.movewolf=getLocationY()-1;
setLocationY(movewolf);
}
else if (getLocationY()==0){
this.movewolf=getLocationY()+1;
setLocationY(movewolf);
}
else {
}
}
}
** Class**
public class extends Animal{
private int movemoose;
public Moose(Simulator simulator){
super(simulator);
}
#Override
public void eat(){
setEnergy((getEnergy()+getSimulator().getEnergyGainFromEatingGrass()));
}
#Override
public Animal reproduce(){
Moose moose=new Moose(getSimulator());
moose.setLocationX(getSimulator().randomInt(getSimulator().getMaxX()));
moose.setLocationY(getSimulator().randomInt(getSimulator().getMaxY()));
moose.setEnergy(getSimulator().getInitialEnergy());
return moose;
}
#Override
public Animal die(){
return null;
}
#Override
public void move(){
int moveX=(int)(Math.random()*2);
int moveY=(int)(Math.random()*2);
if (getLocationX()<getSimulator().getMaxX()&& getLocationX()>0 && getLocationY()<getSimulator().getMaxY()&& getLocationY()>0){
if (moveX==1){
this.movemoose=getLocationX()+1;
setLocationX(movemoose);
}
else{
this.movemoose=getLocationX()-1;
setLocationX(movemoose);
}
if (moveY==1){
this.movemoose=getLocationY()+1;
setLocationY(movemoose);
}
else{
this.movemoose=getLocationY()-1;
setLocationY(movemoose);
}
}
else if (getLocationX()==getSimulator().getMaxX()){
this.movemoose=getLocationX()-1;
setLocationX(movemoose);
}
else if(getLocationX()==0){
this.movemoose=getLocationX()+1;
setLocationX(movemoose);
}
else if (getLocationY()==getSimulator().getMaxY()){
this.movemoose=getLocationY()-1;
setLocationY(movemoose);
}
else if (getLocationY()==0){
this.movemoose=getLocationY()+1;
setLocationY(movemoose);
}
else {
}
}
}
** Class**
public class {
public Simulator getGrass(){
return grass;
}
public void setGrass(Simulator grass){
this.grass = grass;
}
public int getLength(){
return length;
}
public void setLength(int length){
this.length = length;
}
public int getInitialLength(){
return initialLength;
}
public void setInitialLength(int initialLength){
this.initialLength = initialLength;
}
public int getXcord(){
return Xcord;
}
public void setXcord(int xcord){
Xcord = xcord;
}
public int getYcord(){
return Ycord;
}
public void setYcord(int ycord){
Ycord = ycord;
}
private Simulator grass;
private int length;
private int initialLength=0;
private int Xcord;
private int Ycord;
public Grass(Simulator grass, int xcord, int ycord, int length){
this.grass = grass;
this.length = length;
Xcord = xcord;
Ycord = ycord;
setLength(initialLength);
}
}
I see a problem with the second line here:
for (int i=0;i<simulate.getMaxX();i++){
for (int j=0;i<simulate.getMaxY();j++){
I think it should be j<simulate.getMaxY()

How do I make a left arrow of asterisks in Java?

I'am working on a program that makes right and left arrows. They are extended from a ShapeBase class which is implemented from a ShapeInterface. I didn't paste them so it wouldn't look overwhelming. The problem is I have figured out the right arrow ( I believe) but cannot find my way to getting the left arrow. I keep getting confused and cant seem to understand how to do it. Any help is appreciated.
RightArrow Example Output
public abstract class ShapeBase implements ShapeInterface
{
private int offset;
public ShapeBase( )
{
offset = 0;
}
public ShapeBase(int theOffset)
{
offset = theOffset;
}
public abstract void drawHere( );
public void drawAt(int lineNumber)
{
for (int count = 0; count < lineNumber; count++)
System.out.println( );
drawHere( );
}
public void setOffset(int newOffset)
{
offset = newOffset;
}
public int getOffset( )
{
return offset;
}
}
public class RightArrow extends ShapeBase
{
private int tail;
private int width;
public RightArrow()
{
super();
tail = 0;
width = 0;
}
public RightArrow(int theOffset ,int tailSize, int theWidth)
{
super(theOffset);
tail = tailSize;
width = theWidth;
set(tail , width);
}
public void set(int newHeight, int newWidth)
{
tail = newHeight;
width = newWidth;
}
public void drawHere()
{
topArrowhead();
ArrowTail();
bottomArrowHead();
}
public void topArrowhead()
{
skipSpaces(getOffset());
System.out.println("*");
for(int i = 0; i<width/2; i++)
{
skipSpaces(getOffset());
System.out.print("*");
skipSpaces(i);
System.out.println("*");
}
}
// method to draw the arrow tail
public void ArrowTail()
{
for(int count=0; count<tail; count++)
{
System.out.print("*");
}
skipSpaces(tail+width);
System.out.print("*");
}
// method to draw bottom of arrowhead
public void bottomArrowHead()
{
for(int i =1;i<width/2; i--)
{
skipSpaces(getOffset());
System.out.print("*");
skipSpaces(i);
System.out.println("*");
}
skipSpaces(getOffset());
System.out.println("*");
}
private static void skipSpaces(int number)
{
for (int count=0; count< number; count++)
System.out.print(" ");
}
}
FOR THE LEFT ARROW CLASS, I believe these are the only methods need changing.
I just don't know how
public void topArrowhead()
{
skipsSpaces(getOffset());
System.out.println("*");
for(int i = 0 i<width/2; i++)
{
skipSpaces(getOffset());
System.out.print("*");
skipSpaces(i);
System.out.println("*");
}
}
// method to draw the arrow tail
public void ArrowTail()
{
}
// method to draw bottom of arrowhead
public void bottomArrowHead()
{
}
private static void skipSpaces(int number)
{
for (int count=0; count< number; count--)
System.out.print(" ");
}
}
This is how you can draw a left arrow,it doesn't uses your methods(as you not provided source for ShapeBase) but you can see the implementation below how its get created and then can use in your own way.
public class LeftArrow
{
private int tail=15;
private int width=7;
//Method to draw the left arrow
public void DrawArrow()
{
for(int i = 0,r=0; i<width; i++)
{
//for spaces before head
for(int j=0;j<(width/2)-r;j++)
System.out.print(" ");
for(int j=0;j<=r;j++)
{
if(j==r || j==0)
System.out.print("*");
else
System.out.print(" ");//for spaces inside head
}
if(i==width/2)//to draw tail after the mid of head
ArrowTail();
if(i<width/2)//controls the increment & decrements of spaces before head
r++;
else r--;
System.out.println();
}
}
// method to draw the arrow tail
public void ArrowTail()
{
for(int count=0; count<tail; count++)
{
System.out.print("*");
}
}
public static void main(String[] args)
{
LeftArrow Arrow = new LeftArrow();
Arrow.DrawArrow();
}
}

java.lang.IncompatibleClassChangeError: Implementing Utility class

Im trying to implement a simple rectangle, but i am getting incompatible class change error..
Expecting non-static method Utility.printLine()
The error is on line,
util.printLine();
Any help??
the complete code is as follow..
import java.lang.*;
import java.util.*;
class Rectangle
{
private double height;
private double width;
public double getHeight()
{
return height;
}
public double getWidth()
{
return width;
}
public void setHeight(double x)
{
if(x<=0)
{
System.out.println("Invalid Height");
System.exit(0);
}
else
{
height = x;
}
}
public void setWidth(double x)
{
if(x<=0)
{
System.out.println("Invalid Width");
System.exit(0);
}
else
{
width = x;
}
}
public double getArea()
{
double a;
a = height*width;
return a;
}
public double getPerimeter()
{
double p;
p = 2*(height+width);
return p;
}
}
class Utility
{
public void printLine()
{
for(int i=1;i<=40;i++)
{
System.out.print("=");
}
System.out.println();
}
public void printLine(char ch)
{
for(int i=1;i<=40;i++)
{
System.out.print(ch);
}
System.out.println();
}
public void printLine(char ch, int x)
{
for(int i=1;i<=x;i++)
{
System.out.print(ch);
}
System.out.println();
}
}
class RectTest7
{
public static void main(String args[])
{
double area, peri, x;
Rectangle r = new Rectangle();
Scanner input = new Scanner(System.in);
Utility util = new Utility();
System.out.print("Enter height : ");
x = input.nextDouble();
r.setHeight(x);
System.out.print("Enter width : ");
x = input.nextDouble();
r.setWidth(x);
area = r.getArea();
peri = r.getPerimeter();
util.printLine();
System.out.println("Height : "+r.getHeight());
util.printLine();
System.out.println("Width : "+r.getWidth());
util.printLine();
System.out.println("Area : "+area);
util.printLine();
System.out.println("Perimeter : "+peri);
util.printLine();
r = null;
}
}

Better way to assign value to array of object in Java

I have a class in Java which has 2 fields like
Class A
{
int i;
double v;
}
I make an array of object of class A like:
A[] x = new A[3];
After assigning the memory to object I assign value to object like:
A[0].i = 1;
A[0].v = 2.5;
A[1].i = 2;
A[1].v = 3.5;
A[2].i = 55;
A[2].v = 1.5;
I was wondering it there was a better way to initialize the object-values.
public class A {
int i;
double v;
public A(int ii, double dd) {
i = ii;
v = dd;
}
public static void main(String[] args) {
A[] a = new A[10]; // size
for (int i = 0; i < a.length; i++) {
a[i] = new A(1, 1.0);
}
}
}
You can also fill elements by this way:
A[] a = new A[] { new A(1, 2.5), new A(2, 3.5), new A(55, 1.5) };
Yes: use constructors:
A[] x = new A[]{new A(1, 2.5), ... };
Update: wrt. to comment below:
// Fake constructor
public static A new_A(int i, double v) {
A x = new A();
x.i = i;
x.v = v;
return x;
}
A[] x = new A[]{new_A(1, 2.5), ... };
Class A
{
private int i;
private double v;
void setI(int i){
this.i =i;
}
void setV(double v){
this.v =v;
}
}
After that assign values like A[0].setI(1);Also provide getters for the variables.
Go for setters\getters and a constructor.
class A {
int i;
double v;
public A(int i, double v) {
super();
this.i = i;
this.v = v;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public double getV() {
return v;
}
public void setV(double v) {
this.v = v;
}
}
And if you init it with those 3 values you might as well do:
A[] x = {new A(1,2.5), new A(2, 3.5), new A(55,1.5)};
Modify ur code as follows
Class A
{
private int i;
private double v;
public A(int x,double y)
{
i=x;
v=y;
}
}
class mainclass{
public static void Main(String []args)
{
A[] x = new A[3];
double i=1,v=2.5;
for(int i=0;i<2;i++)
{
x[i]=new A(i,v);
i+=1;
v+=1.0;
}
x[3]=new A(55,1.5);
}

Categories

Resources