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 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
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...
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,
only JAVA code /** Create a method as instructed below and then call it appropriately. */...
only JAVA code /** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public class MoreBankCharges { //Constant declarations for base fee and per check fees //Class scope so constants are accessible by all methods static final double BASE_FEE = 10.0; static final double LESS_THAN_20_FEE = 0.10; static final double TWENTY_TO_THIRTYNINE_FEE = 0.08; static final double FORTY_TO_FIFTYNINE_FEE = 0.06; static final double SIXTY_OR_MORE_FEE = 0.04; public static void main(String[] args) { //Variable declarations int numChecks;...
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
In java please create a method that will insert a value after a specific node for...
In java please create a method that will insert a value after a specific node for a linked list. public void insertAfter(Node a, int newData){ }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT