In: Computer Science
C# programming
This application will calculate the speed of an interstellar spacecraft as it accelerates though our galaxy to explore the stars. The spacecraft is using a new engine technology that continues to accelerate over time with no limit, allowing the spacecraft to reach distant areas of the galaxy at record speed. The spacecraft starts out slowly, but for each day it accelerates, it will double the speed it has attained prior to that point. The spacecraft will accelerate from 0 MPD (Miles Per Day) at launch to 1000 MPD by the end of the first day, and will have covered 500 miles (average speed of (0 + 1000)/2= 500). At the end of day two, it will be up to 2000 MPD (double day one's speed), and will have covered 2000 miles total (the previous day’s 500 miles plus the new day’s average of (1000+2000)/2)). Create an app that allows the user to enter the number of days the spacecraft has been traveling and your app will use the formulae given, plus some looping code, to tell the user what the speed of the spacecraft is (in MPD) at the end of that day. Your app will also calculate the total distance traveled by the spacecraft to the end of that day (also done with the fomulae and some looping code). Create a form with the appropriate controls to get the user input and display the answers correctly. Ensure you do proper data validation so that any mistakes the user makes entering data do not crash the program or create/allow strange results
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace InterstellarSpaceCraftForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
int days = Convert.ToInt16(textBox1.Text);
double speed = 1000;
double miles = 1000;
double totalKm = 0;
for (int i = 1; i <= days; i++)
{
miles = miles * i;
speed = miles / 2;
totalKm += miles;
listBox1.Items.Add(speed + " " + totalKm);
}
}
catch(Exception ex)
{
MessageBox.Show("Enter valid input");
textBox1.Text = "";
}
}
}
}
Designer:
namespace InterstellarSpaceCraftForm
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should
be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif",
20F);
this.label1.Location = new System.Drawing.Point(9, 129);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(272, 31);
this.label1.TabIndex = 0;
this.label1.Text = "Enter number of days";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(323, 140);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(108, 20);
this.textBox1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(171, 204);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(114, 38);
this.button1.TabIndex = 2;
this.button1.Text = "Calculate";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(84, 294);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 95);
this.listBox1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(506, 405);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
}
}
Please rate it if the above solution helps you in any way or if you have any concerns comment it, I will help you through again.
Form1 - O X Enter number of days 5 | Calculate 500 1000 1000 3000 3000 9000 12000 33000 60000 153000