Question

This is to be in C# and this project is being made in Microsoft Visual Studio...

This is to be in C# and this project is being made in Microsoft Visual Studio

Create a Date class that does the following:
o A default constructor
o A constructor with a string argument
o An integer member variable that represents the date in the following format:
 YYYYMMDD
 i.e. 20070212 means February 12, 2007
o A string member variable that represents the date in the following format:
 YYYY-MM-DD
o An integer member variable that represents a year
o An integer member variable that represents a month
o An integer member variable that represents a day of the month
o A method that converts a YYYY-MM-DD into YYYYMMDD
o A method that converts a YYYYMMDD into YYYY-MM-DD
o A method that extracts only the year from YYYYMMDD
o A method that extracts only the month from YYYYMMDD
o A method that extracts only the day from YYYYMMDD

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DateDemo
{
class Date
{
public string dateStr;
public int dateInt;
public int year,month,day;

public Date()
{
dateStr = "YYYY-MM-DD";
}
public Date(string date)
{
dateStr = date;
}

public void ConvertStrDateToIntDate()
{
dateStr = dateStr.Replace("-", ""); //removing -
dateInt = Convert.ToInt32(dateStr);
Console.WriteLine("Converted YYYY-MM-DD into YYYYMMDD: " + dateStr);
ConvertIntDateToStrDate();//calling ConvertIntDateToStrDate() function
}

public void ConvertIntDateToStrDate()
{
string newDateStr = Convert.ToString(dateInt);
newDateStr = newDateStr.Insert(4, "-"); //inserting -
newDateStr = newDateStr.Insert(7, "-");
Console.WriteLine("Converted YYYYMMDD into YYYY-MM-DD: " + newDateStr);
}

public void extractYear()
{
year = Convert.ToInt32(dateStr.Substring(0, 4));
Console.WriteLine("Extracted year: "+year);
}

public void extractMonth()
{
month = Convert.ToInt32(dateStr .Substring(4,2));
Console.WriteLine("Extracted month: " + month);
}

public void extractDay()
{
day = Convert.ToInt32(dateStr.Substring(6, 2));
Console.WriteLine("Extracted month: " +day); }
}
class Program
{
static void Main(string[] args)
{
string dateS1;
Console.Write("Enter date string: ");
dateS1 = Console.ReadLine();
Date d1 = new Date(dateS1);
d1.ConvertStrDateToIntDate();
d1.extractYear();
d1.extractMonth();
d1.extractDay();
Console.ReadKey();
}
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
This is to be in C# and this project is being made in Microsoft Visual Studio...
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
  • Create a class called Date212 to represent a date. It will store the year, month and...

    Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String in the form yyyymmdd (such as 20161001 for October 1, 2016), so you will need three private instance variables. Two constructors should be provided, one that takes three integer parameters, and one that takes a String. The constructor with the String parameter and should validate the parameter. As you are reading the dates you should check that...

  • Create class Date with the following capabilities: a) Output the date in multiple formats, such as...

    Create class Date with the following capabilities: a) Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY b) Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first of which represents the day...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • Need help writing beginner C# program, I will make sure to provide feedback to whoever can...

    Need help writing beginner C# program, I will make sure to provide feedback to whoever can help me figure it out! No public or global variables should be used. You need to consider passing arguments between the methods according to the ways described in the lecture. i.e. all variables should be declared inside the methods and passed to other methods by value/ref/out as needed Description: We want to design a Date class to represent a date using three integer numbers...

  • I need this in Java, please! The ISO 8601 Standard date format for Information Interchange indicates...

    I need this in Java, please! The ISO 8601 Standard date format for Information Interchange indicates that a date be written as such: yyyy-MM-dd (eg. 2012-07-02, 1999-12-05, 1998 -01-27 ) where yyyy represents the four digit year MM represents a two digit numerical month dd represents a two digit numerical day Chinese date format is specified as: yyyy-M-d Macedonean date format is specified as: d.M.yyyy where yyyy represents the four digit year M represents a one or two digit numerical...

  • CODING IN JAVA Dates are printed in several common formats. Two of the more common formats...

    CODING IN JAVA Dates are printed in several common formats. Two of the more common formats are: 07/21/55 and July 21, 1955 Write a program that reads a date in the first format and prints that date in the second format. Prompt the user and accept user input for the date in MM/DD/YYYY format. Retrieve the month portion of the date entered and convert it to an integer. Do the same thing for day and year Create a string named...

  • Given the following date class interface: class date {private: int month;//1 - 12 int day;//1 -...

    Given the following date class interface: class date {private: int month;//1 - 12 int day;//1 - 28. 29. 30. 31 depending on month & year int year;//4-digit, e.g.. 2017 public: date();//Default constructor (investigate; find what it is used for)//Postcondition: the newly declared date object is initialized to 01/01/2000 date(int mm, int dd, int yyyy);//Second constructor//Postcondition: the newly declared data object is initialized to mm/dd/yyyy void setDate(int mm. int dd. int yyyy);//Postcondition: set the contents of the calling date object to...

  • C Programming Quesition (Structs in C): Write a C program that prompts the user for a...

    C Programming Quesition (Structs in C): Write a C program that prompts the user for a date (mm/dd/yyyy). The program should then take that date and use the formula on page 190 (see problem 2 in the textbook) to convert the date entered into a very large number representing a particular date. Here is the formula from Problem 2 in the textbook: A formula can be used to calculate the number of days between two dates. This is affected by...

  • Write a c# program in Microsoft visual studio to determine if the following numbers are even...

    Write a c# program in Microsoft visual studio to determine if the following numbers are even or not. Create an app that will read integers from an input file name Number.txt that will consist of one integer. Determine which numbers are even and which are odd. Write the even numbers to a file named Even.txt and the odd numbers to a file named Odd.txt. Number.txt 20 39 45 12 31 62 10 11 21 73 14 42 55 86 109...

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