Continued question 9 10 marks the following applet

This preview shows page 12 - 15 out of 18 pages.

CONTINUED Question 9 (10 marks) The following applet uses a Thing class. It creates two Thing objects and draws them. Whenever the user clicks the button labelled " Swap ", the bottom shape moves to the position of the top shape, and vice versa, as shown in the screen shots below. The screen shot on the left shows what the applet looks like when it initially starts, and the screen shot on the right shows what happens when " Swap " is clicked. You need to complete the code for the Thing class. The source code for the applet class is given below: import java.awt.*; import java.applet.*; import java.awt.event.*; public class Q9 extends Applet implements ActionListener { private Button bSwap; private Thing shape1, shape2; public void init() { bSwap = new Button("Swap"); add(bSwap); shape1 = new Thing(30, 80, 50, Thing.SQUARE); shape2 = new Thing(30, 150, 50, Thing.CIRCLE); bSwap.addActionListener(this); } public void paint(Graphics g) { shape1.draw(g); shape2.draw(g); } public void actionPerformed(ActionEvent e) { if (e.getSource() == bSwap) shape1.swapPosition(shape2); repaint(); } }
Image of page 12

Subscribe to view the full document.

Question/Answer Sheet - Page 13 - CompSci 101 FC SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Complete the Thing class below. You are required to write the parameters for the constructor and methods, write the return type of the methods, and complete the constructor and methods. import java.awt.*; public class Thing { public static final int SQUARE = 0; public static final int CIRCLE = 1; private int posX; // x position of the centre of the shape private int posY; // y position of the centre of the shape private int size; private int shape; public Thing( int x, int y, int sz, int sh ) { posX = x; posY = y; size = sz; shape = sh; } public void swapPosition( Thing other ) { int tempX = posX; int tempY = posY; posX = other.posX; posY = other.posY; other.posX = tempX; other.posY = tempY; } public void draw(Graphics g) { if (shape == SQUARE) g.fillRect(posX-size/2, posY-size/2, size, size); else g.fillOval(posX-size/2, posY-size/2, size, size); } (10 marks) }
Image of page 13
Question/Answer Sheet - Page 14 - CompSci 101 FC SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 10 (10 marks) In an online supermarket shopping system the user is able to type in their requirements as a list of either: quantity and item pairs; or weight and item pairs. A typical list might look like the one below: 1 Loaf of white bread 3 Tins of tomatoes 5kg Rua potatoes 500g Mushrooms You need to complete the splitShoppingLine() method below to split a single line into its quantity (or weight) and the item description. Note that the first space character on a line is the divider between a quantity (or weight) and the item description. There will only ever be one space dividing these two portions of the string. You should ensure that both sub-strings are converted to upper case when they are split up. The two instance variables ( quantityOrWeight and itemDescription ) should contain the extracted sub-strings when the method completes. Between 3 and 5 lines of code are sufficient to answer this question.
Image of page 14

Subscribe to view the full document.

Image of page 15
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, ........., University Of Auckland

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern