1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Zielflug [23.3K]
4 years ago
12

Write a complete Java method that returns an integer. The method declares a Scanner object, then prompts the user for a series o

f integers in a while loop. If the user enters an integer (not a string, double, etc) then read each value entered and keep a running total (a sum). Make sure to validate that the user enters only integers. Include a sentinel value (-1) to allow the user to indicate when they are done. The method returns the total.
Computers and Technology
1 answer:
igor_vitrenko [27]4 years ago
3 0

Answer:

Here is code in java.

import java.util.*;

class Main

{

 // main method of the class

public static void main (String[] args) throws java.lang.Exception

{

   try{

        // print the running total of all inputs

       System.out.println("total of all the inputs is: "+fun());

       

   }catch(Exception ex){

       return;}

}

/* function to take input from user until input is -1

 and keep a running total */

public static int fun()

{

    int in;

     //scanner class object to read the input

    Scanner scr=new Scanner(System.in);

     // variable to keep running total

    int total=0;

    System.out.println("please enter an Integer:(-1 to stop)");

     // read the input first time

    in=scr.nextInt();

    while(in!=-1)

    {

     // this loop keep taking input until input is -1.

     // calculate running total of all inputs

        total=total+in;

        System.out.println("please enter an number:(-1 to stop)::");

        in=scr.nextInt();

       

    }

     // return total of all inputs

    return total;

   

}

}

Explanation:

Create an object of scanner class in the method fun_tot(),it will take input from user.In while loop it will ask user to give input and calculate the sum of all inputs.when user enter -1 then while loop breaks and the method will return the total of all inputs given by the user.

Output:

please enter an number:(-1 to stop)::23

please enter an number:(-1 to stop)::3

please enter an number:(-1 to stop)::5

please enter an number:(-1 to stop)::76

please enter an number:(-1 to stop)::12

please enter an number:(-1 to stop)::-1

total of all the inputs is: 119

You might be interested in
1. Provide Java code for a simple class of your choice. Be sure to include at least one constructor, two methods and two fields.
Ivanshal [37]

Answer:

Explanation:

public class Main

{

public static void main(String[] args) {

 System.out.println("Test Class:");

 Name name = new Name("Dayanand","Ghelaro");

 System.out.println("First Name : "+name.getFirstName());

 System.out.println("Last Name : "+name.getLastName());

}

}

class Name{

   private String firstName;   // first name field

   private String lastName;    // last name field

   public Name(String firstName, String lastName){

       this.firstName = firstName;

       this.lastName = lastName;

   }// end of constructor

   public String getFirstName(){

       return this.firstName;

   } // end of method

   public String getLastName(){

       return this.lastName;

   }// end of method

}// end of Name class

8 0
3 years ago
Write a C++ program to find if a given array of integers is sorted in a descending order. The program should print "SORTED" if t
LenKa [72]

Answer:

The cpp program for the given scenario is as follows.

#include <iostream>

#include <iterator>

using namespace std;

int main()

{

   //boolean variable declared and initialized  

   bool sorted=true;

   //integer array declared and initialized

   int arr[] = {1, 2, 3, 4, 5};

   //integer variables declared and initialized to length of the array, arr

   int len = std::end(arr) - std::begin(arr);

       //array tested for being sorted

    for(int idx=0; idx<len-1; idx++)

    {

        if(arr[idx] < arr[idx+1])

           {

               sorted=false;

            break;

           }

    }

    //message displayed  

    if(sorted == false)

     cout<< "UNSORTED" <<endl;

 else

    cout<< "UNSORTED" <<endl;

return 0;

}

OUTPUT

UNSORTED

Explanation:

1. An integer array, arr, is declared and initialized as shown.

int arr[] = {1, 2, 3, 4, 5};

2. An integer variable, len, is declared and initialized to the size of the array created above.

int len = std::end(arr) - std::begin(arr);

3. A Boolean variable, sorted, is declared and initialized to true.

bool sorted=true;  

4. All the variables and array are declared inside main().

5. Inside for loop, the array, arr, is tested for being sorted or unsorted.  The for loop executes over an integer variable, idx, which ranges from 0 till the length of the array, arr.

6. The array is assumed to be sorted if all the elements of the array are in descending order.

7. If the elements of the array are in ascending order, the Boolean variable, sorted, is assigned the value false and for loop is exited using break keyword. The testing is done using if statement.

8. Based on the value of the Boolean variable, sorted, a message is displayed to the user.

9. The program can be tested for any size of the array and for any order of the elements, ascending or descending. The program can also be tested for array of other numeric data type including float and double.

10. All the code is written inside the main().

11. The length of the array is found using begin() and end() methods as shown previously. For this, the iterator header file is included in the program.

4 0
3 years ago
. ____________is/are the JSP ImplicitObject(s).sessionapplicationconfigAll of GivenNone of Given
Lynna [10]

Answer:

All of Given

Explanation:

JSP provides a number of implicit objects which are made available by the container and can be used directly in scriptlets. These objects include:

  • session
  • application
  • config
  • out
  • request
  • response
  • pageContext
  • page
  • exception

Example usage:

<% session.setAttribute("userid","demoid"); %>

<% application.getContextPath(); %>

<% out.println(config.getServletName()); %>

5 0
4 years ago
As you type your outline, click Demote to move a line of text
polet [3.4K]
<span>C. out one level. i hope this helps</span>
5 0
4 years ago
Read 2 more answers
When saving messages as drafts, it’s important to remember that your draft will not go live unless _______. Choose only ONE best
Nadusha1986 [10]

Answer:

Option (b) is the correct answer.

Explanation:

"Drafts" is a folder of messaging software or mail software whose works are to save the message until the user does not want to send it. So to send the message from drafts, the user needs to edit the message first then he can able to send that message. The above question asks that how a user can send the draft message. Option b suggests that a user needs to click send to send the draft message.

Hence it is a right option while the reason behind the other option is invalid are as follows--

  • Option a suggests that a user needs admin permission to send this but if that is his account then no need for admin permission.
  • Option c suggests to unlock the draft but the draft folder is not locked.
  • Option d suggests to take permission from a superuser but that is not required.
7 0
3 years ago
Other questions:
  • Andy designed a web page for a product-oriented industry using CSS properties. He used several HTML tags to create the web page.
    6·1 answer
  • In your presentation you added a text box to?
    5·1 answer
  • You can use the_______key if your cursor is at the end of the word. Use the______key if you place the cursor on the left side of
    11·1 answer
  • Iisa is creating a resume in which section should she mention her career goals
    6·1 answer
  • "In about 100 words, describe the idea behind software as a service (SaaS). In your answer, include at least three examples of e
    10·1 answer
  • List three things that sally sue can improve
    12·2 answers
  • A(n) is a list of commands that tell your computer what to do.
    14·2 answers
  • Consider the following MARIE Code: 100 If, Load X /Load the first value 101 Subt Y /Subtract the value of Y, store result in AC
    8·1 answer
  • In this section, you will use a stack to implement a program that checks if a string is balanced. Use the skeleton code provided
    5·1 answer
  • Please help i have 15 mins
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!