Simple instance variable issue - java

I'm working on a small personal project involving elliptic curves, and I'm having a bit of difficulty with the curve's instance variables. The variables are printed correctly in the main method, but the print method always returns that each variable is equal to 0. Does anyone see a way to fix this? Please bear with me, I know that this is a fairly trivial problem.
public class ellipticcurve {
public int A, B, p;
public ellipticcurve(int A, int B, int p) {
A = this.A;
B = this.B;
p = this.p;
// E:= Y^2 = X^3 + AX + B
}
public static boolean isAllowed(int a, int b, int p) {
return ((4*(Math.pow(a, 3)) + 27*(Math.pow(b, 2)))%p != 0);
}
public static void printCurve(ellipticcurve E) {
System.out.println("E(F" + E.p + ") := Y^2 = X^3 + " + E.A + "X + " + E.B + ".");
}
public static void main(String[] args) {
ArgsProcessor ap = new ArgsProcessor(args);
int a = ap.nextInt("A-value:");
int b = ap.nextInt("B-value:");
int p = ap.nextInt("Prime number p for the field Fp over which the curve is defined:");
while (isAllowed(a, b, p) == false) {
System.out.println("The parameters you have entered do not satisfy the "
+ "congruence 4A^3 + 27B^2 != 0 modulo p.");
a = ap.nextInt("Choose a new A-value:");
b = ap.nextInt("Choose a new B-value:");
p = ap.nextInt("Choose a new prime number p for the field Fp over which the curve is defined:");
}
ellipticcurve curve = new ellipticcurve(a, b, p);
System.out.println(curve.A + " " + curve.B + " " + curve.p);
printCurve(curve);
System.out.println("The elliptic curve is given by E(F" + p
+ ") := Y^2 = X^3 + " + a + "X + " + b + ".");
}

In your constructor it should be in this way.
public ellipticcurve(int A, int B, int p) {
this.A = A;
this.B = B;
this.p = p;
// E:= Y^2 = X^3 + AX + B
}
instead of
public ellipticcurve(int A, int B, int p) {
A = this.A;
B = this.B;
p = this.p;
// E:= Y^2 = X^3 + AX + B
}
You are assigning the instance variable to the variable passed in the constructor so the instance variable will be initialized to their default value

Related

Java : Add method Generic Class

I'm new to Java. The Following Code is to create a generic class to generate complex numbers from real and Imaginary Parts. The add() method in the class is throwing the following Error.
Not sure how to proceed further. I have been at this for a day. Error Prompt
import java.util.*;
class ComplexNum<T>{
T i;
T r;
public ComplexNum (T r , T i){
this.r = r;
this.i = i;
}
public ComplexNum add(ComplexNum c2)
{
return new ComplexNum (r + c2.r, i +c2.i);
}
#Override
public String toString() {
return this.r + " + " + this.i + "i";
}
}
class Main{
public static void main(String[] args) {
int n1 = 1;
int n2 = 3;
double d1 =4.4;
double d2 = 5.4;
ComplexNum<Integer> c1 = new ComplexNum<Integer>(n1, n2);
ComplexNum<Double> c2 = new ComplexNum<Double>(d1, d2);
ComplexNum<Double> c3 = c1.add(c2);
System.out.println(c1 + " + " + c2 + " = "+ c3) ;
}
}
static class ComplexNum<T extends Number> {
T i;
T r;
public ComplexNum(T r, T i) {
this.r = r;
this.i = i;
}
public ComplexNum<Double> add(ComplexNum c2) {
Double newr = r.doubleValue() + c2.r.doubleValue();
Double newi = i.doubleValue() + c2.i.doubleValue();
return new ComplexNum<>(newr, newi);
}
#Override
public String toString() {
return this.r + " + " + this.i + "i";
}
}
class Main {
public static void main(String[] args) {
int n1 = 1;
int n2 = 3;
double d1 = 4.4;
double d2 = 5.4;
ComplexNum<Integer> c1 = new ComplexNum<>(n1, n2);
ComplexNum<Double> c2 = new ComplexNum<>(d1, d2);
ComplexNum<Double> c3 = c1.add(c2);
System.out.println(c1 + " + " + c2 + " = " + c3);
}
}

Java Interaction not returning

I'm making code in Java to put in numbers for variables a, b, c, and a label that calculates dimensions for a ellipsoid. Typing in Ellipsoid ex1 = new Ellipsoid ("Ex 1", 1, 2, 3); should give me the following in Interactions:
Ellipsoid "Ex 1" with axes a = 1.0, b = 2.0, c = 3.0 units has:
volume = 25.1327 cubic units
surface area = 48.9366 square units
But it gives me nothing. It's blank. I suspect that the last return statement at the bottom is coded wrong, but I can't figure out what exactly I'm doing wrong.
import java.text.DecimalFormat;
import java.util.Scanner;
public class Ellipsoid {
private String label = " ";
private double a = 0;
private double b = 0;
private double c = 0;
public Ellipsoid(String labelIn, double aIn, double bIn, double cIn) {
setLabel(labelIn);
setA(aIn);
setB(aIn);
setC(cIn);
}
public String getLabel() {
return label;
}
public boolean setLabel(String labelIn) {
if(label != null) {
this.label = label.trim();
return true;
}
else {
return false;
}
}
public double getA() {
return a;
}
public boolean setA(double aIn) {
if(a > 0) {
this.a = a;
return true;
}
else {
return false;
}
}
public double getB(){
return b;
}
public boolean setB(double bIn) {
if (b > 0) {
this.b = b;
return true;
}
else {
return false;
}
}
public double getC() {
return c;
}
public boolean setC(double cIn) {
if(c > 0) {
this.c = c;
return true;
}
else {
return false;
}
}
public double volume() {
return (4 * Math.PI * a * b * c) / 3;
}
public double surfaceArea() {
double surfaceAreaDouble = (Math.pow((a * b), 1.6) + Math.pow((a * c), 1.6) + Math.pow((b * c), (1 / 1.6)) / 3);
surfaceAreaDouble = 4 * Math.PI * Math.pow(surfaceAreaDouble, (1 / 1.6));
return surfaceAreaDouble;
}
public String toString() {
DecimalFormat decimalFormat = new DecimalFormat("#,##0.0###");
return "Ellipsoid \"" + label + "\" with axes a = " + getA() + ", b = " + getB() + ", c = " +getC() + " units has:\n volume = " + decimalFormat.format(volume()) + " square units" + "\n surface area = " + decimalFormat.format(surfaceArea()) + " cubic units";
}
}
after looking at your code, I noticed the following issues:
In your setter methods you are not using the right variables. You should use the method parameters in all your setters.
example:
public boolean setA(double aIn) {
if(a > 0) { // a should be aIn here
this.a = a; // = a should be = aIn
return true;
}
else {
return false;
}
}
the surfaceArea() method has a small mistake:
(Math.pow((a * b), 1.6) + Math.pow((a * c), 1.6) + Math.pow((b * c), (1 / 1.6)) / 3);
The last one doesn't need the "1/ 1.6" part but just "1.6" like the rest and then you need another bracket for all 3 math.pow before you divide them by 3. (also switch "cubic" and "square" words to match volume and surface correct)
After these fixes, making an object of class Ellipsoid and printing it gives the output you are looking for:
public static void main(String[] args) {
Ellipsoid ex1 = new Ellipsoid ("Ex 1", 1, 2, 3);
System.out.println(ex1);
}
output:
Ellipsoid "Ex 1" with axes a = 1.0, b = 2.0, c = 3.0 units has:
volume = 25.1327 cubic units
surface area = 48.9366 square units

Can someone explain to me the output of this code?

I am confused on the output of the code. I want to know for each call on variables i and s, which class is used to call the variables. The question involves variable shadowing. Also I want to know how s keeps on changing throughout the lines in the main method.
public class A {
public int i = 0;
public static String s = "";
public A(int i) {
System.out.println(i);
s += "x";
}
public A debug() {
if (this instanceof B) {
System.out.println("Spam");
s += "s";
}
return this;
}
}
public class B extends A {
public int i = 100;
public static String s = "s";
public B(int i, String s) {
super(i);
this.i += 5;
this.s = s;
}
public static void main(String[] argv) {
String s = "";
B b = new B(0, s);
System.out.println(b.i + " " + b.s);
s += "foo";
A a = new B(42, s);
System.out.println(a.i + " " + a.s);
System.out.println(b.debug().s + " " + b.i + " " + b.s);
System.out.println(a.debug().s + " " + a.i + " " + a.s);
}
}
Here is the output of that code:
0
105
42
0 xx
Spam
xxs 105 foo
Spam
xxss 0 xxss
public class A {
public int i = 0; //not changed, because it is not overrided
public static String s = "";
public A(int i) {
System.out.println(i); //1. 0, 3. 42
s += "x"; //After second run s="xx", because it is static
}
public A debug() {
if (this instanceof B) {
System.out.println("Spam"); //5, 7. Spam
s += "s"; //s = "xxs", than "xxss" because it is static
}
return this;
}
}
public class B extends A {
public int i = 100;
public static String s = "s";
public B(int i, String s) {
super(i);
this.i += 5; //First run: i=105, Second run: i=47
this.s = s; //First run: public static String s="", Second run: public static String a.s="foo"
}
public static void main(String[] argv) {
String s = "";
B b = new B(0, s);
System.out.println(b.i + " " + b.s); //2. 105
s += "foo"; //B.s is now foo
A a = new B(42, s);
System.out.println(a.i + " " + a.s); //3. 0 xx
System.out.println(b.debug().s + " " + b.i + " " + b.s); //1. because of (A)b.s = xxs, 2. b.i = 105, 3. b.s = foo
System.out.println(a.debug().s + " " + a.i + " " + a.s); //(A)a.s = "xxss", (A)a.i = 0, (A)a.s = "xxss"
}
}

Getting a "cannot find symbol" error with abstract classes

This is for my university class.
I have this top-level abstract class, Expression.java (provided by the professor):
public abstract class Expression {
public abstract int eval();
public abstract String toString();
}
BinaryExpression.java (also provided by the professor), which extends Expression.java:
public abstract class BinaryExpression extends Expression {
protected Expression left;
protected Expression right;
public BinaryExpression (Expression l, Expression r) {
left = l;
right = r;
}
public abstract int eval();
public abstract String toString();
}
And finally Add.java (I wrote this) extends BinaryExpression.java:
public class Add extends BinaryExpression {
//Fields
protected Expression left;
protected Expression right;
//Constructor
public Add(Expression l, Expression r) {
left = l;
right = r;
}
//Methods
public int eval() {
return left.eval()+right.eval();
}
public String toString() {
return left + "+" + right;
}
}
The tester, Test.java, was provided as well. There are three other classes that extend BinaryExpression, Subtract, Multiply, and Divide, but they are virtually the same as Add. Plus and Minus just store a value, either negative or positive.
public class Test {
public static void main (String [] args) {
Expression s1 = new SimpleExpression(10);
Expression s2 = new SimpleExpression(-3);
System.out.println("s1: " + s1 + " = " + s1.eval());
System.out.println("s2: " + s2 + " = " + s2.eval());
Expression u1 = new Plus(s2);
Expression u2 = new Minus(s1);
System.out.println("u1: " + u1 + " = " + u1.eval());
System.out.println("u2: " + u2 + " = " + u2.eval());
Expression b1 = new Add(u1, s2);
Expression b2= new Subtract(s1, u2);
Expression b3= new Multiply(u1, b1);
Expression b4= new Divide(u2, b3);
System.out.println("b1: " + b1 + " = " + b1.eval());
System.out.println("b2: " + b2 + " = " + b2.eval());
System.out.println("b3: " + b3 + " = " + b3.eval());
System.out.println("b4: " + b4 + " = " + b4.eval());
Expression e = new Minus( new Multiply(b3, b2));
System.out.println("e: " + e + " = " + e.eval());
// example given in the assignment
e = new Divide(new Add(new Minus(new SimpleExpression (10)),
new SimpleExpression (50)),
new Minus(new Multiply(new SimpleExpression(5),
new Minus (new SimpleExpression (3)))));
System.out.println("Another e: " + e + " = " + e.eval());
}
}
When I compile Test.java, I get this error (and a similar one for Subtract, Multiply, and Divide):
./Add.java:8: cannot find symbol
symbol : constructor BinaryExpression()
location: class BinaryExpression
public Add(Expression l, Expression r) {
What's happening?
The root cause is here
public Add(Expression l, Expression r) {
left = l;
right = r;
}
Your child class is trying to implicitly invoke a parameterless super constructor. Your parent class does not have such a parameterless constructor. So, instead, invoke its existing constructor
public Add(Expression l, Expression r) {
super(l, r);
left = l;
right = r;
}
Also, you're redeclaring left and right fields in the Add class. There's no point doing this. Your child class already has access to the left and right fields of its parent class, BinaryExpression.

(Java, Bukkit) Using extended class methods don't work (Noob oop)

I have one class named 'Regions', which make region and check if coordinates are in it. Then I have other class 'ArrowTower' which extends 'Region'. Class 'ArrowTower' creates towers. All day I was experimenting with no results. What I want to do is assign first and second location to class 'Regions' then make region and check if coordinates belongs to it. I also have events - 'BlockTouch' class which creates 'ArrowTower' object.. When I try to tower.insideRegion(e.getClickedBlock()); it gives me my location and then zeros, because it didin't set values in public void buildArrowTower, but values are right here. So I just don't understand why this isn't working :/
My Events class:
import eu.anavicius.TomTom1997.TomTowerDefence.Towers.ArrowTower;
public class BlockTouch implements Listener {
#EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
ArrowTower tower = new ArrowTower();
if (e.getAction() == Action.RIGHT_CLICK_BLOCK ) {
if (e.getItem().getType() == Material.ARROW) {
tower.buildArrowTower(e.getClickedBlock(),e.getPlayer());
} else if (e.getItem().getType() == Material.BONE) {
Bukkit.broadcastMessage("Bone");
tower.insideRegion(e.getClickedBlock());
}
}
}
}
My Region.class:
public class Regions {
private int xl,yl,zl,xh,yh,zh;
public void setLRegion (int x, int y, int z) {
xl = x;
yl = y;
zl = z;
//Bukkit.broadcastMessage("SetLMethod" + " \t " + "|" + xl + "|"+ xh + "||" + "|" + yl + "|" + yh + "||" + "|" +zl + "|" + zh + "||");
}
public void setHRegion (int x, int y, int z) {
xh = x;
yh = y;
zh = z;
//Bukkit.broadcastMessage("SetHMethod" + " \t " + "|" + xl + "|"+ xh + "||" + "|" + yl + "|" + yh + "||" + "|" +zl + "|" + zh + "||");
}
public void insideRegion (Block l) {
int x,y,z;
x = l.getX();
y = l.getY();
z =l.getZ();
Bukkit.broadcastMessage("InsideMethod" + " \\t " + x + "|" + xl + "|"+ xh + "||" +y + "|" + yl + "|" + yh + "||" + z + "|" +zl + "|" + zh + "||");
if (x >= xl && x <= xh ) {
Bukkit.broadcastMessage("Region check 1");
if (z >= zl && z <= zh) {
Bukkit.broadcastMessage("Region check 2");
if (y >= yl && y >= yh) {
Bukkit.broadcastMessage("Regione");
}
}
}
}
}
My ArrowTower.class:
public class ArrowTower extends Regions {
public ArrowTower () {
}
public void buildArrowTower (Block b,Player p) {
if (b.getType().equals(Material.EMERALD_BLOCK)) {
Location loc = b.getLocation();
for (int y = 0; y < 4;y++) {
int layloc = 0;
int by = loc.getBlock().getY()+1 + y;
for (int x = 0;x <3;x++) {
int bx = loc.getBlock().getX()-1 + x;
for (int z = 0;z < 3;z++) { // Pagalvot dar del delay, nes su kintamaisiais pirma blogai buvau
int bz = loc.getBlock().getZ()-1 + z; // sugalvojas, atsispausdina tuscios vietos
Location block = new Location(b.getWorld(),bx,by,bz); // pass loop values to method
if (y == 0 && layloc == 0) {
Bukkit.broadcastMessage("SetR L");
setLRegion(bx,by,bz);
} else if (y == 3 && layloc == 8) {
Bukkit.broadcastMessage("SetR H");
setHRegion(bx,by,bz);
}
block.getBlock().setType(Material.matchMaterial(towerl1(y,layloc)));
layloc++;
}
}
}
}
}
public String towerl1(int h, int layloc) {
String[] layer1 = { // - l
"LOG","AIR","LOG",
"AIR","AIR","AIR",
"LOG","AIR","LOG"};
String[] layer2 = { // - i
"COBBLE_WALL","COBBLESTONE","COBBLE_WALL",
"COBBLESTONE","AIR","COBBLESTONE",
"COBBLE_WALL","COBBLESTONE","COBBLE_WALL"};
String[] layer3 = { // - t
"COBBLE_WALL","AIR","COBBLE_WALL",
"COBBLE_WALL","MOSSY_COBBLESTONE","COBBLE_WALL",
"COBBLE_WALL","AIR","COBBLE_WALL"};
String[] layer4 = {
"AIR","AIR","AIR",
"AIR","JACK_O_LANTERN","AIR",
"AIR","AIR","AIR"};
if (h == 0) {
return layer1[layloc];
} else if (h == 1) {
return layer2[layloc];
} else if (h == 2) {
return layer3[layloc];
} else if (h == 3) {
return layer4[layloc];
} else {
return null;
}
}
}
The problem is that you create a new ArrowTower object on each interact event. I assume you first right-click on a block while holding an arrow in your hand - that's when tower.buildArrowTower(e.getClickedBlock(),e.getPlayer()); is called.
Then you take a bone in your hand, and click again - but this time your event handler will create a completely new ArrowTower with it's first line (actually you don't even have a reference to the first one, since it was only declared in the function's scope):
ArrowTower tower = new ArrowTower();
Then you call:
tower.insideRegion(e.getClickedBlock());
but this tower was just created - actually it's xh, yh, etc. values weren't ever initialized.

Categories