HomeWork02 Solution

10
Homework 2 Note: The numbers in parentheses refer to the “Review Exercises” and “Programming Exercises in Big Java 2 nd Edition. Problem 1, R9.13 (2 nd ed., R11.16) Consider this top-level and inner class. Which variables can the f method access? public class T { public void m(final int x, int y) { int a; final int b; class C implements I { public void f() { . . . } } final int c; . . . } private int t; } Answer: x, b, t. This is because C is a “Local Inner Class” (an inner class defined inside the body of a method) and as such has access only “to the final variables in the current code block and all the members of the enclosing class.” (Eckel, 2002). Problem 2, R9.14 (2 nd ed., R11.17) COMP 121 Homework 2 1

description

Java Homework solution

Transcript of HomeWork02 Solution

Page 1: HomeWork02 Solution

Homework 2

Note: The numbers in parentheses refer to the “Review Exercises” and “Programming Exercises in Big Java 2nd Edition.

Problem 1, R9.13 (2nd ed., R11.16)

Consider this top-level and inner class. Which variables can the f method access?

public class T{ public void m(final int x, int y) { int a; final int b;

class C implements I { public void f() { . . . } }

final int c; . . . }

private int t;}

Answer: x, b, t. This is because C is a “Local Inner Class” (an inner class defined inside the body of a method) and as such has access only “to the final variables in the current code block and all the members of the enclosing class.” (Eckel, 2002).

Problem 2, R9.14 (2nd ed., R11.17)

What happens when an inner class tries to access a non-final local variable? Try it out and explain your findings.

You’ll get a compile-time error message saying that the local variable can’t be accessed from within the inner class unless it is declared final.

COMP 121 Homework 2 1

Page 2: HomeWork02 Solution

Problem 3, R10.1 (2nd ed., R13.1)

What is the balance of b after the following operations?

SavingsAccount b = new SavingsAccount(10); // Balance is 0b.deposit(5000); // Balance is 5000b.withdraw( b.getBalance() / 2 ); // Balance is 2500b.addInterest(); // Balance is 2750

b=2750

Problem 4, R10.3 (2nd ed., R13.3)

Can you convert a superclass reference into a subclass reference? A subclass reference into a superclass reference? If so, give examples. If not, explain why not.

You can convert a subclass reference to a superclass reference (sometimes referred to as upcasting). Therefore, a reference to a SavingsAccount object can be converted to a BankAccount reference:

SavingsAccount collegeFund = new SavingsAccount(10);BankAccount anAccount = collegeFund;

Furthermore, all references can be converted to the type Object:

Object anObject = collegeFund;

On occasion, you will need to carry out the opposite conversion, from a superclass reference to a subclass reference (also called downcasting). For example, you may have a variable of type Object, and you know that it actually holds a BankAccount reference. In that case, you can use an explicit cast to convert the type:

BankAccount anAccount = (BankAccount) anObject;

However, this cast is somewhat dangerous. If you are wrong, and anObject actually refers to an object of an unrelated type, then a run-time exception is thrown.

COMP 121 Homework 2 2

Page 3: HomeWork02 Solution

Problem 5, R10.6 (2nd ed., R13.6)

Draw an inheritance diagram that shows the inheritance relationships between the classes:

Person Employee Student Instructor Classroom Object

COMP 121 Homework 2 3

Object

Person

Student Employee

Classroom

Instructor

Page 4: HomeWork02 Solution

Problem 6, P10.4 (2nd ed., P13.4)

Implement a superclass Person. Make two classes, Student and Instructor that inherit from Person. A person has a name and a year of birth. A student has a major, and an instructor has a salary. Write the class definitions, the constructors, and the methods toString for all classes. Supply a test program that tests these classes and methods.

package hw2.p10_4;

public class Person {

private String name;

private int birthYear;

/** * Constructs a Person object. * @param name Name of the Person * @param birthYear Year of birth */ public Person(String name, int birthYear) {

this.name = name; this.birthYear = birthYear; }

/** * @return Returns the name. */ public String getName() {

return name; }

/** * @return Returns the birthYear. */ public int getBirthYear() {

return birthYear; }

/** * Returns a string representation of the object. * @see java.lang.Object#toString() */ public String toString() {

return "Person[name=" + name + ",birthYear=" + birthYear + "]"; }}

COMP 121 Homework 2 4

Page 5: HomeWork02 Solution

package hw2.p10_4;

public class Student extends Person {

private String major;

/** * Constructs a Student object. * @param name * @param birthYear * @param major */ public Student(String name, int birthYear, String major) {

super(name, birthYear); this.major = major; }

/** * @return Returns the major. */ public String getMajor() {

return major; }

/** * Returns a string representation of the object. * @see hw2.p10_4.Person#toString() */ public String toString() {

return "Student[super=" + super.toString() + ",major=" + major + "]"; }

}

COMP 121 Homework 2 5

Page 6: HomeWork02 Solution

package hw2.p10_4;

public class Instructor extends Person {

private double salary;

/** * Constructs an Instructor object. * @param name Name of the Instructor * @param birthYear Year of birth * @param salary Instructor's salary */ public Instructor(String name, int birthYear, double salary) {

super(name, birthYear); this.salary = salary; }

/** * @return Returns the salary. */ public double getSalary() {

return salary; }

/** * Returns a string representation of the object. * @see hw2.p10_4.Person#toString() */ public String toString() {

return "Instructor[super=" + super.toString() + ",salary=" + salary + "]"; }

}

/** * Description: test class for the Person, Student, and Instructor classes. * * @author Mihajlo Jovanovic * @version 1.0 (May 28, 2008) */package hw2.p10_4;

public class P10_4 {

public static void main(String[] args) { Person person = new Person("Kelso", 1945); Student student = new Student("John", 1977, "Internal Medicine"); Instructor instructor = new Instructor("Perry", 1956, 200000.0); System.out.println(person); System.out.println(student); System.out.println(instructor); }}

COMP 121 Homework 2 6

Page 7: HomeWork02 Solution

References:

Eckel, B. (2002). Thinking in Java. In Chapter 8: Interfaces & Inner Classes. Prentice Hall. Retrieved May 27, 2008 from http://www.mindviewinc.com/Books/

COMP 121 Homework 2 7