1 / 16

CS1020 Data Structures and Algorithms I Tutorial 3

CS1020 Data Structures and Algorithms I Tutorial 3. Problem Solving with Java Library and Java Generics. Q1 Java Random Numbers. (0,1). = = =. (0,0). (1,0). (-1,0). (0,-1). * 4 * 4.

Download Presentation

CS1020 Data Structures and Algorithms I Tutorial 3

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1020 Data Structures and Algorithms ITutorial 3 Problem Solving with Java Library and Java Generics

  2. Q1 Java Random Numbers (0,1) = = = (0,0) (1,0) (-1,0) (0,-1) * 4 * 4 The Monte Carlo simulation works by choosing (x,y) coordinates from [-1.0, 1.0], and counting how many coordinates fall within the circle (this includes points that are exactly on the circumference of the circle). [CS1020 Tutorial 3 AY2013/4 S2]

  3. Q1 Java Random Numbers intnextInt(int n): Returns a pseudorandomintvalue between 0 (inclusive) and the specified value (exclusive). public double customRandom(Random randGen) { returnrandGen.nextInt(2000001) / 1000000.0 – 1; } The random numbers generated by the Random class are considered “pseudo-random” numbers. What does this mean? Using only the nextInt(int) method in the Random class, complete the method to generate a pseudo-random real number x, such that -1.0 ≤ x ≤ 1.0. [CS1020 Tutorial 3 AY2013/4 S2]

  4. Q1 Programming Model public static double hypot (double x, double y); More accurate with more trials. Which Math method can you use to calculate the Euclidean distance between a point P at (x, y) and the origin (0, 0)? Run the simulation with 10 trials, 1000 trials, and 100000 trials. What happens to the accuracy of the estimation? [CS1020 Tutorial 3 AY2013/4 S2]

  5. Q2 ArrayList public static intgetLargestNumber(ArrayList<Integer> numbers) { } intlargestNum = numbers.get(0); for (inti = 0; i < numbers.size(); i++) if (largestNum < numbers.get(i)) largestNum= numbers.get(i); returnlargestNum; public static intgetIndexOfLargestNumber(ArrayList<Integer> numbers) { } returnnumbers.indexOf(getLargestNumber(numbers)); Complete the getLargestNumber() method to find the largest value in numbers. Based on a) complete the getIndexOfLargestNumber() method to return the lowest index of the largest number [CS1020 Tutorial 3 AY2013/4 S2]

  6. Q2 ArrayList public static void main(String[] args) { ArrayList<Transaction> transactions = newArrayList<Transaction>(); Transaction eggs = new Transaction("Eggs", 4.00); Transaction chocolate = new Transaction("Chocolate bar", 3.44); transactions.add(eggs); transactions.add(chocolate); Transaction eggs2 = new Transaction("Eggs", 4.00); intindex = transactions.indexOf(eggs2); System.out.println(index); } -1 What will be printed out? Why? [CS1020 Tutorial 3 AY2013/4 S2]

  7. Q2 ArrayList public intindexOf(Object o): Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index. public booleanequals(Object other) { if(other instanceof Transaction) { Transaction otherTrans = (Transaction) other; return otherTrans.description.equals(this.description) && otherTrans.amount== this.amount; } else { return false; } } [CS1020 Tutorial 3 AY2013/4 S2]

  8. Q3 Autoboxing class MyWrapperTest1 { private static Integer x; public static void main(String [] args) { System.out.println(x); System.out.println(x.toString()); } } null Runtime error public void print(String s): Prints a string. If the argument is null then the string "null" is printed… What is the output for each of the following lines? Can you explain what happened? [CS1020 Tutorial 3 AY2013/4 S2]

  9. Q3 Autoboxing class MyWrapperTest1 { private static intx; public static void main(String [] args) { System.out.println(x); System.out.println(x.toString()); } } Compile error class MyWrapperTest1 { private static intx; public static void main(String [] args) { System.out.println(x); } } 0 class MyWrapperTest1 { public static void main(String [] args) { int x; System.out.println(x); } } Compile error [CS1020 Tutorial 3 AY2013/4 S2]

  10. Q3 Autoboxing class MyWrapperTest2 { public static void main(String [] args) { Integer x = 3; Integer y = 2; intk = x % y; System.out.println(k); } } 1 What is the output for each of the following lines? Can you explain what happened? [CS1020 Tutorial 3 AY2013/4 S2]

  11. Q3 Autoboxing class MyWrapperTest3 { public static void main(String [] args) { Double finalSolution = null; Integer firstNum = 3000; Integer secondNum = 4000; finalSolution= firstNum / secondNum; System.out.println(finalSolution); } } Compile error What is the output for each of the following lines? Can you explain what happened? [CS1020 Tutorial 3 AY2013/4 S2]

  12. Q4 Java Generics classPairGen <T> { privateT first, second; publicPairGen(T a, T b) { first = a; second = b; } public T getFirst() { return first; } publicT getSecond() { return second; } } class PairNot { privateObject first, second; publicPairNot(Object a, Object b) { first = a; second = b; } publicObject getFirst() { return first; } publicObject getSecond() { return second; } } [CS1020 Tutorial 3 AY2013/4 S2]

  13. Q4 Java Generics PairGen<String> pairGen = newPairGen<String>("soccer", "football"); PairNotpairNot = newPairNot("soccer", "football"); PairGen<String> pairGen = newPairGen<String>("soccer", 123); PairNotpairNot = newPairNot("soccer", 123); • How would you initialize a pair of String, “soccer” and “football” into the two classes? What is the advantage of using generics? Advantages of using Generics: • Type-safety: [CS1020 Tutorial 3 AY2013/4 S2]

  14. Q4 Java Generics PairGen<String> pairGen = newPairGen<String>("soccer", "football"); String first = pairGen.getFirst(); PairNotpairNot = newPairNot("soccer", "football"); String first = (String)pairNot.getFirst(); PairGen<String> pairGen = newPairGen<String>("soccer", "football"); System.out.println(pairGen.getSecond()); PairNotpairNot = newPairNot("soccer", 123); System.out.println((String)pairNot.getSecond()); • No casting required • No casting exception [CS1020 Tutorial 3 AY2013/4 S2]

  15. Q4 Java Generics class NewPair <S,T> { private S first; privateT second; publicNewPair(S a, T b) { first = a; second = b; } publicS getFirst() { return first; } publicT getSecond() { return second; } } Can you use the class PairGen to pair two objects of different data types? Why? Can you use the class NewPair to pair two objects of the same data type? Why? [CS1020 Tutorial 3 AY2013/4 S2]

  16. Q4 Java Generics ArrayList<Object> objectList; ArrayList<String> stringList; objectList = stringList; Compile error Can you use the class NewPair to pair two arrays of String and Integer? Why? Can you pass ArrayList<String> to a method that accepts ArrayList<Object>? Why? [CS1020 Tutorial 3 AY2013/4 S2]

More Related