Consider the following applet facetester which uses

This preview shows page 14 - 16 out of 16 pages.

Consider the following applet, FaceTester , which uses this Face class. The init() method is incomplete. Read the questions which follow this code for instructions on how to complete the init() method. import java.awt.*; import java.awt.event.*; import java.applet.*; public class FaceTester extends Applet { private Face face1, face2, face3, face4, face5; public void init() { face1 = new Face(50); face2 = new Face(100); face3 = new Face(150); face4 = new Face(100); face5 = face2; areTheyEqual(); } public void paint(Graphics g) { face1.draw(g); face2.draw(g); face3.draw(g); } public void areTheyEqual(){ if (face2.equals(face4)) System.out.println("Same Face."); else System.out.println("Different Face."); if (face2.equals(face5)) System.out.println("Same Face."); else System.out.println("Different Face."); } }
Image of page 14

Subscribe to view the full document.

Part A (5 marks) The FaceTester applet declares the following 5 instance variables of type Face : private Face face1, face2, face3, face4, face5; Complete the init() method of the FaceTester applet (in the space provided on the previous page) by writing code to construct 4 Face objects. You can use any of the constructors provided in the Face class on page 12. The faces you construct must have the correct sizes, and must be assigned to the variables as specified below: a) face1 – a face with diameter 50, b) face2 – a face with diameter 100, c) face3 – a face with diameter 150, d) face4 – another face with diameter 100. The reference variable face2 should now be pointing at a Face object. e) Include code in the init() method to make the variable face5 refer to the same Face object that face2 refers to. (5 marks) Part B (5 marks) Now that the 5 variables face1, face2, face3, face4 and face5 refer to Face objects, what is the output of the method areTheyEqual() which is called from the init() method? (5 marks)
Image of page 15
Question 9 (10 marks) Complete the printLengths() method below which is passed a Vector of String s as parameter and prints out the length of every String in the Vector on a separate line. public void printLengths(Vector v) { int i = 0; while (i < v.size()) { String s = (String)(v.elementAt(i)); System.out.println(s.length()); i++; } } For example, consider the Vector words , which is created as follows : Vector words; words = new Vector(); words.addElement("success"); words.addElement("is"); words.addElement("a"); words.addElement("matter"); words.addElement("of"); words.addElement("luck"); words.addElement("ask"); words.addElement("any"); words.addElement("failure"); If this Vector is passed to the method printLengths() as below: printLengths(words); the output should be as follows: (10 marks)
Image of page 16
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • public void, html file, Faces, dayofweek

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