Question

You are writing an object that is a list of numbers on which you are able to some simple mathematical computations. Please note that all of the indices that will be passed to you will be valid; you don't have to write error checking code. However, you will have to handle the case where you create a fresh object and call the appropriate methods (e.g. sumOfAllOddNumbers).

The object to implement this is referred to as SumFun and consists of the following public methods:
public SumFun()
public void addToList(int i) - Add an integer to our list of integers.
public int sumOfAllEvenNumbers() - Returns the sum of all even numbers in the list.
public int sumOfEvenNumbers(int start, int end) - Returns the sum of all even numbers from the start index (given by start) and the end index (given by end). Note that it includes both the start and end indices.
public int sumOfAllOddNumbers() - Returns the sum of all odd numbers in the list.
public int sumOfOddNumbers(int start, int end) - Returns the sum of all even numbers from the start index (given by start) and the end index (given by end). Note that it includes both the start and end indices.
public int sumOfOddDigitsForItem(int itemIndex) - Given an index into the list you extract the number and then add all of the odd digits. For example, if you have the number 1234 the sum of the odd digits is 1 + 3 = 4.

Your Main.java should contain code to test your SumFun object. Load multiple values and check to make sure that the values match the expected values. Use looping to load and test your object.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.util.ArrayList;

public class SumFun {

private ArrayList<Integer> list; //List for holding data

public SumFun(){ //Constructor, only needs to initialize the arraylist
list = new ArrayList<Integer>();
}

public void addToList(int i) //Simple add to list, just add to arrayList
{
list.add(i);
}

public int sumOfAllEvenNumbers()
{
int total = 0;
for(int i = 0; i < list.size(); i++) //Go through every element of list
if(list.get(i) % 2 == 0) //If list entry is even (using mod)
total+= list.get(i); //Add to total

return total; //Return sum
}

public int sumOfEvenNumbers(int start, int end)
{
int total = 0;
for(int i = start; i <= end; i++) //Go through every element of list in range (inclusive)
if(list.get(i) % 2 == 0) //If list entry is even (using mod)
total+= list.get(i); //Add to total

return total; //Return sum
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Java Programming Problem
Homework Solution
$28.00
Java
Programming
Codes
Algorithms
Computer Science
Classes
Variables
Statements
Source Files
Input
Output
Constructors
Strings
Integers
Error Handling
Data Structures Questions
Homework Solution
$25.00
Data Structures
Algorithms
Codes
Computer Science
Stack
Values
API
Implementation
Push
Peek
Pop
Development
Fragments
Summation
Order
Java Programming: User Defined Company
Homework Solution
$40.00
Java
Programming
Computer Science
Company
Stock Symbol
Stock Value
Constructor
Methods
Strings
Variables
Business
Finance
Economy
Hardware
Software
Arrays
Lists
Java Program For Restaurants
Homework Solution
$18.00
Java
Restaurants
Computer Science
Programming
Tips
Variables
Functions
Dollar Amounts
Functions
Input
Output
Statements
Multiplication
Bills
Percentage
Division
NumberFormat Class
Java Programming Problems
Homework Solution
$30.00
Java
Programming
Computer Science
Arrays
Fractions
Variables
Strings
Numerator
Denominator
Statements
Constructor
Object
Initialization
Get help from a qualified tutor
Live Chats