0

I am new to programming. My code are listed below.

public abstract class Mark {
  private double mark;

  public double getMark() {
    return Mark;
  }

  public void setMark(double mark) {
    this.mark = mark;
  }

  public abstract void calculateMark(double x, double y, double z);
}
public class Test extends Mark {
  public void calculateMark(double test1, double test2, double test3) {
    setMark(test1 * 0.5 + test2 * 0.3 + test3 * 0.2);
  }
}
public class TestMark {
  public static void main(String[] args) {
    Mark test = new Test();

    test.calculateMark(50, 70, 89);
    test.setMark(90);
    System.out.println("Total = " + test.getMark());
  }
}

In class TestMark, I use the method setMark() to change the mark to 90.
How can I avoid the change of the mark through setMark() method?

Edited by xyxtss: n/a

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by stultuske
0

If you don't want anyone to be able to change the mark just don't have a setMark method. If you want to be able to do it yourself, but not allow anyone else, make the method private.

0

or, if you mean that it may only be changed if it has a certain value, for instance, it has to be a positive value:

public void setMark(double mark) {
    if ( mark >= 0)
    this.mark = mark;
  }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.