You are to create a class called zyMethod. It has one constructor, one class met
ID: 3696290 • Letter: Y
Question
You are to create a class called zyMethod. It has one constructor, one class method ,and 3 versions of an instance method called function(). The class should store the double argument passed to in the constructor as an instance variable. For this lab we will call this number, "the value" all methods should be public.
Constructor
public zyMethod(double dVal)
Methods
boolean threshold()
This should be a class method that returns a boolean value. It returns true if the number of times the constructor has been called is > 5. else it returns false
double function()
Using the pow method of Java's Math class, function should return "the value" squared.
double function(double exp)
Using the pow method of Java's Math class, function should return "the value" raised to the exponent exp
double function(double a, double b)
This version of function should return a if "the value" is nearest the input argument a (in absolute value), else it should return b All methods must be declared public
Explanation / Answer
/* java class zyMethod */
import java.util.*;
import java.lang.*;
import java.io.*;
class zyMethod {
private double value;
private static int number = 0;
// stores the number of times constructor has been called
/* Constructor. */
zyMethod(double dVal) {
this.value = dVal;
number++;
}
// class method that returns a boolean value
public boolean threshold() {
if (number > 5) {
return true;
}
else {
return false;
}
}
public double function() {
return Math.pow(this.value, 2);
}
public double function(double exp) {
return Math.pow(this.value, exp);
}
public double function(double a, double b) {
if (Math.abs(a - value) < 0.00001) {
return a;
}
else {
return b;
}
}
public static void main (String[] args)
{
zyMethod a = new zyMethod(2.0);
zyMethod b = new zyMethod(3.0);
zyMethod c = new zyMethod(4.0);
zyMethod d = new zyMethod(6.0);
zyMethod e = new zyMethod(7.0);
zyMethod f = new zyMethod(8.0);
if(a.threshold() == true)
System.out.println("Constructor called more than 5 times");
else
System.out.println("Constructor called less than 5 times");
System.out.println(a.function(2.0,3.0));
return;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.