RIN :11 17% 10:25 PM Problem Description You are writing an object that is a lis
ID: 3714073 • Letter: R
Question
RIN :11 17% 10:25 PM Problem Description 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 indicies 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 sumOfAlIOddNumbers) 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 sumofAllEvenNumbers0 - 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 indicies Returns the sum of all odd public int sumofAIOddNumbers0 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 indicies. public int sumofOddDigitsForltem(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. Getting StartedExplanation / Answer
Below i had writter code for the above question ...no error checkings done please give proper values as you asked in the question..
I had implemented all the function you asked in the question ,written in simple and convinent way for simple understanding please understand carefully.
note: index of array starts from 0 you can chage according to your usage.
//code to execute above functions...
import java.io.*;
import java.util.*;
class SumFun{
//array which holds our data
static ArrayList<Integer> array = new ArrayList<Integer>();
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int a;
do{
System.out.println(" ~~~~~~Seclec an option~~~~~~~~~");
System.out.println("1.Add to list");
System.out.println("2.Sum of all even numbers");
System.out.println("3.Sum of even numbers in range");
System.out.println("4.Sum of all odd numbers");
System.out.println("5.Sum of odd numbers in range");
System.out.println("6.Sum of odd digits for item");
System.out.println("7.Print all the elements in list");
System.out.println("0.Exit");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
a=in.nextInt();
switch(a){
case 1:
int num=in.nextInt();
addToList(num);
break;
case 2:
sumOfAllEvenNumbers();
break;
case 3:
//note array index starts from 0
System.out.print("Enter Starting index:");
int num1=in.nextInt();
System.out.print("Enter Ending index:");
int num2=in.nextInt();
sumOfEvenNumbers(num1,num2);
break;
case 4:
sumOfAllOddNumbers();
break;
case 5:
//note array index starts from 0
System.out.print("Enter Starting index:");
int num3=in.nextInt();
System.out.print("Enter Ending index:");
int num4=in.nextInt();
sumOfOddNumbers(num3,num4);
break;
case 6:
//note array index starts from 0
System.out.print("Enter item index:");
int x=in.nextInt();
sumOfOddDigitsForItem(x);
break;
case 7:
printArray();
break;
case 0:
System.out.println("Thank you :)");
default:
System.out.println("Select valid option!!");
}
}while(a!=0);
}
public static void addToList(int i){
array.add(i);
}
public static void sumOfAllEvenNumbers(){
int sum=0;
for (int counter=0; counter<array.size(); counter++) {
if(array.get(counter)%2==0)
sum=sum+array.get(counter);
}
System.out.println("sum of all even numbers: "+sum);
}
public static void sumOfEvenNumbers(int m,int n){
int sum=0;
for (int counter=m; counter<=n; counter++) {
if(array.get(counter)%2==0)
sum=sum+array.get(counter);
}
System.out.println("sum of all even numbers in range: "+sum);
}
public static void sumOfAllOddNumbers(){
int sum=0;
for (int counter=0; counter<array.size(); counter++) {
if(array.get(counter)%2!=0)
sum=sum+array.get(counter);
}
System.out.println("sum of all odd numbers: "+sum);
}
public static void sumOfOddNumbers(int m,int n){
int sum=0;
for (int counter=m; counter<=n; counter++) {
if(array.get(counter)%2!=0)
sum=sum+array.get(counter);
}
System.out.println("sum of all odd numbers in range: "+sum);
}
public static void sumOfOddDigitsForItem(int k){
int n=array.get(k);
int sum=0,rem=0;
while(n!=0){
rem=n%10;
if(rem%2!=0)
sum=sum+rem;
n=n/10;
}
System.out.println("sum of odd digits for item: "+sum);
}
public static void printArray(){
if(array.size()==0)
System.out.println("Array is empty!!");
for (int counter=0; counter<array.size(); counter++) {
if(counter==0)
System.out.println("Integers in the array:");
System.out.print(array.get(counter)+" ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.