Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.

Slides:



Advertisements
Similar presentations
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 3 Introduction to Classes, Objects, Methods, and Strings
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Programming Dr. Abraham Most slides are from your textbook.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved. 3 3 Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
1. 2 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Decisions { class.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
1 Console methods Write and WriteLine also have the capability to display formatted data. Figure 3.17 shows another way to use the WriteLine method. Outline.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall
 Simple C# program  Console applications input and output text in a console window, which in Windows XP and Windows Vista is known as the Command Prompt.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Chapter 2: Using Data.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
1.2 Primitive Data Types and Variables
Chapter One Lesson Three DATA TYPES ©
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Jozef Goetz,  2014 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved. expanded by J. Goetz, 2015 credits:
A DVANCED P ROGRAMMING C HAPTER 4: I NTRODUCTION TO I NTRODUCTION TO C LASSES, O BJECTS, M ETHODS AND STRINGS Dr Shahriar Bijani Winter 2016.
Computer Programs CS 1400 Dennis A. Fairclough Version 1.1 CS 1400 Dennis A. Fairclough Version 1.1.
A DVANCED P ROGRAMMING C HAPTER 5 & 6: C ONTROL S TRUCTURES Dr Shahriar Bijani Spring 2016.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Using the Console.
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Yanal Alahmad Java Workshop Yanal Alahmad
Computing with C# and the .NET Framework
DATA HANDLING.
Introduction to Classes and Objects
Dr Shahriar Bijani Winter 2017
Chapter 3 Introduction to Classes, Objects Methods and Strings
Java Enter your code from FRQ to Shell
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I UTPA – Fall 2012 This set of slides is revised from lecture slides.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Advanced Programming Chapters 5 & 6: Control Structures
Variables, Loops, Decision Statements, etc
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall 2012 This set of slides is revised from lecture.
4 Introduction to Classes and Objects.
Introduction to Classes and Objects
Classes, Objects, Methods and Strings
Object Oriented Programming in java
Java 1/31/2017 Back to Objects.
Visual Programming Lecture 4.
Presentation transcript:

Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA

Main Method Starting point of every application Console class has many methods including read and write. When using keyboard for input use readLine. You are going to use the Convert class all the time. Get to know this class well.

Console.Write and WriteLine ConsoleWriteLine(“{0}\n{1}”, “Welcome to”, “C# Programming!”); Format items are enclosed in curly braces and contain a sequence of characters that tell the method which argument to use and how to format it. Here there are two arguments named {0} and {1}. The first argument will be followed by a new line character.

Addition Program explained using System; public class Addition { public static void Main( string[] args ) { int number1; // declare first number to add int number2; // declare second number to add int sum; // declare sum of number1 and number2 Console.Write( "Enter first integer: " ); // prompt user number1 = Convert.ToInt32( Console.ReadLine() ); Console.Write( "Enter second integer: " ); // prompt user number2 = Convert.ToInt32( Console.ReadLine() ); sum = number1 + number2; // add numbers Console.WriteLine( "Sum is {0}", sum ); // display sum } // end Main } // end class Addition

Arithmatic + - * / % precedence given to the last three. Relational operators > = <= Equity Operators == !=

If statement explained using System; public class Comparison { public static void Main( string[] args ) { int number1; // declare first number to compare int number2; // declare second number to compare Console.Write( "Enter first integer: " ); number1 = Convert.ToInt32( Console.ReadLine() ); Console.Write( "Enter second integer: " ); number2 = Convert.ToInt32( Console.ReadLine() ); if ( number1 == number2 ) Console.WriteLine( "{0} == {1}", number1, number2 ); if ( number1 != number2 ) Console.WriteLine( "{0} != {1}", number1, number2 ); if ( number1 < number2 ) Console.WriteLine( "{0} < {1}", number1, number2 ); if ( number1 > number2 ) Console.WriteLine( "{0} > {1}", number1, number2 ); if ( number1 <= number2 ) Console.WriteLine( "{0} <= {1}", number1, number2 ); if ( number1 >= number2 ) Console.WriteLine( "{0} >= {1}", number1, number2 ); } // end Main

A class is like a type You can’t use a class until an instance (an object) of it is made. A class has methods and properties (attributes). You can set and get the properties. Attributes are specified by the class’s instance variable. You can perform operations using methods.

Example of a Class declaration using System; public class GradeBook { private string courseName; // course name for this GradeBook public string CourseName //Property { get { return courseName; } // end get set { courseName = value;} // end set } // end property CourseName public void DisplayMessage() { // use property CourseName to get the // name of the course that this GradeBook represents Console.WriteLine( "Welcome to the grade book for\n{0}!", CourseName ); // display property CourseName } // end method DisplayMessage } // end class GradeBook See next slide how to use this class

Using the class GradeBook using System; public class GradeBookTest { public static void Main( string[] args ) { GradeBook myGradeBook = new GradeBook(); //object created Console.WriteLine( "Initial course name is: '{0}'\n", myGradeBook.CourseName ); Console.WriteLine( "Please enter the course name:" ); myGradeBook.CourseName = Console.ReadLine(); // set CourseName Console.WriteLine(); // output a blank line myGradeBook.DisplayMessage(); } // end Main } // end class GradeBookTest

Setting and Getting values for instance variables Controlled ways of allowing users to access variables Usually get and set are made public so others can access them. Private courseName instance and Public CourseName property. Note the uppercase/lowercase difference. Properties contain accessors that handle the details of returning and modifying data. After defining the property you can just use = to set it or get it by referring to it such as in read. C# auto implements get/set for private instance variables. Example: Public string CourseName {get;set;} – You can assign values like myGradeBook.CourseName=Console.ReadLine();

Value Types vs. Reference Types Reference type contains the address of the location in memory where the data resides (refer to an object).

Initializing Objects with Constructors By default when an object is created it is initialized to null by default. If we rather have a different value initially, we can use constructors. The “new” operator automatically calls the constructor. See next two slides for example.

Gradebook class with a constructor using System; public class GradeBook { public string CourseName { get; set; } public GradeBook( string name ) //constructor { CourseName = name; // set CourseName to name } // end constructor public void DisplayMessage() { Console.WriteLine( "Welcome to the grade book for\n{0}!", CourseName ); } // end method DisplayMessage } // end class GradeBook

Test the class using System; public class GradeBookTest { public static void Main( string[] args ) { GradeBook gradeBook1 = new GradeBook("CS101 Introduction to C# Programming" ); // invokes constructor GradeBook gradeBook2 = new GradeBook( // invokes constructor "CS102 Data Structures in C#" ); Console.WriteLine( "gradeBook1 course name is: {0}", gradeBook1.CourseName ); Console.WriteLine( "gradeBook2 course name is: {0}", gradeBook2.CourseName ); } // end Main } // end class GradeBookTest

About some Types in C# Value Types Size (in bits) Range sbyte to 127 byte 8 0 to 255 short to ushort 16 0 to int to uint 32 0 to long to ulong 64 0 to char 16 0 to booltrue, false Enum types and struct types Reference types include class types, interface types, delegate types, and array types Pointer types

Floating-Point Numbers and decimal Real numbers can be stored as float, double and decimal. Decimal only stores limited range of decimal points precisely. Float and double stores approximation of real numbers in available spaces. Single precision floating point numbers have seven significant digits whereas the double gives 15 to 16 significant digits. If you indicate a real number with M such as 18.33M, it will be treated as a decimal number. (M stands for money).

BANK ACCOUNT EXAMPLE – CLASS WITH CONSTRUCTOR public class Account { private decimal balance; // instance variable that stores the balance public Account( decimal initialBalance ) // constructor { Balance = initialBalance; // set balance using property } // end Account constructor public void Credit( decimal amount ) // credit (add) an amount to the account { Balance = Balance + amount; // add amount to balance } // end method Credit public decimal Balance // a property to get and set the account balance { get { return balance;} // end get set { if ( value >= 0 ) balance = value; } // end set } // validate that value is greater than or equal to 0; } // end class Account

Create and manipulate an account using System; public class AccountTest { public static void Main( string[] args ) { Account account1 = new Account( 50.00M ); // create Account object Account account2 = new Account( -7.53M ); // create Account object Console.WriteLine( "account1 balance: {0:C}", account1.Balance ); // display initial balance of each object using a property Console.WriteLine( "account2 balance: {0:C}\n", account2.Balance ); // display Balance property decimal depositAmount; // deposit amount read from user Console.Write( "Enter deposit amount for account1: " ); // prompt and obtain user input depositAmount = Convert.ToDecimal( Console.ReadLine() ); Console.WriteLine( "adding {0:C} to account1 balance\n", depositAmount ); account1.Credit( depositAmount ); // add to account1 balance Console.WriteLine( "account1 balance: {0:C}", account1.Balance ); // display balances Console.WriteLine( "account2 balance: {0:C}\n", account2.Balance ); Console.Write( "Enter deposit amount for account2: " ); depositAmount = Convert.ToDecimal( Console.ReadLine() ); Console.WriteLine( "adding {0:C} to account2 balance\n", depositAmount ); account2.Credit( depositAmount ); // add to account2 balance Console.WriteLine( "account1 balance: {0:C}", account1.Balance ); Console.WriteLine( "account2 balance: {0:C}", account2.Balance ); } // end Main } // end class AccountTest

Your Third Assignment Write a program to calculate an estimate on a paint job. User will enter Price of paint per gallon, and length and height of each wall. Amount of paint needed will be calculated from sqft as well as labor cost. Create and use classes.