Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name: Sammy Samkough
- // Prog: Number
- // Spec: Completing methods for a Number class and a Number Analyzer class that consists of an ArrayList of Number objects.
- // The Number class stores a number and has the ability to determine if a number is odd or perfect.
- public class Number
- {
- private Integer number;
- public Number()
- {
- number = 0;
- }
- public Number(int num)
- {
- number = num;
- }
- public void setNumber(int num)
- {
- number = new Integer(num);
- }
- // gets the number
- public int getNumber()
- {
- return number;
- }
- // checks if the number is odd
- public boolean isOdd()
- {
- if ((number % 2) == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- // checks if the number is a perfect number (all factors added up equals the total)
- public boolean isPerfect()
- {
- int temp = 0;
- for(int i = 1; i <= number / 2; i++)
- {
- if(number % i == 0)
- {
- temp += i;
- }
- }
- if(temp == number)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public String toString()
- {
- return number + "";
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Name: Sammy Samkough
- // Prog: Number
- // Spec: Completing methods for a Number class and a Number Analyzer class that consists of an ArrayList of Number objects.
- // The Number class stores a number and has the ability to determine if a number is odd or perfect.
- public class NumberRunner
- {
- public static void main(String args[])
- {
- int[] nums = {7, 28, 496, 1111, 199, 201, 17};
- Number x;
- for(int num : nums)
- {
- x = new Number(num);
- if(x.isOdd())
- {
- System.out.println(x + " is odd.");
- }
- else
- {
- System.out.println(x + " is not odd.");
- }
- if(x.isPerfect())
- {
- System.out.println(x + " is perfect. \n");
- }
- else
- {
- System.out.println(x + " is not perfect. \n");
- }
- }
- }
- }
- /*
- 7 is odd.
- 7 is not perfect.
- 28 is not odd.
- 28 is perfect.
- 496 is not odd.
- 496 is perfect.
- 1111 is odd.
- 1111 is not perfect.
- 199 is odd.
- 199 is not perfect.
- 201 is odd.
- 201 is not perfect.
- 17 is odd.
- 17 is not perfect.
- Press any key to continue . . .
- */
- -------------------------------------------------------------------------------------------------------------------------------
- // Name: Sammy Samkough
- // Prog: NumberAnalyzer
- // Spec: Completing methods for a Number class and a Number Analyzer class that consists of an ArrayList of Number objects.
- // The Number class stores a number and has the ability to determine if a number is odd or perfect.
- import java.util.ArrayList;
- import java.util.Scanner;
- public class NumberAnalyzer
- {
- private ArrayList<Number> list;
- public NumberAnalyzer()
- {
- setList("");
- }
- public NumberAnalyzer(String numbers)
- {
- setList(numbers);
- }
- public void setList(String numbers)
- {
- list = new ArrayList<Number>();
- Scanner sc = new Scanner(numbers);
- while(sc.hasNext())
- {
- list.add(new Number(sc.nextInt()));
- }
- }
- // gets the isOdd() method from Number and analyzes it
- public int countOdds()
- {
- int oddCount = 0;
- int size = list.size();
- for (int i = 0; i < size; i++)
- {
- if (list.get(i).isOdd())
- {
- oddCount++;
- }
- }
- return oddCount;
- }
- // gets the isOdd() method from Number and analyzes it, then reverses it to get the evens
- public int countEvens()
- {
- int evenCount = 0;
- int size = list.size();
- for (int i = 0; i < size; i++)
- {
- if (!list.get(i).isOdd())
- {
- evenCount++;
- }
- }
- return evenCount;
- }
- // gets the isPerfect() method from Number and analyzes it
- public int countPerfects()
- {
- int perfectCount = 0;
- int size = list.size();
- for (int i = 0; i < size; i++)
- {
- if (list.get(i).isPerfect())
- {
- perfectCount++;
- }
- }
- return perfectCount;
- }
- public String toString()
- {
- return "";
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Name: Sammy Samkough
- // Prog: NumberAnalyzer
- // Spec: Completing methods for a Number class and a Number Analyzer class that consists of an ArrayList of Number objects.
- // The Number class stores a number and has the ability to determine if a number is odd or perfect.
- import java.util.ArrayList;
- import java.util.Scanner;
- public class NumberAnalyzerRunner
- {
- public static void main(String args[])
- {
- NumberAnalyzer test = new NumberAnalyzer("5 12 9 6 1 4 8 6");
- System.out.print(test);
- System.out.println("odd count = " + test.countOdds());
- System.out.println("even count = " + test.countEvens());
- System.out.println("perfect count = " + test.countPerfects()+ "\n\n\n");
- test.setList("5 12 3 7 28 496 81 65 33 11");
- System.out.println(test);
- System.out.println("odd count = " + test.countOdds());
- System.out.println("even count = " + test.countEvens());
- System.out.println("perfect count = " + test.countPerfects()+ "\n\n\n");
- test.setList("1 2 3 4 5 6 7 8 11 13 151 16 17 18");
- System.out.println(test);
- System.out.println("odd count = " + test.countOdds());
- System.out.println("even count = " + test.countEvens());
- System.out.println("perfect count = " + test.countPerfects()+ "\n\n\n");
- }
- }
- /*
- odd count = 3
- even count = 5
- perfect count = 2
- odd count = 7
- even count = 3
- perfect count = 2
- odd count = 8
- even count = 6
- perfect count = 1
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement