Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
COMPSCI5002, Lab Work Part
Introduction and Aims This lab book covers the material on the first few days of the fast-paced course (equivalent to the first few weeks of a normal paced course). Do the exercises at your own pace, and use the scheduled lab sessions to ask for support. The initial exercises allow you to establish a basic setup of code management, and gives you some practice of compiling and running code. The exercises gradually increase in complexity, and are meant to test your grasp of the programming concepts. 2 Setting up the Environment 2.1 Compiling from the command line The course lectures contain materials on how to create and compile Java files from the command line (using vi or any other editor of your choice). To get yourself started: 1. write a simple one-file Java code (e.g. a HelloWorld), 2. compile it with javac, 3. run the main class with java. 2.2 Practice with Git Now, you should save the program that you had just created on to a git repository. For this purpose, 1. Set up your own git account (if you don’t already have one). 2. Clone the git repository to set up your code repository for the lab exercises. This is where you will find the most up-to-date course material. git clone <source URI> Q 1 (0 marks). Clone the course’s git repository. 3. Add this new practice problem java file to the repository (preferably under a new directory mypractice). git add <path to your java file> 4. Commit and Push.git commit 'my first practice java program' git push Q 2 (0 marks). Push your first commit. 1 Figure 1: Maven tool bar in IntelliJ.2.3 Setting up an IDE (IntelliJ or another) You can use any favourite IDE of your choice. The purpose is to be able to effectively manage large projects with automated build tools like maven etc. In the following instructions, we use IntelliJ. 2. Create a new maven project. 3. A maven project creates a folder structure src/main/java/ – see Figure 1. 4. Move your practice file into src/main/java/. It’s a good idea at this stage to include a package header to your Java file, and put the file into a folder (under src/main/java/) with the same name. For instance, if you choose a package name simple, then insert the line ‘package simple;’ in your Java file and place it under the folder src/main/java/simple. 5. Compile and run your new maven project. From the menu, click on View -- Tool Window -- Maven to display the maven sidebar. 6. Click the compile button to compile your project, and then from your the editor pane of your file, right click and press on the ‘Run YourClassName.main()’ option. 7. Finally commit and push your first practice maven project. 3 Functions and Control Structures This exercise is meant to test your grasp of the concepts of control structures, and writing modular blocks of code using functions. This code in the git repository finds out all prime integers between 2 (the least prime) and a maximum specified bound (say 100). Modify the functionality of this program in the following ways. Note: write separate main methods to test each functionality. 2Q 3 (2 marks). Instead of specifying only a maximum value to restrict search, a user should be able to supply two command line arguments to specify the minimum and the maximum of prime number search. For example, java PrimeFinder 1000 100000 should list all prime numbers between 1000 and 100000. Q 4 (3 marks). Write a program (WAP) to find twin, cousin and sexy primes within a given maximum integer bound. A twin prime is a pair of primes p 1 ,p 2 , where p 2 −p 1 = 2. Similarly, in a cousin prime pair p 2 −p 1 = 4 and in a sexy pair p 2 − p 1 = 6. Examples of twin primes are (3,5), (5,7) etc. An example of cousin prime pair is (13,17), whereas (11,17) is a sexy prime pair. In the lecture notes, we discussed at length the generation of patterns as a case study for thinking in a modular way and about anticipating the generalizations. Take a look at the code GenericPatternDemo. Q 5 (3 marks). Generalize these implementations to be able to generate the patterns shown in Figure 2. During the marking phase, you should be able to explain the command line arguments that you passed to generate the patterns, e.g., what parameters were required to be passed to generalize the implementations of GenericPatternDemo.java and GenericPatternDemoV2.java, e.g., java GenericPatternDemo '*' '*' 20 * * ** ** *** *** **** **** ***** ***** ****** ****** ******* ******* ******** ******** ********* ********* Figure 2: Patterns to generate.4 Arrays, Maps, and Sets Returning to the prime finder program, we now need to modify it to print the prime gaps. A prime gap is defined as the difference between two successive primes. 3 2, 3 1 2, 5 3 2, 7 5 3, 5 2 3, 7 4 5, 7 2 Q 6 (3 marks). WAP to print the histogram (counts) of prime gap values between 1 and 100. For instance, between 1 and 10, there are 4 primes – 2, 3, 5 and 7, with gaps as computed below. The distribution of the prime gaps is thus a list comprising pairs such as (1,1) (meaning the gap 1 occurs once), (2,2) (gap 2 occurs twice) and so on. Your program should print the gap values along with their frequencies, i.e., for the range [1,10] the program should print: 1 1 2 2 3 1 4 1 5 1 Hint: You may want to use a set (HashSet or TreeSet) to first find and store the primes. You may also use either a HashMap or an array to compute the frequency of each gap.5 Polymorphism For this exercise, you have to work on the lab1 maven project. Inside the _4gon package, the project contains an implementation of a general quadrilateral (i.e. a polygon with 4 sides) and its specific types (e.g. parallelogram, rectangle, etc). Also, as a part of a separate package, named shapes, it contains implementation of an abstract shape and its concrete realizations in terms of a circle and a rectangle. This task asks you to implement logic about abstract quadrilaterals. Q 7 (1 mark). Create a container (e.g. ArrayList or HashSet) of objects to store _4gons (abstract quadrilaterals) in an application class. The application/driver class should then be able to iterate over each quadrilateral and print its perimeter — the _4gon class has an implementation of the method perimeter(). Figure out if this is going to work for specific subtypes, e.g. rectangles/squares. Figure 3: A non-convex and a convex quadrilateral, respectively. In a convex 4-gon, none of the angles is greater than 180 degrees. Q 8 (2 marks). Revisit the class design to ensure that you should only be able to store convex quadrilaterals, i.e. those for which the area can be computed in an easy way, e.g., see Figure 3. You should have also write a test function that demonstrates the correctness of this convexity check on two sample quadrilaterals - one convex, and the other a non-convex. 4 Hint: For convexity check, you need to compute the angles between adjacent vertices of a 4-gon, and check if any of these angles are obtuse. Q 9 (2 marks). Develop a method to compute the area of any quadrilateral. Sample code for computing the area of a parallelogram is already a part of the existing code. float area() { return Math.abs(a.getX()*b.getY() - a.getY()*b.getX()); // parallelogram-specific }