Question

In: Computer Science

Hello I have a question about Java. example So when you put “sum 1 2 3”...

Hello I have a question about Java.

example

So when you put “sum 1 2 3” in console, you do not separate those. Need a space between sum 1, 2 in the console.

What is Sum?

sum 1 2 3

6 (answer)

sum 123456

21

sum 100 2000 3000

5100

sum 20 50 40 1

111

public static void main (String[] args) {

System.out.Println(“What is Sum?”);

String a=“”;

a = scnr.nextLine();

String[] b = a.split(“”);

if(b[0].equals(“sum”) {

}

Please do not change the above code. If I put sum in the console, it calculate the sum.

you may need to convert type from String to double.

I don’t know how to make code for the sum. Please help me by above code.

Solutions

Expert Solution

ANSWER: Here I am giving you the code and output if you have any problem then comment or like it please.

CODE:

import java.util.Scanner;

public class ReferenceMystery {

   /**
   * @param args
   */
   static int getSum(int n) // method to find the sum of a digit
{   
int sum = 0;
  
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
  
return sum;
}
  
   public static void main(String[] args) {
       Scanner scnr= new Scanner(System.in);
      
       System.out.println("What is Sum?");

       String a="";

       a = scnr.nextLine();

       String[] b = a.split(" ");
      
       int add=0;
       if(b[0].equals("sum") ){
           if(b.length==2) { // when your array has only 2 elements only
               int a1=Integer.parseInt(b[1]);
               int add0=getSum(a1);
               System.out.println(add0);
              
           }else {
           for(int i=1;i<b.length;i++)
               add=add+Integer.parseInt(b[i]);
               System.out.println(add);
           }
       }
      
       }

}

OUTPUT:


Related Solutions

hello! So I have this CIS assignment lab but when I try to make the code...
hello! So I have this CIS assignment lab but when I try to make the code I don't really know where to start from. My professor is very hard and he likes to see the outcomes as they are shown in the problem. Please help me! Write a program that can be used as a math helper for an elementary student. The program should display two random integer numbers that are to be added, such as:     247 + 129...
Hello! I have a question about the following problem, not sure how to approach it... This...
Hello! I have a question about the following problem, not sure how to approach it... This is from Mathematical Statistics and Data Analysis, Chapter 11, Problem 11.41 The Hodges-Lehmann shift estimate is defined to be d =median(Xi −Yj), where X1 , X2 , . . . , Xn are independent observations from a distribution F and Y1,Y2,...,Ym are independent observations from a distribution G and are independent of the Xi. Show that if F and G are normal distributions, then...
In Java please. I put down my code and what I was able to achieve so...
In Java please. I put down my code and what I was able to achieve so far: public class Animal {   private String gender; //stores the gender of the animal    private String type; //stores the type of the animal(bear of fish)    private int strength; //stores the strength of the animal    public Animal() {        gender = "none";        type = "none";        strength = 0;    }        public Animal (String g, String...
I have created a method in java in my menu list and I have to put...
I have created a method in java in my menu list and I have to put all the work into the method but I cannot get it to print and am not sure where I am going wrong. this is my code: public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); String fName = ""; String lName = ""; double hoursWorked; double hourlyRate; int optionOne = 1; int optionTwo = 2; int optionThree...
I have 1 done, but am unsure about 2 and 3 You have just been hired...
I have 1 done, but am unsure about 2 and 3 You have just been hired as a new management trainee by Earrings Unlimited, a distributor of earrings to various retail outlets located in shopping malls across the country. In the past, the company has done very little in the way of budgeting and at certain times of the year has experienced a shortage of cash. Since you are well trained in budgeting, you have decided to prepare a master...
Language: Java So I am trying to find the MAJOR and MINOR diagonal SUM and AVERAGE...
Language: Java So I am trying to find the MAJOR and MINOR diagonal SUM and AVERAGE of a 2d matrix using only ONE class. However, the output gives me the incorrect calculations. This is my class: public static void MajorAndMinorDiagonalSumAndAvg (Scanner user, int rows, int coluumn, int [][] array) { double majorarray = 0; double majorarraycount = 0; double minorarray = 0; double minorarraycount = 0; for (int i = 0; i<array.length; i++) for(int j =0; j<array[i].length; j++) { majorarray...
Hello, I have a question about the heat equation with Non-homogeneous Boundary Conditions in Differential Equations....
Hello, I have a question about the heat equation with Non-homogeneous Boundary Conditions in Differential Equations. u_t = 4u_xx u(0, t) = 2 u_x(3, t) = 0 u(x, 0) = x. If available, could you explain the solution in detail? Thank you.
Hello, I am currently taking software design and analysis class and have a question about those...
Hello, I am currently taking software design and analysis class and have a question about those 3 in cpp, can you please help me? thank you - What is the complexity of pop()? push()? top()? size()? isEmpty()? - Write a C++ implementation of push() in a linked-chain implementation of stack - Write a C++ implementation of pop() in a linked-chain implementation of stack
Hello I have these questions, I would like not so long answers but something that gives...
Hello I have these questions, I would like not so long answers but something that gives me a good idea about the topic. What is a perfectly competitive market? Why are many agricultural products in a perfectly competitive market? What is a price taker? What are barriers to entry in a market? What is a standardized product? Give an example What is a company’s market share? What is free entry and exit? How do you calculate a company’s total revenues?...
Hello I have these questions. I need good answers in order to undertand not so long...
Hello I have these questions. I need good answers in order to undertand not so long nor so short please Why are consumers willing to pay more for small portions? What is the substitution effect of a higher price on a consumer? What is the income effect of a higher price on a consumer?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT