2.Declare a private instance variable of type int called divisor, then write a line of code in the constructor to initialise divisor to 2.
3. Write a public getter method for divisor.
4. Write a public setter method for divisor, which sets divisor to the value of the argument, provided the latter is not 0. If it is 0, the method does nothing. (This is because, as its name suggests, divisor is going to be used to divide, so the argument’s value cannot be zero, and the setter must only set it to non-zero values.)
Show an instance of the class to be used in testing, like..
something = something
1
Expert's answer
2016-11-19T05:21:16-0500
public class Main { public static void main(String[] args) { Divider d = new Divider();
System.out.println(d.getDivisor());
d.setDivisor(3);
System.out.println(d.getDivisor());
d.setDivisor(0);
System.out.println(d.getDivisor()); } }
class Divider { private int divisor;
public Divider() { divisor = 2; }
public int getDivisor() { return divisor; }
public void setDivisor(int newDivisor) { if (newDivisor == 0) { return; }
Comments
Leave a comment