At the end of the exercise, the students should be able to: Declare and use constants...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
At the end of the exercise, the students should be able to: Declare and use constants and variables in a program; and Convert a type of data to another data type. Materials: One (1) personal computer with pre-installed Windows Operating System Visual Studio IDE 2015 or higher Instructions: 1. Create a console program that will perform the following: • Ask the user to enter the pieces of apple. Ask the user to enter the total price of the apple(s). Print the total price of the entered pieces of apple(s). Convert the entered price into a whole number, then display the values of the original price and the converted price. 2. Name the project as DataTypesApp and the class as DataTypes Program. 3. Example output: file:///C:/Users /Documents/Visual Studio 2015/Projects/ConsoleApp/ConsoleApp/b... Enter the pieces of apple: 6 Enter total price of 6 apple(s): 105.50 The total price of 6 apple(s) is 105.5 The value of original price is 105.5 The value of converted price is 105 Press any key to exit..... ox Data Types and Math Class Objectives: At the end of the exercise, the students should be able to: ● Write a program that uses different operators; and Use the methods of the Math class. Instructions: 1. Create a console program that will perform the following: Ask the user to enter five (5) grades Compute the average of the grades Round of the average using method of Math class. Name the project as ComputeAverageApp and the class as ComputeAverage Program. Example output: 2. 3. Enter 5 grades separated by new line: 90 83 87 98 93 The average is 90.2 and round off to 90 Press any key to exit... X Identifiers and Keywords An identifier is a name of a program component programmers use to uniquely identify namespaces, classes, methods, variables, constants, etc. Identifiers are user-defined words. For example, in the program shown in Code Listing 1, the identifiers are ConsoleApp, ComputeRectangleArea, length, width, area, WriteLine, and ReadKey. Code Listing 1. Sample class with identifiers using System; namespace ConsoleApp { class ComputeRectangleArea { } score first Name grade1 object public sizeof throw unsafe static void Main() { abstract catch default explicit foreach internal } The following are the syntax rules when naming an identifier in C#: It must start with a letter of the English alphabet or an underscore character. The identifier's name can only have any combination of letters, digits, and underscores. White spaces are not allowed. Identifiers are case sensitive. For example, the identifier Area is not the same with identifiers area or AREA. It cannot be a reserved keyword. The classes and methods in C# must always begin with a capital letter. The following are the examples of valid and invalid identifiers: Valid Identifiers Invalid Identifiers score ScoreClass ComputeScore 1score is invalid because it begins with number. first Name is invalid because it contains white space. class is an invalid identifier because it is a reserved keyword. Table 1. Example valid identifiers Table 2. Example invalid identifiers Keywords are reserved words a programming language uses for its own use, and they have a special predefined meaning to the compiler. These cannot be used as identifiers. If you do use a keyword in a program, the compiler will throw an error message. Table 3 shows the list of keywords in C#. base checked int length, width, area; length = 50; width = 8; area as char length * width; Console.WriteLine("The area of the rectangle is " + area); Console.ReadKey(); goto is delegate extern Data Types operator readonly stackalloc true ushort do false if lock out ref static try using 1score first Name class bool class double finally implicit long break const else fixed in namespace params sbyte struct override return string typeof virtual Table 3. C# keywords (Harwani, 2015) uint void byte continue enum float int new private sealed switch ulong volatile case decimal event for interface null protected short this unchecked while Variables A variable is an identifier and a memory location that stores a specific value. Variables hold a value that can be changed during program execution. For example, a variable named score assigned an initial value of 25. When the program starts, the value of variable score will change to 85. The basic syntax of declaring a variable is as follows: data type identifier; The data_type is one (1) of C#'s data types, and the identifier is the name of the variable. For example, int score; Variables are initialized, or assigned a value, with an equal sign followed by the value. The following are some valid examples of declaring and initializing variables: You can initialize a variable at the time of declaration. For example: ● int score 25; You can declare and initialize more than one (1) variable of the same time data type using a comma in a single statement: int score, age; score = 85; age = 24; int length 8, width = 5; When creating a program, you must define a variable with a meaningful name that is easy to understand what value must store on it. For example, the variables gameScore and age must store integer type values. Use camelCase notation that starts with a lowercase letter for naming local variables. For example, numberOfStudents, age, totalPrice, etc. Constants A constant is an identifier and a memory location whose value cannot be changed during program execution. Constants must initialize a value at the time of declaration. The basic syntax of initializing a constant in C# is as follows: const data_type IDENTIFIER = value; Constants in C# are defined using the const keyword. For example: const double PI = 3.14159;. In the example, the constant PI with the value of 3.14159 cannot be changed during program execution. The name of the constants must be in all capital to make it easy to identify that its value must not change. Data Types Data types are used to specify a set of values and their operations associated with variables or constants. The values have a particular data type because they are stored in memory in the same format and have the same operations defined for them. A variable or constant stores data of a specific type. When declaring a variable or constant to store a data, the appropriate data type for that data must be identified. The data type will instruct the compiler what kind of value a variable or constant can hold and its operations. There are two (2) types of primitive data types in C#: • Value types. These data types store the value directly. The data type int is a value type that stores its value in a memory location directly. • Reference types. These data types do not store the actual value in a variable or constant. Instead, they store the reference (or address) where the value is stored. The class objects, strings, and arrays are reference types because they hold references to blocks of memory and are managed on the heap. Value Types A data type is a value type if it holds the data within its own memory allocation. Value types directly store the values within the variable. For example, consider the following figure: variable length memory location int length = 50; 0x0008 50 RAM Figure 1. Memory allocation for value type In Figure 1, the variable length of int type is assigned a value of 50. When this statement is executed, the compiler will instruct the computer system to directly store 50 in the memory space allocated for the variable length. At the end of the exercise, the students should be able to: Declare and use constants and variables in a program; and Convert a type of data to another data type. Materials: One (1) personal computer with pre-installed Windows Operating System Visual Studio IDE 2015 or higher Instructions: 1. Create a console program that will perform the following: • Ask the user to enter the pieces of apple. Ask the user to enter the total price of the apple(s). Print the total price of the entered pieces of apple(s). Convert the entered price into a whole number, then display the values of the original price and the converted price. 2. Name the project as DataTypesApp and the class as DataTypes Program. 3. Example output: file:///C:/Users /Documents/Visual Studio 2015/Projects/ConsoleApp/ConsoleApp/b... Enter the pieces of apple: 6 Enter total price of 6 apple(s): 105.50 The total price of 6 apple(s) is 105.5 The value of original price is 105.5 The value of converted price is 105 Press any key to exit..... ox Data Types and Math Class Objectives: At the end of the exercise, the students should be able to: ● Write a program that uses different operators; and Use the methods of the Math class. Instructions: 1. Create a console program that will perform the following: Ask the user to enter five (5) grades Compute the average of the grades Round of the average using method of Math class. Name the project as ComputeAverageApp and the class as ComputeAverage Program. Example output: 2. 3. Enter 5 grades separated by new line: 90 83 87 98 93 The average is 90.2 and round off to 90 Press any key to exit... X Identifiers and Keywords An identifier is a name of a program component programmers use to uniquely identify namespaces, classes, methods, variables, constants, etc. Identifiers are user-defined words. For example, in the program shown in Code Listing 1, the identifiers are ConsoleApp, ComputeRectangleArea, length, width, area, WriteLine, and ReadKey. Code Listing 1. Sample class with identifiers using System; namespace ConsoleApp { class ComputeRectangleArea { } score first Name grade1 object public sizeof throw unsafe static void Main() { abstract catch default explicit foreach internal } The following are the syntax rules when naming an identifier in C#: It must start with a letter of the English alphabet or an underscore character. The identifier's name can only have any combination of letters, digits, and underscores. White spaces are not allowed. Identifiers are case sensitive. For example, the identifier Area is not the same with identifiers area or AREA. It cannot be a reserved keyword. The classes and methods in C# must always begin with a capital letter. The following are the examples of valid and invalid identifiers: Valid Identifiers Invalid Identifiers score ScoreClass ComputeScore 1score is invalid because it begins with number. first Name is invalid because it contains white space. class is an invalid identifier because it is a reserved keyword. Table 1. Example valid identifiers Table 2. Example invalid identifiers Keywords are reserved words a programming language uses for its own use, and they have a special predefined meaning to the compiler. These cannot be used as identifiers. If you do use a keyword in a program, the compiler will throw an error message. Table 3 shows the list of keywords in C#. base checked int length, width, area; length = 50; width = 8; area as char length * width; Console.WriteLine("The area of the rectangle is " + area); Console.ReadKey(); goto is delegate extern Data Types operator readonly stackalloc true ushort do false if lock out ref static try using 1score first Name class bool class double finally implicit long break const else fixed in namespace params sbyte struct override return string typeof virtual Table 3. C# keywords (Harwani, 2015) uint void byte continue enum float int new private sealed switch ulong volatile case decimal event for interface null protected short this unchecked while Variables A variable is an identifier and a memory location that stores a specific value. Variables hold a value that can be changed during program execution. For example, a variable named score assigned an initial value of 25. When the program starts, the value of variable score will change to 85. The basic syntax of declaring a variable is as follows: data type identifier; The data_type is one (1) of C#'s data types, and the identifier is the name of the variable. For example, int score; Variables are initialized, or assigned a value, with an equal sign followed by the value. The following are some valid examples of declaring and initializing variables: You can initialize a variable at the time of declaration. For example: ● int score 25; You can declare and initialize more than one (1) variable of the same time data type using a comma in a single statement: int score, age; score = 85; age = 24; int length 8, width = 5; When creating a program, you must define a variable with a meaningful name that is easy to understand what value must store on it. For example, the variables gameScore and age must store integer type values. Use camelCase notation that starts with a lowercase letter for naming local variables. For example, numberOfStudents, age, totalPrice, etc. Constants A constant is an identifier and a memory location whose value cannot be changed during program execution. Constants must initialize a value at the time of declaration. The basic syntax of initializing a constant in C# is as follows: const data_type IDENTIFIER = value; Constants in C# are defined using the const keyword. For example: const double PI = 3.14159;. In the example, the constant PI with the value of 3.14159 cannot be changed during program execution. The name of the constants must be in all capital to make it easy to identify that its value must not change. Data Types Data types are used to specify a set of values and their operations associated with variables or constants. The values have a particular data type because they are stored in memory in the same format and have the same operations defined for them. A variable or constant stores data of a specific type. When declaring a variable or constant to store a data, the appropriate data type for that data must be identified. The data type will instruct the compiler what kind of value a variable or constant can hold and its operations. There are two (2) types of primitive data types in C#: • Value types. These data types store the value directly. The data type int is a value type that stores its value in a memory location directly. • Reference types. These data types do not store the actual value in a variable or constant. Instead, they store the reference (or address) where the value is stored. The class objects, strings, and arrays are reference types because they hold references to blocks of memory and are managed on the heap. Value Types A data type is a value type if it holds the data within its own memory allocation. Value types directly store the values within the variable. For example, consider the following figure: variable length memory location int length = 50; 0x0008 50 RAM Figure 1. Memory allocation for value type In Figure 1, the variable length of int type is assigned a value of 50. When this statement is executed, the compiler will instruct the computer system to directly store 50 in the memory space allocated for the variable length.
Expert Answer:
Related Book For
Posted Date:
Students also viewed these programming questions
-
Managing Scope Changes Case Study Scope changes on a project can occur regardless of how well the project is planned or executed. Scope changes can be the result of something that was omitted during...
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
The owner of Atlantic City Confectionary is considering the purchase of a new semiautomatic candy machine. The machine will cost $25,000 and last 10 years. The machine is expected to have no salvage...
-
This exercise should be used in conjunction with Starter 4-12. 1. How much in current assets does Belleville Services have for every dollar of current liabilities that it owes? What ratio measures...
-
The standard enthalpy of combustion of ethyl acetate (CH 3 COOC 2 H 5 ) is 2231 kJmol 1 at 298K and its standard molar entropy is 259.4 JK 1 mol 1 . Calculate the standard Gibbs energy of formation...
-
The Nielsen family formed their corporation, N. Robert Nielsen, Inc., to conduct farming operations. Morre, Grider & Co. is a certified public accounting firm that has provided accounting, tax, and...
-
Capitalization of Interest on July 31, 2010, Bismarck Company engaged Duval Tooling Company to construct a special-purpose piece of factory machinery. Construction was begun immediately and was...
-
Bleeker Street Pizza and Keste Pizza & Vino are located close to each other on Bleeker Street in the West Village. There are 450 potential customers every day, and suppose that each of them is...
-
Chickadee Ski Club (CSC) is located in southern Ontario near large urban centres but not far enough north to guarantee a full winter of snow on the ground. The club's revenue and profit jump in years...
-
Joel has four transactions involving the sale of capital assets during the year resulting in a STCG of $5,000, a STCL of $12,000, a LTCG of $1,800 and a LTCL of $1,000. As a result of these...
-
2 Find the inverse: f(x) = 3x+2
-
The Big Steel Box (BSB) produces industrial-grade shipping boxes for the local and international markets. Their factory is designed to produce at most 1400 boxes per week (with 40 working hours in a...
-
The new hospice in town has created a marketing campaign that has some great visual imagery to support building awareness of all the services the hospice offers to families of the critically ill in...
-
Explain the Deming chain reaction.Summarize the four components of Profound Knowledge. How do they mutually support each other?Why is it important to understand variation from a statistical...
-
A famous painting was sold in 1946 for $21,230. In 1960 the painting was sold for $31.4 million. What rate of interest compounded continuously did this investment sam As an investment, the painting...
-
Put the letter of the text next to the best matching description. Each item is worth 2 points. An instance of a class a) Abstraction b) Allocation Bundling data, and the functions that use them, as a...
-
Hotel Majestic is interested in estimating fixed and variable costs so that the company can make more accurate projections of costs and profit. The hotel is in a resort area that is particularly busy...
-
The volumetric flow rate through a reactor is 10 dm 3 /min. A pulse test gave the following concentration measurements at the outlet: a. Plot the external-age distribution E(t) as a function of time....
-
If you used the Boussinesq (or Westergaard) theory for Problem 10.1, do the problem again but use the Westergaard (or Boussinesq) theory instead. Comment on the differences between the two theories.?
-
a. For each of the reactions in parts (a), (b), and (c), suggest a mechanism and apply the PSSH to learn if the mechanism is consistent with the rate law. b. The gas-phase homogeneous oxidation of...
-
What do you understand by a bank lending policy in the context of a monetary policy?
-
A columnist in the Wall Street Journal argues: Whether youre a borrower or a saver, what matters isnt the nominal interest rate but the real, post-inflation rate of return. Do you agree? Briefly...
-
What is the relationship between the MPC and the multiplier? Would a larger value for the MPC tend to increase or decrease the stability of the economy? Briefly explain.
Study smarter with the SolutionInn App