Sign in to follow this  
HelloSkitty

[java] Redundancy

Recommended Posts

HelloSkitty    152
Everything here is in the context of package M.

Suppose I have a class M, with a member variable of instance ArrayList<C> called C.

So

class M {
ArrayList<C> C;
}

This is legal and will compile.

Now say my class C has a static method called i whose header is

public static void i(C a,C b,ArrayList<C> c);

Naturally, the way I would call this method in the M class is the following line

C.i(this.C.get(i),this.C.get(j),this.C);

Where the first C is a reference to the static class C, and the other C's refer to the member variable ArrayList<C> C. Unfortunately, this will not compile, because the compiler thinks that the first C is also a reference to the member C variable.

So typically, when I want to access something from a package without importing it, I'll do this:

(M.C).i(this.C.get(i),this.C.get(j),this.C);

This doesn't work either, because now I'm referring to a non-static variable in a static context (it thinks the M is the class M, rather than the package M).


So is there any way to call the static i method from class C, and not the missing i method from the ArrayList class that the compiler thinks I'm trying to do? I know this is rather ambiguous, hence the title, so I'll be happy if anyone even understands my problem let alone knows how to fix it (when I say "fix", I mean without requiring me to change variable names). The entirety of the two classes are below.

[code]

package M;

import N.*;
import java.awt.Polygon;
import java.awt.Color;
import java.awt.Graphics;

public class C extends Polygon {

int t;
final int W=0;
final int O=1;
final int P=2;
final int G=1;
final int B=-1;
int x,y;
double r=0,d=0,f=0.9;
double R=0,D=0;
double X,Y;
Color c=Color.BLACK;

public C() {
super();
}

public C(int[] x,int[] y,int n) {
super(x,y,n);
}

public void c(Color c) {
this.c=c;
}

public void w() {
t=W;
}

public void a(boolean a) {
if (a) t*=G;
else t*=B;
}

public void t(boolean t) {
if (t) this.t=O;
else this.t=P;
}

public void l(int x,int y) {
this.x=x;
this.y=y;
this.X=(double)x;
this.Y=(double)y;
}

public void v(int r,int d) {
this.r=r;
this.d=d;
}

public void m(double R,double D) {
this.R=R;
this.D=D;
}

public void m(double R,N n) {
this.R=R;
}

public void m(N n,double D) {
this.D=D;
}

public void d(Graphics g) {
g.setColor(c);
g.fillPolygon((Polygon) this);
}

public void l(int n) {
r+=R*n/1000;
d+=D*n/1000;
X+=n*r/1000;
Y+=n*d/1000;
translate((int)X-x,(int)Y-y);
x=(int)X;
y=(int)Y;
r*=(1-(1-f)*n/1000);
d*=(1-(1-f)*n/1000);
b();
}

public void b() {
if (x<0) {
r=Math.abs(r);
}
if (x>400) {
r=-Math.abs(r);
}
if (y<0) {
d=Math.abs(d);
}
if (y>400) {
d=-Math.abs(d);
}
}

public static void i(C a,C b, java.util.ArrayList<C> c) {
//write later
}

}
[/code]


[code]package M;

import D.*;
import java.util.ArrayList;
import java.awt.Graphics;

public class M extends D implements A, R {

public ArrayList<C> C=new ArrayList<C>();
int e=1000;
int c=0;

public M() {
int[] x={195,205,205,195};
int[] y={195,195,205,205};
int n=4;
C m=new C(x,y,n);
m.a(true);
m.t(true);
m.l(200,200);
C.add(m);
}

public void d() {
d(i.getGraphics());
}

public void d(Graphics g) {
super.d(g);
for (C c:C) {
c.d(g);
}
}

public void l(int n) {
for (C c:C) {
c.l(n);
}
c+=n;
if (c>this.e) {
c=0;
int X=Math.abs((new java.util.Random()).nextInt()%400);
int Y=Math.abs((new java.util.Random()).nextInt()%400);
int[] x={X-5,X+5,X+5,X-5};
int[] y={Y-5,Y-5,Y+5,Y+5};
E e=new E(x,y,4);
e.t(true);
e.a(false);
e.l(X,Y);
C.add(e);
}
int l=C.size();
for (int i=0;i<l;i++) {
for (int j=i+1;j<l;j++) {
(M.C).i(this.C.get(i),this.C.get(j),this.C);
}
}
}

public void w(boolean b) {
if(b) C.get(0).m(null,-100);
else C.get(0).m(null,0);
}

public void a(boolean b) {
if(b) C.get(0).m(-100,null);
else C.get(0).m(0,null);
}

public void s(boolean b) {
if(b) C.get(0).m(null,100);
else C.get(0).m(null,0);
}

public void d(boolean b) {
if(b) C.get(0).m(100,null);
else C.get(0).m(0,null);
}

public void p(int x,int y) {
int[] X={C.get(0).x-2,C.get(0).x+2,C.get(0).x+2,C.get(0).x-2};
int[] Y={C.get(0).y-2,C.get(0).y-2,C.get(0).y+2,C.get(0).y+2};
int n=4;
C p=new C(X,Y,n);
p.l(C.get(0).x,C.get(0).y);
p.a(true);
p.t(false);
p.v((x-C.get(0).x)/2,(y-C.get(0).y)/2);
p.m((x-C.get(0).x)/5,(y-C.get(0).y)/5);
C.add(p);
}

public void d(int x,int y,boolean d) {

}

}[/code]

Pretty much my problem is I know how to distinguish between global class variables and local method variables (this.x vs. x), but I don't know how to distinguish between packages and classes, or classes and member variables. Thanks in advance!

P.S. And yes, I am using only single letter everythings in my code and no documentation. Long story behind the dare.

Share this post


Link to post
Share on other sites
rip-off    10976
Package names are traditionally lower case, which might help a little (if you are allowed).

Does using a fully qualified name disambiguate? M.C.i(this.C.get(i), this.C.get(j), this.C)?

Share this post


Link to post
Share on other sites
HelloSkitty    152
I tried this:

[color=#1C2837][size=2](M.C).i(this.C.get(i),this.C.get(j),this.C);[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]and [/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]M.C.i(this.C.get(i),this.C.get(j),this.C);[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]And both result in the compiler error[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]
M\M.java:55: non-static variable C cannot be referenced from a static context
M.C.i(this.C.get(i),this.C.get(j),this.C);
^
M\M.java:55: cannot find symbol
symbol : method i(M.C,M.C,java.util.ArrayList<M.C>)
location: class java.util.ArrayList<M.C>
M.C.i(this.C.get(i),this.C.get(j),this.C);
^
2 errors

I would think that packages would be stronger than classes and M would be seen as a package instead of a class, but that doesn't seem to be the case. I'll change the package names to lower case and it should fix it, but I'm still curious if there's a way to specifically tell java using some keyword that something refers to a class or package...[/size][/color]

Share this post


Link to post
Share on other sites
CaptainJester    523
Don't name a variable the same name as a class or it will confuse the compiler. This is a horrible practice. Also using 1 letter variable, method and class names is also heavily discouraged. If your goal is to try and have a small program, you can include an obfuscator like ProGuard that will reduce your output class file size.

Share this post


Link to post
Share on other sites
rip-off    10976
[quote]
Don't name a variable the same name as a class or it will confuse the compiler. This is a horrible practice. Also using 1 letter variable, method and class names is also heavily discouraged. If your goal is to try and have a small program, you can include an obfuscator like ProGuard that will reduce your output class file size.
[/quote]
The OP explained that this is for a "dare".

That said, the obfuscator isn't a bad idea. You write your program normally. You then find/write a second program that generates the "short hand" from Java source/class files. Finally, run the latter on the former!

If there are any open source Java source obfuscators, they might help as a base on which to build.

Share this post


Link to post
Share on other sites
CaptainJester    523
[quote name='rip-off' timestamp='1312929779' post='4846935']
[quote]
Don't name a variable the same name as a class or it will confuse the compiler. This is a horrible practice. Also using 1 letter variable, method and class names is also heavily discouraged. If your goal is to try and have a small program, you can include an obfuscator like ProGuard that will reduce your output class file size.
[/quote]
The OP explained that this is for a "dare".

That said, the obfuscator isn't a bad idea. You write your program normally. You then find/write a second program that generates the "short hand" from Java source/class files. Finally, run the latter on the former!

If there are any open source Java source obfuscators, they might help as a base on which to build.
[/quote]

I missed the dare part.

ProGuard is in SourceForge so it is opensource.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this