Lab - 1. Solution : 1. // Building a Simple Console Application. class HelloCsharp. static void Main() System.Console.WriteLine ("Hello from C#.

Similar documents
Engr 123 Spring 2018 Notes on Visual Studio

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

RegEx - Numbers matching. Below is a sample code to find the existence of integers within a string.

CALCULATOR APPLICATION

RegEx-validate IP address. Defined below is the pattern for checking an IP address with value: String Pattern Explanation

INFORMATICS LABORATORY WORK #2

Object oriented lab /second year / review/lecturer: yasmin maki

Programming in C# with Microsoft Visual Studio 2010

10266 Programming in C Sharp with Microsoft Visual Studio 2010

"Charting the Course... MOC Programming in C# with Microsoft Visual Studio Course Summary

GradeBook code. Main Program

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

Developing for Mobile Devices Lab (Part 1 of 2)

Now find the button component in the tool box. [if toolbox isn't present click VIEW on the top and click toolbox]

Chapter 1 Getting Started

CSCE 110: Programming I

Programming Using C# QUEEN S UNIVERSITY BELFAST. Practical Week 7

You can call the project anything you like I will be calling this one project slide show.

Department of Networks College of Bardarash Technical Institute DUHOK Polytechnic University Subject: Programming Fundamental by JAVA Course Book

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

DC69 C# and.net JUN 2015

To start we will be using visual studio Start a new C# windows form application project and name it motivational quotes viewer

Conventions in this tutorial

Yes, this is still a listbox!

Learn C# Errata. 3-9 The Nullable Types The Assignment Operators

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

Advanced Programming C# Lecture 2. dr inż. Małgorzata Janik

Cleveland State University. Lecture Notes Feb Iteration Arrays ArrayLists. V. Matos

Programming in Visual Basic with Microsoft Visual Studio 2010

PROGRAMMING IN VISUAL BASIC WITH MICROSOFT VISUAL STUDIO Course: 10550A; Duration: 5 Days; Instructor-led

This is an open-book test. You may use the text book Be Sharp with C# but no other sources, written or electronic, will be allowed.

Working with Objects. Overview. This chapter covers. ! Overview! Properties and Fields! Initialization! Constructors! Assignment

Start Visual Studio and create a new windows form application under C# programming language. Call this project YouTube Alarm Clock.

Experiment 5 : Creating a Windows application to interface with 7-Segment LED display

CSCE 145 Exam 2 Review No Answers. This exam totals to 100 points. Follow the instructions. Good luck!

Visual Studio.NET.NET Framework. Web Services Web Forms Windows Forms. Data and XML classes. Framework Base Classes. Common Language Runtime

if (say==0) { k.commandtext = "Insert into kullanici(k_adi,sifre) values('" + textbox3.text + "','" + textbox4.text + "')"; k.

Create a Java project named week10

DigiPen Institute of Technology

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

First start a new Windows Form Application from C# and name it Interest Calculator. We need 3 text boxes. 4 labels. 1 button

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

The Open Core Interface SDK has to be installed on your development computer. The SDK can be downloaded at:

CSIS 1624 CLASS TEST 6

Problem Statement. B1: average speed

IBSDK Quick Start Tutorial for C# 2010

Step4: Now, Drag and drop the Textbox, Button and Text block from the Toolbox.

Teacher Training Solutions

Start Visual Studio, start a new Windows Form project under the C# language, name the project BalloonPop MooICT and click OK.

LISTING PROGRAM. // // TODO: Add constructor code after the InitializeComponent() call. //

We are going to use some graphics and found a nice little batman running GIF, off course you can use any image you want for the project.

DC69 C# &.NET DEC 2015

This is an open-book test. You may use the text book Be Sharp with C# but no other sources, written or electronic, will be allowed.

Form Properties Window

Getting Started with Banjos4Hire

204111: Computer and Programming

UNIVERSITY OF THE FREE STATE DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS RIS 214 MODULE TEST 1

In-Class Worksheet #4

IT 374 C# and Applications/ IT695 C# Data Structures

MATFOR In Visual C# ANCAD INCORPORATED. TEL: +886(2) FAX: +886(2)

FDSc in ICT. Building a Program in C#

Start Visual Studio, create a new project called Helicopter Game and press OK

Getter and Setter Methods

C# Language Changes and Updates

DAD Lab. 1 Introduc7on to C#

Langage C# et environnement.net M1 Année

Introduction to C# Applications

Problem Statement. A1: being lapped on the track

Representing Recursive Relationships Using REP++ TreeView

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science And Engineering

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

Instructions for writing Web Services using Microsoft.NET:

Your First Windows Form

Click on the empty form and apply the following options to the properties Windows.

Visual C# 2010 Express

C# Programming. Unit 1: Introducing C# and the.net FrameworkThis module explains the.net Framework, and using C# and

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

A method is a code block that contains a series of statements. Methods. Console.WriteLine(); Console.ReadKey(); Console.ReadKey(); int.

Answer the following questions on the answer sheet that is provided. The computer must be switched off while you are busy with Section A.

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

UCT Algorithm Circle: Number Theory

Loops / Repetition Statements

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter Lab 8 - Structures

Getting started 7. Storing values 21. Creating variables 22 Reading input 24 Employing arrays 26 Casting data types 28 Fixing constants 30 Summary 32

Visual Basic/C# Programming (330)

CS/COE 1501 cs.pitt.edu/~bill/1501/ More Math

Computer Programming: C++

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

This is the empty form we will be working with in this game. Look under the properties window and find the following and change them.

Lesson11-Inheritance-Abstract-Classes. The GeometricObject case

SUN Certified Programmer for J2SE 5.0 Upgrade. Download Full Version :

IST311 Chapter13.NET Files (Part2)

Lesson07-Arrays-Part4. Program class. namespace Lesson07ArraysPart4Prep { class Program { static double METER_TO_INCHES = 100 / 2.

-.Net Lab Programs Index S.no. Particulars Page no Write a Program in C# to demonstrate Command line arguments processing.

C# Programming Tutorial Lesson 1: Introduction to Programming

C# Crash Course Dimitar Minchev

Creating and Running Your First C# Program

Lecture 4 Overview of Classes, Containers & Generics

EEE-425 Programming Languages (2013) 1

Unit 1 Workbook ICS3U

Transcription:

Lab - 1 Solution : 1 // Building a Simple Console Application class HelloCsharp static void Main() System.Console.WriteLine ("Hello from C#."); Solution: 2 & 3 // Building a WPF Application // Verifying the Application Open Visual Studio 2008 and choose "File", "New", "Project..." in the main menu. Choose "WPF Application" as project type. Choose a folder for your project and give it a name. Then press "OK"

Visual Studio creates the project and automatically adds some files to the solution. A Window1.xaml and an App.xaml. The structure looks quite similar to WinForms, except that the Window1.designer.cs file is no longer code but it's now declared in XAML as Window1.xaml/MainWindow.xaml Open the Window1.xaml/MainWindow.xaml file in the WPF designer and drag a Button and a TextBox from the toolbox to the Window Select the Button and switch to the event view in the properties window (click on the little yellow lightning icon). Doubleclick on the "Click" event to create a method in the codebehind that is called, when the user clicks on the button.

Visual Studio automatically creates a method in the code-behind file that gets called when the button is clicked. private void button1_click(object sender, RoutedEventArgs e) textbox1.text = "Hello WPF"; The textbox has automatically become assigned the name textbox1 by the WPF designer. Set text Text to "Hello WPF!" when the button gets clicked and we are done! Start the application by hit [F5] on your keyboard.

Solution : 4 // Generating Documentation for an Application namespace Hello /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window public MainWindow() InitializeComponent(); private void button1_click(object sender, RoutedEventArgs e) textbox1.text = "Hello WPF";

Solution: 1 Lab - 2 // Calculating Square Roots with Improved Accuracy public static int Isqrt(int num) if (0 == num) return 0; // Avoid zero divide int n = (num / 2) + 1; // Initial estimate, never low int n1 = (n + (num / n)) / 2; while (n1 < n) n = n1; n1 = (n + (num / n)) / 2; // end while return n; // end Isqrt() Solution: 2 // Converting Integer Numeric Data to Binary namespace ConsoleApplication1 class Program static void Main(string[] args) Console.Write("Number: "); int datanumber = int.parse(console.readline()); int remainder; string result = string.empty;

while (datanumber > 0) remainder = datanumber % 2; datanumber /= 2; result = remainder.tostring() + result; Console.WriteLine("Binary: 0", result); Console.ReadKey();

Lab - 3 Solution: 1 //Calculating the Greatest Common Divisor of Two Integers by Using Euclid's Algorithm namespace ConsoleApplication1 class Program static void Main(string[] args) Console.WriteLine("GCD of 0 and 1 is 2", 1, 1, gcd(1, 1)); Console.WriteLine("GCD of 0 and 1 is 2", 1, 10, gcd(1, 10)); Console.WriteLine("GCD of 0 and 1 is 2", 10, 100, gcd(10, 100)); Console.WriteLine("GCD of 0 and 1 is 2", 5, 50, gcd(5, 50)); Console.WriteLine("GCD of 0 and 1 is 2", 8, 24, gcd(8, 24)); Console.WriteLine("GCD of 0 and 1 is 2", 36, 17, gcd(36, 17)); Console.WriteLine("GCD of 0 and 1 is 2", 36, 18, gcd(36, 18)); Console.WriteLine("GCD of 0 and 1 is 2", 36, 19, gcd(36, 19)); for (int x = 1; x < 36; x++) Console.WriteLine("GCD of 0 and 1 is 2", 36, x, gcd(36, x)); Console.Read(); private static int gcd(int a, int b) int t;

// Ensure B > A if (a > b) t = b; b = a; a = t; // Find while (b!= 0) t = a % b; a = b; b = t; return a;

Solution: 2 // Calculating the GCD of Three, Four, or Five Integers namespace ConsoleApplication1 class Program static void Main() Console.WriteLine(GCD(new[] 10, 15, 30, 45 )); Console.ReadKey(); static int GCD(int a, int b) return b == 0? a : GCD(b, a % b); static int GCD(int[] integerset) return integerset.aggregate(gcd);

Lab - 4 Solution: 1 // Find the second largest element namespace ConsoleApplication1 class Program static void Main() int[] myarray = new int[] 0, 1, 2, 3, 13, 8, 5 ; int largest = int.minvalue; int second = int.minvalue; foreach (int i in myarray) if (i > largest) second = largest; largest = i; else if (i > second) second = i; System.Console.WriteLine(second); Console.ReadKey(); Solution: 2 // Multiplying Matrices namespace ConsoleApplication1 class Program static void Main(string[] args) int i, j;

int[,] a = new int[2, 2]; Console.WriteLine("Enter no for 2*2 matrix"); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) a[i, j] = int.parse(console.readline()); Console.WriteLine("First matrix is:"); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) Console.Write(a[i, j] + "\t"); Console.WriteLine(); int[,] b = new int[2, 2]; Console.WriteLine("Enter no for 2*2 matrix"); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) b[i, j] = int.parse(console.readline());

Console.WriteLine("second matrix is:"); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) Console.Write(b[i, j] + "\t"); Console.WriteLine(); Console.WriteLine("Matrix multiplication is:"); int[,] c = new int[2, 2]; for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) c[i, j] = 0; for (int k = 0; k < 2; k++) c[i, j] += a[i, k] * b[k, j]; for (i = 0; i < 2; i++) for (j = 0; j < 2; j++)

Console.Write(c[i, j] + "\t"); Console.WriteLine(); Console.ReadKey(); Solution: 3 //Find sum of all the elements present in a jagged array of 3 inner arrays namespace ConsoleApplication1 class Program int[][] jagged = new int[3][]; public void ReadArrays() Console.Write("\n Enter the size of First inner array: "); jagged[0] = new int[int.parse(console.readline())]; Console.WriteLine("\n Enter the elements of First inner array: "); for (int i = 0; i < jagged[0].length; i++) jagged[0][i] = int.parse(console.readline());

Console.Write("\n Enter the size of Second inner array: "); jagged[1] = new int[int.parse(console.readline())]; Console.WriteLine("\n Enter the elements of Second inner array: "); for (int i = 0; i < jagged[1].length; i++) jagged[1][i] = int.parse(console.readline()); Console.Write("\n Enter the size of Third inner array: "); jagged[2] = new int[int.parse(console.readline())]; Console.WriteLine("\n Enter the elements of Third inner array: "); for (int i = 0; i < jagged[2].length; i++) jagged[2][i] = int.parse(console.readline()); public void FindSum() int sum = 0; for (int i = 0; i < jagged[0].length; i++) sum = sum + jagged[0][i]; for (int i = 0; i < jagged[1].length; i++) sum = sum + jagged[1][i]; for (int i = 0; i < jagged[2].length; i++) sum = sum + jagged[2][i]; Console.WriteLine("\n\n\n Sum of all the three inner arrays is = 0", sum); public void PrintArrays() Console.Write("\n\n Elements of First inner array: "); for (int i = 0; i < jagged[0].length; i++)

Console.Write(jagged[0][i] + "\t"); Console.Write("\n\n Elements of Second inner array: "); for (int i = 0; i < jagged[1].length; i++) Console.Write(jagged[1][i] + "\t"); Console.Write("\n\n Elements of Third inner array: "); for (int i = 0; i < jagged[2].length; i++) Console.Write(jagged[2][i] + "\t"); class ArrayTest public static void Main() Program JA = new Program(); JA.ReadArrays(); JA.PrintArrays(); JA.FindSum(); Console.ReadLine();

Lab - 5 Q) Write a C# program with a class named Animal, make the animal class as Base class and Inheritance Animal class properties in Dog class and Cat class? Animal Class using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AnimalIh class Animal public void sleep() Console.WriteLine("Sleeping"); public void eating() Console.WriteLine("Eating");

Cat Class using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AnimalIh class Cat:Animal public void meow() Console.WriteLine("Meow Meowwwwwwwww"); Dog Class using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AnimalIh class Dog:Animal public void bark() Console.WriteLine("Whof Whof Whof Whof"); InheritanceEx Class using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Animal class InheritanceEx public static void Main(string[] args) Dog d = new Dog(); d.sleep();

d.bark(); Cat c = new Cat(); c.eating(); c.meow(); Console.ReadKey(); Procedure: 1. Create a New C# Console Application Project and Name it as AnimalIh. 2. By default you will get a class Named Program.cs with the Namespace AnimalIh 3. Rename Program.cs to Animal.cs and Click Yes button. 4. Write the Animal Class code inside it. 5. Goto Solution Explorer do right click on AnimalIh a drop down menu appears. 6. Select Add option you will get a submenu form that select class and name it as Cat.cs 7. Write the Cat Class code inside it. 8. Goto Solution Explorer do right click on AnimalIh a drop down menu appears. 9. Select Add option you will get a submenu form that select class and name it as Dog.cs 10. Write the Dog Class code inside it. 11. Goto Solution Explorer do right click on AnimalIh a drop down menu appears. 12. Select Add option you will get a submenu form that select class and name it as InheritanceEx.cs 13. Write the InheritanceEx Class code inside it. Save the solution and Run Debug button to get the below code. OUTPUT:

Lab - 6 Q1) Write a C# program to Generate Random Number using C# Console Application? Constructor without Parameters: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace RandomNumberGeneration class RandomNG static void Main(string[] args) Random r = new Random(); // Constructor without parameters int a = r.next(1000); string result = Convert.ToString(a); MessageBox.Show(result); OUTPUT:

Q2) Write a C# program to Generate Day, using C# Console Application? Constructor with Parameters: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace DateTimeEx class DateAndTime static void Main(string[] args) DateTime dt = new DateTime(2013, 11, 21);// Constructor with parameters int iday = dt.day; int imonth = dt.month; int iyear = dt.year; Console.WriteLine("MID-2 Exam Date:0-1-2",iday,imonth,iyear); // To Get Current Day Month and Year string Day = DateTime.Now.ToString("dd"); string Month = DateTime.Now.ToString("mm"); string Year = DateTime.Now.ToString("yyyy"); MessageBox.Show("Todays Date:"+Day+"-"+ Month+"-"+ Year); OUTPUT:

Lab - 7 Q1) Write a C# Program to Demonstrate Method Overloading? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace PolymorphismEx class OverloadingEx public void Add(int x, int y) int c = x + y; string result = Convert.ToString(c); MessageBox.Show("Addition of Numbers - "+result); public void Add(String a, String b) String c = a + b; MessageBox.Show("Addition of Strings - " + c); static void Main(string[] args) OverloadingEx ol = new OverloadingEx(); ol.add(20, 30); ol.add("object", "Oriented"); OUTPUT:

Q2) Write a C# Program to Demonstrate Method Overriding? OverridingM Class using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace OverridingEx class OverridingM public virtual void cal(int a, int b) MessageBox.Show("Calculation is started"); Calculation Class using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace OverridingEx class Calculation:OverridingM public override void cal(int a, int b) int c = a + b; Console.WriteLine("Overriding method Add"); base.cal(a,b); Console.WriteLine(c); Console.ReadKey();

OverridingMain Class using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace OverridingEx class OverridingMain public static void Main(string[] args) Calculation c = new Calculation(); c.cal(10, 20); OUTPUT: