fundamentals of software development 1slide 1 recap: “from scratch” projects today’s software...

8
Fundamentals of Softw are Development 1 Slide 1 Recap: “From scratch” Recap: “From scratch” projects projects Today’s software engineering is almost NEVER “from Today’s software engineering is almost NEVER “from scratch” scratch” Compilers translate from high-level language to machine code Compilers translate from high-level language to machine code We use libraries for printing and (in Java) much more We use libraries for printing and (in Java) much more We reuse existing code We reuse existing code During a 20 year career, a typical software engineer During a 20 year career, a typical software engineer might: might: Work on about 20 projects Work on about 20 projects Be involved with the creation of only 1 or 2 projects from Be involved with the creation of only 1 or 2 projects from scratch scratch For all the other projects, she modifies/extends existing projects For all the other projects, she modifies/extends existing projects You have done a “from scratch” project called You have done a “from scratch” project called HelloWorld HelloWorld So that you can see what one is like So that you can see what one is like And so that you can see “under the hood” of some of the And so that you can see “under the hood” of some of the concepts that you have been studying concepts that you have been studying

Upload: avice-mclaughlin

Post on 28-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Fundamentals of Software Development 1Slide 1 Recap: “From scratch” projects Today’s software engineering is almost NEVER “from scratch”Today’s software

Fundamentals of Software Development 1

Slide 1

Recap: “From scratch” Recap: “From scratch” projectsprojects

• Today’s software engineering is almost NEVER “from Today’s software engineering is almost NEVER “from scratch”scratch”– Compilers translate from high-level language to machine codeCompilers translate from high-level language to machine code– We use libraries for printing and (in Java) much moreWe use libraries for printing and (in Java) much more– We reuse existing codeWe reuse existing code

• During a 20 year career, a typical software engineer During a 20 year career, a typical software engineer might:might:– Work on about 20 projectsWork on about 20 projects– Be involved with the creation of only 1 or 2 projects from Be involved with the creation of only 1 or 2 projects from

scratchscratch• For all the other projects, she modifies/extends existing projectsFor all the other projects, she modifies/extends existing projects

• You have done a “from scratch” project called HelloWorldYou have done a “from scratch” project called HelloWorld– So that you can see what one is likeSo that you can see what one is like– And so that you can see “under the hood” of some of the concepts And so that you can see “under the hood” of some of the concepts

that you have been studyingthat you have been studying

Page 2: Fundamentals of Software Development 1Slide 1 Recap: “From scratch” projects Today’s software engineering is almost NEVER “from scratch”Today’s software

Fundamentals of Software Development 1

Slide 2

Hello World concepts – Hello World concepts – outlineoutline

• The next slide shows the entire The next slide shows the entire HelloWorldHelloWorld program/ program/It illustrates the following concepts:It illustrates the following concepts:– The The mainmain method method– StaticStatic methods methods– ConsoleConsole projects projects– How to use How to use System.out.printlnSystem.out.println to to printprint a a

String to the consoleString to the console• A subsequent slide will illustrate these A subsequent slide will illustrate these

additional concepts:additional concepts:– How to How to read from the consoleread from the console (i.e., get input (i.e., get input

from the human)from the human)– How and why to use How and why to use “helper” methods“helper” methods

Page 3: Fundamentals of Software Development 1Slide 1 Recap: “From scratch” projects Today’s software engineering is almost NEVER “from scratch”Today’s software

Fundamentals of Software Development 1

Slide 3

Hello World – the complete Hello World – the complete programprogram

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello world"); }}

What is special about the main method?

The main method is static. What does that mean?

Is System a class, method or field? How can you tell?

Is out a method or field? How can you tell?

Is println a method or field? What does println do?

Questions?

Page 4: Fundamentals of Software Development 1Slide 1 Recap: “From scratch” projects Today’s software engineering is almost NEVER “from scratch”Today’s software

Fundamentals of Software Development 1

Slide 4

Answers to questions on Answers to questions on previous slideprevious slide

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello world"); }}

main is the name of the special method at which any Java application begins

A static method is a method that “belongs” to the class instead of to each instance of the class. The static method cannot refer to the class’ non-static fields.

The special main method is, by definition, static.

System is a class that has “system” stuff

System has a public static

field called out that is a PrintStream – a thing that

can print to the console

main has command-line arguments sent as a String array

println is a PrintStream method that prints its argument on a line

Instructor: run this zipped HelloWorld project

Page 5: Fundamentals of Software Development 1Slide 1 Recap: “From scratch” projects Today’s software engineering is almost NEVER “from scratch”Today’s software

Fundamentals of Software Development 1

Slide 5

HelloWorld extended HelloWorld extended concepts – outlineconcepts – outline

• We just saw the following concepts from We just saw the following concepts from HelloWorld:HelloWorld:– The The mainmain method method– StaticStatic methods methods– ConsoleConsole projects projects– How to use How to use System.out.printlnSystem.out.println to to printprint a String to a String to

the consolethe console

• Now we turn to these additional concepts from Now we turn to these additional concepts from HelloWorld:HelloWorld:– How to How to read from the consoleread from the console (i.e., get input from (i.e., get input from

the human)the human)– How and why to use How and why to use “helper” methods“helper” methods

• We continue to omit comments in order to focus on the codeWe continue to omit comments in order to focus on the code

Page 6: Fundamentals of Software Development 1Slide 1 Recap: “From scratch” projects Today’s software engineering is almost NEVER “from scratch”Today’s software

Fundamentals of Software Development 1

Slide 6

import java.util.Scanner;

public class ScannerExample {

public static void main(String[] args) {

Scanner scanner;

String input;

scanner = new Scanner(System.in);

System.out.print(“Enter a String: ”);

input = scanner.next();

System.out.println(input.substring(3)); }}

To read from the console:When you use a library class (like

Scanner), you usually need to import the class to tell Java where to find it.

Declare a Scanner local variable

Construct the Scanner, sending it System.in

Prompt the human to type something next()

waits for the user to type and returns the result.

We could have done anything we want with the input – printing a substring of it is just an example.

Page 7: Fundamentals of Software Development 1Slide 1 Recap: “From scratch” projects Today’s software engineering is almost NEVER “from scratch”Today’s software

Fundamentals of Software Development 1

Slide 7

““Helper” methodsHelper” methods• Encapsulation:Encapsulation: bundling things together bundling things together

to make them easier to deal withto make them easier to deal with• Encapsulation in methods:Encapsulation in methods:

– Bundle a chunk of codeBundle a chunk of code– Give it a nameGive it a name– Perhaps give it Perhaps give it parametersparameters

• ““Helper” methodsHelper” methods– Private methods that you create to help the Private methods that you create to help the

rest of your programrest of your program– Valuable for the same reasons that Valuable for the same reasons that

encapsulating in methods is valuable in encapsulating in methods is valuable in generalgeneral

Allows a chunk of code to be referenced via a well-chosen name

Allows the code to be reused in a more general way (by sending different values as arguments)

Page 8: Fundamentals of Software Development 1Slide 1 Recap: “From scratch” projects Today’s software engineering is almost NEVER “from scratch”Today’s software

Fundamentals of Software Development 1

Slide 8

import java.util.Scanner;public class ScannerExample {

public static void main(String[] args) {

ScannerExample.cubeRoot(12.0); for (int k = 100; k < 110; k = k + 1) { ScannerExample.cubeRoot((double) k); }

ScannerExample.cubeRoot(); ScannerExample.cubeRoot(); }

private static void cubeRoot(double number) {

System.out.println(“Cube root of ” + number

+ “ is ” + Math.pow(number, 1.0 / 3.0));

}

private static void cubeRoot() {

Scanner scanner = new Scanner(System.in);

System.out.print(“Enter a number: ”);

ScannerExample.cubeRoot(scanner.nextDouble());

}

}

This slide illustrates:

“helper” methods

“for” loops

Reading numbers from the console

Methods that call other methods

Study this code.

ASK QUESTIONS!

You will apply these ideas in HelloWorld, Part 2 (homework).

The boxes are there to help your eyes – not part of the code

If you wish, download and unzip ScannerExample