Question: In this lab you will use loops and if statements to to classify Number objects. ArrayList list is filled with Number type objects. Remember that
In this lab you will use loops and if statements to to classify Number objects.
ArrayList list is filled with Number type objects. Remember that ArrayLists can only hold objects, not primitive types. The Number class has two missing methods:
- boolean isOdd() which returns true if a number is odd and false otherwise.
- boolean isPerfect() which returns true if a number is perfect and false otherwise. A number is considered perfect if it is equal to the sum of its divisors.
The NumberAnalyzer class has two constructors and four methods:
- A default constructor that fills list with 10 random Number objects.
- An overloaded constructor that takes in a string parameter. The parameter has ints separated by a space.
- Methods countOdds(), countEvens(), and countPerfects().
- A toString(). Use the sample output shown below for formatting.
Directions:
- Complete the missing methods in Number class.
- Complete the constructors and methods in NumberAnalyzer class.
- Test your code in Main.
And here are my code in Number Class:
public class Number { private Integer number;
public Number() { number = 0; }
public Number(int num) { number = num; }
public void setNumber(int num) { number = num; }
public int getNumber() { return number; }
public boolean isOdd() { //your code here if (number % 2 != 0) { return true; } else{ return false; } }
//a number is perfect if it is equal to sum of its divisors. public boolean isPerfect() { //your code here int sum = 0; for (int i = 1; i <= number/2; i++) { sum += i; } if (number == sum) { return true; } else{ return false; } } public String toString( ) { String output = ""; if(isOdd()) { output += number + " is odd."; } else { output += number + " is not odd."; } if(isPerfect()) { output += " " + number +" is perfect."; } else{ output += " " + number +" is not perfect."; } return output; } }
Here are my code in NumberAnalyzer Class:
import java.util.ArrayList;
public class NumberAnalyzer
{ private ArrayList
//default constructor //initialize list and fill it with 10 random Number objects integers public NumberAnalyzer() { list = new ArrayList
//overloaded constrcuctor //initilize list //parameter numbers a string of integers separated by a space. //Loop through numbers and add each to list. //use .IndexOf() to repeatedly find the next " " in string numbers public NumberAnalyzer(String numbers) { list = new ArrayList<>(numbers); for (int i = 0; i < numbers.length(); i++) { list.add(numbers); } }
public int countOdds() { int oddCount=0; for (Number value : list) { if (value.isOdd() == true) { oddCount++; } } return oddCount; }
public int countEvens() { int evenCount=0; for (Number value : list) { if (!value.isOdd() == true) { evenCount++; } } return evenCount; }
public int countPerfects() { int perfectCount=0; for (Number value : list) { if (value.isPerfect() == true) { perfectCount++; } } return perfectCount; } public String toString( ) { return "" + list; } }
Finally I use the code in Main Class to test my previous code, and here is the code in Main Class:
import java.util.ArrayList; import java.util.Scanner;
public class Main { public static void main(String[] args) { NumberAnalyzer test = new NumberAnalyzer("5 12 9 6 1 4 8 6"); System.out.println(test); //prints [5, 12, 9, 6, 1, 4, 8, 6]
System.out.println("odd count = "+test.countOdds()); //prints 3 System.out.println("even count = "+test.countEvens()); //prints 5
System.out.println("perfect count = "+test.countPerfects()+" "); //prints 2 } }
Please correct my code so I can run the project as instructed, thanks!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
