1. Trang chủ
  2. » Giáo án - Bài giảng

Hello world extended concepts

12 148 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 12
Dung lượng 93,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Fundamentals o Slide 3“From scratch” projects scratch” – Compilers translate from high-level language to machine code – We use libraries for printing and in Java much more – We reuse exi

Trang 1

Fundamentals o Slide 1

Review: Why start with

WordGames?

• You wrote your first lines of code in this

course in WordGames.

• Why start with such a complex project?

• Answer:

– It is practical to do so, by using an

interface to connect your code to ours

– It makes the point that most software

engineers modify/extend existing

programs rather than creating their

own “from scratch”

See next slides for details

on these two points

Trang 2

UML class diagram for

WordGames

Capitalizer NameDropper xxx … xxx

<<interface>

StringTransformable -transform(String) : String

All our stuff

The

StringTransformable

interface is how our code knows how to

“connect” to your code

Trang 3

Fundamentals o Slide 3

“From scratch” projects

scratch”

– Compilers translate from high-level language to machine code

– We use libraries for printing and (in Java) much more

– We reuse existing code

During a 20 year career, a typical software engineer

might:

Work on about 20 projects

Be involved with the creation of only 1 or 2 projects from

scratch

For all the other projects, she modifies/extends existing projects

– So that you can see what one is like

– And so that you can see “under the hood” of some of the concepts

that you have been studying

Trang 4

HelloWorld concepts –

outline

• The next slide shows the entire HelloWorld

program

– It illustrates the following concepts:

• The main method

Static methods

Console projects

• How to use System.out.println to print a String to the

console

– As before, the slide omits comments to focus on

the code

– You experienced the above concepts in your own

HelloWorld, Part 1

Trang 5

Fundamentals o Slide 5

HelloWorld – the complete

program

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

Questions

on any of these

ideas?

Trang 6

HelloWorld extended concepts – outline

HelloWorld:

Static methods

Console projects

console

HelloWorld:

human)

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

Trang 7

Fundamentals o Slide 7

HelloWorld – the complete

program

public class HelloWorld {

public static void main (String[] args) {

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

}

}

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

To read from the console:

1 Declare and construct a new Scanner

object, sending it System.in

2 Use the Scanner object’s next method to

read and return a String

• Related methods to read int, double, etc.

Exercise:

What do you think is the name of the public field in System that is

an InputStream – a thing that

can read from

the console?

Answer: in

Exercise: Write the statements that would read a String from the console Ask questions!!!

The answer is on the next slide, but you maximize your learning by thinking and asking, not looking.

Trang 8

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

waits for the user to type and returns the result

Trang 9

Fundamentals o Slide 9

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: This is the same

slide as the previous slide Study this code now

ASK QUESTIONS!

Trang 10

“Helper” methods

Encapsulation: bundling things together

to make them easier to deal with

rest of your program

encapsulating in methods is valuable in general

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)

Trang 11

Fundamentals o Slide 11

import java.util.Scanner;

public class ScannerExample {

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

Trang 12

HelloWorld extended concepts – summary

HelloWorld:

– The main method

– How to use System.out.println to print a String to

the console

– How to read from the console (i.e., get input from

the human)

– How and why to use “helper” methods

Ngày đăng: 07/07/2017, 08:27