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

3 2-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 2-4 MicrosoftAP Computer Science with.NET and J# Lecture — Objectives “Procedural programming is all about code — you solve a problem by focusing on the procedures you need to write, and the order in which you want these procedures to execute. While this is now considered an old- fashioned way of programming, it is still viewed by some as the best way to teach beginners to program. The good news is that Java supports procedural programming, albeit a but clumsily…” Topics: –Visual Studio 2005 –Procedural programming –A quick tour of Java [ Joe Hummel, Lake Forest College ]

5 2-5 MicrosoftAP Computer Science with.NET and J# Part 1 A Quick Introduction to Visual Studio 2005

6 2-6 MicrosoftAP Computer Science with.NET and J# Visual Studio 2005 Microsoft’s flagship integrated development environment (IDE) One environment for all forms of software development: –Application types: console, desktop, web, … –Languages: J#, C#, C++, Visual Basic, … Many versions are available: –Express editions — free! — target one language / application type –Standard edition for entry-level programmers (no database provided) –Professional edition for enterprise programmers (with database) –Team editions = professional edition + software engineering tools –http://msdn.microsoft.com/vstudiohttp://msdn.microsoft.com/vstudio

7 2-7 MicrosoftAP Computer Science with.NET and J# Example Let’s rewrite our “ Hello world! ” program using Visual Studio –A simple console-based application…

8 2-8 MicrosoftAP Computer Science with.NET and J# Step 1: Create new project…

9 2-9 MicrosoftAP Computer Science with.NET and J# Step 2: Select J#, console app, name, location

10 2-10 MicrosoftAP Computer Science with.NET and J# Step 3: Implement… Code the program in coding window –use solution explorer window for navigating between program files –if you lose these windows, make visible via “View” menu coding window solution explorer

11 2-11 MicrosoftAP Computer Science with.NET and J# Step 4: Run! Just press F5 and off we go! –or use Debug menu, or VCR-like "Play" button on the toolbar –Visual Studio’s titlebar changes to display “Running”

12 2-12 MicrosoftAP Computer Science with.NET and J# Demo! “Hello world” console app in Visual Studio 2005…

13 2-13 MicrosoftAP Computer Science with.NET and J# Oops — the “console” flash By default, Visual Studio does the following: –opens DOS-like console window –runs program –closes window –so all you see is a flash! Solution? Read from keyboard… public class Program { public static void main(String[] args) throws Exception { System.out.println("Hello world!"); System.in.read(); // keep console window open until user presses ENTER… } public class Program { public static void main(String[] args) throws Exception { System.out.println("Hello world!"); System.in.read(); // keep console window open until user presses ENTER… } we’ll explain later…

14 2-14 MicrosoftAP Computer Science with.NET and J# Powerful Visual Studio features… IntelliSense Overloading Background compilation –to name just a few…

15 2-15 MicrosoftAP Computer Science with.NET and J# IntelliSense! IntelliSense is a fantastic advance –context-sensitive programming aid –reduces programming errors –encourages experimentation and exploration –TAB or ENTER to accept, ESC to dismiss… System.o

16 2-16 MicrosoftAP Computer Science with.NET and J# Overloading Java allows multiple procedures under the same name –known as overloading –procedures must differ in their parameters –Visual Studio depicts as a scrollable list… System.out.println(

17 2-17 MicrosoftAP Computer Science with.NET and J# Background Compilation Visual Studio is error checking in the background Errors are often detected as soon as stop typing –denoted by underlining much like a spelling mistake…

18 2-18 MicrosoftAP Computer Science with.NET and J# Part 2 Procedural Programming in Java and J#

19 2-19 MicrosoftAP Computer Science with.NET and J# Thinking in Terms of Procedures Procedural programming is thinking in terms of tasks –functions that compute & return values –subroutines that perform 1 or more operations Examples –Compute square root –Sort a list of names –Find median value –Input an integer –Output the maximum value –Output the minimum value –…

20 2-20 MicrosoftAP Computer Science with.NET and J# Example A program to apply Pythagorean’s theorem (c 2 = a 2 + b 2 ) –Given 2 sides of a right triangle, compute the length of the 3 rd side import java.io.*; // gain access to Java library for keyboard input public class Program { public static void main(String[] args) throws Exception { double a, b, c; // local variables System.out.println("** Welcome to Pythagorean Program **"); System.out.println(); c = getLength("Please enter length of hypotenuse (or 0): "); a = getLength("Please enter length of one side (or 0): "); b = getLength("Please enter length of other side (or 0): "); solveForUnknown(a, b, c); // c^2 = a^2 + b^2 System.in.read(); // keep console window open until user presses ENTER… } import java.io.*; // gain access to Java library for keyboard input public class Program { public static void main(String[] args) throws Exception { double a, b, c; // local variables System.out.println("** Welcome to Pythagorean Program **"); System.out.println(); c = getLength("Please enter length of hypotenuse (or 0): "); a = getLength("Please enter length of one side (or 0): "); b = getLength("Please enter length of other side (or 0): "); solveForUnknown(a, b, c); // c^2 = a^2 + b^2 System.in.read(); // keep console window open until user presses ENTER… }

21 2-21 MicrosoftAP Computer Science with.NET and J# Demo! Pythagorean program…

22 2-22 MicrosoftAP Computer Science with.NET and J# getLength Function Prompts the user and returns the user’s input as a real # –Requires creation of a keyboard object –User’s input is a string, must be converted to a real number ( double ) // create object we need to read from the keyboard: public static BufferedReader keyboard = new BufferedReader( new InputStreamReader(System.in)); public static double getLength(String prompt) throws Exception { String input; double value; System.out.print(prompt); // prompt the user input = keyboard.readLine(); // read a line of input as a string value = Double.parseDouble(input); // convert input string to a real # return value; } // create object we need to read from the keyboard: public static BufferedReader keyboard = new BufferedReader( new InputStreamReader(System.in)); public static double getLength(String prompt) throws Exception { String input; double value; System.out.print(prompt); // prompt the user input = keyboard.readLine(); // read a line of input as a string value = Double.parseDouble(input); // convert input string to a real # return value; } return type

23 2-23 MicrosoftAP Computer Science with.NET and J# solveForUnknown Subroutine Applies Pythagorean theorem to solve for unknown: public static void solveForUnknown(double a, double b, double c) throws Exception { int numberUnknowns = 0; double length; if (a == 0.0) numberUnknowns++; if (b == 0.0) numberUnknowns++; if (c == 0.0) numberUnknowns++; if (numberUnknowns == 1) { if (c == 0.0) // we are solving for hypotenuse c = sqrt(a^2 + b^2): { length = Math.sqrt(Math.pow(a, 2.0) + Math.pow(b, 2.0)); } else // we are solving for one of the legs, a = sqrt(c^2 – b^2) or b = sqrt(c^2 – a^2): { if (a == 0.0) length = Math.sqrt(Math.pow(c, 2.0) - Math.pow(b, 2.0)); else length = Math.sqrt(Math.pow(c, 2.0) - Math.pow(a, 2.0)); } System.out.println("length of unknown side = " + length); } else System.out.println("Sorry, you need to specify exactly 1 unknown value."); } public static void solveForUnknown(double a, double b, double c) throws Exception { int numberUnknowns = 0; double length; if (a == 0.0) numberUnknowns++; if (b == 0.0) numberUnknowns++; if (c == 0.0) numberUnknowns++; if (numberUnknowns == 1) { if (c == 0.0) // we are solving for hypotenuse c = sqrt(a^2 + b^2): { length = Math.sqrt(Math.pow(a, 2.0) + Math.pow(b, 2.0)); } else // we are solving for one of the legs, a = sqrt(c^2 – b^2) or b = sqrt(c^2 – a^2): { if (a == 0.0) length = Math.sqrt(Math.pow(c, 2.0) - Math.pow(b, 2.0)); else length = Math.sqrt(Math.pow(c, 2.0) - Math.pow(a, 2.0)); } System.out.println("length of unknown side = " + length); } else System.out.println("Sorry, you need to specify exactly 1 unknown value."); } no return type (subroutines do not return a value)

24 2-24 MicrosoftAP Computer Science with.NET and J# Summary Procedural programming is all about code –Writing subroutines and functions –Specifying their execution order Java provides decent support for procedural programming –though keywords can be daunting at first ( static, void, …) Visual Studio provides excellent support –IDE makes quick work of building applications, once you learn Java :-)

25 2-25 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/

26 2-26 MicrosoftAP Computer Science with.NET and J# That’s it! Next up: LectureTopic.. 3Object-oriented Programming in Java and J#......

27


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

Similar presentations


Ads by Google