Question

Your instructor would like to thank to Marty Stepp and Hélène Martin at the University of Washington, Seattle, who originallyYou can construct a MyDate object as follows: MyDate name = new MyDate (month, day); To figure out the number of days until twhich it was called. The following table lists the number of days in each of the twelve months of the year: 1 2 3 4 5 6 7 8 9Development Strategy and Hints: Complete Part A before Part B, to get a good understanding of how MyDate objects work from thStyle Guidelines: For Part A, you are to solve the problem by creating and using MyDate objects as much as possible. This is1 2 using System; // if you want to use the instructors version of MyDate: using Date_SOLUTION; // leave this line UNcommeConsole.WriteLine(Now test The MyDate(3,2) constructor (the one with two parameters)); // change the testobject variable toConsole.WriteLine(Now test The MyDate(6, 31) constructor (the one with two parameters)); // This should create a date for Jif (testobject.getMonth() == 8) Console.WriteLine(\tTEST PASSED: testobject.setDate(8, 12) correctly stored s for the montresultofToString); numberOfFailingTests++; // equals - should return true Console.WriteLine(Now test the equals() method on303 384 385 306 // nextDay - mid-month Console.WriteLine(Now test the nextDay() method for the middle of the month (Dec 7th)11 (LESLUU JELL.keludYU = 1) Console.WriteLine(\tTEST PASSED: nextDay else method for nextDay() method the end of the month

Code is in C#

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

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

namespace TestMyDate
{
class BirthDay
{
static void Main(string[] args)
{
try
{
Console.WriteLine("What is today's month?");
var todaysMonth = int.Parse(Console.ReadLine());
Console.WriteLine("What is today's day?");
var todaysDay = int.Parse(Console.ReadLine());

Console.WriteLine("What is your birthday month?");
var birthMonth = int.Parse(Console.ReadLine());

Console.WriteLine("What is your birthday day?");
var birthDay = int.Parse(Console.ReadLine());

MyDate todaysDate = new MyDate(todaysMonth, todaysDay);
MyDate birthDate = new MyDate(birthMonth, birthDay);

Console.WriteLine("There are {0} days in month.#{1}", birthDate.daysInMonth(), birthDate.getMonth());

if (todaysDate.equals(birthDate))
{
Console.WriteLine("Happy Birthday!");
}
else
{
int daysCount = 0;
while (!todaysDate.equals(birthDate))
{
if (daysCount > 365) break; // if wrong date entered
daysCount++;
todaysDate.nextDay();// advance 1 day to check for birthday;
}
if (daysCount <= 365)
{
Console.WriteLine("Number of days until birthday {0}: {1}", birthDate.toString(), daysCount);

}
else
{
Console.WriteLine("Wrong dates entered");
}

}

}
catch
{
Console.WriteLine("Wrong dates entered");
}
TestMyDate();
Console.ReadKey();//wait for close
}

public static void TestMyDate()
{
int noOfFailingTests = 0;
Console.WriteLine("Test the MyDate() constructor (the one with no parameters)");
MyDate myDate = new MyDate();
if (myDate.getDay() == 1)
{
Console.WriteLine("\tTest PASSED. new MyDate() correctly stored '1' for day");
}
else
{
Console.WriteLine("\tTest FAILED. new MyDate() did not stored '1' for day(but it should have).It stored {0}", myDate.getDay());
noOfFailingTests++;
}

if (myDate.getMonth() == 1)
{
Console.WriteLine("\tTest PASSED. new MyDate() correctly stored '1' for month");
}
else
{
Console.WriteLine("\tTest FAILED. new MyDate() did not stored '1' for month(but it should have).It stored {0}", myDate.getMonth());
noOfFailingTests++;
}

Console.WriteLine("Test the MyDate(3,2) constructor (the one with two parameters)");
myDate = new MyDate(3, 2);
if (myDate.getDay() == 2)
{
Console.WriteLine("\tTest PASSED. new MyDate(3,2) correctly stored '2' for day");
}
else
{
Console.WriteLine("\tTest FAILED. new MyDate(3,2) did not stored '2' for day(but it should have).It stored {0}", myDate.getDay());
noOfFailingTests++;
}

if (myDate.getMonth() == 3)
{
Console.WriteLine("\tTest PASSED. new MyDate(3,2) correctly stored '3' for month");
}
else
{
Console.WriteLine("\tTest FAILED. new MyDate(3,2) did not stored '3' for month(but it should have).It stored {0}", myDate.getMonth());
noOfFailingTests++;
}

Console.WriteLine("Test the MyDate(22,33) constructor (the one with two parameters)");
myDate = new MyDate(22, 33);
if (myDate.getDay() == 1)
{
Console.WriteLine("\tTest PASSED. new MyDate(22,33) correctly stored '1' for day");
}
else
{
Console.WriteLine("\tTest FAILED. new MyDate(22,33) did not stored '1' for day(but it should have).It stored {0}", myDate.getDay());
noOfFailingTests++;
}

if (myDate.getMonth() == 1)
{
Console.WriteLine("\tTest PASSED. new MyDate(22,33) correctly stored '1' for month");
}
else
{
Console.WriteLine("\tTest FAILED. new MyDate(22,33) did not stored '1' for month(but it should have).It stored {0}", myDate.getMonth());
noOfFailingTests++;
}

Console.WriteLine("Test the MyDate(22,14) constructor (the one with two parameters)");
myDate = new MyDate(22, 14);
if (myDate.getDay() == 14)
{
Console.WriteLine("\tTest PASSED. new MyDate(22,14) correctly stored '14' for day");
}
else
{
Console.WriteLine("\tTest FAILED. new MyDate(22,14) did not stored '14' for day(but it should have).It stored {0}", myDate.getDay());
noOfFailingTests++;
}

if (myDate.getMonth() == 1)
{
Console.WriteLine("\tTest PASSED. new MyDate(22,14) correctly stored '1' for month");
}
else
{
Console.WriteLine("\tTest FAILED. new MyDate(22,14) did not stored '1' for month(but it should have).It stored {0}", myDate.getMonth());
noOfFailingTests++;
}

Console.WriteLine("Test the MyDate(6,31) constructor (the one with two parameters)");
myDate = new MyDate(6, 31);
if (myDate.getDay() == 1)
{
Console.WriteLine("\tTest PASSED. new MyDate(6,31) correctly stored '1' for day");
}
else
{
Console.WriteLine("\tTest FAILED. new MyDate(6,31) did not stored '1' for day(but it should have).It stored {0}", myDate.getDay());
noOfFailingTests++;
}

if (myDate.getMonth() == 6)
{
Console.WriteLine("\tTest PASSED. new MyDate(6,31) correctly stored '6' for month");
}
else
{
Console.WriteLine("\tTest FAILED. new MyDate(6,31) did not stored '6' for month(but it should have).It stored {0}", myDate.getMonth());
noOfFailingTests++;
}

Console.WriteLine("Test the MyDate(MyDate other) constructor (the one with one MyDate parameters)");

myDate = new MyDate(3, 17);
MyDate myDate_2 = new MyDate(myDate);
if (myDate_2.getDay() == 17)
{
Console.WriteLine("\tTest PASSED. new MyDate(other) correctly stored '17' for day");
}
else
{
Console.WriteLine("\tTest FAILED. new MyDate(other) did not stored '17' for day(but it should have).It stored {0}", myDate_2.getDay());
noOfFailingTests++;
}

if (myDate_2.getMonth() == 3)
{
Console.WriteLine("\tTest PASSED. new MyDate(other) correctly stored '3' for month");
}
else
{
Console.WriteLine("\tTest FAILED. new MyDate(other) did not stored '3' for month(but it should have).It stored {0}", myDate_2.getMonth());
noOfFailingTests++;
}


Console.WriteLine("Test the setDate(6,31) method");
myDate = new MyDate(6, 31);
if (myDate.getDay() == 1)
{
Console.WriteLine("\tTest PASSED. setDate(6,31) correctly stored '1' for day");
}
else
{
Console.WriteLine("\tTest FAILED. setDate(6,31) did not stored '1' for day(but it should have).It stored {0}", myDate.getDay());
noOfFailingTests++;
}

if (myDate.getMonth() == 6)
{
Console.WriteLine("\tTest PASSED. setDate(6,31) correctly stored '6' for month");
}
else
{
Console.WriteLine("\tTest FAILED. setDate(6,31) did not stored '6' for month(but it should have).It stored {0}", myDate.getMonth());
noOfFailingTests++;
}

Console.WriteLine("Test the setDate(6,31) method");
myDate = new MyDate(12, 30);
myDate.nextDay();
if (myDate.getDay() == 31)
{
Console.WriteLine("\tTest PASSED. setDate(6,31), nextday() correctly stored '31' for day");
}
else
{
Console.WriteLine("\tTest FAILED. setDate(6,31) , nextday()did not stored '31' for day(but it should have).It stored {0}", myDate.getDay());
noOfFailingTests++;
}

if (myDate.getMonth() == 12)
{
Console.WriteLine("\tTest PASSED. setDate(6,31), nextday() correctly stored '12' for month");
}
else
{
Console.WriteLine("\tTest FAILED. setDate(6,31) , nextday() did not stored '12' for month(but it should have).It stored {0}", myDate.getMonth());
noOfFailingTests++;
}

myDate.nextDay();
if (myDate.getDay() == 1)
{
Console.WriteLine("\tTest PASSED. setDate(6,31), nextday()++ correctly stored '1' for day");
}
else
{
Console.WriteLine("\tTest FAILED. setDate(6,31) , nextday()++ did not stored '1' for day(but it should have).It stored {0}", myDate.getDay());
noOfFailingTests++;
}

if (myDate.getMonth() == 1)
{
Console.WriteLine("\tTest PASSED. setDate(6,31), nextday()++ correctly stored '1' for month");
}
else
{
Console.WriteLine("\tTest FAILED. setDate(6,31) , nextday()++ did not stored '1' for month(but it should have).It stored {0}", myDate.getMonth());
noOfFailingTests++;
}

Console.WriteLine("No of failed test cases : {0}", noOfFailingTests);

}
}
class MyDate
{
private int _month;
private int _day;
private int[] daysInMonthTable = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public MyDate()
{
_month = 1;
_day = 1;
}
public MyDate(int month, int day)
{
_month = month <= 12 && month > 0 ? month : 1;
_day = day <= daysInMonth() && day > 0 ? day : 1;
}
public MyDate(MyDate myDate)
{
_month = myDate.getMonth();
_day = myDate.getDay();
}

public int getDay()
{
return _day;
}

public int getMonth()
{
return _month;
}

public void setDate(int month, int day)
{
_month = month <= 12 && month > 0 ? month : 1;
_day = day <= daysInMonth() && day > 0 ? day : 1;
}

public string toString()
{
return _month.ToString() + "/" + _day.ToString();
}

public bool equals(MyDate other)
{
if (_month == other.getMonth() && _day == other.getDay())
return true;
return false;
}

public int daysInMonth()
{
return daysInMonthTable[_month - 1];
}

public void nextDay()
{
if (daysInMonth() == _day)
{
_day = 1;
_month++; //advance month when last date
if (_month > 12)
{
_month = 1;// if end of last month found
}
}
else
{
_day = _day + 1; // advance one day
}

}
}
}

Add a comment
Know the answer?
Add Answer to:
Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin...
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
  • Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java,...

    Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java, which will contain code that utilizes your Date.java class. You will submit both files (each with appropriate commenting). A very similar project is described in the Programming Projects section at the end of chapter 8, which you may find helpful as reference. Use (your own) Java class file Date.java, and a companion DateClient.java file to perform the following:  Ask user to enter Today’s...

  • General Requirements: • You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; • You sho...

    General Requirements: • You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; • You should create identifiers with sensible names; • You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve. • Logical structures and statements are properly used for specific purposes. Objectives This assignment requires you to write...

  • Object Oriented Programming Please write the code in C++ Please answer the question in text form...

    Object Oriented Programming Please write the code in C++ Please answer the question in text form 9.5 (complex Class) Create a class called complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form where i is V-I Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in...

  • Please create a C++ class called myDate. It should have the following operations: myDate() – default...

    Please create a C++ class called myDate. It should have the following operations: myDate() – default constructor. This will set the date to May 10, 1959. myDate(int M, int D, int Y) – overloaded constructor. This will set the date to the values passed in through the parameter list represented by Month, Day and Year. void display() – display the date in the following format (May 11, 1959) Do NOT print a linefeed after the date. void incrDate(int N) –...

  • I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the fo...

    I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the form (month day year): 2 28 2018  Ask user to enter Birthday in the form (month day year): 11 30 1990  Output the following: The date they were born in the form (year/month/day). The day of the week they were born (Sunday – Saturday). The number of days between their birthday and today’s...

  • Write the code for the Song class. 1. Instance variables: title:String, lyrics:String, numAwards:int 2. For the...

    Write the code for the Song class. 1. Instance variables: title:String, lyrics:String, numAwards:int 2. For the 2-arg constructor (parameters are title and lyrics), use the following default value: - numAwards: 3 3. Create the 3-argument constructor. 4. No additionalconstructors. 5. Generate all getters and setters for instance variables 6. toString: Return a nicely formatted string describing this object 7. toSummary: This instance method will return a String that contains up to the first 10 characters of the lyrics. Be sure...

  • Part 1: Song Class Write the code for the Song class. 1. Instance variables: title:String, lyrics:String,...

    Part 1: Song Class Write the code for the Song class. 1. Instance variables: title:String, lyrics:String, numAwards:int 2. For the 2-arg constructor (parameters are title and lyrics), use the following default value: - numAwards: 3 3. Create the 3-argument constructor. 4. No additionalconstructors. 5. Generate all getters and setters for instance variables 6. toString: Return a nicely formatted string describing this object 7. toSummary: This instance method will return a String that contains up to the first 10 characters of...

  • SaffSpring 2019 Professor Question 1: Answer ALL of the following questions based on this provide...

    I need sub-questions e, f, g, h. SaffSpring 2019 Professor Question 1: Answer ALL of the following questions based on this provided int daya int montha Date int dnt int y day-d year class Event ring locatson eventbat Event (5tring , Dated ocatlon- event Date-d pubiie String toStringt String detalla-Locatlons"LocatlonDatesevent Date.day return detalia eventDate-year publie statie weld mai arg Event 2ew EventCBayan" hew Date1.3,20191 Systen.out peintil The event n (11.201,as scheduled-ee . 내 1 eentDate.yeari ystem.st-printeinte yatem.ost-printcin(e2) a) What is...

  • Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks...

    Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks for the help. Specifications: The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods. A no- arg constructor that sets the monthNumber field to 1. A constructor that accepts the number of the month as an argument. It should...

  • Using simple, college level c++ programming Design a class called NumDays. The class’s purpose is to...

    Using simple, college level c++ programming Design a class called NumDays. The class’s purpose is to store a value that represents a number of work hours and convert it to a number of days. For example, 8 hours would be converted to 1 day, 12 hours would be converted to 1.5 days, and 18 hours would be converted to 2.25 days. The class should have a constructor that accepts a number of hours, as well as member functions for storing...

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