Presentation is loading. Please wait.

Presentation is loading. Please wait.

Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#

Similar presentations


Presentation on theme: "Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#"— Presentation transcript:

1 Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#

2 “AP Computer Science with.NET and J#” Lecture 5: Writing Your Own Classes Microsoft.NET Workshops for Teachers

3 5-3 MicrosoftAP Computer Science with.NET and J# Workshop Track LectureTopic 1Java — History, Philosophy, and Overview 2Procedural Programming in Java and J# 3Object-oriented Programming in Java and J# 4Primitive Types vs. Object References (==, equals, toString, arrays) 5Writing Your Own Classes 6Keeping Track of Your Objects with the Collection Classes 7Algorithms and Algorithm Analysis 8Debugging and Exception Handling...... 18Collection Classes and Iteration 19Additional Resources and Ideas

4 5-4 MicrosoftAP Computer Science with.NET and J# Lecture — Objectives “One of the motivations for object-oriented programming is the idea that objects can more closely model your problem domain — customers in a sales application, shapes in a graphics program, and documents in a word processor. This also represents the next step up the Java ladder, learning to identify and create your own custom objects…” Topics: –Working with Java objects (part 2) –Creating your own classes –Designing your own classes [ Joe Hummel, Lake Forest College ]

5 5-5 MicrosoftAP Computer Science with.NET and J# Part 1 Working with Java Objects (part 2)

6 5-6 MicrosoftAP Computer Science with.NET and J# More Java Objects Let’s experiment with a few more built-in objects: –java.util.Date –java.util.GregorianCalendar import java.util.*; // Output today’s date: Date today; today = new Date(); System.out.println( today.toString() ); // How many days until Christmas? GregorianCalendar calendar, xmas2006; int daysTilXmas; calendar = new GregorianCalendar(); // a new Calendar object set to today xmas2006 = new GregorianCalendar(2006, 11, 25); // Calendar object set to xmas // DAY_OF_YEAR is # between 1..365, so we subtract today’s DOY from xmas DOY: daysTilXmas = xmas2006.get(Calendar.DAY_OF_YEAR) – calendar.get(Calendar.DAY_OF_YEAR); System.out.println(daysTilXmas); import java.util.*; // Output today’s date: Date today; today = new Date(); System.out.println( today.toString() ); // How many days until Christmas? GregorianCalendar calendar, xmas2006; int daysTilXmas; calendar = new GregorianCalendar(); // a new Calendar object set to today xmas2006 = new GregorianCalendar(2006, 11, 25); // Calendar object set to xmas // DAY_OF_YEAR is # between 1..365, so we subtract today’s DOY from xmas DOY: daysTilXmas = xmas2006.get(Calendar.DAY_OF_YEAR) – calendar.get(Calendar.DAY_OF_YEAR); System.out.println(daysTilXmas); Date Calendar

7 5-7 MicrosoftAP Computer Science with.NET and J# Visual Studio 2005 Object Test Bench Visual Studio 2005 has another way to experiment with objects –Object Test Bench ( View menu, Other Windows ) let’s you create objects & call methods, without having to write an entire program… –This feature is *not* available in the Visual Studio Express editions :-(

8 5-8 MicrosoftAP Computer Science with.NET and J# Demo! Working with objects…

9 5-9 MicrosoftAP Computer Science with.NET and J# Using the Object Test Bench (OTB) Step-by-step for using Object Test Bench with Java objects: –Switch to class view: View menu, Class View –Find Solution Explorer window (top-right) –Find the class you want to instantiate: Expand Project References Expand vjslib Expand package (e.g. java.util) Right-click on class (e.g. Date) Select Create Instance… Pick constructor Assign a variable name for the object (e.g. “today2”, avoiding names in main method) Ok –Switch to Object Test Bench window: View menu, Other Windows –Call methods! Right-click on object instance Invoke Method…

10 5-10 MicrosoftAP Computer Science with.NET and J# Part 2 Creating Your Own Classes

11 5-11 MicrosoftAP Computer Science with.NET and J# Recall… Why OOP? Object-oriented programming enables us to: –think in terms of real-world objects –improve organization since code & data reside together Common examples of objects in computer programs: –students –customers, products, shopping carts –graphical shapes (rectangles, circles, …) –graphical user interfaces (windows, buttons, text boxes, …) –files Object

12 5-12 MicrosoftAP Computer Science with.NET and J# Creating Your Own Classes Creating a class involves 5 activities: 1.Deciding what the class is going to represent 2.Defining one or more fields for storing an object’s data 3.Writing one or more constructors to initialize the fields 4.Writing one or more methods to perform needed computation 5.Using the class and testing it

13 5-13 MicrosoftAP Computer Science with.NET and J# Example Consider a Banking application… Possible objects: –Customer –Employee –Manager –Account Let’s create an Account class…

14 5-14 MicrosoftAP Computer Science with.NET and J# (1) The Account Class The Account class represents exactly one bank account –It could represent Savings, Checking, Loan, … –Account data: Account number Customer id Balance –Account operations: Deposit Withdrawal

15 5-15 MicrosoftAP Computer Science with.NET and J# Demo! Working with the Account class via the OTB…

16 5-16 MicrosoftAP Computer Science with.NET and J# (2) Fields Every account is represented by 3 pieces of data: –Account number –Customer id –Balance public class Account { // Fields: public int accountNumber; public int customerID; public double balance; public class Account { // Fields: public int accountNumber; public int customerID; public double balance; Object Account

17 5-17 MicrosoftAP Computer Science with.NET and J# (3) Constructors Constructors are defined to ensure fields are initialized –Recall that constructors are methods with same name as class public class Account { // Fields: public int accountNumber; public int customerID; public double balance; // Constructor: public Account(int acctNum, int custID, double balance) { // initialize *this* object’s fields based on parameters: this.accountNumber = acctNum; this.customerID = custID; this.balance = balance; } public class Account { // Fields: public int accountNumber; public int customerID; public double balance; // Constructor: public Account(int acctNum, int custID, double balance) { // initialize *this* object’s fields based on parameters: this.accountNumber = acctNum; this.customerID = custID; this.balance = balance; } Object Account

18 5-18 MicrosoftAP Computer Science with.NET and J# (4) Methods Methods perform computation on the fields –In this case, deposit and withdrawal –For simplicity, we ignore error conditions (e.g. insufficient funds ) public class Account { public int accountNumber; public int customerID; public double balance;. public void deposit(double amount) { this.balance = this.balance + amount; } public void withdrawal(double amount) { this.balance = this.balance - amount; } public class Account { public int accountNumber; public int customerID; public double balance;. public void deposit(double amount) { this.balance = this.balance + amount; } public void withdrawal(double amount) { this.balance = this.balance - amount; } Object Account

19 5-19 MicrosoftAP Computer Science with.NET and J# (5) Using the Account Class Let’s create an account, then make a deposit: public static void main(String[] args) throws Exception { Account acct; double amt; System.out.print("Enter initial balance: "); amt = Double.valueOf(keyboard.readLine()).doubleValue(); acct = new Account(12345, 99216, amt); System.out.println(acct.toString()); System.out.println(); System.out.print("Enter an amount to deposit: "); amt = Double.valueOf(keyboard.readLine()).doubleValue(); acct.deposit(amt); System.out.println(acct.toString()); public static void main(String[] args) throws Exception { Account acct; double amt; System.out.print("Enter initial balance: "); amt = Double.valueOf(keyboard.readLine()).doubleValue(); acct = new Account(12345, 99216, amt); System.out.println(acct.toString()); System.out.println(); System.out.print("Enter an amount to deposit: "); amt = Double.valueOf(keyboard.readLine()).doubleValue(); acct.deposit(amt); System.out.println(acct.toString()); Account —————— 12345 99216 1020.53

20 5-20 MicrosoftAP Computer Science with.NET and J# Demo! Working with the Account class in code…

21 5-21 MicrosoftAP Computer Science with.NET and J# Provide toString Method One of Java’s class design rules: provide a toString method –So objects can be converted to strings for output, debugging, … public class Account { public int accountNumber; public int customerID; public double balance;. // Returns a string-based representation of this object: public String toString() { java.text.DecimalFormat formatter; String formattedBalance; formatter = new java.text.DecimalFormat("$#,##0.00"); formattedBalance = formatter.format(this.balance); return "Bank Account #" + this.accountNumber + ": " + formattedBalance; } public class Account { public int accountNumber; public int customerID; public double balance;. // Returns a string-based representation of this object: public String toString() { java.text.DecimalFormat formatter; String formattedBalance; formatter = new java.text.DecimalFormat("$#,##0.00"); formattedBalance = formatter.format(this.balance); return "Bank Account #" + this.accountNumber + ": " + formattedBalance; }

22 5-22 MicrosoftAP Computer Science with.NET and J# Summary Object-oriented programming requires a new way of thinking –a program is built from classes –classes are templates for creating objects –objects store program data & perform program computations Eventually, you’ll want to start writing your own classes –define fields to hold data –define constructors to initialize fields –define methods to perform computation –define toString to return meaningful, string-based representation

23 5-23 MicrosoftAP Computer Science with.NET and J# Resources Web site for slides, demos, associated lab exercises: –http://blogs.msdn.com/daryllmc/default.aspxhttp://blogs.msdn.com/daryllmc/default.aspx –http://www.lakeforest.edu/~hummel/workshops-HS.htmhttp://www.lakeforest.edu/~hummel/workshops-HS.htm –https://www.mainfunction.com/home/training/https://www.mainfunction.com/home/training/ Supplemental Reading: –M. Weisfeld, “ The Object-Oriented Thought Process ” (language-neutral) –D. West, “ Object Thinking ” (language-neutral) –Sierra & Bates, “ Head First Java ” Your students may really like this one, a very hip approach to learning Java

24 5-24 MicrosoftAP Computer Science with.NET and J# That’s it! Next up: LectureTopic.. 6Keeping Track of Your Objects with the Collection Classes......

25


Download ppt "Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#"

Similar presentations


Ads by Google