CSF2 Flashcards
What are characteristics of the datatype DateTime?
- it’s a datatype from the FCL (framework class library)
- used to create variables that hold date and time info
- time defaults to 12am
- it’s a complex data type, unlike strings, ints, etc…which are all intrinsic data types
What does a complex datatype need?
Complex data types often need the NEW keyword and
a constructor method, which has the same name as the data type.
What’s the syntax for DateTime?
…and what format specificers can be used with DateTime?
DateTime x= new DateTime();
You can use format specificers on DateTime.
(t, T, d, D, f, F)
How do you explore an object with intellisense?
…using a period
How do you determine the current date?
DateTime.Today
How do you determine the current date AND time?
DateTime.Now
How do you explore a method with intellisense?
Use an open paren (
Random myRandomNbr = new Random();
int myNextRandomNbr = myRandomNbr.Next(5, 11);
Console.WriteLine(“Number between 5-10: “ +
myNextRandomNbr);
Is Random a complex or instrinsic data type?
**is a complex and requires a the NEW keyword **
What’s happening here…
** int nbr0and20 = randomNbrGen.Next(21);
Console.WriteLine(“A number between 0-20 is “+ nbr0and20);**
a random number is output less than 21
What’s this doing…
** int Nbr50and105 = randomNbrGen.Next(50, 106);
Console.WriteLine(“A number between 50-105 is “ + Nbr50and105);**
How are METHODS OUTSIDE the class, but INSIDE the project, called?
ClassName.MethodName
How is the console cleared?
Console.Clear();
bool exit = false; //COUNTER
do { Console.WriteLine("\nPlease choose a program\n" \+ "(B) Break Change\n" \+ "(W) Water Weight\n" \+ "(N) Numbers\n" \+ "(T) Display Time\n" \+ "(C) Calendar App\n" \+ "(X) Exit");
string choice = Console.ReadLine().ToUpper();
Console.Clear();
switch (choice) { case "B": Console.WriteLine("Break Change"); BreakChange(); break; case "W": Console.WriteLine("Water Weight"); WaterWeight(); break; case "N": Console.WriteLine("Numbers"); CountNumbers(); break; case "T": Console.WriteLine("Display Time"); DateTimeWarehouse.DisplayTime(); break; case "C": Console.WriteLine("Calendar App");
Console.WriteLine(“Please type a number of days from today to display”);
int userDays = int.Parse(Console.ReadLine()); DateTime newDate = DateTimeWarehouse.CalculateDate(userDays);
DateTimeWarehouse.DisplayTime(newDate);
break;
case "X": Console.WriteLine("Thank you for using the program"); exit = true; break; default: Console.WriteLine("Input not recognized. Please try again."); break; }//end switch
} while (!exit);
bool exit = false; //COUNTER
do { //print menu Console.WriteLine("\nPlease choose a program\n" \+ "(B) Break Change\n" \+ "(W) Water Weight\n" \+ "(N) Numbers\n" \+ "(T) Display Time\n" \+ "(C) Calendar App\n" \+ "(X) Exit");
string choice = Console.ReadLine().ToUpper(); Console.Clear();//clears the console of the previous display //BEST USED AFTER ReadLine() don't do this BEFORE or they //will never see the menu...
switch (choice) { case "B": Console.WriteLine("Break Change"); BreakChange(); break; case "W": Console.WriteLine("Water Weight"); WaterWeight(); break; case "N": Console.WriteLine("Numbers"); CountNumbers(); break; case "T": Console.WriteLine("Display Time"); DateTimeWarehouse.DisplayTime(); break; case "C": Console.WriteLine("Calendar App");
//print out today's date for now, we will come //back and figure out a calculated date next //DateTimeWarehouse.DisplayTime(DateTime.Now); Console.WriteLine("Please type a number of days from today to display"); int userDays = int.Parse(Console.ReadLine()); //ask the user for a number of days
//use the calculate date method DateTime newDate = DateTimeWarehouse.CalculateDate(userDays);
//print the calculated date with our custom format DateTimeWarehouse.DisplayTime(newDate); break;
case "X": Console.WriteLine("Thank you for using the program"); exit = true; break; default: Console.WriteLine("Input not recognized. Please try again."); break; }//end switch
//} while (exit == false); //revere logic } while (!exit); //see !...think not since the CONDITION needs a TRUE statement //the ! merely states that it should look for a NOT TRUE
What is Random?
** Random is a class in the FCL
(Framework Class Library).
* It has an instance method, Next() which generates the “next”
* random number in a desired range if specified.**
What is the difference between a static method and an instance method?
???
Give 3 examples of static methods…
int.Parse();
Console.ReadLine();
Console.WriteLine();
Give 3 examples of an instance method.
whereas ToUpper()., ToLower()., AddDays()
**all require an **
INSTANCE or variable of the datatype to call them.
** Console.ForegroundColor = ConsoleColor.Yellow;**
How do you reset colors in a console program
Console.ResetColor();
bool colorMenu = true;
do
{
Console.WriteLine(“Pick your text color: Green, Blue,”
+ “ Red, Cyan”);
string textColor = Console.ReadLine().ToLower();
switch (textColor)
{
case “green”:
case “g”:
Console.ForegroundColor = ConsoleColor.Green;
break;
case "blue": case "b": Console.ForegroundColor = ConsoleColor.Blue; break; case "red": case "r": Console.ForegroundColor = ConsoleColor.Red; break; case "cyan": case "c": Console.ForegroundColor = ConsoleColor.Cyan; break; default: Console.WriteLine("Invalid entry. Re-run program!!"); break; }//end switch Console.WriteLine("Here is your text now!\n");
What’s happening here:
Volunteer v1 = new Volunteer();
Volunteer is complex datatype…it’s pulling from the Volunteer.cs file
v1 is the variable to call the code from that file
it requires the use of the NEW keyword
If you do not define a constructor in the class file, what happens?
you get a free one with no parameters
What’s happening here…
Volunteer v1 = new Volunteer();
- *v1._name = “Spider Man”;
v1. _isActive = true;
v1. _yearsOfService = 12;**
Volunteer is a complex data…meaning there is a Volunteer.cs file
the variables initialized are datatypes from the Volunteer.cs file
Console.WriteLine(“{0} has volunteered for {1} years.\n”
+ “Still Active? {2}\n”,
v1._name, v1._yearsOfService, v1._isActive);
Volunteer v2 = new Volunteer();
v2._name = “Iron Man”;
v2._isActive = true;
v2._yearsOfService = 5;
Console.WriteLine(“{0} has volunteered for {1} years.\n”
+ “Still Active? {2}”,
v2._name, v2._yearsOfService, v2._isActive);
What 4 things makeup a dataytype class and what letter casing must they be in?
fields (camelCase) …for parameters
properites (PascalCase) …for how outside files will use the data
constructors (ClassName is PascalCase and parameter is camelCase) …a specialized method within a class definition that let’s you create objects from the class
methods …performs some action or block of code when called
What is “call a method”
calling a method to perform its action, passing whatever arguments are needed to be used as parameters
What is this…
** public override string ToString()
{**
** return string.Format(“{0}\t\tSalary: {1:c}\n”**
** + “Hired: {2:d}\nPaid Bi-Weekly?: {3}\n”
+ “Exmployer: {4}\t\tfounded {5:d}\n”,
Name, Salary, DateOfHire, IsPaidBiWeekly, Employer.Name, Employer.DateFounded);
}**
What represents the info that can be stored for an object of this type?
fields
For a datatype class, are fields public or private?
private
What do properties do?
encapsulate or protect fields;
controls access to getting or setting info within the fields
Are properties public or private?
public
What does the keyword value do?
it represents whatever value that is trying to be set to a field
Whata is this an example of:
**public DateTime DateFounded
{
get { return _dateFounded; } //get…allows program to get data
set { _dateFounded = value;
} **
What does get and set do?
get and set are part of the scope of a variable;
get allows a program to get data
set allows a program to set or put data in
What are constructors a.k.a. ctors?
Constructors are specialized methods to create objects of this type; they are called with the new keyword.
The constrcutor method always has the same name as the data type where it’s defined.
NO return type is listed in a constructor’s method
Note: this is what is ultimately displayed in intellisense
What’s the sytax for a constrcutor?
public className(datatype name, …)
{
Property = parameter
}
What’s missing here…
**
return string.Format(“\nOrganization: {0}\nRevenue:
{1:c}\n”
+ “Is not for profit?: {2}\nDate Founded: {3:d}”,
Name, Revenue, IsNotForProfit, DateFounded);
}**
** public override string ToString() { //return base.ToString(); //the base.ToString() gives us namespace.class which is //NOT what we want return string.Format("\nOrganization: {0}\nRevenue: {1:c}\n" \+ "Is not for profit?: {2}\nDate Founded: {3:d}", Name, Revenue, IsNotForProfit, DateFounded); }//end ToString**
** public string _name;
public bool _isActive;
public int _yearsOfService;**
How should parameters be named?
underscore camelCase
_name
…CSF2 …3Wed …MoreCollections