Click here to load reader
Upload
hock-leng-puah
View
736
Download
0
Embed Size (px)
DESCRIPTION
Variables - Value and Reference Type, Non-Static Vs Static Classes
Citation preview
Software Programming and Fundamentals
Recap what we covered in week #1
Variable
• Variable: imagine like a box to store one thing (data)
• Eg: int age; age = 5;
Variable
• Variable: imagine like a box to store one thing (data)
• Eg: int age; age = 5;
int - Size of variable must be big enough to store Integer
Variable
• Variable: imagine like a box to store one thing (data)
• Eg: int age; age = 5;
int - Size of variable must be big enough to store Integer
age - Name of variable
age
Variable
• Variable: imagine like a box to store one thing (data)
• Eg: int age; age = 5;
int - Size of variable must be big enough to store Integer
age - Name of variable
age = 5 – Put a data 5 into the variable
age
5
Value Type Vs Reference Type
• Variables: two types– Value type (simple type like what you just saw)• Only need to store one thing (5, 3.5, true/false, ‘C’ and
“string”)
– Reference type (complex type for objects)• Need to store more than one thing (age + height + run()
+ … )
Reference Type
• Reference type (complex type for objects)• Eg: Human john; john = new Human();
Compare this to int age;age = 5;
Reference Type
• Reference type (complex type for objects)• Eg: Human john; john = new Human();
Human - Size of variable must be big enough to store an Address
john
Reference Type
• Reference type (complex type for objects)• Eg: Human john; john = new Human();
Human - Size of variable must be big enough to store an Address
john - Name of variable
Reference Type
• Reference type (complex type for objects)• Eg: Human john; john = new Human();
Human - Size of variable must be big enough to store an Address
john - Name of variable
john = new Human() – Get a house with enough space for john (age, height, etc)
john
D403
age
height
…
Non-Static Vs Static Class
• Non-Static: need New() to instantiate / create an object – like what you see just now
• Static: no need to use New() to use, there is just one copy of the class. This type of class basically to provide special functions for other objects
• So if you see a class being used without New(): it is a static class
• Eg Math class age = Math.Round(18.5); // Math rounding
Using Console Program for problem solving
• Console program:– Simple– Procedural (from top to bottom)– Getting inputs:
• From arguments: How? • From keyboard: How?
• Exercise 2.1 – 2.5– Language fundamental: int, double, string, Console.WriteLine(),
Console.Write(), Console.ReadLine(), int.Parse(), double.Parse(), simple if then statement
– Problem solving: repeating, swopping of 2 variables, simple sorting