• Tidak ada hasil yang ditemukan

CompSci 230 Software Construction

N/A
N/A
Protected

Academic year: 2023

Membagikan "CompSci 230 Software Construction"

Copied!
5
0
0

Teks penuh

(1)

CompSci 230 Software Construction

Lecture Slides #2: Hello World! S1 2016

Agenda

COMPSCI 230: OOD 2

Topics:

Java Basics

Getting started – our first .javafile

“Hello world!” in Java and Python

Backward and forward compatibility

Syntax and semantics

Java Basics –source code

COMPSCI 230: OOD 3

In Java, each program is a class.

In source code, each class sits in a test file that has the same name as the class and the extension .java

E.g., if our class is called HelloWorld, we’ll put it into a file called HelloWorld.java

We can edit the .javafile with any text editor

In 230, we will be using an integrated development environment (Eclipse) to do this

Java Basics - compilation

COMPSCI 230: OOD 4

We compile the class with the Java compiler javac:

On the command line: javac HelloWorld.java

Compilation results in a bytecode file with the extension .class (if we don’t have any syntax errors in the source code)

E.g., HelloWorld.class

Note: In Eclipse, we can invoke javacdifferently – more on this shortly

(2)

Java Basics – running a program

COMPSCI 230: OOD 5

Once we have the .classfile, we can run the program using the Java VM, e.g.:

java HelloWorld

Note: We omit the .class– the Java VM knows what to look for

In Eclipse, we just hit the “Play” button (green circle with white triangle)

This takes care of compilation AND running of the program in the VM

That’s provided we’re building our program as part of an Eclipse project, in which case we get a little extra complexity thrown in.

Java Basics – application structure

COMPSCI 230: OOD 6

A program may use additional classes (we’ll get to that later), which also sit in their own files (except in the case of nested classes, which we’ll discuss much later)

The class that “starts” the program must have a class method (also called static method) which returns nothing (void).

OK, and now it’s time to look at our HelloWorld.java

“Hello World!” in Java

COMPSCI 230: OOD 7

HelloWorld.java(Java source code):

public class HelloWorld {

public static void main(String[] args) { System.out.println("Hello World!");

} }

What does all this mean?

“Hello World!” in Java

COMPSCI 230: OOD 8

HelloWorld.java(Java source code):

public class HelloWorld {

public static void main(String[] args) { System.out.println("Hello World!");

} }

publicis our first nod to “development in the large”

It’s a so-called access modifier. publicmeans that code from outside the application can see this class (and use it)

This allows the developers of other classes to use the HelloWorld class in their code (if they want to)

It also allows our program to compile (in this case)

(3)

“Hello World!” in Java

COMPSCI 230: OOD 9

HelloWorld.java(Java source code):

public class HelloWorld {

public static void main(String[] args) { System.out.println("Hello World!");

} }

class HelloWorldmeans that what follows is a class declaration of a class named “HelloWorld”

In that respect, Java’s no different from Python

“Hello World!” in Java

COMPSCI 230: OOD 10

HelloWorld.java(Java source code):

public class HelloWorld {

public static void main(String[] args) { System.out.println("Hello World!");

} }

Curly braces in Java have the same purpose as whitespace indentation in Python.

An opening curly brace marks the beginning of a block of code

A closing curly brace marks the end of a block of code

Curly braces must match, i.e., for every opening brace, there must be a closing brace

The concept is very similar to parentheses in mathematical formulas

Here, the curly braces marked by the boxes indicate the block that contains the class declaration

Note: javacignores whitespace indentation completely – we use it only to make the code easier to read for humans!

“Hello World!” in Java

COMPSCI 230: OOD 11

HelloWorld.java(Java source code):

public class HelloWorld {

public static void main(String[] args) { System.out.println("Hello World!");

} }

This is the declaration of our main()method

It’s also public, meaning code from outside the class can invoke the method

The staticmeans that we can invoke the method without having to build an objectfrom the blueprint that the HelloWorld class represents

The voidmeans that the method returns nothing

Note there’s no defas you have it in Python

“Hello World!” in Java

COMPSCI 230: OOD 12

HelloWorld.java(Java source code):

public class HelloWorld {

public static void main(String[] args) { System.out.println("Hello World!");

} }

In the declaration of our main()method

The method signature is very similar to a Python method (method name followed by parentheses with a parameter list)

New here: the String[]in front of the parameter args

Remember that all variables (and function return values) in Java need a type(unlike in Python, where the Python interpreter figures this out at runtime)

Here, the type of the parameter argsis an array (“[]”) whose elements are of type String

This parameter contains an array of arguments passed in from the command line (if any)

(4)

“Hello World!” in Java

COMPSCI 230: OOD 13

HelloWorld.java(Java source code):

public class HelloWorld {

public static void main(String[] args) { System.out.println("Hello World!");

} }

In the declaration of our main()method

Note again the curly braces that enclose the block with the code of the method

“Hello World!” in Java

COMPSCI 230: OOD 14

HelloWorld.java(Java source code):

public class HelloWorld {

public static void main(String[] args) { System.out.println("Hello World!");

} }

In the declaration of our main()method

Guess what this does!

See http://stackoverflow.com/questions/3406703/whats-the-meaning-of- system-out-println-in-javafor the gory details

In Eclipse

COMPSCI 230: OOD 15

File -> New -> Java Project

Enter HelloWorld as the project name and save

You’ll see HelloWorld pop up in the Package Explorer on the left

Add a class (File -> New -> Class) named HelloWorld and Eclipse will create almost everything else for you.

All you need to do is add the line with the code:

System.out.println("Hello World!");

In Eclipse

COMPSCI 230: OOD 16

(5)

Review questions

COMPSCI 230: OOD 17

Which form does a program take in Java?

What are the essential elements of any Java program?

How do we store the source code for classes in Java?

How do we name Java class files?

What does the modifier “public” do?

What is the purpose of curly braces in Java?

Can you give a few examples of types in Java?

What does the “static” mean in front of a method declaration?

How do we compile a Java program, and how do we run it?

What is a key difference in the way we specify method parameters in Java and in Python?

Referensi

Dokumen terkait

public class Test{ public static void mainString args[]{ //tambahkan nasabah pada bank dengan nama Andi Sucipto dengan konstruktor tambahNasabahString,String, besar tabungan

import javax.swing.*; import java.awt.event.*; public class SimpleGui1B implements ActionListener { JButton button; public static void mainString[] args { SimpleGui1B = new SimpleGui1B;

import java.io.*; import java.util.*; public class LineNumberer { public static void mainString[] args throws FileNotFoundException { Scanner console = new ScannerSystem.in;

Contoh Penggunaan Object 2 public class TesterPenggunaanObjek { public static void mainString[] args { Objek x=new Objek; Integer y=10; x.setDatay;

Listing 13-7: Make the Calling Method Handle the Exception package com.allmycode.naptime; class GoodNightsSleepB { public static void mainString args[] { System.out.println“Excuse

Statement WHILE Contoh : 10 import java.util.Scanner; public class PenghitungBeratBadan{ public static void mainString args[]{ Scanner data= new ScannerSystem.in; int urutan;

import java.util.Scanner; public class FinalExamA{ public static void mainString []args{ Scanner s=new ScannerSystem.in; System.out.println"Choose one of the

import java.util.Scanner; public class FinalExamB{ public static void mainString []args{ Scanner s=new ScannerSystem.in; System.out.println"Choose one of the