Preview from Notesale.co.uk Page 3 of 36

Similar documents
CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Introduction to Programming Using Java (98-388)

CSC 1214: Object-Oriented Programming

JAVA: A Primer. By: Amrita Rajagopal

Chapter 1: A First Program Using C#

( &% class MyClass { }

COMP 202 Java in one week

Lab5. Wooseok Kim

PROGRAMMING FUNDAMENTALS

Controls Structure for Repetition

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

In Java, to create a class named "B" as a subclass of a class named "A", you would write

Simple Java Reference

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Object-Oriented Programming Concepts

Full file at

Array. Array Declaration:

Computer Science II Lecture 1 Introduction and Background

CS111: PROGRAMMING LANGUAGE II

Typescript on LLVM Language Reference Manual

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

4. Inputting data or messages to a function is called passing data to the function.

Strings, Strings and characters, String class methods. JAVA Standard Edition

Building Java Programs

COMPUTER PROGRAMMING LOOPS

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Binghamton University. CS-140 Fall Dynamic Types

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Lesson 3: Accepting User Input and Using Different Methods for Output

Your First C++ Program. September 1, 2010

Building Java Programs

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Friend Functions, Inheritance

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Classes. Classes as Code Libraries. Classes as Data Structures

Programming with Java

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

SOAPScript For squeaky-clean code, use SOAPScript Language Summary Bob Nisco Version 0.5 β

Fundamentals of Programming Session 4

Character Stream : It provides a convenient means for handling input and output of characters.

Programming Paradigms

Object-Oriented Programming

Basics of Java Programming

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Tutorial 1 CSC 201. Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا

Java for Non Majors Spring 2018

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Lecture 2: Java & Javadoc

Defining Classes and Methods

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

An overview of Java, Data types and variables

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Lab5. Wooseok Kim

Methods. Bok, Jong Soon

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Programming with Java

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

CS313D: ADVANCED PROGRAMMING LANGUAGE

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Java Fundamentals (II)

EEE-425 Programming Languages (2013) 1

FRAC: Language Reference Manual

Repetition Structures

Java Programming Tutorial 1

Final Exam CS 251, Intermediate Programming December 10, 2014

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Java+- Language Reference Manual

4. Structure of a C++ program

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

QUESTION BANK: INTRODUCTION TO PROGRAMMING (HCS102) a. Compare and contrast unboxing and widening using code snippets [10]

Lexical Considerations

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Fall 2017 CISC124 9/16/2017

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Darrell Bethea May 25, 2011

Software and Programming 1

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

3. Java - Language Constructs I

1 Lexical Considerations

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Getting started with Java

CSc 520 Principles of Programming Languages

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Overview. Lab 5 Methods and Parameters

Welcome to Python 3. Some history

Chapter 1 Getting Started

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Transcription:

all people who know the language. Similarly, programming languages also have a vocabulary, which is referred to as the set of keywords of that language, and a grammar, which is referred to as the syntax. Consider a scenario, where one person speaks in English with the other person. However, the person who is listening understands Chinese and not English. Therefore, he calls a translator who can translate the words of English to Chinese. Similarly, computers cannot directly understand the instructions written in a programming language. Therefore, you need a translator to convert the instructions written in a programming language to machine language. A compiler is a special program that processes the statements written in a particular programming language and converts them into machine language. Like everything else in the computer, the compiler also follows the Input- as input. It processes these instructions to convert them to machine language. Process-Output (I-P-O) cycle. It takes the programming language instructions These instructions can then be executed by the computer. This process of conversion is called compilation. The following figure shows the working of a compiler. Page 3 of 36 Working of a Compiler For each programming language, there is a different compiler available. For example, to compile a program written in the C language, you require a C compiler. For a Java program, you require a Java compiler. For C# programs, you will use the csc compiler.

Page 4 of 36 Using C# for Writing Programs Similar to the various programming languages, C# also has some predefined keywords that can be used for writing programs. For example, class is a keyword in C# that is used to define classes. Keywords are reserved words that have a special meaning. Further, the syntax for C# defines rules for grammatical arrangement of these keywords. For example, the syntax of declaring a class in C# is: class <class name>... In the preceding syntax, the braces, known as delimiters, are used to indicate the start and end of a class body.

You can also create your own namespaces that contain classes that you need to use across several programs. In addition to using the predefined classes, you can also define your own classes. Consider the following code, which defines a class named Hello: public class Hello public static void Main(string[] args) System.Console.WriteLine("Hello, World! \n"); The preceding class declaration includes the method, Main() that will display the message, Hello, World! on your screen. The preceding code includes the following components: The class keyword The class name The Main() method The System.Console.WriteLine() method Escape sequences Page 6 of 36 Let us discuss these components in detail. The class Keyword The class keyword is used to declare a class. In the preceding code, the class keyword declares the class, Hello. The Class Name The class keyword is followed by the name of the class. In the preceding code, Hello is the name of the class defined by using the class keyword. Class Naming Conventions in C# Class names should follow certain naming conventions or guidelines. A class name:

Page 10 of 36 Let s Practice Michelle wrote the following code snippet to print the personal details for a resume: using System; public class Hello public static void Main(string[] args) //Insert code here She wants to print the following output: Michelle Gracias Email: michelle2012@abc.net

type can be used to store a value of any type. A variable of this data type is declared by using the dynamic keyword. Consider the following statement: dynamic id; In the preceding statement, the variable id is declared as a dynamic variable. This means that it can be used to store any type of value, as shown in the following statements: id = 1; //id is considered as int id = A001 ; //id is considered as string In the preceding statements, the variable, id inherits the data type of the assigned value. In the first assignment statement, id is assigned an int value. Therefore, it is treated as an integer. In the second assignment statement, it is assigned a string value. Therefore, it is considered a string. Dynamic variables can use the methods associated with the data type they are resolved to. For example, a dynamic variable containing a string value can access all the string related attributes and methods, as shown in the following code snippet: dynamic d = Sample ; Page 18 of 36 int len = d.length; The Length attribute of a string stores the length of a string. For dynamic variables, type checking is done at run time and not at compile time. This is known as duck typing. For example, consider the following code snippet: dynamic var = 1; int len = var.length; In the preceding code snippet, no error will be thrown at compile time because dynamic variables are allowed to access members associated with any type. However, at run time the code will generate an error, as var is resolved as an int type and length property is not associated with int data type.

The Member Variables Data members of a class are called member variables. In the given code, the Car class has two member variables, Engine and NoOfWheels. Thesee variables are used to store the dataa provided by the user. The variables are declared as private. This means that the variables are accessible only inside the class in which they are declared. Any code outside the class cannot access the private variables declared in the class. The Member Functions Member methods are also known as member functions. The code for the Car class contains two member functions, InputDetails() and DisplayDetails(). Both the methods are declared as public methods. This means that the methods are accessible from any other class outside the Car class. The InputDetails() and DisplayDetails() methods are declaredd with the return type, void. This means that the methods do not return any value. The GetEngine() method is declared with the return type, string. This means that the method returns a string value. The InputDetails() method prompts the user to enter the engine model and number of wheels in the vehicle. It then stores these details in the respective variables. The DisplayDetails() method displays the values stored in the member variables. Page 24 of 36 You will learn more about methods in the subsequent chapters. The Instantiating Class The MainClass class contains the Main() method. This class is used to instantiate the Car class. The first line of code that a C# compiler looks for is the Main() method. To implement the functionality of a class, you need to create an object of the class. Consider a class named Car. To create an object named MyCar of the Car class, the following statement can be used in the Main() method: Car MyCar = new Car( ();