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;
}
Hello I'm trying to do a little java course and it tells me to make a little program with a class(Point) that creates X, Y coordinates.
In a second part it asks me to make an array of an object of said class. My attempt was to create a method in it's own class to create it, I don't know if it's possible(or useful) and couldn't find a way to phrase it to google that gave me an answer I could understand.
Any way to improve the little thing I made is greatly appreciated.
import folderPointer.Point;
public class MainFile {
public static void main(String[] args) {
System.out.println("\n\n\n");
Point firstPoint = new Point();
firstPoint.printPoint();
System.out.println("\n\n");
Point secondPoint[] = secondPoint.fillPArray();
secondPoint = secondPoint.fillPArray();
}
}
edit* Missed my main
Point secondPoint[] = secondPoint.fillPArray();
secondPoint = secondPoint.fillPArray();
this part does not work.
package folderPointer;
import java.util.Random;
public class Point {
private int coordX;
private int coordY;
public Point() {
coordX = 0;
coordY = 0;
}
public Point(int coordX, int coordY) {
this.coordX = coordX;
this.coordY = coordY;
}
public Point(Point o) {
this.coordX = o.coordX;
this.coordY = o.coordY;
}
public void printPoint() {
System.out.println("Coord X: " + this.coordX + ", CoordY:" + this.coordY);
}
public void modifyPoint(int coordX, int coordY) {
this.coordX = coordX;
this.coordY = coordY;
}
private Point[] createPArray() {
Random ran = new Random();
Point[] toReturn = new Point[ran.nextInt(19) + 1];
return toReturn;
}
public Point[] fillPArray() {
Point[] filledPoint = createPArray();
Random randInt = new Random();
for (int i = 0; i < filledPoint.length; i++) {
filledPoint[i].coordX = randInt.nextInt(100);
filledPoint[i].coordY = randInt.nextInt(100);
}
return filledPoint;
}
}
If all you are trying to do is create an array of Point objects, I found that an easy way to do this is to make the "Point" class an inner class, initialize a Point array with:
Point[] pointArray = new Point[3];
I then made a few example points followed by a for loop to iterate through and print the results.
import java.util.Random;
public class PointHelp {
public static void main(String[] args) {
Point[] pointArray = new Point[3];
pointArray[0] = new Point(3, 4);
pointArray[1] = new Point(4, 5);
pointArray[2] = new Point(4,7);
for(int i = 0; i < pointArray.length; i++){
pointArray[i].printPoint();
}
}
}
class Point {
private int coordX;
private int coordY;
public Point() {
coordX = 0;
coordY = 0;
}
public Point(int coordX, int coordY) {
this.coordX = coordX;
this.coordY = coordY;
}
public Point(Point o) {
this.coordX = o.coordX;
this.coordY = o.coordY;
}
public void printPoint() {
System.out.println("Coord X: " + this.coordX + ", CoordY:" + this.coordY);
}
public void modifyPoint(int coordX, int coordY) {
this.coordX = coordX;
this.coordY = coordY;
}
private Point[] createPArray() {
Random ran = new Random();
Point[] toReturn = new Point[ran.nextInt(19) + 1];
return toReturn;
}
public Point[] fillPArray() {
Point[] filledPoint = createPArray();
Random randInt = new Random();
for (int i = 0; i < filledPoint.length; i++) {
filledPoint[i].coordX = randInt.nextInt(100);
filledPoint[i].coordY = randInt.nextInt(100);
}
return filledPoint;
}
}
The output for this is as follows:
Coord X: 3, CoordY:4
Coord X: 4, CoordY:5
Coord X: 4, CoordY:7
I'm new to processing/Java and I try to make a little game ...
I already created a class for making something like a map or a layout, I called it Map Class, it worked fine and I could use arrays/ images or just objects to create a "Map".
So now I want to add some units into my game. They shouldn't be so difficult or complex. I just want to move them, heres my class for units :
class Unit
{
int X;
int Y;
int Breite;
int Laenge;
boolean ausgewaelht = false;
Unit()
{
}
Unit(int x, int y, int breite, int laenge)
{
}
/-------------------------------------------------------------------
void create(UnitContent function)
{
function.form();
}
void move(float geschwindigkeit)
{
if(isTriggerd(X,Y,Breite,Laenge) == true){
X = X+(int)geschwindigkeit;
if(X > width)
{
X = 0;
}
}
}
void setXandY(int x , int y)
{
X = x;
Y = y;
}
void setBreiteandLaenge(int breite, int laenge)
{
Breite = breite;
Laenge = laenge;
}
/--------------------------------------------------------------------
int getX()
{
return X;
}
int getY()
{
return Y;
}
int getBreite()
{
return Breite;
}
int getLaenge()
{
return Laenge;
}
/--------------------------------------------------------------------
boolean isTriggerd(int x, int y, int breite, int laenge)
{
if(mouseX > x && mouseX < x+breite && mouseY > y && mouseY < y+laenge )
{
return true;
}
else
{
return false;
}
}
}
If I only use 1 Unit it works fine ^ I can personally select outside the class in what kind of form my unit draws on the screen... But how do I draw 10 of them?
Heres my interface UnitContent:
interface UnitContent
{
void form();
}
And heres my MainClass
Map testKarte;
Unit[] TestEinheit = new Unit[100];
int[][] array = {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}};
int a = 0;
void setup()
{
size(500,500);
testKarte = new Map(1,1,"",50,50);
for(int i = 0; i < TestEinheit.length; i++)
{
TestEinheit[i] = new Unit();
TestEinheit[i].setXandY(0,0);
TestEinheit[i].setBreiteandLaenge(50,50);
}
}
void draw()
{
background(#FFFFFF);
testKarte.setCisClickable(true);
testKarte.setIsRectOrPicture(true);
testKarte.ubdate(new MapContent(){
public void setMapContent(int a, int b, int c, int d){
// rect(a,b,c,d);
for( int i = 0; i < TestEinheit.length; i++)
{
TestEinheit[i].create(new UnitContent()
{
public void form()
{
rect(TestEinheit[i].getX(),TestEinheit[i].getY(),TestEinheit[i].getBreite(),TestEinheit[i].getLaenge());
}
});
}
}
public void setMapContentMouseOver(){
}
public void elseMapContentMouseOver()
{
// wenn die Maus nicht über dem Object ist
// ebenfalls nicht unbedingt
}});
}
void mousePressed()
{
}
When I try following :
for( int i = 0; i < TestEinheit.length; i++)
{
TestEinheit[i].create(new UnitContent()
{
public void form()
{
rect(TestEinheit[i].getX(),TestEinheit[i].getY(),TestEinheit[i].getBreite(),TestEinheit[i].getLaenge());
}
});
It gives me following error: Cannot refer to the non final local variavle I definied in an enclosing scope
By the way, the public void form is just an interface method :)
Anyone got an idea how to handle this ?
im searching a way to handle that... how can i do that without that error?
You have a couple of options, depending on what you want the form method to see
The value of i as it is when the UnitContent is created, or
The value of i as it is when UnitContent#form is called later
The value of i when UnitContent is created
You'll need a subclass of UnitContent that accepts i as an argument in the constructor, stores it on a field, and uses it in form.
E.g.:
abstract class MyUnitContent extends UnitContent {
protected int i;
public MyUnitContent(int i) {
this.i = i;
}
}
and
TestEinheit[i].create(new MyUnitContent(i) {
public void form() {
Unit entry = TestEinheit[this.i];
rect(entry.getX(), entry.getY(), entry.getBreite(), entry.getLaenge());
}
});
(Note that if TestEinheit is a local, it must be final.)
The value of i when UnitContent#form is called later
The restriction is you can only access final variables; it doesn't necessarily follow that those variables can't refer to objects with mutable state.
So you could make i an array, for instance:
final int[] i = new int[1];
for (i[0] = 0; i[0] < TestEinheit.length; i[0]++) {
TestEinheit[i].create(new UnitContent() {
public void form() {
Unit entry = TestEinheit[i[0]];
rect(entry.getX(), entry.getY(), entry.getBreite(), entry.getLaenge());
}
});
}
Now, when form is called, it will read the value i[0] has when it's called (in the code above, it'll be TestEinheit.length for all of them).
(Note that if TestEinheit is a local, it must also be final.)
I wrote this java code with synchronized and busy waiting but I don't know why it doesn't work right! for example I get this output
produced:6
produced:7
consumed:0
consumed:0
how 6 and 7 are produced but 0 is consumed ??? I don't know what's wrong with it,please help me ,here is my code:
package conper;
import java.util.*;
import java.util.concurrent.TimeUnit;
class Queue<Integer> {
int items=0;
int q=0;
public int[] array= new int[10];
Queue(int i) {
}
public boolean isEmpty(){
if (this.q > 0)
{
return false ;
}else{
return true ;}
}
public boolean isFull(){
if (this.q > 10)
{
return true ;
}else{
return false ;
}
}
public int size(){
return items;
}
public synchronized void add(int x){
if(!isFull()){
array[++q]=x;
}
}
public synchronized void remove(int x){
if(!isEmpty()){
array[--q]=x;
}
}
}
class Producer implements Runnable {
private Random random = new Random();
private Queue<Integer> queue;
private boolean working = true;
public Producer(Queue<Integer> q) {
queue = q;
}
public void run() {
int product=0;
int loop = random.nextInt(10) + 20;
for (int i = 0; i < loop; i++) {
double h = Math.pow(2, Math.E * i);
}
while(working){
product = produce();
if(!queue.isFull() ){
break;
}
}
queue.add(product);
System.out.println("produced:"+product);
}
public void stop() {
working = false;
}
private int produce() {
int result = 0;
int loop = random.nextInt(10) + 20;
for (int i = 0; i < loop; i++) {
double h = Math.pow(2, Math.E * i);
}
result = random.nextInt(10);
return result;
}
}
class Consumer implements Runnable {
private Random rnd = new Random();
private Queue<Integer> queue;
private boolean working = true;
public Consumer(Queue<Integer> q) {
queue = q;
}
public void run() {
int product = 0;
while(working){
if(!queue.isEmpty())
break;
}
queue.remove(product);
System.out.println("consumed:"+product);
consume(product);
}
public void stop() {
working = false;
}
public void consume(int product) {
int loop = rnd.nextInt(10000) + 2000;
for (int i = 0; i < loop; i++) {
double h = Math.pow(2, Math.E * i);
}
}
}
public class Main {
public static void main(String[] args) {
Queue<Integer> queue = new Queue<Integer>(5);
Producer p1 = new Producer(queue);
Producer p2 = new Producer(queue);
Consumer c1 = new Consumer(queue);
Consumer c2 = new Consumer(queue);
Thread pt1 = new Thread(p1);
Thread pt2 = new Thread(p2);
Thread ct1 = new Thread(c1);
Thread ct2 = new Thread(c2);
pt1.start();
pt2.start();
ct1.start();
ct2.start();
try {
TimeUnit.MILLISECONDS.sleep(200);
p1.stop();
p2.stop();
c1.stop();
c2.stop();
}
catch (Exception e) {
return;
}
}
}
any suggestion?
Your consumer code reads
int product = 0;
// code which doesn't change "product"
System.out.println("consumed:"+product);
I suggest you need a remove() method which returns the value from the queue instead of being passed a value.
I also suggest you get the code to work in one thread before attempting to use in multiple threads.
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.