You are on page 1of 4

De Anza College - CIS Name ___Daihua Ye______

35 A

Midterm

Total 80 points Time allowed: 120 minutes Aids allowed: Textbook / Notes 1. [10 points] (a) Write a class "Telephone" to describe Telephone. Assume that the characteris tics of telephone s we wish to model are: the number of buttons on the telephone the color (a string such as "green"). the serial number (a string). Wired or Wireless. All these characteristics should be set in the constructor, and "get" methods sh ould be provided for each of them. In addition, a method must be provided to ch ange the color. You must choose your own method names, parameter lists, and retu rn types. (b) Write statements (that might go in a main( ) method) to do these tasks: i) create a Telephone object that has 12 button, is red and has serial number 5K 959111. ii) paint the Telephone object created in (i) so that it is now blue. Answers: class Telephone { private int buttons; private String color; private String serialNumber; private String telephone_type; Telephone( int aButtons, String aColor, String aSerialNumber, String aTelephon e_type ) { buttons = aButtons; color = aColor; serialNumber = aSerialNumber; telephone_type = aTelephone_type; } protected int getButtons() { return buttons; } protected String getColor() { return color; } protected String getSerialNumber() { return serialNumber; } protected String getTelephone_type() { return telephone_type; } protected void changeColor( String newColor ) { this.color = newColor;

} public static void main(String[] args) { Telephone cellPhone = new Telephone( 12, "red", "5K959111", "wireless" ); cellPhone.changeColor( "blue" ); } } 2. [10 points] a. Given this code fragment: int sum = 0 ; for (int i = 1 ; i <= 5 ; i++) sum += i ; What is the value of sum, after the code is executed? Answer: The sum is 15. b. Given the code fragment: int c; c = 4.8 + 2; What is value of c? Answer: The value of c is 6. c. What are the data types of the following constants? 3.14f true Answer: first one is float; Second one is boolean. 3. [10 points] Give an example of overloading a static function. Answer: There are two static functions with the same name but different types pa rameters. The function area is to calculate rectangle s area by using length and wid th. static double areas( int length, double width) // length is integer, width is do uble { return length * width; } static double areas( double length, double width) // length and width both are double { return length * width; } 4. [10 points] a. Declare a variable name and assign it a value of John. Answer: String name = John ; b. What are the values of the following two expressions? In each line, assum e that: double double double double x y n m = = = = 10.0; 5.0; 2.0; 1.0;

a. x + n * y (x + n) * y Answer: -40.0 b. (m / n + m) % n Answer: 1.5

5. Write a function to find even numbers between 0 and 100, using while loop. (N ote: 0 is an even number). Answer: static void findEven() { int start = 0; int end = 100; while( start <= end ) { if( (start % 2) == 0 ) { System.out.printf("Even number is %d.\n", start); } start++; } return; } 6. When overriding constructors what rules if any you must follow? Answer: 1,constructors is defining in the subclass that has the same name, same argument s and same return type as the constructors in the super class. 2, The constructors must match the order and type of instance variable declared in the superclass. 3, A subclass constructors uses the keyword super to invoke the constructor of t he superclass. 7. In Java, is it OK for a class to have two methods with the same name? If yes, explain when you would have two methods with the same name in a class. Answer: Yes, when objects are required to perform similar tasks, but different i nput parameters are passed to it. 8. What is the output of the following Java program? class Acura { Acura (String s) { System.out.println("Acura " + s); } } class Honda { Honda (String s) { System.out.println("Honda " + s); } } public class Cars { public static void main(String args[]) { Acura integra = new Acura ("Integra"); Honda civic = new Honda ("Civic"); Honda accord = new Honda ("Accord"); } } Answer: Acura Integra

Honda Civic Honda Accord

You might also like