Presentation is loading. Please wait.

Presentation is loading. Please wait.

Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.

Similar presentations


Presentation on theme: "Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2."— Presentation transcript:

1 Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2

2 Department of Computer Science2 Lectures - Today - Random Numbers - Timing - Assignment 1 Review

3 Department of Computer Science3 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 -> n-1  nextDouble() – random number 0 -> 1 (exclusive) Constructors use system clock – no params or you can “seed” the generator

4 Department of Computer Science4 Example Random generator = new Random( ); int oneToTen = 1 + generator.nextInt(10);

5 Department of Computer Science5 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 int[length]; for(int i=0; i<a.length; i++) a[i] = generator.nextInt(n); return a; } public static void print(int[] a) { for (int x : a) System.out.print(x + " "); System.out.println(); }

6 Department of Computer Science6 Test for Array Util public class TestArrayUtil { public static void main(String[] args) { // 20 numbers in range 0 -> 99 int a[] = ArrayUtil.randomIntArray(20, 100); ArrayUtil.print(a); }

7 Department of Computer Science7 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 Department of Computer Science8 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 Department of Computer Science9 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() { elapsedTime = 0; isRunning = false; }

10 Department of Computer Science10 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 Department of Computer Science11 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 Department of Computer Science12 Question 1 – check chars public class palindrome { public static void main (String [] args) { String s = args[0]; for (int i=0; i<s.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 Department of Computer Science13 Question 1 – use String methods public static void main (String[ ] args) { StringBuffer s = new StringBuffer (args[0]); StringBuffer t = new StringBuffer (args[0]); if (s.toString().equals(t.reverse().toString())) System.out.println(s+" is a palindrome"); else System.out.println(s+" is not a palindrome"); }

14 Department of Computer Science14 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 Department of Computer Science15 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) { boolean result; result = (this.real == y.real && this.imag == y.imag); return result; }

16 Department of Computer Science16 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 Department of Computer Science17 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 Department of Computer Science18 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 Department of Computer Science19 Scanner methods Scanner(InputStream in) Scanner(Reader in) boolean hasNext() String next() int nextInt() String nextLine()

20 Department of Computer Science20 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(); Complex result; if (op.equals("+")) { result = x.add(y); System.out.println("result is "+result.toString()); } else System.out.println("Something went wrong"); }

21 Department of Computer Science21 Question 4 import java.util.ArrayList; public class TestShapes { public static void main(String[] args) { ArrayList shapes = new ArrayList (); 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 Department of Computer Science22 Implement Shape as an Interface public interface Shape { double area(); }

23 Department of Computer Science23 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.PI*radius*radius; }

24 Department of Computer Science24 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 width*length; }

25 Department of Computer Science25 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.5*base*height; }


Download ppt "Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2."

Similar presentations


Ads by Google