COMP20608S General Programming 2 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

COMP20608S General Programming 2

Description:

for (int i=0; i s.length()/2; i ) { if (!(s.charAt(i) == (s.charAt(s.length()-1-i) ... { return width*length; Department of Computer Science. 25. And Triangle ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 26
Provided by: GeoffH8
Category:

less

Transcript and Presenter's Notes

Title: COMP20608S General Programming 2


1
COMP206-08SGeneral Programming 2
2
Lectures
  • Today
  • Random Numbers
  • Timing
  • Assignment 1 Review

3
Random Numbers
  • import java.util.Random
  • Implements a random number generator that appears
    to generate random numbers
  • Construct object of Random class
  • Use methods
  • nextInt(n) random number 0 -gt n-1
  • nextDouble() random number 0 -gt 1 (exclusive)
  • Constructors use system clock no params or you
    can seed the generator

4
Example
  • Random generator new Random( )
  • int oneToTen 1 generator.nextInt(10)

5
Useful utility
  • import java.util.Random
  • public class ArrayUtil
  • private static Random generator new Random()
  • public static int randomIntArray(int length,
    int n)
  • int a new intlength
  • for(int i0 ilta.length i)
  • ai generator.nextInt(n)
  • return a
  • public static void print(int a)
  • for (int x a)
  • System.out.print(x " ")
  • System.out.println()

6
Test for Array Util
  • public class TestArrayUtil
  • public static void main(String args)
  • // 20 numbers in range 0 -gt 99
  • int a ArrayUtil.randomIntArray(20, 100)
  • ArrayUtil.print(a)

7
Timing program runs
  • Want to be able to assess how long something
    takes to run
  • Can be complicated due to threads
  • We will make use of a system call
  • System.currentTimeMillis()
  • This provides the current time in milliseconds so
    we need to do some work to convert to the elapsed
    time of the thing we are assessing.
  • Can get elapsed time while running

8
Stop Watch Class
  • public class StopWatch
  • private Boolean isRunning
  • private long startTime, elapsedTime
  • public StopWatch()
  • reset()
  • public void start()
  • if (isRunning) return
  • isRunning true
  • startTime System.currentTimeMillis()

9
  • public void stop()
  • if (!isRunning) return
  • isRunning false
  • long endTime System.currentTimeMillis()
  • elapsedTime elapsedTime endTime - startTime
  • public long getElapsedTime()
  • if (isRunning)
  • long endTime System.currentTimeMillis()
  • return elapsedTime endTime - startTime
  • else
  • return elapsedTime
  • public void reset()

10
Test program for StopWatch
  • public class TestStopWatch
  • public static void main(String args)
  • StopWatch timer new StopWatch()
  • timer.start()
  • try
  • Thread.sleep(6000)
  • catch (InterruptedException exception)
  • timer.stop()
  • System.out.println("Time to sleep
    "timer.getElapsedTime())

11
Assignment 1 Review
  • Java naming conventions
  • All variables start with a lowercase letter and
    new words start with an uppercase letter
  • Eg int num, int numFound
  • Class names uppercase letter first (then as
    above)
  • Eg Circle, Student, BinarySearchTree
  • Method names as per variables
  • Eg processFile, main, findOutliers

12
Question 1 check chars
  • public class palindrome
  • public static void main (String args)
  • String s args0
  • for (int i0 ilts.length()/2 i)
  • if (!(s.charAt(i) (s.charAt(s.length()
    -1-i))))
  • System.out.println(s" is not a palindrome")
  • return
  • System.out.println(s" is a palindrome")

13
Question 1 use String methods
  • public static void main (String args)
  • StringBuffer s new StringBuffer (args0)
  • StringBuffer t new StringBuffer (args0)
  • if (s.toString().equals(t.reverse().toString()))
  • System.out.println(s" is a palindrome")
  • else
  • System.out.println(s" is not a
    palindrome")

14
Complex Numbers
  • public class Complex
  • private float real
  • private float imag
  • public Complex(float r, float i)
  • real r
  • imag i
  • public Complex()
  • real 0
  • imag 0

15
  • public Complex add(Complex a)
  • Complex result new Complex()
  • result.real a.real this.real
  • result.imag a.imag this.imag
  • return result
  • public String toString()
  • String result new String()
  • result "Real part " this.real
  • result " Imag part " this.imag
  • return result
  • public boolean equals (Complex y)

16
equals
  • The equals method can be either overloaded or
    overridden
  • Here it is overloaded so there are two equals
    methods
  • boolean equals(Complex other) defined in
    Complex
  • boolean equals(Object other) defined in Object
  • boolean equals(Complex x, Complex y) is a
    different method entirely

17
Equals formal defn
  • public boolean equals(Object otherObject)
  • if (otherObject null) return false
  • if (getClass() ! otherObject.getClass())
  • return false
  • Complex other (Complex) otherObject
  • return (this.real other.real this.imag
    other.imag)

18
Question 3 - Scanner
  • import java.util.Scanner
  • public class TestComplex
  • public static void main (String args)
  • Scanner in new Scanner(System.in)
  • int xr in.nextInt()
  • int xi in.nextInt()
  • Complex x new Complex(xr,xi)
  • String op in.next()
  • int yr in.nextInt()
  • int yi in.nextInt()
  • Complex y new Complex(yr, yi)
  • Complex result
  • if (op.equals(""))
  • result x.add(y)
  • System.out.println("result is
    "result.toString())
  • else System.out.println("Something went wrong")

19
Scanner methods
  • Scanner(InputStream in)
  • Scanner(Reader in)
  • boolean hasNext()
  • String next()
  • int nextInt()
  • String nextLine()

20
One line input
  • import java.util.Scanner
  • import java.util.StringTokenizer
  • public class TestComplex
  • public static void main (String args)
  • Scanner in new Scanner(System.in)
  • String s in.nextLine()
  • StringTokenizer tokens new StringTokenizer(s)
  • String openBracket tokens.nextToken()
  • int xr Integer.parseInt(tokens.nextToken())
  • int xi Integer.parseInt(tokens.nextToken())
  • Complex x new Complex(xr, xi)
  • String op tokens.nextToken()
  • int yr Integer.parseInt(tokens.nextToken())
  • int yi Integer.parseInt(tokens.nextToken())
  • Complex y new Complex(yr, yi)
  • String closeBracket tokens.nextToken()

21
Question 4
  • import java.util.ArrayList
  • public class TestShapes
  • public static void main(String args)
  • ArrayListltShapegt shapes new ArrayListltShapegt()
  • shapes.add(new Circle(4))
  • shapes.add(new Triangle(5,10))
  • shapes.add(new Rectangle(4,5))
  • double totalArea 0
  • for (Shape x shapes)
  • totalArea x.area()
  • System.out.println("Total area is "totalArea)

22
Implement Shape as an Interface
  • public interface Shape
  • double area()

23
And each shape implements area
  • public class Circle implements Shape
  • private double radius
  • public Circle()
  • radius 0
  • public Circle(double r)
  • radius r
  • public double area()
  • return Math.PIradiusradius

24
Similarly for others
  • public class Rectangle implements Shape
  • private double width, length
  • public Rectangle()
  • width 0
  • length 0
  • public Rectangle(double w, double l)
  • width w
  • length l
  • public double area()
  • return widthlength

25
And Triangle
  • public class Triangle implements Shape
  • private double base, height
  • public Triangle()
  • base 0
  • height 0
  • public Triangle(double b, double h)
  • base b
  • height h
  • public double area()
  • return 0.5baseheight
Write a Comment
User Comments (0)
About PowerShow.com