Programming se7
This presentation is the property of its rightful owner.
Sponsored Links
1 / 279

Programming (SE7) PowerPoint PPT Presentation


  • 120 Views
  • Uploaded on
  • Presentation posted in: General

Programming (SE7). 環境工程系 丁文輝 [email protected] 環境工程系電腦課程 ( 規画案 ). 計算機概論 (BCC) 計算機程式 (Programming) 計算機圖學 (CG) 資料庫管理系統 (DBMS) 數値分析 (NA) 地理資訊系統 (GIS) 環工電腦應用. 逢甲大學水利工程學系工學士 中央大學 ( 教育部委訓 ) 大專資訊師資班 成功大學環境工程學系工學碩士 成功大學環境工程學系博士候選人. 自己紹介 - 澄原日先 ( すみはら ひさき ). 台中市坤財營造廠工地主任

Download Presentation

Programming (SE7)

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Programming (SE7)

[email protected]


()

  • (BCC)

  • (Programming)

  • (CG)

  • (DBMS)

  • (NA)

  • (GIS)

Java programming


()

-()

Java programming


FORTRAN, BASIC, PASCAL, JAVA

8086/8088, 80286

PE, dBASE+, Lotus 123, Office, ACAD, SAS/SPSS, Sigma Plot, GIS

,

()

Java programming


  • JAVA2 (JDK7)

    () (2012)

  • Java SE7

    (2011)

  • Java

    (), (2006)

  • JAVA SE6

    (2009)

Java programming


Java

  • (),

    • http://kgclass.jp/text/java

  • (JAVA Note),

    • http://irw.ncut.edu.tw/peterju/java.html

  • (),

    • http://elearning.ksu.edu.tw

Java programming


  • 4

Java programming


  • 40%)20%)40%)

  • (101_2)java1002626

(A 20%, B 31%)

Java programming


  • 10:00-11:30

  • 10:00-11:30

  • 10:00-11:30

    • 34F(D3402)

      • tingmail.ksu.edu.tw

Java programming


Introduction

Datatype

Input/Output

Assignment

Selection

Iteration

Array

Method

(Java SE7, 2013)

Java programming


Console

Assignment

Expression

Divisor

If / If-else

Switch-case

For loop

While loop

Random

Bubble sort

Method

Recursive

Java programming


a=b+c;

a*ba>b

for,if

Java programming


Delphi

C++Builder

FORTRAN

PL/1

COBOL

ALGOL

ALGOL68

PASCAL

Ada

Object PASCAL

BASIC

C

C++

Lisp

Java

Prolog

Smalltalk

Java programming


FORTRAN

BASIC

Pascal

Java programming

Java (2011)


Java programming


Java programming


Java programming


Java programming


(1)

  • SmallTalk

  • C++

    • C

Java programming


(2)

  • Java

    • C

    • Unix, Windows(Write Once, Run Anywhere)

Java programming


(3)

  • Ruby

  • Visual Basic, Visual C++

    • Microsoft

  • C#

    • MicrosoftJava like

Java programming


  • (object)

    • (property)

    • (method)

  • (class)

    • (variable)

    • (instance)

Java programming


(1)

  • (encapsulation)

    • () ()

  • (inheritance)

    • (Parent Class)

Java programming


(2)

  • (polymophism)

      • (Overloading):

      • (Overriding):

Java programming


Java version history

JDK 1.0 (January 23, 1996)

JDK 1.1 (February 19, 1997)

J2SE 1.2 (December 8, 1998)

J2SE 1.3 (May 8, 2000)

J2SE 1.4 (February 6, 2002)

J2SE 5.0 (September 30, 2004)

Java SE 6 (December 11, 2006)

Java SE 7 (July 28, 2011)

Java programming


Java

  • Java(1.2) JDK(Java Deve-lopment Kit) Java 2 SDK(Software Development Kit)

    • J2EE(Enterprise Edition)

    • J2SE(Standard Edition)

    • J2ME(Micro Edition)

Java programming


Java Platforms

Java Card for smartcards.

Java Platform, Micro Edition (Java ME).

Java Platform, Standard Edition (Java SE).

Java Platform, Enterprise Edition (Java EE).

Java programming


Java Platform Edition

Java programming


JAVA

Java programming


Development Environments

  • Sun Java Development Kit (JDK)

  • Sun NetBeans

  • IBM Eclipse, GNU Eclipse

  • Borland JBuilder

  • MetroWerks CodeWarrior

  • BlueJ

  • jGRASP

Java programming


Interpretation vs. Compilation

C++ Source (*.c)

Java Source (*.java)

C++ Compiler

Java Compiler

Byte-code (*.class)

Java Virtual Machine

Client Platform (hardware/operating system)

Client Platform (hardware/operating system)

Java programming


JDK(Java Development Kit)

Java programming


JDK(Java Development Kit)

Java programming


JDK(Java Development Kit)

  • javac(.exe)

    • Java, .java.class

  • java(.exe)

    • JVM, Java ApplicationBytecode(.class)

Java programming


Java Virtual Machine

JavaVM

JavaVM

javac

JavaVM

Java programming


Java Virtual Machine

Java programming


Java Virtual Machine

Java programming


Garbage Collection (1)

  • Garbage Collection

  • []:

    int[ ] a = new int[100];

    int[ ] b = new int[200];

    a = b;

Java programming


Garbage Collection (2)

  • nullGarbage Collection

  • []:

    int[ ] c = new int[300];

    c = null;

Java programming


Garbage Collection (3)

  • Garbage Collection ;

Java programming


IDE (Integrated Development Environment)

  • NetBeans (Sun)

  • JBuilder (Borland)

  • Eclipse (IBM)

Java programming


Java

Windows

Mac

Linux

JVMWindows

JVMMac

JVM Linux

Java

Java programming


Java (1)

(Simple)C/C++

(Object-oriented)OOPPIE()

Java programming


Java (2)

(Network-Savvy)API

(Interpreted)bytecodeJVMJIT

(Robust)

Java programming


Java (3)

(Secure) (Garbage Collection)JVM (Exception)

(Architecture Neutral)

Java programming


Java (4)

(Portable) Write once, run any-whereJVM

(High Performance)HotSpot, JIT

(Multithreaded)Java

Java programming


Java (5)

  • (Dynamic)Java

  • (Unicode)

Java programming


(Data type)

  • (primitive data type)

  • (structured data type)

  • (abstract data type)

Java programming


Java(1)

JavaC/C++

JavacharUnicode, ,

Java

1.2.

Java programming


Java(2)

  • (wrapper class) java.lang

  • (byte, short, int, long)10, 816

  • (float, double)double

Java programming


Java(3)

boolean()

(operators)+= , -=, *= , /=()

Java programming


data type

Java programming


(Data type)

  • (primitive type)

    • (byte, short, int, long)

    • (float, double)

    • (boolean)

    • (char)

  • (non-primitive type)

Java programming


primitive types

Java programming


(primitive type)

Java programming


Java programming


Java (1)

  • boolean :

    • truefalse

  • byte:

    • -128127

  • short

    • -3276832767

  • int

    • 32

      -21474836482147483647

Java programming


Java (2)

  • long

    • -92233720368547758089223372036854775807

  • floatIEEE754

  • doubleIEEE754

  • char

    • 16UCS-2ASCII

Java programming


Java (3)

  • ; 32 bits (-231 ~ 231-1)int, long

  • double

Java programming


Default Values

Java programming


Javafinal

Java programming


Primitive Data Types

Java programming


Primitive Data Type Wrapper class

Java programming


  • long, Ll

  • float, Ff

  • , :

    • ()

Java programming


String (1)

String.valueOf(boolean b)

String.valueOf(char c)

String.valueOf(char[] data)

String.valueOf(char[] data, int offset, int count)

Java programming


String (2)

String.valueOf(double d)

String.valueOf(float f)

String.valueOf(int i)

String.valueOf(long l)

String.valueOf(Object obj)

Java programming


String

Byte.parseByte(String s)

Double.parseDouble(String s)

Float.parseFloat(String s)

Integer.parseInt(String s)

Long.parseLong(String s)

Java programming


  • //

  • a = b + c; /* */

  • /*

    *

    */

  • /**

    * javadoc

    */

Java programming


commenting

// This is an example of a single line comment using two slashes

/* This is an example of a multiple line comment using the slash and asterisk. This type of comment can be used to hold a lot of information or deactivate code but it is very important to remember to close the comment. */

/**

* This is an example of a Javadoc comment;

* Javadoc can compile documentation from this text.

*/

Java programming


Java

Java programming


java.lang.Math

Java programming


Math.();

class MathExample {

public static void main(String[] args) {

System.out.println("-5" + Math.abs(-5));

System.out.println("3.0" + Math.sqrt(3.0));

System.out.println

("2" +2*2*Math.PI);

System.out.println

("sin60" + Math.sin(60.0*Math.PI / 180.0));

}

}

Java programming


I/O(byte)

Java programming


I/O(char)

Java programming


BufferedReader

import java.io.*;

public class {

public static void main(String args[]) throws IOException

{

BufferedReader buf;

String str1;

buf= new

BufferedReader(new InputStreamReader(System.in));

str1=buf.readLine();

............

}

}

Java programming


(1)

import java.io.*;

class Sample1 {

public static void main (String args[])throws IOException

{

System.out.println();

BufferedReader br =

new BufferedReader(new InputStreamReader(System. in));

String str = br.readLine();

System.out.println( + str);

}

}

Java programming


(2)

import java.io.*;

class Sample2 {

public static void main(String args[]) throws IOException

{

System.out.println();

BufferedReader br =

new BuffereReader(new InputStreamReader(System.in));

String str = br.readLine();

int num = Integer.parseInt(str);

System.out.println( = + num);

}

}

Java programming


Console

import java.io.Console;

public class {

public static void main(String args[])

{

Console console=System.console();

String str1;

str1=console.readLine()

int i = Interger.parseInt(str1);

............

}

}

Java programming


java.util.Scanner

  • Scanner

    • BufferedReader

    • Integer.parselnt()

Java programming


Scanner

  • import java.util.Scanner;

    • Scanner sc = new Scanner(System.in);

    • Scanner sc =

      new Scanner(new FileStream("filename.txt"));

Java programming


Scanner Methods

Java programming


Numeric and String Methods

Java programming


Boolean Methods

Java programming


Scanner(1)

import java.util.Scanner;

public class {

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

System.out.println();

int i = sc.nextInt();

}

}

Java programming


Scanner(2)

import java.util.Scanner;

public class Input {

public static void main(String[] args) {

Scanner cin = new Scanner(System.in);

//

float a = cin.nextFloat(), b = cin.nextFloat();

//

System.out.println(a + b); //a+b

String c = cin.next(); //

System.out.println(c); //c

}

}

Java programming


(Escape sequence character)

Java programming


java.util.Formatter

Java programming


format

%[argument_index$] [flags] [width] [.precision] conversion

Java programming


Common Formatting Codes

Java programming


Conversion (1)

Java programming


Conversion (2)

Java programming


Java programming


flag

Java programming


printf (1)

  • Example 1:

    • System.out.printf(%5.3f, Math.PI)

      53

      3.142

      ()

Java programming


printf (2)

  • Example 2: argument_index

    • System.out.printf(%s and %s, You, Me)

      You and Me

    • System.out.printf(%2$s and %2$s, You, Me)

      Me and Me

Java programming


Console

Assignment

Expression

Divisor

If / If-else

Switch-case

For loop

While loop

Random

Bubble sort

Method

Recursive

Java programming


Java Virtual Machine

JavaVM

JavaVM

javac

JavaVM

Java programming


Java programming


java

Eclipse, JCreator LE

Jbuilder, IntelliJ IDEA, Visual Cafe, Workshop, Forte for Java

Crimson, ConText, NotePad

UltraEdit, EditPlus

Java programming


JDK(Java Development Kit)

  • javac(.exe)

    • Java, .java.class

  • java(.exe)

    • JVM, Java ApplicationBytecode(.class)

Java programming


Java programming


Java

1)

  • ()

  • .javaTest.java

    2)

  • javac javac Test.java

    3)

  • java Testjava Test

Java programming


Java10 (1)By David Reilly

  • 10Accessing non-static member variables from static methods (such as main)

  • 9 Mistyping the name of a method when overriding

  • 8 Comparison assignment ( = rather than == )

  • 7 Comparing two objects ( == instead of .equals)

Java programming


Java10 (2)By David Reilly

  • 6 Confusion over passing by value, and passing by reference

  • 5 Writing blank exception handlers

  • 4 Forgetting that Java is zero-indexed

  • 3 Preventing concurrent access to shared variables by threads

  • 2 Capitalization errors

  • 1 Null pointers

Java programming


  • MS Word (filename = A4010N099_09),

  • , , mail

    ([email protected])

(, , _, )

Java programming


Java programming


Java

  • (Application)

    • Java

  • (X-let)

    • (Applet)Java appletHTML

    • (Servlet)

Java programming


Java Application

Java programming


Application()

/* package */package exam.test;import java.awt.Button; import java.util.*;

public class HelloWorld {public static void main( String[] args ) {System.out.println( Hello World ); tmp obj = new tmp(); obj.i = 6; System.out.println( obj.i );}}class tmp{int i = 5; }

Java programming


Java

Class Name

Variable NameMethod Name

,2

4

,javadoc

Java programming


Java(1)

80

if

()

Java programming


Java(2)

class

method

("_")

Java programming


Java(3)

    • Code Conventions for the Java Program-ming Language

      http://www.oracle.com/technetwork/java/

      codeconvtoc-136057.html

Java programming


System.out.println() /.print()

  • System.out.println()

    • \r\nConsole()

  • System.out.print()

    • Console()

  • (+)

Java programming


System.out.printf()

Java programming


System.out.printf() -()

Java programming


java

  • D:\>md myjava

  • D:\>cd myjava

  • D:\myjava>

  • ,filename: Ex01.javaD:\myjava

  • D:\myjava>javac Ex01.java //

  • D:\myjava>java Ex01 //

Java programming


Hello.java

public class Hello

{

public static void main(String args[ ])

{

System.out.println("Hello");

}

}

Java programming


/*

1: Hello, java!

: Ex01.java

A, 4010N901,

*/

package myjava.ex01;

import java.lang.*;

import java.io.Console;

Ex01: java

Java programming


()

public class Ex01

{

public static void main (String args[])

{

System.out.println(Hello, java!);

}

}

Java programming


K&R(Kernighan & Ritchie)

if () {

for ( ; ; ) {

while () {

}

}

}

Java programming


java(1)

/*......*///..............., javadoc /**........*/

.java

class, public

Java programming


java(2)

  • class, .class

  • Java

Java programming


Java (1)

Unicode(Keywords)

Class Name Variable Name Method Name

Java programming


Java (2)

  • ,(camelcase) (Hungarian Notation)

Java programming


Java Programming Language Keywords

Java programming


class

class

class , public

Java programming


package(1)

  • package

  • package

  • JavaclassespackagesJDK src.zip

    • ()java.awt.Button.javapackage package java.awt

Java programming


package(2)

  • packageclass package packageclass

    • () java.io.InputStreamjava.ioInputStream

  • classpath + package =

Java programming


import(1)

Java import C include java import VM

java.io FileReader import java.io.FileReader

Java programming


import(2)

  • java.io , import java.io.*

  • import , import; import java.io.File File,FileReader

Java programming


import(3)

  • java.lang import java.lang;

Java programming


import(4)

  • javaAPIclasspackage java.util java.sql Date class, import package import class

Java programming


java(1)

java.lang

java.io

java.util

javax.swing

Java programming


java(2)

  • java.awt

    • JDK1.1GUI

  • java.awt.event

    • GUI

  • java.awt.image

    • GUIImage

Java programming


class, interface, enum

class

class , public

Java programming


(1)

  • imoport java.io.console;

  • Console = System.console();

  • String str = .readLine();

[]: String()

Java programming


(2)

  • imoport java.util.Scanner;

  • Scanner = new Scanner (System.in);

  • String str = .next();

[]:

Java programming


Scanner

Java programming


(3)

  • import java.io.*

  • main(String args[]) throws IOException

  • BufferedReader = new BufferedReader (new InputStreamReader (System.in));

  • String str = .readLine();

[]: String()

Java programming


Java programming


1:

  • Hello, java SE7 !

Java programming


1:

Java programming


(assignment)

  • = ;

Java programming


  • 1[=1], 2[=2];

Java programming


(Arithmetic)

Java programming


(/)

  • doubledouble

  • doubleintdouble

  • intint

Java programming


(Logic)

Java programming


(Bit)

Java programming


Java programming


Operator Precedence (1/2)

Java programming


Operator Precedence (2/2)

Java programming


(Operator)

Java programming


Java printf( ) method

System.out.printf(format-string [, arg1, arg2, ]);

Java programming


Format String

  • % [flags] [width] [.precision] conversion-character

Java programming


Flags

  • - : left-justify ( default is to right-justify )

  • + : output a plus ( + ) or minus ( - ) sign for a numerical value

  • 0 : forces numerical values to be zero-padded ( default is blank padding )

  • , : comma grouping separator (for numbers > 1000)

  • : space will display a minus sign if the number is negative or a space if it is positive

Java programming


Conversion-Characters

  • d : decimal integer [byte, short, int, long]

  • f : floating-point number [float, double]

  • c : character. Capital C will uppercase the letter

  • s : String. Capital S will uppercase all the letters in the string

  • h : hashcode. A hashcode is like an address. This is useful for printing a reference

  • n : newline. Platform specific newline character- use %n instead of \n for greater compatibility

Java programming


printf format specifiers

Java programming


special printf characters

Java programming


2: (1)

  • Celsiustemperature ()

  • Fahrenheit temperature ()

  • = + 32

Java programming


2: (1)

Java programming


()

http://www2.health.ne.jp/theme/index.html

Java programming


()

http://www2.health.ne.jp/theme/index.html

BMI25

B:

A:

BMI25

85cm

90cm

Pickwick

CT

100cm2

A:

B:

Java programming


http://www2.health.ne.jp/theme/index.html

Java programming


(18)

19992 ""

Java programming


http://www2.health.ne.jp/theme/index.html

Java programming


http://www2.health.ne.jp/theme/index.html

    • 11

    • 31

(jogging)

(training)

Java programming


Java programming

98.5.14. A8


Java programming

98.5.14. A8


Java programming


    • BMI 18.5-249080

Java programming


    • 2drinks/day,15360120~15030~401

    • 14090 mmHg

Java programming


BMIkgm2 ; kg22m2

http://www.uemura-clinic.com/dmlecture/bmi.htm

Java programming


Java programming

(2000)


3: (2)

  • BMI [()] [(m)]2

  • () 22m2

  • BMIBody Mass Index

Java programming


3: (2)

Java programming


4:

  • 1 = 12

  • 1 = 2.54

Java programming


Math

Java programming


Math

Java programming


java.lang.Math round(x)

Java programming


4:

Java programming


(1)

    • if (expression) statement;

    • if (expression) statement_true; else statement_false;

Java programming


switch (expression) { case value_1: [statement_1;] [break;]

case value_2: [statement_2;] [break;]

...

default: statement;

[break;]

}

(2)

Java programming


  • if () ;

  • if ()

    ;

  • if () {

    ;

    }

Java programming


if ()

A;

else

B;

if () {

A;

}

else {

B;

}

Java programming


  • = () ? Y : N;

  • []

    • int n1 = -20, n2 = 5;

      String compare;

      compare = (n1 >= n2) ? : ;

Java programming


if () {

if (1) {

A;

}

else {

B;

}

}

else {

if (2) {

C;

}

else {

D;

}

}

Java programming


5: if else

  • If

    • If...

    • If...Else ...

    • (34.0)

      + [-]

    • ()

Java programming


(1). ( - )

(2).

Java programming


[]61930

Java programming


5: if else

Java programming


6: switch

  • Select Case (VB6 )

    • Case is <= 5

    • Case 6, 7, 8

    • Case 9 To 14

    • Case is >= 15

    • Case Else

Java programming


switch () {

case 1:

1;

break;

case 2:

2;

break;

:

case n:

n;

break;

default:

default;

[break;]

}

: char, byte, short, intenum

:

Java programming


6: switch

  • ()(100)

    • 0 - 4915%

    • 50 - 7910%

    • 80 - 1005%

Java programming


6: switch

Java programming


(1)

: for

for( init_Statement ; condition ; incre_statement ) statement

Java programming


(2)

  • : while

    • while( expression ) statement

Java programming


(3)

  • : do while

    • do statement while( expression ) ;

Java programming


(1)

  • label

    • breakcontinue,(multi-level loop)

  • break

    • for, while, do while,switch

Java programming


(2)

  • continue

    • for, while, do while,

  • return

    • , ,

Java programming


(for loop)

  • for (; ; ) ;

  • for (; ; ) {

    ;

    }

Java programming


<>

while () {

;

}

<>

do {

;

} while ();

(while loop)

Java programming


  • sum = 1 + 2 + 3 + + 100

  • sum = 2 + 4 + 6 + + 100

  • sum = 1 + 1/2 + 1/3 + + 1/20

  • sum = 1 - 1/2 + 1/3 1/20

Java programming


    • 1400

Java programming


    • tan 1 /4 tan-1(1) /4

    • tan-1(1) tan-1(1) = 1 1/3 + 1/5 - 1/7 + 1/9 +/- 4

Java programming


7: for

Java programming


8: while

  • 1,0003

Java programming


8: while

Java programming


8-1: while

Java programming


Randomize Statement and the Rnd Function

  • The Randomize statement initializes Visual Basics random-number generator.

  • The Rnd function generates random decimal numbers within the 0 to 1 range, including 0 but not including 1.

Java programming


Rnd Function

  • To generate random decimal numbers in a range other than 0 to 1:

    (upperbound - lowerbound + 1) * Rnd + lowerbound

  • To generate random integers:

    Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

Java programming


(VB6)

  • Rnd

    • Int(( - + 1) * Rnd + )

  • Randomize Timer

Java programming


- Math.random()

  • import java.lang.Math;

  • Math.random()

    • 0.0 - 1.0()(double)

  • :

    • (int) (Math.random() * ( - + 1) + )

Java programming


Math

random110

public class Example {

public static void main(String[] args) {

int randomNumber = (int) (Math.random() * 10 + 1);

System.out.println(randomNumber);

}

}

Java programming


()

  • Random

  • import java.util.Random;

    Random r = new Random();

    r.nextInt(n)//0 - (n-1)

    r.nextDouble()

    //0.0 1.0()

Java programming


  • private int randomInRange(int min, int max)

    {

    return min + random.nextInt(max min + 1);

    }

  • int lotto;

    lotto = randomInRange(1, 49);

Java programming


= 4

Java programming


= ((/)) / 2

= /

Java programming


= (11/4) / (11)

= /

if (x*x + y*y <= 1)

hit = hit + 1;

y

1

*

(x,y)

x

0

1

Java programming


Monte Carlo method

Monte Carlo method applied to approximating the value of .

After placing 30000 random points, the estimate for is within 0.07% of the actual value.

Java programming


9:

Java programming


(Array)

Java programming


(Array)

  • (class)new

  • 2

    • [ ];

      • : int[] arr1, int[ ][ ][ ] arr3;

    • [ ];

      • : int arr1[ ];

Java programming


Declaring a Reference to Refer to an Array

  • type (reference)name

    type[ ] name;

  • []

    • int[ ] intArray;

    • float[ ] floatArray;

Java programming


Creating an Array

  • Java new

  • new elementType[arrayLength];

  • []

    • int[ ] intArr;

    • intArr = new int[100];

    • double[ ] doubleArr = new double[200];

Java programming


Array Initializers

  • { }

    • int[ ] anArray = {0,10,20,30,40};

    • //Create and initialize an array called anArray

    • //The length of anArray is 5

Java programming


Accessing an Array Element

  • [ ](index)

  • []

    • intArray[3] = 10;

    • float val = floatArray[3];

  • arrayReference.length

Java programming


Sorting/Searching an Array

  • bubble sort

  • binary search on a sorted array

Java programming


Multiple-Subscripted Arrays

  • Java xy

    • int[ ][ ] a;

    • a = new int[x][y];

  • Array Initializers

    • int[ ][ ] b = { { 1, 2}, { 3, 4, 5}};

Java programming


  • 20021, (), 142, 6;

  • 20041, , 149, 6(6/49);

Java programming


Java programming


(6/49)

  • 01 - 496

Java programming


Java programming


Java programming

, EB631, p.6-36


(Java2)

class BBKMath {

void GenRnd( int[] x, int min, int max, int rnd_no) {

int tot_no, rem_no, t_no;

int i, j;

tot_no = max - min + 1;

int[] t = new int[tot_no];

for(i = 0; i < tot_no; i++) {

t[i] = min + i;

}

rem_no = tot_no;

for( i = 0; i < rnd_no; i++) {

t_no = (int)Math.floor(Math.random() * rem_no);

x[i] = t[t_no];

for(j = t_no; j < (rem_no - 1); j++) {

t[j] = t[j+1];

}

rem_no--;

}

}

}

Java programming

, EB631, p.6-37


(Java2)

for(int j=1;j<=i;j++) { //

int[] lotto= new int[49]; //

for (int k=0;k<49;k++) // 149

lotto[k]=k+1;

int count=0; //

do {

int guess = (int)(Math.random()*49);

if(lotto[guess]==0) // 0,

continue; // , ,

else {

System.out.print(lotto[guess]+\t");

lotto[guess]=0; // 0,

count++;

}

} while (count<6); // 6

}

Java programming

, FS720, p.17-32~33


(Java2)

import java.util.*;

public class ListLotto {

public static void main(String args[]) {

System.out.println("Java/ArrayList ");

ArrayList num = new ArrayList();

for (int i=1;i<50;i++)

num.add(new Integer(i)); //

Collections.shuffle(num); //

System.out.println(num.subList(1,7));

} // 6

}

Java programming

, FS720, p.17-60~61


(VB2008)

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _

System.EventArgs) Handles Button1.Click

Dim guess, A(5) As Integer

Randomize()

For index As Integer = 0 To 5

While A(index) = 0

guess = CInt(Int(Rnd() * 59) + 1)

If Array.IndexOf(A, guess) = -1 Then

A(index) = guess

End If

End While

Next

TextBox1.Text = A(0)

TextBox2.Text = A(1)

TextBox3.Text = A(2)

TextBox4.Text = A(3)

TextBox5.Text = A(4)

TextBox6.Text = A(5)

End Sub

End Class

Java programming

, HB0902, p.11-28


(VB6.0)

Dim A(6) as Integer

Dim i, j, k, y as Integer

Dim temp, check as Integer

Randomize Timer

i = 0

While i <= 6

check = 0

A(i) = Int(49*Rnd() + 1)

For y = 0 to i 1

If A(i) = A(y) Then

check = -1

End if

Next y

If check = 0 Then

i = i + 1

End if

Wend

Java programming


with VB.NET

Function GenerateArray(ByVal size As Integer) _ As Integer()

'Dim arr(size - 1) As IntegerDim i As IntegerFor i = 0 To size - 1arr(i) = iNext i

Java programming

blog.craft-works.jp/uru/id/vb1


'Dim r As Random = New Random()Dim tmp As IntegerFor i = 0 To size - 1Dim src As Integer = r.Next(i, size)tmp = arr(src)arr(src) = arr(i)arr(i) = tmpNext iReturn arr

End Function

Java programming


(1)

public class Ran{

public static void main(String[] args){

int ran = (int)(Math.random()*10);

System.out.println(ran);

}

}

Mathrandom()

Java programming


(2)

import java.util.Random;

public class Ran{

public static void main(String[] args){

Random rnd = new Random();

int ran = rnd.nextInt(10);

System.out.println(ran);

}

}

Random nextInt

Java programming


import java.util.Random;public class Test{public static void main(String args[]){Random random = new Random(); int number = 0;;int array[] = new int[6];boolean b;for(int i=0; i<6; i++){b = true;while(b) {b = false;number = random.nextInt(49)+1;for(int j=0; j<array.length; j++) {if(array[j] == number) {b = true;}}}array[i] = number;System.out.printf("array[%d]=%d\n",i,number);}}}

Java programming


()

  • int[] data = {10, 4, 55, 20, 15, 12};

    for (int i = 0; i < data.length - 1; i++){

    for (int j = i + 1; j < data.length; j++){

    if (data[i] >data[j]){

    int tmp = data[i];

    data[i] = data[j];

    data[j] = tmp;

    }

    }

    }

Java programming


(sorting)

  • (bubble sort)

  • (selection sort)

  • (insertion sort)

  • (Shell sort)

Java programming


(sorting)

  • (quick sort)

  • (merge sort)

  • (heap sort)

  • (radix sort)

Java programming


Bubble sort method

source[i + 1] = exchange;

} // if end

} //for end

} //for end

for (short i = 0;

i < source.length; i++) {

System.out.print(source[i] + " ");

}

} //main method end

} //class end

public class BubbleSort {

public static void main(String args[]) {

short source[] = {

540, 94, 458, 47, 64};

short exchange = 0;

for (short pass = 1;

pass < source.length; pass++) {

for (short i = 0; i < source.length - 1;

i++) {

if (source[i] > source[i + 1]) {

exchange = source[i];

source[i] = source[i + 1];

Java programming


Sorting Array in Java

import java.util.Arrays;

public class HowToSortAnArray {

public static void main(String[] args) {

//Array of Integers

int[] myIntArray = new int[]{20,100,69,4};

//SORTS ARRAY FROM SMALLEST TO LARGEST INT

Arrays.sort(myIntArray);

//for loop to print Array values to console

for (int a = 0; a < myIntArray.length; a++) {

System.out.println(myIntArray[a]);

}

}

}

Java programming


10:

Java programming


*

**

***

****

*****

11: (1)

*

**

***

**

*

***

**

*

**

***

Java programming


*

***

*****

*******

*********

11: (2)

*****

***

*

***

*****

*

***

*****

***

*

Java programming


1

12

123

1234

12345

123

12

1

12

123

11: (3)

1

12

123

12

1

Java programming


1

121

12321

1234321

123454321

1

121

12321

121

1

11: (4)

32123

212

1

212

32123

Java programming


11: (_1)

Java programming


11: (_2)

Java programming


(method)

  • [public | private | protected] static [ | void] ([]) [throws ]

    {

    ;

    [return ;]

    }

Java programming


java

  • :

    [].([]);

  • :

    = [].([]);

Java programming


[] [] (<>)

{

}

Java programming


()

{

return ;

}

Java programming


java

  • (call by value)

  • (call by reference)

    []All parameters to methods in Java are

    pass-by-value.

Java programming


Defining methods ()

public double calculateAnswer(double

wingSpan, int numberOfEngines,

double length, double grossTons)

{

//do the calculation here

}

Java programming


method declaration

All parameters to methods in Java arepass-by-value.

Modifierssuch as public, private, and others you will learn about later.

The return typethe data type of the value returned by the method, or void if the method does not return a value.

Java programming


method definition (1)

class MyClass {

...

public ReturnType methodName(Type1 param1,

Type2 param2 ) throws ExceptionName {

ReturnType retType;

...

return retType;

}

...

}

Java programming


method definition (2)

private void methodName(

String param1, String param2 )

{

...

return;

}

Java programming


Create a simple Java method

public class Hi { public static void main(String[] args) {sayHi(); System.out.println(The second Hi!!); } public static void sayHi() { System.out.println(The first Hi!); }}

Java programming


2:

  • Function

  • BMI bmi(height, weight)

    • BMI [()] [(m)]2

  • std_weight(height)

    • 22m2

Java programming


2:

Java programming


method

modifier returnValueType

methodName(list of parameters)

{

// Method body;

}

Java programming


Recursive method

Method that invokes itself.

The arguments passed to the recursion take us closer to the solution with each call.

Java programming


recursive algorithms

if (the stopping case is reached){solve it}else{reduce the problem using recursion}

Java programming


Recursive method ()

public class RecursiveFactorial{ public static void main( String [] args ) { // compute factorial of 7 and output it System.out.println( Factorial ( 7 ) is + factorial( 7 ) ); } public static int factorial( int n ) {if ( n <= 0 ) // base casereturn 1;else// general casereturn ( n * factorial ( n - 1 ) ); }}

Java programming


, (int n)1+2+3+ ... +n

public class Example {

public static void main(String[] argv) {

System.out.println(sum(100));

}

public static int sum(int n) {

int total = 0;

for (int i = 1; i <= n; i++)

total += i;

return total;

}

}

Java programming


(recursion)1+2+3+...+n

public class Example {

public static void main(String[] argv) {

System.out.println(sum(100));

}

public static int sum(int n) {

if (n <= 1)

return 1;

else

return (n + sum(n - 1));

}

}

Java programming


(searching)

  • (sequential search)

  • (binary search)

  • (interpolation search)

  • (Fibonacci search)

Java programming


Recursive Binary Search

public static int search (int[] a, int first, int size, int target) { int middle;

if (size <= 0) return -1; else { middle = first + size/2; if (target == a[middle]) return middle; else if (target < a[middle]) // the target is less than a[middle], so search before the middle return search(a, first, size/2, target); else // the target must be greater than a[middle], so search after the middle return search(a, middle+1, (size-1)/2, target); } }

Java programming


3:

Java programming


  • [email protected]

Java programming


Java

  • [email protected]

Java programming


  • Login