



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




This 90-minute exam has 6 questions (numbered 0..5) worth a total of 100 points. Spend a few minutes looking at all questions before begin- ning so that you can see what is expected. Budget your time wisely. Use the back of the pages if you need more space. Question 0 (2 points). Write your netid and your name, legibly, at the top of each page (Hint: do it now). Gries’s netid is djg17. Question 1 (16 points). (a) What is the difference between a static method and a non-static method? (b) Name three types of variables and their scopes. (c) What is a parameter? (d) For each of the following expressions, write its value beside the expression. If the evaluation leads to an error, write “error” instead. false && (33/0 == 1) (“5” + 9)/ true && (4 > 5) 0 ___________ out of 02 1 ___________ out of 16 2 ___________ out of 18 3 ___________ out of 22 4 ___________ out of 22 5 ___________ out of 20 Total ________ out of 100
Question 2 (18 points): At the bottom of the page are two class definitions. Draw one folder (object, instance) of class Politican and then a folder of class Governor. You need not include the partition for superclass Object. public class Politician { private String name= null ; // politician’s name /** = “p1 and p2 are the same folder” / public static boolean areEqual (Politician p1, Politi- cian p2) { return p1 == p2; } /* Constructor: politician named n / public Politician (String n) { name= n; } /* Set this politician’s name to n / public void setName (String n) { name= n; } /* = this politician’s name / public String getName () { return name; } /* = the String representation of this Politician / public String toString () { return “ politician “ + getName (); } } public class Governor extends Politician { private String state= null ; // the governor’s // state (e.g. NY) /* Constructor: a governor of state s, named n / public Governor (String n, String s) { super (n); state= s; } /* = this governor’s state / public String getState () { return state; } /* Set this governor’s state to s / public void setState (String s) { state= s; } /* = a String representation of this instance */ public String toString () { return super .toString () + “ the governor of “ + getState (); } }
Question 4 (20 points). This question refers to class Politician of Question 2. (a) Write a subclass President of Politician, according to the following specifications: (1) Class President contains a field for the number of months in office. (2) The constructor of the class should let one specify the President’s name and the number of months in office. (3) It should have a method that returns the term of the President. ie. this method returns 1 if the President has been in office less than 4 years and 2 otherwise. (4) The class should have a method “toString()” that returns more information than the default toString() method —Anything reasonable is fine.
Question 4 part (b) Complete the code below to make an appropriate JUnit test-case method to test your constructor, the method specified in part (3) of 4a, and toString(). public void testPresident() { }