Directions: Part 1: NumberOperations: Method Stubs (a.k.a. skeleton code) orget
ID: 3907073 • Letter: D
Question
Directions: Part 1: NumberOperations: Method Stubs (a.k.a. skeleton code) orget to add your Javadoe comments; the class, constructor and each method will need one Create a class called NumberOperations with an instance variable called number of type int. l public class Numberoperations ( - private int number: Add method stubs for the following methods. The first two are given; do the rest on your own. The constructor takes an int parameter called numberin number!n) o public Numberoperations (int { o getvalue: takes no parameters; returns an int value -public int getValue() Create method stubs with treturn 0; // placeholder retur2 or each return 0; 1/ placeholder return placeholder returns f method on your own. o oddsUnder: takes no parameters; returns a String o powersTwoUnder: takes no parameters; returns a String o isGreater: takes an int parameter called compareNumber, returns an int o toString: takes no parameters; returns a String Compile NumberOperations and run the following in interactions. Do not continue until your program compiles and the following code runs without runtime errors in interactions. Note that return values will be the placeholder values in the "stubbed" methods. NumberOperations numops new Numberoperations (5): String s1numops.oddsUnder (O String s2 numops.powersTwoUnder () int n1 numOps.isGreater (2); string s3 numops.tostring();Explanation / Answer
Here is the completed code for this problem. I also found the incomplete NumberOpsDriver program after some digging (as you didn’t provide it) which I have completed for testing the NumberOperations class according to the specs , Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// NumberOperations.java
public class NumberOperations {
// attribute
private int number;
// constructor to initialize the number
public NumberOperations(int numberIn) {
this.number = numberIn;
}
/**
* @return the number
*/
public int getValue() {
return number;
}
/**
* @return a String containing all odd values under number
*/
public String oddsUnder() {
String output = "";
int i = 0;
while (i < number) {
if (i % 2 != 0) {
output += i + " ";
}
i++;
}
return output;
}
/**
* @return a String containing all powers of 2 under number
*/
public String powersTwoUnder() {
String output = "";
int powers = 1;
while (powers < number) {
output += powers + " ";
powers *= 2;
}
return output;
}
/**
* @return 1 if number > compareNumber, -1 if less than, 0 if equal
*/
public int isGreater(int compareNumber) {
if (number > compareNumber) {
return 1;
} else if (number < compareNumber) {
return -1;
} else {
return 0;
}
}
/**
* @return a String containing the number
*/
public String toString() {
return number + "";
}
}
// NumberOpsDriver.java
import java.util.Scanner;
import java.util.ArrayList;
public class NumberOpsDriver {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//creating an array list of NumberOperations objects
ArrayList<NumberOperations> numOpsList = new ArrayList<NumberOperations>();
// prompt user for set of numbers
System.out.println("Enter a list of positive integers separated "
+ "with a space followed by 0:");
// getting first user input using in.nextInt()
int input = in.nextInt();
// looping until the input is 0
while (input != 0) {
// creating a NumberOperations object using the input
NumberOperations object = new NumberOperations(input);
// Adding the object to the array list
numOpsList.add(object);
//getting next integer
input = in.nextInt();
}
int index = 0;
while (index < numOpsList.size()) {
NumberOperations num = numOpsList.get(index);
System.out.println("For: " + num);
System.out.println(" Odds under: " + num.oddsUnder());
System.out.println(" Powers of 2 under: " + num.powersTwoUnder());
index++;
}
}
}
/*OUTPUT*/
Enter a list of positive integers separated with a space followed by 0:
12 9 17 0
For: 12
Odds under: 1 3 5 7 9 11
Powers of 2 under: 1 2 4 8
For: 9
Odds under: 1 3 5 7
Powers of 2 under: 1 2 4 8
For: 17
Odds under: 1 3 5 7 9 11 13 15
Powers of 2 under: 1 2 4 8 16
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.