Anda di halaman 1dari 10

M257 TMA

Table of content PREFACE ...........................................................................................................................................1 Question 1 [25 marks] .........................................................................................................2 1.1 What is the difference between a constructor and a method? (6 marks)2 1.2 What are the different types of access modifiers available in Java? Give brief definitions. (8 marks) ..........................................................................................................2 1.3 What gets printed when the following code is compiled and run? Try this code in NetBeans and include a screenshot in your answer. (3 marks) ........................3 1.4 Answer the following in your OWN WORDS: (8 marks) ...............................3 Question 2 [30 marks] .........................................................................................................3 Question 3 [35 marks] .........................................................................................................6 Question 4 [10 marks] ...................................................................................................... 10

PREFACE Your marks will divided through doing these: Your Java code style. Good naming of identifiers, layout and use of comments. Add a comment at the top of each code page with your own name as the author. In NetBeans make the following:

A copy of the code in your solution word document. 2 to 4 screenshots of the code and the output in NetBeans. The screenshots should include evidence that you have written the code on your own computer (e.g. your Windows username from the start menu is visible). Named as follows: YourID-YourFullName.docx

Question 1 [25 marks] 1.1 What is the difference between a constructor and a method? (6 marks) The comparison Constructor Method

1.2 What are the different types of access modifiers available in Java? Give brief definitions. (8 marks)

1.3 What gets printed when the following code is compiled and run in NetBeans. (3 marks) for(int i = 0; i < 2; i++) for(int j = 2; j>= 0; j--) { if(i == j) break; System.out.println("i=" + i + " j="+j); } 1.4 Answer the following in your OWN WORDS: (8 marks) 1.4.1 Threaded classes can be defined in two ways: inheriting from a particular class or implementing a special interface. Give the names of the particular class and special interface referred to.

1.4.2 When should we use each of the above two ways of creating a thread?

1.4.3 What is the problem of shared objects in Java? And how can it be solved?

Question 2 [30 marks] 2.1 Develop an interface PointInterface that includes two methods: (3 marks) public void reset(): it resets the values of the instance parameters to their default values. public double distance(Point p): it returns the distance between two points of the xyplane. For example, p1.distance(p2) returns the distance between the two points p1 and p2.

Hints: The distance d between (x1, y1) and (x2, y2) is given by:

Java static method Math.sqrt(v) can be used to get the square root of v. 2.2 Develop a public class Point to the following specifications: (7 marks) The class implements PointInterface. The class has two public instance variables: int x, and int y. The default value for y is 100, and for x is any value of your choice between 102 and 398. The class has a no-argument constructor that sets the values of x and y to their default values. The class has a two-argument constructors that sets the values of x and y to given values. The class implements any additional necessary methods, if applicable. 2.3 Develop a public class Pixel to the following specifications: (15 marks) The class is a subclass of Point. The class has a private instance variable Color color. Assume the default value for color is your preferred color. The class has a no-argument constructor that sets the values of all instance variables to their default values. The class has a three-argument constructor that sets the values of x, y, and color to given values.

The class overrides reset() method in order to reset the color as well. The class should implement any additional necessary methods, if applicable. The class overrides equals(Object o) in java.lang.Object to compare the instance values of two Point objects and return true if they are equal, and false otherwise. The class has a method public void loadPos(String filename) which is used to read the values of x and y as integers from a file stored on the hard drive with the name of filename. Use Scanner class for reading. Handle any unchecked exceptions using atrycatch statement. The class has a method public void savePos(String filename) which is used to save the current values of x and y to a file stored on the hard drive with the name of filename. Use PrintWriter class for writing. Handle any unchecked exceptions using trycatch statement. Hint: a sample text file coord.txt is included with this TMA to show you the content and structure of the file to be read or saved. 2.4 Develop a class PixelTest to test the classes in 2.1. The class PixelTest will only have the main method that should: (5 marks) Creates two Pixel instances: p1 using the three-argument constructor. Assume any values for x, y, and color. p2 using the no-argument constructor. Loads the values for x and y from the file coord.txt for p2. The file coord.txt must be stored in MyDocuments folder (for Windows XP) or Documents folder for (Windows 7). The complete path must be included in your loadPos() method call.

Saves the values of x and y of p1 to the same coord.txt file described above. Print on the screen the distance between p1 and p2. Resets the values of p1. Print on the screen the values all instance variables of p1. Print on the screen whether p1 and p2 have equal values for their instance variables.

Question 3 [35 marks] Develop a class MyFrame that extends JFrame class and has a main method to instantiate the class. The design and operation of the program are described as follows: Design: An illustration to the design of MyFrame is given in Figures 2 & 3 on the next page, where most details are given. However, the complex shape shown in red color needs further explanation. All parts of the complex shape (e.g. rectangle, circle, etc) are drawn with respect to a reference pixel which is located at the top-left corner of the shape (seen Figure 1). Assuming the reference pixel is located at (x,y), the top left corner of the rectangle, for example, would be located at (x+c, y+b), where a, b, c, etc are constant values

(e.g. a = 15). The line, circle, and string could be painted in a similar way. It is left to the student to choose the values of a, b, c, d, e, and f.

The reference pixel should be coded in your program as an instance of Pixel class described in Question 2, with its x and y values initialized with the Pixel default values. Also, the color of the complex shape is set according to the value of the variable color of the Pixel instance.

Operation: After the main method is invoked, the user could do one of the following: 1. Click on one or more of the checkboxes on the left to show/hide the relevant parts (see Figure 3). Whenever a checkbox is clicked, the repaint() method should be invoked.

Hints: - All checkboxes should have the same action listener attached. The action listener should repaint the whole shape whenever a checkbox is clicked. - The program should use the method isSelected() within thepaint() method to determine which checkbox is checked, and hence draw only the selected parts of the shape.

2. Click on the five buttons at the bottom of the JFrame which do the following: o Move left: moves the complex shape 5 pixels to the left. o Move right: moves the complex shape 5 pixels to the right. o Reset coordinates: moves the complex to its default (x, y) location. o Save current coordinates: saves the current coordinates of the complex shape to the file coord.txt described in Question 2. This could be done by calling an appropriate method from Pixel instance. o Load last saved coordinates: reads the (x, y) coordinates from the file coord.txt described in Question 2, and then reposition the complex shape to the loaded values. This could be done by calling an appropriate method from Pixel instance.

Hints: - All five buttons should have the same action listener attached. The method getSource() should be used to identify which button is clicked, and hence execute the appropriate code. - All above actions are performed by modifying the state of the Pixel instance. For instance, Move left could be achieved by incrementing the x value of the Pixel instance by 5. After adjusting the state of the Pixel instance, the repaint() method should be invoked.

Question 4 [10 marks] 4.1 Using the AOUs e-Library facility and the internet, locate the article indicated below and answer the following in your own words: Source: Shaofeng, Wang "A method of Java-based electronic document publishing system", The Electronic Library, Vol.22, Iss.4, pp.351, 2004. 4.1.1 What JEPS stands for? 4.1.2 What is the main use of JEPS? 4.1.3 From the conclusions section of the paper, briefly explain the main goal and functions of JEPS.