You can assume that the value passed as a parameter

This preview shows page 7 - 11 out of 26 pages.

You can assume that the value passed as a parameter to this method will always be greater than 1. private boolean isPrime( int value ) { int i = 2; while ( i < value ) { if (value % i == 0) { return false; } i++; } return true; (5 marks) }
Image of page 7

Subscribe to view the full document.

- 8 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 4 (10 marks) Write a method called hideMiddle() that accepts a String as a parameter. The method returns a String which has the same length as the parameter String. The first and the last characters of the String which is returned by the method are the same as the first and last characters of the parameter String, but all the middle characters should be replaced by the character: ' * '. NOTE: If the hideMiddle() method is passed a parameter with length less than three characters then the parameter String is returned unchanged by the method. For example, when the start() method below is executed with the completed hideMiddle() method, the output would be: H***y b**y p****e public void start() { String word1 = hideMiddle(“Happy”); String word2 = hideMiddle(“busy”); String word3 = hideMiddle(“people”); System.out.println(word1 + " " + word2 + " " + word3); } Complete the hideMiddle() method below: private String hideMiddle( String word ) { String newWord = ""; int len = word.length(); if (len < 3) { return word; } newWord += word.charAt(0); for(int i=0; i<len-2; i++) { newWord += '*'; } newWord += word.charAt(len-1); return newWord; } (10 marks)
Image of page 8
- 9 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 5 (10 marks) Draw the output produced by the following code in the grid given at the bottom of the page. The grid lines are not part of the output but are there to help you place the drawing in the correct position. The size of each square in the grid is 10 pixels by 10 pixels. import java.awt.*; import javax.swing.*; public class DrawJPanel extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); draw(g, 30, 20, 10); } private void draw(Graphics g, int x, int y, int size){ g.setColor(Color.BLACK); g.drawLine(x, y+size*5, x+size*3, y); g.fillRect(x, y+size*2, size, size); g.drawOval(x+size, y, size, size); g.drawOval(x+size*2, y+size*2, size*2, size*2); g.drawString("Wow", x, y); } } (10 marks)
Image of page 9

Subscribe to view the full document.

- 10 - COMPSCI 101 Question/Answer Sheet ID: ..................................................................... CONTINUED Question 6 (12 marks) The JPanel defined on the next page contains four components: A JTextField which contains an integer indicating the number of weekday hours, A JTextField which contains an integer indicating the number of weekend hours, A JTextField which contains a String indicating the total pay. The "CALCULATE PAY" JButton . Below is a screenshot of the JPanel when it is first displayed. Whenever the user presses the "CALCULATE PAY" button, the total pay is calculated and displayed in the "Pay" JTextField . The pay rate for each weekday hour is $15 and the pay rate for each weekend hour is $25. You are required to complete the JPanel definition on the next page so that the JPanel behaves as described above. You must use the variables and constants given in the code.
Image of page 10
Image of page 11
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, English-language films, Following, Subroutine, ........., The Return

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