Question

In: Computer Science

HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT...

HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT BY THE USER AS SPECIFIED IN THEH FIRST PART OF THE QUESTION?

Ask the user to enter a number and display the number, followed by ***, followed by the number squared, followed by &&&, and followed by the number cubed. Allow the user to enter as many numbers as he/she wishes. So, use a reasonable sentinel value to end the loop (for example, -999). Don’t use Yes/No type prompts to exit the loop. Also, calculate and display the total of the numbers entered by the user, total of the squares and the total of the cubes. If the user enters the sentinel value at the beginning of the process, display a message that there is nothing to process.

import java.util.*;

public class numbers {

   public static void main(String args[])

   {

       Scanner scan = new Scanner(System.in);

       int tmp,tmp1;

       int sum=0,sq_sum=0,cu_sum=0,count=0;

       while(true)

       {

           System.out.print("Enter a number(-999 to exit):");

           tmp = scan.nextInt();

           if(tmp==-999)

           {

               break;

           }

           sum += tmp;

           tmp1 = tmp*tmp;

           sq_sum += tmp1;

           cu_sum += tmp1*tmp;

           count++;

       }

       if(count==0)

       {

           System.out.println("Nothing to process");

       }

       else

       {

           System.out.println("There are total of "+count+" numbers.");

           System.out.println("The sum of all numbers is "+sum);

           System.out.println("The sum of sqaures all numbers is "+sq_sum);

           System.out.println("The sum of cubes all numbers is "+cu_sum);

       }

   }

}

Solutions

Expert Solution

// Numbers.java
import java.util.*;
public class Numbers {
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int tmp,tmp1, tmp2;
int sum=0,sq_sum=0,cu_sum=0,count=0;
while(true)
{
System.out.print("Enter a number(-999 to exit):");
tmp = scan.nextInt();
if(tmp==-999)
{
break;
}

sum += tmp;
tmp1 = tmp*tmp;
sq_sum += tmp1;
tmp2 = tmp1*tmp;
cu_sum += tmp2;

count++;

System.out.println(tmp+" *** " + tmp1 + " &&& " + tmp2);
}
if(count==0)
{
System.out.println("\nNothing to process");
}
else
{
System.out.println("\nThere are total of "+count+" numbers.");
System.out.println("The sum of all numbers is "+sum);
System.out.println("The sum of squares all numbers is "+sq_sum);
System.out.println("The sum of cubes all numbers is "+cu_sum);
}
}
}

/*
output:

Enter a number(-999 to exit):1
1 *** 1 &&& 1
Enter a number(-999 to exit):2
2 *** 4 &&& 8
Enter a number(-999 to exit):3
3 *** 9 &&& 27
Enter a number(-999 to exit):4
4 *** 16 &&& 64
Enter a number(-999 to exit):5
5 *** 25 &&& 125
Enter a number(-999 to exit):6
6 *** 36 &&& 216
Enter a number(-999 to exit):7
7 *** 49 &&& 343
Enter a number(-999 to exit):8
8 *** 64 &&& 512
Enter a number(-999 to exit):9
9 *** 81 &&& 729
Enter a number(-999 to exit):10
10 *** 100 &&& 1000
Enter a number(-999 to exit):-999

There are total of 10 numbers.
The sum of all numbers is 55
The sum of sqaures all numbers is 385
The sum of cubes all numbers is 3025


*/


Related Solutions

QUESTION: Add code so that if no parameter values are specified for method “main”, then the...
QUESTION: Add code so that if no parameter values are specified for method “main”, then the error message “file name expected” is printed out instead of the "Opening file " message. Hint: If no parameter values are specified for method “main”, then the length of the array “args” will be zero. If that error message “file name expected” is printed out, the program should stop. You can make any Java program stop using this line … System.exit(0); skeleton code: /**...
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
How do i get the code below to add an element before another without making the...
How do i get the code below to add an element before another without making the array off one element? (javascript) public void addBefore(double element) { itemCount++; double data[] = new double[this.data.length]; if(currentIndex <= itemCount) { if(currentIndex != 0) { for(int index = currentIndex; index >= itemCount; index ++) { data[index] = this.data[index]; } currentIndex--; data[currentIndex] = element; } if(currentIndex == 0) { data[0] = element; currentIndex = 0; } } }
How do I get the following code to accept a float input, while also fixing the...
How do I get the following code to accept a float input, while also fixing the syntax errors in the code because I can't figure out what is wrong with it. def chemical_strength(pH): if 0<pH>14: if pH <= 7: if pH = 7: ans = str("neutral") else: # ph < 7 if 0<pH>=1: ans = str("very strong acid") else: # 1<pH>7 if 1<pH>4: ans = str("strong acid") else: # 4=<ph>7 if pH = 4: ans = str("plain acid") else: #...
JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
C++ Add a function that finds & displays all the words that begin with the letter...
C++ Add a function that finds & displays all the words that begin with the letter 's'. The text file "dickens.txt" is as follows: It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was...
Can you add more comments explaining what this code does? i commented what I know so...
Can you add more comments explaining what this code does? i commented what I know so far #include<stdio.h> #include<pthread.h> #include<semaphore.h> #include<unistd.h> sem_t mutex,writeblock; int data = 0,rcount = 0; int sleepLength = 2; // used to represent work void *reader(void *arg) { int f; f = ((int)arg); sem_wait(&mutex); // decrement by 1 if rcount = rcount + 1; if(rcount==1) sem_wait(&writeblock); sem_post(&mutex); printf("Data read by the reader%d is %d\n",f,data); //shows current reader and data sleep(sleepLength); // 1 second of "work" is...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that the following test cases work. Note: I have removed all the unnecessary inherited List implementations. I have them to: throw new UnsupportedException(); For compilation, you could also add //TODO. Test (Main) List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size...
Hello, I Have create this code and Tried to add do while loop but it gives...
Hello, I Have create this code and Tried to add do while loop but it gives me the error in string answar; and the areas where I blod So cloud you please help me to do ( do while ) in this code. // Program objective: requires user to input the data, program runs through the data,calcualtes the quantity, chacks prices for each iteam intered,calautes the prices seperatly for each item, and calculates the amount due without tax, and then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT