How to debug "illegal start of expression" in Java? - java

In Jcreator, there are 12 errors here, I don't know how to fix it. It says "illegal start of expr....".
If I change something, suddenly, 50 errors more.
public class Practica_figura {
Class Figura() {
private float base;
private float altura;
private float radio;
}
public void asignar(float ba, float al, float ra) {
base = ba;
altura = al;
radio = ra;
}
class Cuadrado extends Figura()
{
private float base;
private float altura;
public void calcular_area(float b, float a) {
float res = base * altura;
}
public void calcular_perimetro(float a) {
float resp = 4 * a;
}
public void rareac() {
return area;
}
public void rperic() {
return perimetro;
}
}
class Triangulo extends Figura()
{
private float base;
private float altura;
private float la;
private float lb;
private float lc;
public void asignar(float a, float b, float c) {
la = a;
lb = b;
lc = c;
}
public void calcular_area(float b, float a) {
float res = (base * altura) / 2;
}
public void calcular_perimetro(float a) {
float resp = 4 * a;
}
public void rareat() {
return area;
}
public void rperit() {
return perimetro;
}
}
public static void main(String[] args) {
// TODO code application logic here
float base = 0, altura = 0, radio = 0;
JOptionPane.showMessageDialog(null, "Programa para calcular área y perímetro");
}
}

To have a correct class definition change
public class Practica_figura { Class Figura(){
to
public class Figura{
I don't really understand what you were trying to do, if what you wanted was a constructor then you would have to add:
public Figura(){
}

First, the class is not quite defined properly.
public class Practica_figura
{
private float base;
private float altura;
private float radio;
public Practica_figura()
{
}
public void asignar(float ba, float al, float ra)
{
base = ba;
altura = al;
radio = ra;
}
//edits assuming you want inner classes
class Figura
{
private float base, altura, radio;
// you will need getters/setters for the variables
}
class Cuadrado extends Figura
{
// insert class logic here
}
class Triangulo extends Figura
{
// insert class logic here
}
}
Second, you are shadowing variables in your child classes, and this approach may make things confusing.

Related

How do I compare 2 objects with 1 parameter?

I made a java program that compares the distance between 2 objects
I managed to figure out how to solve it when I make a class with 2 parameters and compare these with the formula seen below;
public class RasterServices {
public static double distance (SimpleRasterElement a, SimpleRasterElement b) {
double d;
d = Math.sqrt(((b.x-a.x)*(b.x-a.x)) + ((b.y-a.y)*(b.y-a.y)));
return d;
}
public class SimpleRasterElement {
public int id;
public double x;
public double y;
public double height;
}
public class SimpleRasterElementTest {
public static void main (String[] args)
{
SimpleRasterElement a, b ; // Deklarera variabeln
a = new SimpleRasterElement (); // Skapa en instans (med ’new’),
b = new SimpleRasterElement ();
// Tilldela variablerna i ’a’ värden:
a.id = 1;
a.x = 6.0;
a.y = 8.0;
a.height = 10.5;
// Tilldela variablerna i ’b’ värden:
b.id = 1;
b.x = 9.0;
b.y = 12.0;
b.height = 15.5;
System.out.println (RasterServices.distance(a,b));
}
}
I can then test this via using my test program RasterServices.distance(a,b)
But now I want to make my variables private, use getters and create the distance() method within the RasterElement-class, and now I'm hardstuck.
public class RasterElementTest {
public static void main(String[] args) {
RasterElement re_a = new RasterElement(1, 6.0, 8.0, 10.5);
RasterElement re_b = new RasterElement(1, 9.0, 12.0, 15.5);
double d = re_a.distance(re_b);
System.out.println(d);
}
}
asd
public class RasterElement {
private final int id;
private final double x;
private final double y;
private final double height;
public RasterElement (int id_nr, double x_val, double y_val, double height_val) {
id = id_nr;
x = x_val;
y = y_val;
height = height_val;
}
public int getId () {
return id;
}
public double getX () {
return x;
}
public double getY () {
return y;
}
public double getHeight () {
return height;
}
public double distance (RasterElement a) {
double d;
d = Math.sqrt(((b.getX()-a.getX())*(b.getX()-a.getX())) + ((b.getY()-a.getY())*b.getY()-a.getY()));
return d;
}
}
But here in distance() i'm only allowed ONE parameter, can someone please explain to me how I can compare two elements/objects when I'm only allowed one parameter in the function?
(By using re_a.distance(re_b); in my test-code)
Thanks, sorry for the long post B-)
(how do I get the b-value into the equation in the method distance in the class RasterElement..?)
Change b to this. Also, you can eliminate d and return directly. Like,
public double distance (RasterElement a) {
return Math.sqrt(((this.getX()-a.getX())*(this.getX()-a.getX()))
+ ((this.getY()-a.getY())*this.getY()-a.getY()));
}

How to make a singleton reinitialize variables?

My singleton class:
public class XandY {
private double x, y;
private static XandY xy;
//Constructor sets an x and y location
private XandY() {
x = 210.0;
y = 100.0;
}
public static XandY getXandY() {
if (xy == null)
xy = new XandY();
return xy;
}
public void updateXandY() {
x += 10;
y += 5;
}
}
Other class that changes singleton values and tries to reinitialize. My question is if I call changeXandY a few times then want to call resetXandY how do I make it reset back to the original x and y?
public class GameWorld {
private List<GameObject> objects;
public void initialize() {
objects = new ArrayList<GameObject>();
objects.add(XandY.getXandY());
...add other objects that are not singletons
}
public void changeXandY {
for (int i=0; i<gameObject.size(); i++) {
if (gameObject.get(i) instanceof XandY)
((XandY)gameObject.get(i)).updateXandY();
}
public void resetXandY {
initialize();
}
}
For this use case, you could simply store them as default values. Such as
private double x, y;
private static XandY xy;
private static final double default_x = 210.0;
private static final double default_y = 100.0;
That way when you reset, just:
public void resetXandY {
this.x = default_x;
this.y = default_y;
}
That being said, you may want to change your default constructor to look the same way.
If you can make the XandY reference protected, you can use a static initializer in an anonymous subclass:
// I need to reset the singleton!
new XandY(){
{ xy = null; }
};
But really, if you need to be able to (re)initialize the singleton, you should put a method to that effect into its signature. Obscure solutions are, at best, still obscure...
Create a resetXandY() method to set default value:
public class XandY {
private double x, y;
private static XandY xy;
//Constructor sets an x and y location
private XandY() {
x = 210.0;
y = 100.0;
}
//reset x=0 and y=0
public void resetXandY() {
x = 0;
y = 0;
}
public static XandY getXandY() {
if (xy == null)
xy = new XandY();
return xy;
}
public void updateXandY() {
x += 10;
y += 5;
}
}

How to Implement modifiable activation function in the neuron class in java?

I'm learning the concept of neural networks. I decided to try making the neuron class by myself. What is the best way to implement different activation functions in my code? Now it uses only the binary step function.
It's my first try in coding neural networks so if you have any suggestions about my code, or it is completely dumb, please let me know.
Here is my code:
public class Neuron {
// properties
private ArrayList<Neuron> input;
private ArrayList<Float> weight;
private float pot, bias, sense, out;
private boolean checked;
// methods
public float fire(){
pot = 0f;
if (input != null) {
for (Neuron n : input){
if (!n.getChecked()){
pot += n.fire()*weight.get(input.indexOf(n));
} else {
pot += n.getOut()*weight.get(input.indexOf(n));
} // end of condition (checked)
} // end of loop (for input)
} // end of condition (input exists)
checked = true;
pot -= bias;
pot += sense;
out = actFunc(pot);
return out;
} // end of fire()
// getting properties
public float getPot(){return pot;}
public boolean getChecked(){return checked;}
public float getOut(){return out;}
// setting properties
public void stimulate(float f){sense = f;}
public void setBias(float b){bias = b;}
public void setChecked(boolean c){checked = c;}
public void setOut(float o){out = o;}
// connection
public void connect(Neuron n, float w){
input.add(n);
weight.add(w);
}
public void deconnect(Neuron n){
weight.remove(input.indexOf(n));
input.remove(n);
}
// activation function
private float actFunc(float x){
if (x < 0) {
return 0f;
} else {
return 1f;
}
}
// constructor
public Neuron(Neuron[] ns, float[] ws, float b, float o){
if (ns != null){
input = new ArrayList<Neuron>();
weight = new ArrayList<Float>();
for (Neuron n : ns) input.add(n);
for (int i = 0; i < ws.length; i++) weight.add(ws[i]);
} else {
input = null;
weight = null;
}
bias = b;
out = o;
}
public Neuron(Neuron[] ns){
if (ns != null){
input = new ArrayList<Neuron>();
weight = new ArrayList<Float>();
for (Neuron n : ns) input.add(n);
for (int i = 0; i < input.size(); i++) weight.add((float)Math.random()*2f-1f);
} else {
input = null;
weight = null;
}
bias = (float)Math.random();
out = (float)Math.random();
}
}
First, define interface of any activation function:
public interface ActivationFunction {
float get(float f);
}
Then write some implementations:
public class StepFunction implements ActivationFunction {
#Override
public float get() {return (x < 0) ? 0f : 1f;}
}
public class SigmoidFunction implements ActivationFunction {
#Override
public float get() {return StrictMath.tanh(h);}
}
Finally, set some implementation to your Neuron:
public class Neuron {
private final ActivationFunction actFunc;
// other fields...
public Neuron(ActivationFunction actFunc) {
this.actFunc = actFunc;
}
public float fire(){
// ...
out = actFunc.get(pot);
return out;
}
}
as following:
Neuron n = new Neuron(new SigmoidFunction());
Note, neural netoworks are using signal propagation through neurons, where weights are produced. Computing of weight depends also on first derivative of an activation function. Therefore, I would extend ActivationFunction by method, which will return first derivative at specified point x:
public interface ActivationFunction {
float get(float f);
float firstDerivative(float x);
}
So the implemenations will look like:
public class StepFunction implements ActivationFunction {
#Override
public float get(float x) {return (x < 0) ? 0f : 1f;}
#Override
public float firstDerivative(float x) {return 1;}
}
public class SigmoidFunction implements ActivationFunction {
#Override
public float get(float x) {return StrictMath.tanh(x);}
// derivative_of tanh(x) = (4*e^(2x))/(e^(2x) + 1)^2 == 1-tanh(x)^2
#Override
public float firstDerivative(float x) {return 1 - Math.pow(StrictMath.tanh(x), 2);}
}
Then, use actFunction.firstDerivative(x); in fire() method where weight is being computed.

Return value in a class from another class and method

Note that I am new with Java. I have a class and and a method in this class hat computes some values and I want to return them in the first class. I have a code but it returns 0.
That's in class that computes the values
public class ImageProcessing{
int i,j;
int R[][]=new int[640][320];
int G[][]=new int[640][320];
int B[][]=new int[640][320];
public double meanR[]=new double[320];
public double meanG[]=new double[320];
public double meanB[]=new double[320];
public double varianceR[]=new double[320];
public double varianceG[]=new double[320];
public double varianceB[]=new double[320];
public double skewnessR[]=new double[320];
public double skewnessG[]=new double[320];
public double skewnessB[]=new double[320];
public double round(double value){
// int decimalPlace = 2;
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(2,BigDecimal.ROUND_UP);
return (bd.doubleValue());
}
public void Mean(Bitmap image){
int width = image.getWidth();
int height = image.getHeight();
int pixel = 0;
for (i=0; i<width;i++){
for (j=0; j<height; j++){
pixel = image.getPixel(i,j);
R[i][j] = Color.red(pixel);
G[i][j]= Color.green(pixel);
B[i][j] = Color.blue(pixel);
meanR[j]=meanR[j]+R[i][j];
meanG[j]=meanG[j]+G[i][j];
meanB[j]=meanB[j]+B[i][j];
}
}
In the main class I have:
method.Mean(rescaledBitmap1);
meanR1=method.meanR;
meanG1=method.meanG;
meanB1=method.meanB;
System.out.println(meanR1);
You must specify what you want to return in the definition of the method and actually return a value using the keyword return:
public int sum(int a, int b){
int result = a + b;
return result;
}
You've declared Mean() as void public void Mean(Bitmap image) and thus it returns no value.
Also, you can only return 1 variable so you should either put those 3 values in some sort of array or create a new class and encapsulate the variables inside an object. Here is an example:
public class MeanResult(){
private double meanR[]=new double[320];
private double meanG[]=new double[320];
private double meanB[]=new double[320];
//Maybe declare more stuff here
public MeanResult(Bitmap image){
//... code n stuff here to calculate width, height and pixel
}
public double getMeanR(){ return this.meanR[]; }
public double getMeanG(){ return this.meanG[]; }
public double getMeanB(){ return this.meanB[]; }
}
And you can use it like this:
MeanResult mean = new MeanResult(image);
meanR1=mean.getMeanR();
meanG1=mean.getMeanG();
meanB1=mean.getMeanB();
The method mean(Bitmap image) is void. For it to return something, you must change void to what it will return, and at the end of the method return the variable.
Example:
public int add(int x, int y)
{
return x + y;
}
Then if you called add(1,2) it would have a value of 3.

Is there a way to ensure at compile time that certain fields in an immutable object are set whilst keeping role of the arguments clear?

I'm experimenting with ways of creating immutable objects. The following builder objects
are quite attractive because they keep the role of the arguments clear. However I would like
to use the compiler to verify that certain fields are set, like with the Immutable() constructor invocation. StrictImmutableBuilder provides those checks, but is rather noisy. Is there some way to get the same checks but with the form of LaxImmutableBuilder? Perhaps using annotations?
public class Immutable {
public static void main(String[] args) {
new Immutable("13272873C", 23, false);
// nice but what where those arguments?
new LaxImmutableBuilder() {{
refCode("13272873C");
age(23);
subscribed(false);
}}.build();
// now I know what each value represents
// but what if I forgot to set one?
new StrictImmutableBuilder() {
public String refCode() { return "13272873C"; }
public int age() { return 23; }
public boolean subscribed() { return false; }
}.build();
// now I'm forced to set each field, but now
// we have the extra noise of "return"
// and also "public" if we want to use
// this outside the current package
// is there another way? maybe using annotations?
}
private final String refCode;
private final int age;
private final boolean subscribed;
public String getRefCode() {
return refCode;
}
public int getAge() {
return age;
}
public boolean isSubscribed() {
return subscribed;
}
public Immutable(String a, int b, boolean c) {
this.refCode = a;
this.age = b;
this.subscribed = c;
}
}
abstract class StrictImmutableBuilder {
public abstract String refCode();
public abstract int age();
public abstract boolean subscribed();
public Immutable build() {
return new Immutable(refCode(), age(), subscribed());
}
}
abstract class LaxImmutableBuilder {
private String refCode;
private int age;
private boolean subscribed;
protected void refCode(String refCode) {
this.refCode = refCode;
}
protected void age(int age) {
this.age = age;
}
protected void subscribed(boolean subscribed) {
this.subscribed = subscribed;
}
public Immutable build() {
return new Immutable(refCode, age, subscribed);
}
}
Here's the pattern I use:
class YourClass {
// these are final
private final int x;
private final int y;
private int a;
private int b;
// finals are passed into the constructor
private YourClass(int x, int y) {
this.x = x;
this.y = y;
}
public static class Builder {
// int x, int y, int a, int b
// whatever's final is passed into constructor
public Builder(int x, int y) {
this.x = x;
this.y = y;
}
// a and b are optional, so have with() methods for these
public Builder withA(int a) {
this.a = a;
return this;
}
public Builder withB(int b) {
this.b = b;
return this;
}
public YourClass build() {
YourClass c = new YourClass (x, y);
c.a = a;
c.b = b;
return c;
}
}
}
there is this trick: Type-safe Builder Pattern
http://michid.wordpress.com/2008/08/13/type-safe-builder-pattern-in-java/
but that's just too crazy.

Categories

Resources