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

Please code in C# - (C - Sharp) Assignment Description Write out a program that will...
Please code in C# - (C - Sharp) Assignment Description Write out a program that will ask the user for their name; the length and width of a rectangle; and the length of a square. The program will then output the input name; the area and perimeter of a rectangle with the dimensions they input; and the area and perimeter of a square with the length they input. Tasks The program needs to contain the following A comment header containing...
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...
Description: The purpose of this assignment is to write code that calculates the amount of interest...
Description: The purpose of this assignment is to write code that calculates the amount of interest accrued on a credit card on its expenses. You will create a C program that prompts the user for the total amount of purchases on the credit card, an annual percentage rate (APR), and the number of days the money is borrowed. The program will calculate the amount of the compound interest for the purchases, then print the original amount of purchases, the amount...
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...
For c++, please do not recycle other peoples code as they do not satisfy the requirements....
For c++, please do not recycle other peoples code as they do not satisfy the requirements. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr. The...
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?
This C++ CODE has many functions that needs to be tested piece by piece For example,...
This C++ CODE has many functions that needs to be tested piece by piece For example, you going to display each function with [ ] and a , separating each piece of data, like so: [1,2,3,4]? results must be copied and pasted as commnets at the bottom of this file this is the code: #include <iostream> #include <string> #include <cstddef> //for NULL using namespace std; class List {    private:        struct Node        {           ...
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...
C# Tip Calculator. I can't figure the code out for this assignment can I get some...
C# Tip Calculator. I can't figure the code out for this assignment can I get some help For this assignment, you'll create a simple tip calculator. Expected program flow: Ask the user for the bill total. Ask the user for the tip percent. Print the final output in the following format when the user enters 10 and 15 for the first two prompts: Total for bill $10.00 with a 15% tip is $11.50 Note that the money values should be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT