Week 12 Practical Class Exercises Lists in Java

Reviews
Shared by: Ryan Denney
Stats
views:
0
rating:
not rated
reviews:
0
posted:
5/30/2009
language:
English
pages:
0
Department of Computer Science, Australian National University COMP1100 — Introduction to Programming and Algorithms Semester 2, 2009 Week 12 Practical Class Exercises Lists in Java Objectives The aim of these exercises is to give you some practice using lists in Java. The focus is on the ArrayList class and the “for each” loop, but you can experiment with other approaches if you feel confident. List of Integers The Java class ListInts was discussed in lectures to demonstrate some simple list processing algorithms, similarly to those we studied earlier in Haskell. The ListInts.java file is available on the course website. Exercise 1 (Count occurrences) There is a method Boolean elem(Integer n) in ListInts.java, which checks whether an integer n is in the list. Write a method Integer count ( Integer n) which returns a count of the number of times the integer n occurs in the list. You may wish to experiment using DrJava, but you should also modify the TestList.java class to test your function. Think carefully about your choice of tests. What happens if n doesn’t appear in the list, or if the list is empty? Exercise 2 (Double each element) In Haskell, we defined a function to double each element of a list of integers as follows double :: [ Int ] -> [ Int ] double xs = map (*2) xs Add a function to the Java ListInts class ListInts times2 () which constructs a new list of integers, where each element on the result list is twice the corresponding element of this ListInt object. (We can’t call the method “double” because that’s a reserved word in Java.) Check your implementation, either using DrJava or by extending TestList.java. Lists in Java 1 The Path and Point classes Java classes Point and Path respectively representing points in a cartesian coordinate system and paths as a list of points, were discussed in lectures and can be found on the course website. The following exercises are to extend these classes, with the emphasis again being on processing lists and the “for each” loop. As you attempt each of these exercises you should also add some tests to the TestPoints class. Exercise 3 (Scale and rotate paths) The Point class has a method to translate a point an x-distance and a y -distance. Path has a corresponding method to translate a path, by translating each point. Add similar methods to scale points and paths by a specified factor. For example, scaling point (1.0,2.0) by a factor of 3.0 changes it to (3.0,6.0). Add methods to rotate points and paths a specified angle. The formula for rotating point (x; y ) an angle  is (x cos   y sin ; x sin  +y cos ). The trigonometric functions are in java.lang.Math so you will need to write Math.sin(theta), for example. Like Haskell, angles are in radians. Exercise 4 (Is a path a cycle?) A path is a cycle if its first and last points are equal. Add a method public Boolean isCycle () to class Path to check if a path is a cycle. (Remember the difference between the (==) operator and the equals method.) A better version: Since our coordinates are of type Double, they are only approximate representations of real numbers. Therefore it may be more appropriate to check whether the distance between the points is sufficiently small, say less than 10e-6. Exercise 5 (Closest to the origin) Add a method to the Path class which uses the distance method in Point to determine which point in a path is closest to the origin. Lists in Java 2

Related docs
Practical Exercises in English
Views: 44  |  Downloads: 7
SCWCD- Exercises
Views: 61  |  Downloads: 26
Java class
Views: 1  |  Downloads: 0
exercises
Views: 543  |  Downloads: 23
JAVA
Views: 25  |  Downloads: 0
AC11001 Practical 5 - Inheritance in Java
Views: 0  |  Downloads: 0
Programming in Java
Views: 0  |  Downloads: 0
Practical Grammar and Composition
Views: 64  |  Downloads: 11
stretching exercises
Views: 340  |  Downloads: 33
Java 1
Views: 3  |  Downloads: 0
Creativity Exercises
Views: 12  |  Downloads: 1
Key to Exercises
Views: 10  |  Downloads: 0
Other docs by Ryan Denney
Board Resolution Designating a Purchasing Agent
Views: 205  |  Downloads: 3
Demand for Inspection of Books and Records
Views: 305  |  Downloads: 6
Property Analysis
Views: 3078  |  Downloads: 373
E-mail Policy
Views: 403  |  Downloads: 13
Crito
Views: 231  |  Downloads: 0
understanding_and_managing
Views: 350  |  Downloads: 1
Kraft Foods Inc Ammendments and Bylaws
Views: 183  |  Downloads: 1
CUSTOMER COMPLAINT RESPONSE LETTER
Views: 4979  |  Downloads: 64
craven-all
Views: 185  |  Downloads: 4
Copyright Compliance Photocopying Policy
Views: 290  |  Downloads: 2
Form 4797 Sales of Business Property
Views: 581  |  Downloads: 3