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

Slides:



Advertisements
Similar presentations
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Advertisements

Chapter 10 Introduction to Arrays
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Requirements and Design
April 20023CSG11 Electronic Commerce Java (1) John Wordsworth Department of Computer Science The University of Reading Room 129,
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Chapter 8: Writing Graphical User Interfaces
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College Lecture 2: Working with Visual.
Introduction to Object-Oriented Programming
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Chapter 3 Implementing Classes. Assignment Read 3.1 – 3.5 and take notes complete Self Check Exercises 1-10; Due September 24 th Read 3.6 – 3.8 and take.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 21 COP 3540 Data Structures with OOP Overview: Chapter 1.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Topic 4 Inheritance.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College Lecture 2: Working with Visual.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Class Design I Class Contracts Readings: 2 nd Ed: Section 9.5, Advanced Topic nd Ed: Section 8.5, Advanced Topic 8.2 Some ideas come from: “Practical.
Session 6 Comments on Lab 3 & Implications of Inheritance.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Chapter 3 Implementing Classes
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Electronic Commerce Java (1)
Objects Real and Java COMP T1 3
Java OOP Overview Classes and Objects, Members and Class Definition, Access Modifier, Encapsulation Java OOP Overview SoftUni Team Technical Trainers.
Implementing Classes Yonglei Tao.
CS 302 Week 11 Jim Williams, PhD.
Lecture 3: The J# Language
Java IO and Testing made simple
Introduction to Object-Oriented Programming
Using java libraries CGS3416 spring 2019.
slides created by Ethan Apter
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

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

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

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 Collection Classes and Iteration 19Additional Resources and Ideas

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 MicrosoftAP Computer Science with.NET and J# Part 1 Working with Java Objects (part 2)

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 , 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 , 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

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 :-(

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

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…

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

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

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

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…

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

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

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

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

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

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 ——————

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

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; }

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

5-23 MicrosoftAP Computer Science with.NET and J# Resources Web site for slides, demos, associated lab exercises: – – – 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

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