Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

At the end of the exercise, the students should be able to: Declare and use constants and variables in a program; and Convert a

image text in transcribed image text in transcribedimage text in transcribedimage text in transcribed

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.

Step by Step Solution

3.40 Rating (156 Votes )

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Elements Of Chemical Reaction Engineering

Authors: H. Fogler

6th Edition

013548622X, 978-0135486221

More Books

Students also viewed these Programming questions