Information about the pattern the gap between each

This preview shows page 21 - 27 out of 28 pages.

Information about the pattern: The gap between each filled square in all the rows is given by the constant, GAP . The gap between each row is given by the constant, GAP . The left position of the first filled square in each row is given by the constant, GAP . The size of each filled square is given by the constant, SIZE . Each row contains one filled square less than the row above it. The filled squares are all drawn in black. Adding an instance of the Q9JPanel (with the completed drawPattern() method) to a JFrame produces the following JFrame. The output JFrame shown below is obtained by making the call: drawPattern (g, 9, 4); to the drawPattern () method. The output JFrame shown below is obtained by making the call: drawPattern (g, 7, 2); to the drawPattern () method.
Image of page 21

Subscribe to view the full document.

Question/Answer Sheet - Page 22 - CompSci 101 SS C SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED import java.awt.*; import javax.swing.*; public class Q9JPanel extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); drawPattern(g, 11, 5); } private void drawPattern(Graphics g, int numFirst, int numLines){ final int SIZE = 30; // size of each filled square final int GAP = SIZE/2; // gap between squares int numInRow = numFirst; int y = GAP; int x; for(int row=0; row<numLines; row++) { x = GAP; for(int col=0; col<numInRow; col++) { g.fillRect(x, y, SIZE, SIZE); x += GAP + SIZE; } y += GAP + SIZE; numInRow--; } } } (8 marks)
Image of page 22
Question/Answer Sheet - Page 23 - CompSci 101 SS C SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 10 (8 marks) Consider the Meddle class which is defined below: public class Meddle { private static int number; private int num; public Meddle(int n){ num = n; number = number - num; } public void change(int n){ num = num + n; number = number + num; } public String toString(){ return "number: "+number + ", num: " + num; } public static void setNumber(int newN){ number = newN; } } The application Q10 below uses the Meddle class: public class Q10 { public static void main(String[] args) { Meddle.setNumber(20); Meddle a = new Meddle(3); a.change(4); System.out.println("1. a: "+ a.toString()); Meddle b = new Meddle(5); System.out.println("2. b: "+ b.toString()); a.change(4); System.out.println("3. b: "+ b.toString()); } }
Image of page 23

Subscribe to view the full document.

Question/Answer Sheet - Page 24 - CompSci 101 SS C SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Show the output when the Q10 application is executed: > java Q10 1. a: number: 24, num: 7 2. b: number: 19, num: 5 3. b: number: 30, num: 5 (8 marks)
Image of page 24
Question/Answer Sheet - Page 25 - CompSci 101 SS C SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 11 (10 marks) What is the output when the Q11 application shown below is executed? public class Q11 { public static void main(String[] args) { String str = "OKAY"; if (method1(str)) str = method2(str, 1); else str = method2(str, 2); System.out.println("1. " + str); str = "2"; method3(str, 5); } private static boolean method1(String s){ int num = s.length(); System.out.println("2. " + s); if (num%2 == 0) return true; return false; } private static String method2(String s, int num1){ int num2 = s.length(); int pos = num2 - num1; System.out.println("3. " + pos); return s.substring(0, pos); } private static void method3(String s, int num){ System.out.println("4. " + s); String str = s + num; System.out.println("5. " + method4(str)); } private static int method4(String s){ System.out.println("6. " + s); int num = Integer.parseInt(s); return num-1; } }
Image of page 25

Subscribe to view the full document.

Question/Answer Sheet - Page 26 -
Image of page 26
Image of page 27
You've reached the end of this preview.

{[ 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