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 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...
(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.
Implement a method/function in java that produces running sum runningSum2DArray(int[][] array, int dir) across rows (left...
Implement a method/function in java that produces running sum runningSum2DArray(int[][] array, int dir) across rows (left to right or right to left) or columns (top to bottom or bottom to top) Input to the method: A 4x4 two dimensional int array and an integer (1, 2, 3 or 4 for left, right, up,down respectively). Output: The modified array after producing the running sums according to the direction. For example: If the input to the method is the same as the...
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}
Java Write a method, makeUserName, that is passed two Strings and an int: the first is...
Java Write a method, makeUserName, that is passed two Strings and an int: the first is a first name, the second is a last name, and the last is a random number. The method returns a user name that contains the last character of the first name, the first five characters of the last name (assume there are five or more characters in last name), and the random number. An example: Assume that “John”, “Smith”, 45, are passed when the...
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 }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT