Question

In: Computer Science

Please code in C# (C-Sharp) Assignment Description A pirate needs to do some accounting and has...

Please code in C# (C-Sharp)

Assignment Description

A pirate needs to do some accounting and has asked for your help. Write a program that will accept a

pirate’s starting amount of treasure in number of gold pieces. The program will then run one of two

simulations, indicated by the user:

1) The first simulation runs indefinitely, until one of two conditions is met: the pirate’s treasure

falls to 0 or below, or the pirate’s treasure grows to 1000 or above.

2) The second simulation runs for a number of years set by the user.

For both simulations, each year the pirate has an equal chance to either gain or lose 50 gold pieces. At

the end of each year, the pirate’s total gold and the year is displayed to the user. Validate all user input.

Tasks

1) The program needs to contain the following

a.

A comment header containing your name and a brief description of the program

b. At least 5 comments besides the comment header throughout your code

c.

A prompt for the starting treasure amount

d. A prompt to choose a simulation

i. The first simulation continues until the treasure amount becomes 1000 or more

or 0 or less

ii. The second simulation will prompt the user for number of years, then simulate

that many years. The gold amount can go below zero

e. For both simulations, treasure amount has an equal chance to increase by 50 or

decrease by 50 each year

f.

Output the year and treasure amount after each year

g.

“Press enter to continue” and Console.ReadLine(); at the end of your code

h. Validate all user input. Either through exception handling or boolean logic.

2) Upload a completed .cs file onto the Assignment 5 submission folder and a word document

containing the following six (6) screenshots:

a.

One run of the first simulation, starting amount 500

i. Your screenshot only needs to show up to the last 10 years

b. Two runs of the second simulation, starting amount 300 for 10 years and starting

amount 500 for 20 years

c.

Three test runs with invalid input for the following: starting treasure amount, simulation

choice, number of years

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

using System;
using System.Collections.Generic;
                  
public class Program
{
    //Random object for random number generation
    static Random random=new Random();
    
    //method to run the first type simulation, given the number of starting
    //pieces of gold
    public static void runSimulation1(int pieces){
        int year=0;
        //displaying gold at initial year
        Console.WriteLine("Year: "+year+", Gold: "+pieces);
        //looping until pieces becomes 0 or negative or 1000 or above
        while(pieces>0 && pieces<1000){
            //generating an integer between 0 and 99
            if(random.Next(100)<50){
                //if the number is less than 50 (50%), subtracting 50 pieces
                pieces-=50;
            }else{
                //otherwise, adding 50 pieces
                pieces+=50;
            }
            //updating year and displaying current values
            year++;
            Console.WriteLine("Year: "+year+", Gold: "+pieces);
        }
    }
        
    //method to run the second type simulation, given initial gold value and
    //number of years to simulate
    public static void runSimulation2(int pieces, int years){
        //displaying gold at initial year
        Console.WriteLine("Year: 0, Gold: "+pieces);
        //looping for years number of times
        for(int year=1;year<=years;year++){
            //repeating the same process as the below method
            if(random.Next(100)<50){
                pieces-=50;
            }else{
                pieces+=50;
            }
            Console.WriteLine("Year: "+year+", Gold: "+pieces);
        }
    }
    
    //helper method to read a valid positive integer input from user
    static int getInt(string prompt){
        bool done=false;
        int num=0;
        //looping until done is true
        while(!done){
            //displaying prompt
            Console.Write(prompt);
            //checking if next line contains a valid number, if yes, reading
            //to num
            if(int.TryParse(Console.ReadLine(),out num)){
                   if(num>0){
                    //done, end of loop
                       done=true;
                   }else{
                    //negative value
                       Console.WriteLine("Value must be positive.");
                   }
               }else{
                //non numeric input
                   Console.WriteLine("Value must be an integer");
               }
        }
        //returning the value
        return num;
    }
    
    public static void Main(){
        //reading initial number of gold
        int gold=getInt("Enter a starting amount of gold: ");
        //displaying simulation options
        Console.WriteLine("1. Infinite simulation until gold reaches 0 or 1000");
        Console.WriteLine("2. Simulation for specified number of years");
        //reading choice
        int choice=getInt("Your choice: ");
        //identifying choice
        if(choice==1){
            //running simulation 1
            runSimulation1(gold);
        }else if(choice==2){
            //reading years and running simulation 2
            int years=getInt("Please enter the number of years: ");
            runSimulation2(gold,years);
        }else{
            //no other choices are accepted
            Console.WriteLine("That's an invalid option!");
        }
        
        
           Console.WriteLine("\nPress enter to continue");
           Console.ReadLine();
    }
}

/*OUTPUT (multiple runs)*/



Related Solutions

This code needs to be in C++, please. Step 1: Add code to prompt the user...
This code needs to be in C++, please. Step 1: Add code to prompt the user to enter the name of the room that they are entering information for. Validate the input of the name of the room so that an error is shown if the user does not enter a name for the room. The user must be given an unlimited amount of attempts to enter a name for the room. Step 2: Add Input Validation to the code...
PLEASE DO IN C++ (Math: The Complex class) The description of this project is given in...
PLEASE DO IN C++ (Math: The Complex class) The description of this project is given in Programming Exercise 14.7 in the Chapter 14 Programming Exercise from the Book section. If you get a logical or runtime error, please refer https://liangcpp.pearsoncmg.com/faq.html. Design a class named Complex for representing complex numbers and the functions add, subtract, multiply, divide, abs for performing complex-number operations, and the toString function for returning a string representation for a complex number. The toString function returns a+bi as...
Please do it in C++. Please comment on the code, and comments detail the run time...
Please do it in C++. Please comment on the code, and comments detail the run time in terms of total operations and Big O complexities. 1. Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase letters in an arbitrary order and uses that as the encoder for a cipher (that is, A is mapped to the first character of the parameter, B is mapped to the second, and so on.) Please derive the decoding...
Assignment Details This assignment has 3 parts: How do accounting policies and practices affect financial accounting...
Assignment Details This assignment has 3 parts: How do accounting policies and practices affect financial accounting information used for "external" decision making purposes? What governing and oversight bodies exist to help ensure timely and accurate reporting of financial information by publicly traded companies? How to internal controls help ensure that financial results are accurately and fairly presented for use by external users?
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header containing your name and a brief description of the program Output prompting the user for the following inputs: Name as a string Length of a rectangle as a double Width of a rectangle as a double Length of a square as an int After accepting user input, the program outputs the following: User name Area of a rectangle with dimensions matching the inputs Area...
PROGRAM DESCRIPTION: In this assignment, you are provided with working code that does the following: 1....
PROGRAM DESCRIPTION: In this assignment, you are provided with working code that does the following: 1. You input a sentence (containing no more than 50 characters). 2. The program will read the sentence and put it into an array of characters. 3. Then, it creates one thread for each character in the sentence. 4. The goal of the program is to capitalize on each letter that has an odd index. The given program actually does this but lacks the synchronization...
C++ This needs to be a recursive combinational array please. This NEEDS to be recursive and...
C++ This needs to be a recursive combinational array please. This NEEDS to be recursive and no vectors. Thank you Write a program that solves the knapsack problem recursively for an arbitrary knapsack capacity and series of weights. Assume the weights are sorted in an array. Hint: The arguments to the knapsack function are target weight and the array index where the remaining items start. The knapsack problem in its simplest form involves trying to fit items of different weights...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void remove_node(struct linked_list* list, int rm_node_value) (the function to make) This function removes a node with specified value. If there is only one node in the list, remove the node and remove the list also since there is nothing left. While removing a node, the node should be perfectly freed. If the type of list is...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void pop_Stack (struct linked_list* list, int number) //*This is the function to make and below is the explanation that should be written in given code. This function removes some nodes in stack manner; the tail of the list will be removed, repeatedly. The parameter (variable number_of_nodes) means the number of nodes that will be removed. When...
Please do this in C++ only. Please Show your code and your output too. Would you...
Please do this in C++ only. Please Show your code and your output too. Would you focus on number 2 in the Required functions:? Please use all the variables in the program. The assignment problem: You are asked to develop an application that prints a bank’s customers' names, IDs, and accounts balances in different ways. In this program, you need to read a file of customer’s data into different arrays, pass these arrays to functions as (array parameter), calculate the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT