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 3: Object-Oriented Programming in Java and J# Microsoft.NET Workshops for Teachers

3 3-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, and 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 3-4 MicrosoftAP Computer Science with.NET and J# Lecture — Objectives “While procedural programming focuses on the code, object-oriented programming (OOP) is all about objects — solving a problem using objects that model the problem domain. Examples of common objects include Students, Customers, ShoppingCarts, Rectangles, and Files. Today, OOP is considered the best overall approach since it more closely models how we think about problems…” Topics: –Object-oriented programming –Creating objects –Writing classes –Working with objects [ Joe Hummel, Lake Forest College ]

5 3-5 MicrosoftAP Computer Science with.NET and J# Part 1 Object-Oriented Programming

6 3-6 MicrosoftAP Computer Science with.NET and J# Motivation 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

7 3-7 MicrosoftAP Computer Science with.NET and J# public static void main(String[] args) throws Exception { String first, last; int midExam, finalExam; first = getString("Please enter first name: "); last = getString("Please enter last name: "); midExam = getInt("Enter midterm exam score: "); finalExam = getInt("Enter final exam score: "); outputNameAndExamAverage(first, last, midExam, finalExam); System.in.read(); // keep console window open until user presses ENTER… } public static void main(String[] args) throws Exception { String first, last; int midExam, finalExam; first = getString("Please enter first name: "); last = getString("Please enter last name: "); midExam = getInt("Enter midterm exam score: "); finalExam = getInt("Enter final exam score: "); outputNameAndExamAverage(first, last, midExam, finalExam); System.in.read(); // keep console window open until user presses ENTER… } Example — Before OOP Suppose we want to compute a student's exam score average Procedural approach: –We use local vars to hold data –We use procedures to compute

8 3-8 MicrosoftAP Computer Science with.NET and J# Same Example using OOP Object-oriented approach: –Think in terms of a Student object –The Student object holds the data & performs the computations public static void main(String[] args) throws Exception { Student s; s = new Student(); // create a Student object to hold data s.outputNameAndExamAverage(); // ask object to output name and exam average System.in.read(); } public static void main(String[] args) throws Exception { Student s; s = new Student(); // create a Student object to hold data s.outputNameAndExamAverage(); // ask object to output name and exam average System.in.read(); } Key idea: the same work is being performed (prompt, input, compute), it’s just being done by a separate, self-contained program entity — an “object”. Objects give us a more natural way to organize programs… Student

9 3-9 MicrosoftAP Computer Science with.NET and J# Advantages? Makes it easier to represent lots of students –What if you had 100 students? –Easier to have 100 self-contained objects than 400 pieces of data… Makes it easier to work with complex problems –What if you had 1200 students, 100 faculty & 3000 courses to track? –Easier to think in terms of self-contained Student, Faculty and Course objects…

10 3-10 MicrosoftAP Computer Science with.NET and J# Student Class Objects are defined by templates called classes Here’s our Student class: –A student has 4 variables (“ fields ”) and two procedures (“ methods ”) public class Student { public String first, last; // fields for name and exam scores public int midExam, finalExam; public Student() throws Exception // method {. } public void outputNameAndExamAverage() throws Exception // method {. } public class Student { public String first, last; // fields for name and exam scores public int midExam, finalExam; public Student() throws Exception // method {. } public void outputNameAndExamAverage() throws Exception // method {. } Kim Lee 100 81 Method with same name as class is called a “constructor” — called automatically by Java’s new operator to initialize object’s fields Ordinary subroutine method — note the lack of “static” keyword, because method is called via “object.” syntax

11 3-11 MicrosoftAP Computer Science with.NET and J# Demo! Student exam average using objects…

12 3-12 MicrosoftAP Computer Science with.NET and J# Implementation Details Here’s a complete implementation of the Student class: public class Student { public String first, last; public int midExam, finalExam; public Student() throws Exception { System.out.print("Please enter first name: "); first = Program.keyboard.readLine(); System.out.print("Please enter first name: "); last = Program.keyboard.readLine(); System.out.print("Enter midterm exam score: "); midExam = Integer.parseInt(Program.keyboard.readLine()); System.out.print("Enter final exam score: "); finalExam = Integer.parseInt(Program.keyboard.readLine()); } public void outputNameAndExamAverage() throws Exception { double avg; avg = (midExam + finalExam) / 2.0; System.out.println(first + " " + last + ", your average is " + avg); } public class Student { public String first, last; public int midExam, finalExam; public Student() throws Exception { System.out.print("Please enter first name: "); first = Program.keyboard.readLine(); System.out.print("Please enter first name: "); last = Program.keyboard.readLine(); System.out.print("Enter midterm exam score: "); midExam = Integer.parseInt(Program.keyboard.readLine()); System.out.print("Enter final exam score: "); finalExam = Integer.parseInt(Program.keyboard.readLine()); } public void outputNameAndExamAverage() throws Exception { double avg; avg = (midExam + finalExam) / 2.0; System.out.println(first + " " + last + ", your average is " + avg); } import java.io.*; public class Program { public static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); import java.io.*; public class Program { public static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); Note use of “Classname.” when accessing static item

13 3-13 MicrosoftAP Computer Science with.NET and J# Part 2 Working with Objects

14 3-14 MicrosoftAP Computer Science with.NET and J# Java Objects Java is an object-oriented programming language As a result, most things in Java are objects –Keyboard, console screen, strings, … import java.io.*; public class Program { public static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception {. String s; s = keyboard.readLine(); // ask keyboard object to read next line of input int len; len = s.length(); // ask string object for length of its string System.out.println(len); // now ask console screen object to output this value for us char c; c = s.charAt(len – 1); // ask string object for last char in string (0-based indexing) System.out.println(c); import java.io.*; public class Program { public static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception {. String s; s = keyboard.readLine(); // ask keyboard object to read next line of input int len; len = s.length(); // ask string object for length of its string System.out.println(len); // now ask console screen object to output this value for us char c; c = s.charAt(len – 1); // ask string object for last char in string (0-based indexing) System.out.println(c);

15 3-15 MicrosoftAP Computer Science with.NET and J# Demo! Working with common Java objects…

16 3-16 MicrosoftAP Computer Science with.NET and J# Beware — Java vs.NET objects Java contains a large class library called the JCL –Java Class Library.NET also contains a large class library called the FxCL –.NET Framework Class Library How do you tell them apart in J#? –Look for the little “J” in the IntelliSense icons, which means Java… len = s.l

17 3-17 MicrosoftAP Computer Science with.NET and J# Beware — not everything is an object? Wait! Not everything in Java is an object…  The good news is this makes Java programs more efficient  The bad news is this makes Java programming more complex Example: –Integers and reals are not objects, so you cannot treat them as such Object obj; obj =...; // let obj represent *any* object in Java int i; i =...; // let i represent any integer value String s1, s2; s1 = obj.toString(); // every object has a toString() method to convert object to a string s2 = i.toString(); s2 = String.valueOf(i); // since i is not an object, we have to ask String class to convert it Object obj; obj =...; // let obj represent *any* object in Java int i; i =...; // let i represent any integer value String s1, s2; s1 = obj.toString(); // every object has a toString() method to convert object to a string s2 = i.toString(); s2 = String.valueOf(i); // since i is not an object, we have to ask String class to convert it

18 3-18 MicrosoftAP Computer Science with.NET and J# Demo! Not everything in Java is an object…

19 3-19 MicrosoftAP Computer Science with.NET and J# Differentiating between Objects and Non-Objects The easiest way to identify objects is via capitalization –If type starts with an UPPER-CASE letter, it represents an object –If type starts with a lower-case letter, it is not an object int i = 1234; double d = 3.14159; String s = "Hello world!"; BufferedReader keyboard = new...; int i = 1234; double d = 3.14159; String s = "Hello world!"; BufferedReader keyboard = new...; non-objects objects

20 3-20 MicrosoftAP Computer Science with.NET and J# Learning more about Java Objects Java contains 1,000’s of classes One of the hurdles to learning Java is learning its classes… How to learn more? –Java books and tutorials –Explore via Visual Studio’s IntelliSense –Visual Studio’s integrated help (F1) Position cursor on type and press F1 Lookup in index using Java package name + type (e.g. “java.lang.String”) –Sun’s online documentation: http://java.sun.com/j2se/1.5.0/docs/api/

21 3-21 MicrosoftAP Computer Science with.NET and J# Summary Java programming is object-oriented programming –though procedural programming is alive and well 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 Is object-oriented programming easier? –at first, no –in the long-term, yes…

22 3-22 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

23 3-23 MicrosoftAP Computer Science with.NET and J# That’s it! Next up: LectureTopic.. 4Primitive Types vs. Object References (==, equals, toString, arrays)......

24


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

Similar presentations


Ads by Google