Getting a "cannot find symbol" error with abstract classes - java

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.

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);
}
}

How to fix "Cannot invoke "java.awt.Point.getX()" because "this.p" is null"

For an inheritance practice assignment, I have a class Square which extends rectangle, and here are the relevant methods:
public class Square extends Rectangle {
private Point p;
private int sideLength;
public Square(Point p, int sideLength){
super(p);
this.sideLength = sideLength;
}
public String toString(){
return getClass().getName() + "\nCenter point: (" + p.getX() + ',' + p.getY() + ") \nSide Length: " + sideLength + " \nArea: " + this.getArea() + " \nPerimeter: " + this.getPerimeter();
}
}
there's also a getArea() and getPerimeter() method but neither of those are causing this issue.
I have a separate class to test this one:
public class SquareTest {
public static void main(String[] args){
ArrayList<Square> squareList = new ArrayList<>();
Point p1 = new Point(1, 1);
Point p2 = new Point(1, 2);
Point p3 = new Point(4, 1);
Point p4 = new Point(2, 3);
Point p5 = new Point(5, 4);
Square one = new Square(p1, 1);
Square two = new Square(p2, 2);
Square three = new Square(p3, 4);
Square four = new Square(p4, 4);
Square five = new Square(p5,5);
squareList.add(one);
squareList.add(two);
squareList.add(three);
squareList.add(four);
squareList.add(five);
for (Square each : squareList){
System.out.println(each.toString());
System.out.println();
}
}
}
What it's supposed to do is print out the toString() result for each of the five square objects, where one square looks like this:
Square
Center point: (1,1)
Side Length: 1
Area: 1
Perimeter: 1
but instead i get the following runtime error:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.awt.Point.getX()" because "this.p" is null
at Square.toString(Square.java:21)
at SquareTest.main(SquareTest.java:23)
I'm confused as to why it thinks this.p is null when I've clearly instantiated five Point objects. Any ideas?
NullPointerException was caused because you haven't initialized the p in Square class. Yu have just passed it to the Rectangle constructor so it is the p in Rectangle class which is getting initialized.
Since you are not using the point object in your Square rather than just passing it to the super class, there is no need to declare a Point object in the Square class. If your Rectangle class has a getter method for Point p then, you could use that in the Square class.
Assuming that Rectangle class has a getter method for p, You could refactor your Square class to :
public class Square extends Rectangle {
private int sideLength;
public Square(Point p, int sideLength){
super(p);
this.sideLength = sideLength;
}
public String toString(){
return getClass().getName()
+ "\nCenter point: (" + getP().getX() + ',' + getP().getY()
+ ") \nSide Length: " + sideLength
+ " \nArea: " + this.getArea()
+ " \nPerimeter: " + this.getPerimeter();
}
}

java string toString with condition

I have to write a PhonePlan object that will represent the type of plan that a customer has for his/her phone. So the object must keep track of the minutesAllowed (int), minutes used (int), dataAllowed (int), data used (int) and planType (boolean):
public class PhonePlan {
int minutesAllowed;
int minutesUsed;
int dataAllowed;
int dataUsed;
boolean planType;
}
I needed to write a constructor which has minutesAllowed, dataAllowed, and the planType as arguments which I did:
public PhonePlan (int ma, int da, boolean pt){
this.minutesAllowed = ma;
this.planType = pt;
this.dataAllowed = da;
}
And finally I have to write a string method that displays the plan depending on the type of plan.
I also have to test my code with the following test program;
public class PlanTestProgram {
public static void main(String args[]) {
System.out.println(new PhonePlan(200, 2500000, false));
System.out.println(new PhonePlan(250, 500000, true));
System.out.println(new PhonePlan(300, 5000000, false));
System.out.println(new PhonePlan(60, 1000000, false));
System.out.println(new PhonePlan(30, 0, true));
}
The first element being the minutesAllowed, the second one being the amount of dataAllowed and the third one is stating if planType is true of false.
I tried many different things but I am not able to construct a toString() method that take into account if my planType is either true or false...
My Attempt:
public String toString(){
return ( "Regular(" + minutesAllowed + " minute," + dataAllowed
+ "GB data) Monthly Plan with "
+ getMinutesRemaining() + " minutes remaining and "
+ getDataRemaining() + "KB remaining");
}
You can easily update return in your suggested (attempt) toString method, by using the shorthand if/else, into the following:
return ("Regular(" + minutesAllowed + " minute," + dataAllowed
+ "GB data) " + ((PlanType)? "Monthly": "Annual") + " Plan with "
+ getMinutesRemaining() + " minutes remaining and "
+ getDataRemaining() + "KB remaining");
Metod toString() should never be used to showcase the value of the object properties. For the overrides you can use commons library
public String toString() {
return new ToStringBuilder(this).
append("minutesAllowed", minutesAllowed).
append("minutesUsed", minutesUsed).
append("dataAllowed", dataAllowed).
append("dataUsed", dataUsed).
append("planType", planType).
toString();
}
you could implement the toString method as described in the following code
public class PhonePlan {
int minutesAllowed;
int minutesUsed;
int dataAllowed;
int dataUsed;
boolean planType;
public PhonePlan (int ma, int da, boolean pt){
this.minutesAllowed = ma;
this.planType = pt;
this.dataAllowed = da;
}
#Override
public String toString() {
return planType ? getPlanTypeBasedString("Weekly Plan") : getPlanTypeBasedString("Monthly Plan");
}
public String getPlanTypeBasedString(String planType){
return ( "Regular(" + minutesAllowed + " minute," + dataAllowed
+ "GB data) "+planType+" with "
+ getMinutesRemaining() + " minutes remaining and "
+ getDataRemaining() + "KB remaining");
}

Simple instance variable issue

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

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"
}
}

Categories