OBJECT ORIENTED PROGRAMMING PRELAB #8

oatmealbrothersΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 6 μήνες)

70 εμφανίσεις

1



Faculty of Engineering and Architecture

Computer Engineering Department

COM 102


OBJECT ORIENTED PROGRAMMING

PRELAB
#8

Academic Year:
Spring 2011
-
12

Submission Date and Hour:
12
.04
.2012
-

12:00

Course Instructor:
Hürevren KILIÇ

Course Assistant:
Gökhan

AKYOL

MAKE A MATCH (10 points)

Read Chapter 10

of your textbook and match the terms and descriptions given below.

1

downcasting

A

We use this operator in order to be sure that given
object is a subclass object of its superclass.


2

late binding

B

Type
resolution at compile
-
time is a
n example of


3

abstract

C

The other name for dynamic binding.

4

String

D

In some cases, declaring a class as final increases

5

instanceof

E

A technique to invoke subclass methods that
do not exist

in the superclass.

6

italic

F

A
n example
final class
.

7

easily extensible

G

The keyword used to indicate an abstract class.

8

static binding

H

Polymorphism provides designing and implementing
such systems

9

incomplete

I

In UML, the name of abstract classes are written in

10

security

J

Abstract classes are




2


FILL IN THE BLANKS
(24

points)

1.

A final class _________ be a super
class.

2.

Polymorphism deals with generality but not ___________.

3.

_____________ promotes extensibility and supports one or more alternative forms of
behavior
.

4.

_________ object is an object of its
superclass.

5.

If we try to instantiate an abstract class object, we get ________________
.

6.

__________ software systems can be designed and implemented based on the concept of
polymorphism
.

7.

W
e __________ superclass methods to obtain concrete subclasses.

8.

Keeping an ______
__
__ of objects
and processing each element polymorphically in an
enhanced
-
for loop structure is an example of good software engineering practice.

9.

When we apply

a downcasting
operation, possible non
-
existing is
-
a relation between the
specified types in the cast operator results in getting ______________________ exception
error.

10.

Trying to assign _____________ reference to a subclass variable causes compilation error.

11.

A final
method
in a
super _______

cannot be overridden

in a sub ________
.



3


SHORT ANSWER QUESTIONS
(
5 x 5
=
2
5

points)

Answer the following questions in at most two or three sentences.

1.

What is the difference between dynamic and static binding
?

Explain it, shortly.







2.

Abstract classes cannot be used to instantiate
objects. Why is it so? Give a brief
explanation for this.







3.

Is it possible to extend an abstract subclass from a concrete superclass
?
Why and how?
or Why not?
Explain your answer
briefly.







4


4.

Explain the sentence “
Constructor methods cannot be declared abstract
”, briefly.








5.

In Java, how is it possible for an object to know its own class?

Explain it.






5


PROGRAMMING OUTPUT
(24 points)

public abstract
class Employee {


private String firstName;


private String lastName;


private String socialSecurityNumber;


public Employee(String first, String last, String ssn) {



firstName = first;



lastName = last;



socialSecurityNumber = ssn;


}


public void setF
irstName(String first) {



firstName = first;


}


public String getFirstName() {



return firstName;


}


public void setLastName(String last) {



lastName = last;


}


public String getLastName() {



return lastName;


}


public void setSocialSecurityNumber(
String ssn) {



socialSecurityNumber = ssn;


}


public String getSocialSecurityNumber() {



return socialSecurityNumber;


}


public String toString() {

return String.format(“%s %s
\
nsocial security number:
%s”,getFirstName(), getLastName(),
getSocialSecurityNumber());

}





public class SalariedEmployee extends Employee {


private double weeklySalary;


public SalariedEmployee(String first, String last, String ssn, double salary) {



super(first, last, ssn);



setWeeklySalary(salary);


}


pu
blic void setWeeklySalary(double salary) {



weeklySalary = salary < 0.0 ? 0.0 : salary;


}


public double getWeeklySalary() {



return weeklySalary;


}


public double earnings() {



return getWeeklySalary();


}


public String toString() {

return String
.format(“Salaried employee:%s
\
n%s: $%, .2f”,
super.toString(), “weekly salary”, getWeeklySalary());

}

6


}

public class CommissionEmployee extends Employee {


private double grossSales;


private double commissionRate;

public CommissionEmployee(String first,
String last, String ssn,


double sales, double rate) {



super(first, last, ssn);



setGrossSales(sales);



setCommissionRate(rate);


}


public void setCommissionRate(double rate) {



commissionRate = (rate > 0.0 && rate < 1.0) ? rate : 0.0;


}


public d
ouble getCommissionRate() {



return commissionRate;


}


public void setGrossSales(double sales) {



grossSales = (sales < 0.0) ? 0.0 : sales;


}


public double getGrossSales() {



return grossSales;


}


public double earnings() {



return getCommissionRat
e() * getGrossSales();


}


public String toString() {



return String.format(“%s: %s
\
n%s: $%, .2f; %s: %.2f”,




“commission employee”, super.toString(),




“gross sales”, getGrossSales(),




“commission rate”, getCommissionRate());


}

}









7


1.

What is output by the following code segment? Assume that the code appears in the main
method of
the

above
application
.



SalariedEmployee emp1 =

new SalariedEmployee(“Tom”, “Jones”, “123
-
45
-
789”, 1000,00);


CommissionEmployee emp2 =



new
CommissionEmployee(“Eric”, “Clapton”, “23
-
34”, 15000,00, 0,10);


System.out.printf(“Employee 1:
\
n%s
\
n
\
n”, emp1);


System.out.printf(“Employee 1:
\
n%s
\
n
\
n”, emp2);





















2.

What is output by the following code segment? Assume that the code appears in the main
method of
the

above
application
.



Employee firstEmployee =

new SalariedEmployee(“Tom”, “Jones”, “123
-
45
-
789”, 1000,00);


Employee secondEmployee =



new
CommissionEmployee(“Eric”, “Clapton”, “23
-
34”, 15000,00, 0,10);

System.out.printf(“Employee 1:
\
n%s
\
n
\
n”, firstEmployee);


System.out.printf(“Employee 1:
\
n%s
\
n
\
n”, secondEmployee);



















8


3.

What is output by the following code segment? Assume that
the code follows the statements in
Programming Output Exercise 2
.


SalariedEmployee salaried = (SalariedEmployee) firstEmployee;

System.out.printf(“salaried:
\
n%s
\
n”, salaried);

















4.

What is output by the following code segment? Assume that the code follows the statements in
Programming Ou
tput Exercise 3
.


CommissionEmployee commission = (CommissionEmployee) firstEmployee;

System.out.printf(“commission:
\
n%s
\
n”,
commission
);






9


CORREC
T THE CODE

(16

points)

Determine if there is an error in each of the following program segments, find the errors and write the
correct ones. If there is no error in the following codes, write “No Error”.

1.


public class Shape {


public void A() {



System.out.println(“Shape::A()”);


}

}

public class Triangle extends Shape {


public void A() {



System.out.println(“Triangle::A()”);


}


public void B() {



System.out.println(“Triangle::B()”);


}

}

public class Test {


public static void main(String[] a
rgs) {



Shape[] sArray = new Shape[2];



sArray[0] = new Triangle();



sArray[1] = new Shape();



sArray[0].A();



sArray[0].B();



sArray[1].A();



sArray[1].B();


}

}









2.


public abstract class Employee {


private String name;


private int salary;


public double raise();

}


public class Manager extends Employee {


public double raise() {



return super.salary;

}







GOOD LUCK