Java Programs - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Java Programs

Description:

Java Programs. Examples 11 to 15. Index. 11. Rotate vector - rotate.java. 12. Threads - bouncer.java ... Thread demo bouncer.java. Class Ball extends Thread ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 15
Provided by: cs03
Category:
Tags: bouncer | java | programs

less

Transcript and Presenter's Notes

Title: Java Programs


1
Java Programs
  • Examples 11 to 15

2
Index
  • 11. Rotate vector - rotate.java
  • 12. Threads - bouncer.java
  • 13. Network - client.jav
  • 14. Network - nrserv.jav
  • 15. Grahics - plot.java
  • 16. Genetic algorithm - ga2.java
  • GA description in separate doc

3
Example 12
  • Thread demo bouncer.java
  • Class Ball extends Thread
  • Init adds buttons
  • Event handler makes balls
  • Start is a lib method of thread
  • ball.start() works with run()
  • Graphics g getGraphics()

4
Example 12 continued
  • ball new Ball(g) makes balls
  • Draw circle - g.filloval(x,y, dia, dia )
  • Boolean - keepgoing
  • Starts or stops just the last ball
  • Display size rectRightX
  • Only last ball can be stopped

5
Example 13 background
  • Iterative function - Newton Raphson
  • Used to calculate a square root
  • estnew is a native float
  • estnew (estold Value/estold)/2
  • Sqr root (Value is 100) 10
  • Final answer 10
  • Initial estimate - estold 1000

6
Example 13 bgnd cont.
  • estnew (estold Value/estold)/2
  • estnew (1000100/1000)/2500
  • estnew (500100/500)/2250 etc
  • Calc needs estold and Value
  • During run client sends strings
  • Text command - run Value est
  • nrserv sends back - ansnew

7
Example 13 client.java
  • Network client.java
  • Works with nsrerv.java
  • Uses sockets
  • Input stream inpstr
  • Output stream outstr
  • sock new Socket
  • (157.228.x.x, 4444)

8
Example 13 continued.
  • Server makes all calculations
  • Outval 100
  • Outstr.println(outval)
  • Iterate - ansoldansnew
  • Edit IP address connect to server
  • Command ipconfig/all or use wincfg
  • Answer is printed on screen

9
Example 14 nrserv.java
  • Server accepts sock when
  • Client connects
  • Worker thread created
  • class Worker extends Thread
  • new Worker( sock ).start()
  • Method run() is invoked
  • Remember inputs to server - strings

10
Example 14 continued
  • To read string convert to integer
  • valstrg inpstr.readline()
  • Value Integer.parseInt(valstrg)
  • Wrapper classes
  • Lower case float normal native float
  • Upper case Float denotes
  • Special class/object with methods wrapped around

11
Example 14 continued
  • To read and convert string to float
  • Float temp Float.valueOf(eststrg)
  • float estold temp.floatValue()
  • This is special feature of Java
  • Input - Ctrlstrg valstrg eststrg
  • Uses ctrlstrg - Value(int) - estold(float)
  • Output value for client estout

12
Example 15 plot.java
  • A prototype graph plotting program
  • Normalised device coordinates NDC
  • All methods use g, xMAX, yMAX
  • Improved version 2 uses graph object and set
    graphics method
  • Polyline draws more than one line to produce a
    curve

13
Example 15 plot.java cont.
  • Operations
  • Draw a border
  • Get min, max and range for arrays
  • Normalise arrays
  • Scale arrays
  • Translates arrays
  • Plots graph using polyline

14
Example 15 plot.java cont.
  • Marks title and axes
  • Marks points on graph
  • Ver 2 plotgrafs uses
  • Normalised graphics class NormG
  • GraphObj class - model class
  • Ver 3 gplot
  • show how to creates three
  • graph instances
Write a Comment
User Comments (0)
About PowerShow.com