Question

In: Computer Science

(JAVA) ExceptionSum The objective of this exercise is to create a method that adds all the...

(JAVA)

ExceptionSum

The objective of this exercise is to create a method that adds all the numbers within an array with an exception you cant sum the 6, and the 7, and the numbers between them cannot be added too, taking into account that whenever a 6 appears will be followed by a 7.

If we have an array such that [1,2,3,6,10,9,8,7,5] when we pass it through the method it will give us an int such that the sum will be 11.

In order to do this exercise, you will have to make a static public method, called sum, this method will return int, which will be the sum of all the numbers in the array except the numbers between 6 and 7, and the same 6 and 7.

Here is the class diagram.

ExceptionSum

+ sum(int [] a) : int

Solutions

Expert Solution

CODE:

import java.util.*;
public class ExceptionSum
{
   public static void main(String[] args)
   {
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter Number of elements : ");
       int n=scan.nextInt();//taking input
       int[] a = new int[n];//array declaration
       int i=0;//loop variable
       System.out.print("Enter elements : ");
       //loop to get the elements from user.
       for(i=0;i<n;i++)
       {
           a[i] = scan.nextInt();//taking input
       }
       System.out.print("The sum is : ");
       System.out.print(sum(a));//printing sum.
   }
   public static int sum(int[] a)
   {
       //varibles required and two flags.
       int sum_value = 0,flag_6=0,flag_7=0;
       for(int i=0;i<a.length;i++)
       {
           if(a[i] == 6)
           {
               flag_6 = 1;//turning on flag
           }
           else if(a[i] == 7)
           {
               flag_7 = 1;//turning on flag
           }
           //if there is no 6 and after 7 is encountered.
           else if(flag_6 == flag_7)
           {
               sum_value += a[i];//adding the value to the sum.
           }
       }
       return sum_value;//returning sum value
   }
}

CODE ATTACHMENTS:

OUTPUT:

I have used flags here to get the exception.

The flags are equal before 6 is encountered and after 7 is encountered.

Please do comment for any queries.
Please like it.
Thank you.


Related Solutions

Mini JAVASCRIPT Algorithm Exercise "Sum All In Array" Objectives Create a function that adds all numbers...
Mini JAVASCRIPT Algorithm Exercise "Sum All In Array" Objectives Create a function that adds all numbers of a provided array (arr), accounting for non-integer values in arr. The output should return an integer. Notes Remember your data types? If the element is an integer (8), it should be added. If the element is a string with a number value ("8"), it should be added. If the element is not a number, or if it is a string with a non-number...
Having some trouble with this Java Lab (Array.java) Objective: This lab is designed to create an...
Having some trouble with this Java Lab (Array.java) Objective: This lab is designed to create an array of variable length and insert unique numbers into it. The tasks in this lab include: Create and Initialize an integer array Create an add method to insert a unique number into the list Use the break command to exit a loop Create a toString method to display the elements of the array Task 1: Create a class called Array, which contains an integer...
Create a java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
In Java Create an interface Create an interface Printing, which have a method getPageNumber () that...
In Java Create an interface Create an interface Printing, which have a method getPageNumber () that return page number of any printing. Create an abstract class named Novel that implements Printing. Include a String field for the novel's title and a double field for the novel price. Within the class, include a constructor that requires the novel title, and add two get methods--one that returns the title and one that returns the price. Include an abstract method named setPrice().. Create...
Java Create a method to display a menu. When this method is called it should receive...
Java Create a method to display a menu. When this method is called it should receive the user's name, great the user, and ask them to make a selection. 1 - Change your name, 2 - Test your IQ, 3 - Display a table, 4 - Play a game, 5 - Exit.
java code: adds a new regular task, delete a task , show all tasks, and show...
java code: adds a new regular task, delete a task , show all tasks, and show regular tasks, mark a task as important (possibly through ID), complete task, show all completed tasks, show important tasks. I also need a UML diagram for the code update: The "task" is like something in a to do list. example) task 1. name: get carrots important: no completed: yes. you can run my code as an example if needed
Write a static method called generateSubstrings that adds all subsets of the letters in its first...
Write a static method called generateSubstrings that adds all subsets of the letters in its first argument to the ArrayList that is its second argument. For example,
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
please use java swing and recursion and the suggested method hints listed Objective: Write a program...
please use java swing and recursion and the suggested method hints listed Objective: Write a program in which draws (yes it actually makes a picture) a triangular fractal using recursion. This is best if done using a java applet. Suggested Methodology The idea for it is this First draw a filled equilateral triangle Next draw another filled equilateral triangle of a different color that’s upside down in the middle of that triangle Using the other triangles formed repeat step 2...
For this coding exercise, you need to create a new Java project in Eclipse and finish...
For this coding exercise, you need to create a new Java project in Eclipse and finish all the coding in Eclipse. Run and debug your Eclipse project to make sure it works. Then you can just copy and paste the java source code of each file from Eclipse into the answer area of the corresponding box below. The boxes will expand once you paste your code into them, so don’t worry about how it looks J All data members are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT