CST252 Network Software Design - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

CST252 Network Software Design

Description:

basic types, variables (constant), operators, casts/conversions, comments, Strings ... Palaver. This example may seem like a long way of doing something very simple ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 37
Provided by: Davi825
Category:

less

Transcript and Presenter's Notes

Title: CST252 Network Software Design


1
CST252 Network Software Design
  • Lecture 2 Fundamental Programming Structures
    Java Syntax

2
Resources
  • Java Tutorial (java.sun.com)
  • Basic structures trail
  • Course Website
  • users.wmin.ac.uk/lancasd/CCTM91
  • Book
  • First chapters of any Java book
  • or C. Horstmann and G. Cornell. Core Java
  • Volume 1 chapter 3

3
Lecture 1 Review
  • Course Structure
  • Java in General
  • About Java, hype and reality
  • Compiling and Interpreting
  • HelloWorld
  • next slide
  • Introduction to Syntax
  • basic types, variables (constant), operators,
    casts/conversions, comments, Strings

4
Review HelloWorld
  • Case Sensitive
  • Name of class name of file.java
  • File contains
  • public class ....
  • Class contains
  • public static void main(String args) ....
  • Note semicolons, braces, comments
  • Cycle Edit, javac, java

5
Quiz
  • Which lines are OK, which are not?
  • A) boolean done 0
  • B) float x 97.3
  • double x2 x
  • x2 x
  • C) static final double PI 3.145
  • D) byte _Flag 253
  • E) short 9ofSpades

6
Lecture 2 Overview
  • Applications not Applets (L9)
  • Fundamental Programming Structures
  • Flow Control if, then etc.....
  • Reading Input
  • More Syntax
  • Arrays, More about Strings
  • Elementary classes
  • methods, instance fields, test classes

7
Control Flow
  • Block Scope
  • Conditional
  • if if, else
  • Loops
  • while
  • do while
  • for
  • Switch and break

8
Block Scope
  • public static void main(String args)
  • int n
  • ...
  • int k
  • k n
  • ....
  • // k only defined up to here
  • public static void main(String args)
  • int n
  • ...
  • int k
  • int n // error cant redefine n
  • ....

9
Conditional Statement
  • if (YourSales gt Target)
  • performance OK
  • bonus 100

NO
Sales gt target
YES
performanceOK
bonus 100
10
If/Else conditional
  • if (YourSales gt Target)
  • performance OK
  • bonus 100
  • else
  • performance bad
  • bonus 0

NO
YES
Sales gt target
performanceOK
performancebad
bonus 100
bonus 0
11
While Statement
  • while (balance lt goal)
  • balance payment
  • double interest balance 4.2/100
  • balance interest
  • years
  • System.out.println(years)

NO
balance lt goal
YES
update balance
years
print years
12
Do/While
  • While loop tests at the top, if you want the test
    at the end of the loop - so that it executes at
    least once - use do/while
  • do
  • balance payment
  • double interest balance 4.2/100
  • balance interest
  • years
  • // ask if ready to retire - get input
  • while (input.equals(N))

13
For loop
  • For loop executes a fixed number of times
    determined by a counter
  • for (int i 0 i lt 10 i ) // common syntax
  • System.out.println(i)
  • Or
  • int i
  • for (i 0 i lt 10 i )
  • System.out.println(i)
  • // in this case i is still defined after loop
    (i10)

14
Other Control Statements
  • Switch for multiple selections - like many
    if/else statements, as in C
  • Breaking control flow - to jump out of a loop
  • break
  • labelled break
  • continue

15
Screen Input/Output
  • Output to terminal window is easy
  • System.out.println(s)
  • (actually - formatting issues - look in a book)
  • Input - not so simple - 3 ways (more)
  • From command line arguments
  • Direct coding uses ugly I/O classes (can hide by
    creating a special class Console.java)
  • A pop up window (InputTest.java)
  • JOptionPane.showInputDialog(prompt)

16
Input of Numbers
  • Output of numbers is easy - the system takes care
    of changing int, float etc to String
  • But irrespective of how input is done, it is
    understood as a String and the user must turn it
    into a number type
  • Changing String input to numbers
  • int age Integer.parseInt(inputstring)

17
Examples
  • InputTest.java (uses popup window)
  • Console input (ugly)
  • Loops
  • Retirement.java
  • Retirement2.java
  • Lab session (calculate VAT)

18
Arrays
  • A way of storing a bunch of values of the same
    data type
  • Eg an array x of int, access the ith integer as
    ai
  • Must know the size, how many items are to be
    stored
  • if you dont - use an ArrayList or some other
    collection data structure that automatically grows

19
Creating Arrays
  • Use new to create empty arrays
  • int a new int20
  • Loops
  • for(int i0 ilt20 i)
  • ai 43 2i
  • If you try and access a25 you will get an error
  • The size of the array is a.length so often see
    for(int i0 ilta.length i)
  • An alternative syntax for arrays
  • int a instead of int a

20
Filling Arrays
  • Can create and initialise in one step
  • int smallprimes 2, 3, 5, 7, 11, 13
  • Copying
  • int luckynumbers smallprimes
  • luckynumbers5 12 //now smallprimes5 is
    also 12
  • Both variables refer to the same array
  • For a real copy, must copy each element or use
    System.arraycopy(from,fromIndex,to,toIndex,count)
  • Java has no C-like pointer arithmetic

21
Command line parametersan example of an Array
  • public static void main(String args)
  • Then compile and run with the command line
  • java myclass -d gray 34
  • args0 -d
  • args1 gray
  • args2 34
  • The args parameter is an array of Strings

22
Arrays continued
  • Sorting arrays
  • Arrays.sort(a)
  • This sorts according to numerical size or
    alphabetically depending on what type is stored
    in the array
  • LotteryDrawing.java code. Put 1,2,3..40 into one
    array, pick 5 (distinct) entries and place into
    another array. Sort and print.

23
Multidimensional Arrays
  • Arrays can themselves hold other arrays - so you
    can have multidimensional arrays
  • final int NROWS 8
  • final int NCOLUMNS 8
  • char chessboard new charNROWSNCOLUMNS
  • char06 Q // Queen here
  • char32 // Blank (not occupied)

24
Strings
  • Double quotes
  • String greeting Hello
  • Concatenation
  • String s2 greeting David
  • Equality
  • s.equals(t)
  • Empty String
  • String empty //Not a blank space
  • Length (brackets cf arrays)
  • int len s2.length()

25
String Methods
  • boolean equals(String)
  • boolean equalsIgnoreCase(String)
  • boolean startsWith(String)
  • String subString(int,int)
  • Need documentation to lookup all this
  • Lab exercise

26
Documentation
  • On-line Java API documentation (intranet copy)
  • Created from Sun Java source implementation using
    javadoc
  • Recall javadoc comments
  • /
  • This class prints HelloWorld
  • /
  • Comment your own code the same way

27
Simple Classes
  • Think of classes as software packages that
    contain data and methods
  • HelloWorld is not the common kind of class
    because it only contains a special method - the
    main() method
  • A more typical class with similar function is
    Greeter

28
Greeter.java
  • public class Greeter
  • public String sayHello()
  • String message Hello World
  • return message

29
Testing classes
  • Can compile Greeter.java
  • gt javac Greeter.java
  • But cant run it by itself since it doesnt have
    a main method
  • Some development environments allow you to check
    classes by calling their methods
  • We create a test class - the purpose is just to
    test the Greeter class - very common technique

30
GreeterTest.java
  • public class GreeterTest
  • public static void main(String args)
  • Greeter wG new Greeter()
  • System.out.println(wG.sayHello())

31
Testing
  • GreeterTest creates an object of type Greeter,
    invokes its sayHello() method and prints the
    result on the console
  • Running
  • gt javac Greeter.java
  • gt javac GreeterTest.java
  • gt java GreeterTest

32
Instance fields
  • The Greeter class isnt very interesting - it
    contains a method but no data
  • Modify it to contain an instance field - data or
    state that the class saves
  • This field is labelled private, so external
    classes cannot access it directly - only through
    methods

33
Greeter.java (Modified)
  • public class Greeter
  • public Greeter(String aname)
  • name aname
  • public String sayHello()
  • String message Hello name
  • return message
  • private String name

34
GreeterTest.java (Modified)
  • public class GreeterTest
  • public static void main(String args)
  • Greeter wG new Greeter(World)
  • System.out.println(wG.sayHello())
  • Greeter dG new Greeter(David)
  • System.out.println(dG.sayHello())

35
Palaver
  • This example may seem like a long way of doing
    something very simple
  • In C, might just call a function
  • In Object Oriented programming methods are
    contained in classes - which may also contain
    data (instance fields) - these data are not
    directly accessible from outside
  • This control is useful in big software projects

36
Summary L2
  • Syntax
  • if/else, loops, arrays, Strings
  • Inputting information
  • Elementary classes (cover again with a slightly
    more complicated eg next week)
  • methods and how to call them
  • instance fields, private
  • Test class to create and invoke methods
Write a Comment
User Comments (0)
About PowerShow.com