Question

Instructions Were going to create a program which will allow the user to add values to a list, print the values, and print t
public statie void AddValueToListo // code goes here Remember, we can use the Add) method on the List class to add items to a
Instructions We're going to create a program which will allow the user to add values to a list, print the values, and print the sum of all the values. Part of this program will be a simple user interface. 1. In the Program class, add a static list of doubles: private statie List _values new List) this is the list of doubles well be working with in the program. 2. Add ReadInteger (string prompt) , and ReadDouble(string prompt) to the Program class. These should be identical to those in 5.1P. 3. Add a public enum named Useroption Give it the values/options of: Newvalue Sum, Quit . This enumeration will be used later on in the program to create the user Print interface menu. and public enum Useroption // values 4. One of the actions our user will be able to take is to add a new value into the list. Let's create the method that will do this. Here is something to get you started:
public statie void AddValueToListo // code goes here Remember, we can use the Add) method on the List class to add items to a list, and we can use the ReadDouble() method to get a value from the user. 5. The second action our program can perform is printing values to the screen. To do this, create another public static void method named Print() .In Print0, use a foreach loop to iterate over values ,printing each value to the console. 6. Finaly, we need to be able to sum the values. This is yet another public static void method, which should do the following: Create a local double variable called sum and initialise it to O Use a foreach loop to iterate over each element in values - .Each iteration, add the value in each element to the sum. Write sum to the console. 7. Now that we have the functionality of our list implemented in our code, let's add the code which will allow the user to interact with the program! Let's take a look at the ReadUserOption) method: public static Useroption ReadUseroption() Console.WriteLine ( "Enter 0 to add a value") Console.WriteLine( "Enter 1 to add a sus all values) Console.WriteLine( Enter 2 to print a sum all values) Console.WriteLine("Enter 3 to quit) int option 3: Int.32.TryParse(Console.Readtine) out option)s return (Useroption)option You can see it simply presents the possible options to the user, and then reads the option from the user. Note that by default, the method will return 3 (exit). ° Create the Main ( ) method. The Main method should loop until the user opts to quit, each time it should switch (ReadUseroption()) value to call the appropriate method and use the returned Useroption
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

uick Launch (Ci+a Fie Feit w Pmjert Build Dehug Team Toele Tet Anaye Windew Help //Packages using System; using System.Collec--------------------------------------------

Program

//Packages
using System;
using System.Collections.Generic;
namespace Program
{
    class Program
    {
        //Create a list
        private static List<double> _values = new List<double>();
        //Enum variable
        public enum UserOption
        {
            NewValue,
            Sum,
            Print,
            quit
        }
        //Main method
        static void Main(string[] args)
        {
            //Call method to get user choice
            UserOption option = ReadUserOption();
            //Loop unti quit
            while (option != UserOption.quit)
            {
                //Select each option
                switch (option)
                {
                    //Add a new value into list
                    case UserOption.NewValue:
                        AddValueToList();
                        break;
                    //Print the list
                    case UserOption.Print:
                        Print();
                        break;
                    //Find sum of the elements in the list
                    case UserOption.Sum:
                        Sum();
                        break;
                    default:
                        break;
                }
                //Repeat
                Console.WriteLine();
                option = ReadUserOption();
            }
          
        }
        //Method to read value for the list input
        public static double ReadDouble(String prompt)
        {
            Console.Write(prompt);
            double value;
            if (!double.TryParse(Console.ReadLine(), out value))
            {
                Console.WriteLine("Not a double value!!");
            }
            return value;
        }
        //Method to add values intp list
        public static void AddValueToList()
        {
            _values.Add(ReadDouble("Enter a double value into list: "));
        }
        //Method to print elements in the list
        public static void Print()
        {
            Console.Write("List: [ ");
            foreach(double value in _values)
            {
                Console.Write(value + " ");
            }
                Console.Write("]");
        }
        //Method to find sum of all the elements in the list
        public static void Sum()
        {
            double sum = 0;
            foreach (double value in _values)
            {
                sum += value;
            }
            Console.WriteLine("Sum of all values in the list is " + sum);
        }
        //User choice display
        public static UserOption ReadUserOption()
        {
            Console.WriteLine("Enter 0 to add a value");
            Console.WriteLine("Enter 1 to add a sum all values");
            Console.WriteLine("Enter 2 to print a sum all values");
            Console.WriteLine("Enter 3 to quit");
            int option = 3;
            Int32.TryParse(Console.ReadLine(), out option);
            return (UserOption)option;
        }

    }
}

------------------------------------------------------------

Output

Enter 0 to add a value
Enter 1 to add a sum all values
Enter 2 to print a sum all values
Enter 3 to quit
0
Enter a double value into list: 5.5

Enter 0 to add a value
Enter 1 to add a sum all values
Enter 2 to print a sum all values
Enter 3 to quit
0
Enter a double value into list: 6.2

Enter 0 to add a value
Enter 1 to add a sum all values
Enter 2 to print a sum all values
Enter 3 to quit
0
Enter a double value into list: 7.1

Enter 0 to add a value
Enter 1 to add a sum all values
Enter 2 to print a sum all values
Enter 3 to quit
1
Sum of all values in the list is 18.8

Enter 0 to add a value
Enter 1 to add a sum all values
Enter 2 to print a sum all values
Enter 3 to quit
2
List: [ 5.5 6.2 7.1 ]
Enter 0 to add a value
Enter 1 to add a sum all values
Enter 2 to print a sum all values
Enter 3 to quit
3

Add a comment
Know the answer?
Add Answer to:
Instructions We're going to create a program which will allow the user to add values to a list, p...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For the following C# program: Let’s add one more user defined method to the program. This...

    For the following C# program: Let’s add one more user defined method to the program. This method, called ReverseDump is to output the values in the array in revere order (please note that you are NOT to use the Array.Reverse method from the Array class). The approach is quite simple: your ReverseDump method will look very similar to the Dump method with the exception that the for loop will count backwards from num 1 to 0 . The method header...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

  • // Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class...

    // Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class (SwitchDoLab) //--> {    //   Declare the main method    //-->    {        //   Declare Constant integers SUM = 1, FACTORIAL = 2, QUIT = 3.        //-->        //-->        //-->        //   Create an integer variable named choice to store user's option.        //-->        //   Create a Scanner object        //   Create...

  • Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a...

    Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a method named FillArray to interactively fill the array with any number of values up to 10 or until a sentinel value (999) is entered. If an entry is not an integer, reprompt the user. Call a second method named Statistics that accepts out parameters for the highest value in the array, lowest value in the array, sum of the values in the array, and...

  • Let's fix the displayMenu() method! First, edit the method to do the following: We want to...

    Let's fix the displayMenu() method! First, edit the method to do the following: We want to also allow the user to quit. Include a "Quit" option in the print statements! Have it get the user input from within the method itself and return an int based on the user's choice. (Make sure to also edit the method header and how it is called back in the main() method!) What is the method's return type now?                  ...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • You may not add any instance variables to any class, though you may create local variables...

    You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here.    Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [ ] Name: courseCreditArray Access...

  • You may not add any instance variables to any class, though you may create local variables...

    You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here. (I need step 2 please.) Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [...

  • This is for a java program public class Calculation {    public static void main(String[] args)...

    This is for a java program public class Calculation {    public static void main(String[] args) { int num; // the number to calculate the sum of squares double x; // the variable of height double v; // the variable of velocity double t; // the variable of time System.out.println("**************************"); System.out.println(" Task 1: Sum of Squares"); System.out.println("**************************"); //Step 1: Create a Scanner object    //Task 1. Write your code here //Print the prompt and ask the user to enter an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT