Question

In: Computer Science

Just that method --> Java code Write a method “int sumPos(List aList)” to calculate the sum...

Just that method --> Java code

Write a method “int sumPos(List aList)” to calculate the sum of positive integers in an ADT List aList of integers using ADT List operations.

ADT List operations: isEmpty(), size(), add(index, item), remove(index), get(index), and removeAll().

You should not assume how an ADT List is implemented.

Using array indexing, head/tail references, or any operation not defined in ADT List is not allowed.

Solutions

Expert Solution

The required method is given below:

//method to calculate the sum of positive integers
public static int sumPos(List aList)
{
//variable declarationa and initialization
int sum=0;
  
//check if the size of the list is zero
if(aList.size()==0)
return 0;
  
//calcuate the sum of the positive number
while(!aList.isEmpty())
{
if((int)aList.get(0)>0)
{
sum = sum + (int)aList.get(0);
}
aList.remove(0);
}
  
//return the sum
return sum;
}

The complete program source code including the above method for testing is given below:

import java.util.ArrayList;
import java.util.List;

public class Main
{
//method to calculate the sum of positive integers
public static int sumPos(List aList)
{
//variable declarationa and initialization
int sum=0;
  
//check if the size of the list is zero
if(aList.size()==0)
return 0;
  
//calcuate the sum of the positive number
while(!aList.isEmpty())
{
if((int)aList.get(0)>0)
{
sum = sum + (int)aList.get(0);
}
aList.remove(0);
}
  
//return the sum
return sum;
}
  
   public static void main(String[] args)
   {
   //list declaration
   List<Integer> aList = new ArrayList<Integer>();

//add number to the list
   aList.add(0, 5);
   aList.add(1, 3);
   aList.add(2, -3);
     
   //method calling and display the result
   System.out.println(sumPos(aList));
   }
}

OUTPUT:

8


Related Solutions

how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the addPlayers(int players) method * Complete the passPotatoe(int passes) method * No other methods/variables should be added/modified */ public class A3CircleLL {    /*    * Grading:    * Correctly uses helpers to play game - 1pt    * Prints correct winner when game is complete - 0.5pt    */    public void playGame(int players, int passes) {        /*        * Use...
write a java code Write a generic program to compute the sum of 30 terms of...
write a java code Write a generic program to compute the sum of 30 terms of the following series. (181 - 5)/31 + (186 + 9)/34 - (191 - 13)/37 + (196 + 17)/40 + . . . . . 1 5 11 Note: the 3rd, 6th, 9th term etc, has a negative sign in front of parenthesis. User Input: None Expected output: Term Ratio Sum 1 5.677 5.677 2 5.735 11.413 3 -4.811 6.602
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
27) Consider the student class. There is a method setSemesterGrade(int grade) In that method write code...
27) Consider the student class. There is a method setSemesterGrade(int grade) In that method write code to throw a new IllegalStateException if the average is less than 0 And an IndexOutOfBoundsException if the average is > 0 28) Consider a student represented by the variable s1 and we are trying to set the average of s1. Write a try catch routine to catch either error listed above try { s1.setSemesterGrade(-30); s1.setSemesterGrade(200);
Which row has the largest sum? Write a method that takes a 2D int array and...
Which row has the largest sum? Write a method that takes a 2D int array and prints: of The index of the row that has the largest sum The sum of elements in that row java
In java, Write a recursive function to calculate the sum of the nodes only on even...
In java, Write a recursive function to calculate the sum of the nodes only on even levels of the subtree. please do not add any parameters to do this function. private int sumEvenLevels(Node current){ //you can only pass in root. //code }
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int...
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int index) void DeleteAt(int index) void Swap(int index, int index) index starts at 0 (just like array's subscript) If the index is out of bound, RetrieveAt returns 0, DeleteAt does nothing, do nothing in Swap. Write your own testing program to test the modified class -----------------------------------------DLinkedlist.java---------------------------- public class DLinkedList { private class Node { String data; Node next; Node prev; public Node(String s) { data...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method receives the following arrangement: [1,2,3,4,5] The invert method returns the array [5,4,3,2,1] Example 2: the print method receives the following array: [5,4,3,2,1] The print method prints: 5,4,3,2,1 (data separated by commas). TIP: for the print method use System.out.print () without the "ln".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT