Download - SEEM3460 Tutorial

Transcript
Page 1: SEEM3460 Tutorial

SEEM3460 Tutorial

Java Programming in Unix

Page 2: SEEM3460 Tutorial

Code Translation

Java sourcecode

Javabytecode

Javacompiler

Bytecodeinterpreter

machine codefor targetmachine 1

Bytecodeinterpreter

machine codefor targetmachine 2

Page 3: SEEM3460 Tutorial

Compilation and Execution

Compilation: command > javac Gasmileage.java generates bytecode: Gasmileage.class

Execution: command > java Gasmileage load bytecode: Gasmileage.class call main of class Gasmileage

Page 4: SEEM3460 Tutorial

Structure of A Java Program

public class MyProgram

{

}

// comments about the class

public static void main (String[] args)

{

}

// comments about the method

method headermethod body

class header

class body

Page 5: SEEM3460 Tutorial

New Concepts in Java Code – create a class Holder with main Class – create a class Coin

Constructor – create method Coin Methods – create method Coin.flip Property – during flip, generate random Head=1;Tail=0

as Coin.value(import java.util.Random, nextInt/nextFloat)

Encapsulation – during flip, generate random orientation 0~360 (float) as private variable Coin.orientation

Casting – return integer value of Coin.orientation as read-only property

Page 6: SEEM3460 Tutorial

Flip a Coin

Holder

Coin

value and orientation


Top Related