Screenshot
--------------------------------------------
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
Instructions We're going to create a program which will allow the user to add values to a list, p...
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 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 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 (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 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 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 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 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 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) { 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...