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...
-
Compute the missing amounts (shown by a ?) for each company (in millions). At the end of the year, which company has the Highest net income? Highest percentage of net income to revenues? Prepare a...
-
What is the company's market penetration strategy (intensive, selective, etc.), and how do the distribution channels being used reflect that strategy? Which of the channels being used do you believe...
-
Sigma Security, Inc., began the year with $15,000 of common stock and $32,000 of retained earnings. On May 5, investors bought $12,000 of additional stock in the business. On August 22, the business...
-
A design engineer at Sperling Manufacturing, a supplier of high-quality ball bearings, claims a new machining process can result in a higher daily output rate. Accordingly, the production group is...
-
The following are transactions of Robina Corporation for the month of February 2020. Provide the corresponding journal entries for each transaction: 1. Purchase of materials (direct and indirect), on...
-
Tom Scott is the owner, president, and primary salesperson for Scott Manufacturing. Because of this, the companys profits are driven by the amount of work Tom does. If he works 40 hours each week,...
-
Dolls are fabricated in a process with two resources. The first resource has a capacity of 1.5 dolls per hour. The capacity of the second resource is 0.99 dolls per hour. The first resource has 7...
-
Harley-Davidson is my industry/brand of interest. As you continue working on your final marketing plan in this course, pricing will be a crucial aspect of your plan. You will want to consider...
-
A finite-length discrete signal r[n] is given as t[n]d[n-2] + [n 4] + 8[n 6] +6[n 8] + [n - 10]. (a) Find the phase of X(e), which is the Fourier tranform of r[n]. (b) Define Y(e) = w X* (ew), where...
-
On the date of the alleged offence, Tom was subject to a 5-year driving prohibition when a police officer observed Tom driving on a country road heading into a small town. Tom was stopped by police...
-
Cost of holding the cones in inventory is $0.10 percent What is the annual total cost associated with an order size of 400 cones?
-
ed Franklin Corporation produces products that it sells for $14 each. Variable costs per unit are $9, and annual fixed costs are $104,000. Franklin desires to earn a profit of $11,500. Required a....
-
The Notify shape produces what type of user alert? A. Atom.status B. Process.execution C. User.notification D. Production.notification
-
Arlington Merchants reported the following on its income statement for the fiscal years ending December 31, 2016 and 2015. 2016 2015 Sales $4,857,500 $4,752,900 Cost of goods sold 3,258,950 3,207,000...
-
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...
-
At the same temperature and pressure, the chemical potentials of a component in two phases under equilibrium conditions (a) Are equal (b) Are different (c) Can not be predicted (d) None of these.
-
The influence of pressure on chemical potential can be expressed as (a) \(\left(\frac{\partial \overline{V_{i}}}{\partial P} ight)_{T, n_{i}}=\mu_{i}\) (b) \(\left(\frac{\partial \mu_{i}}{\partial T}...
-
The activity coefficient is a measure of (a) The ideal behaviour of chemical substances in a mixture. (b) The deviation from ideal behaviour of chemical substances in a mixture (c) The effective...
Study smarter with the SolutionInn App