Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Computer Programming II (CS141) Q.1. Why would you use an inner class instead of

ID: 668379 • Letter: C

Question

Computer Programming II (CS141)

Q.1.

Why would you use an inner class instead of regular class?

Q.2.

What does this code print? Why is it polymorphic?

DataSet data = new DataSet();

data.add(new BankAccount(1000));

data.add(new Coin(0.1, "dime"));

System.out.println(data.getAverage());

Q.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.

Q.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 declarations, the constructors, and the methods

toString for all classes. Supply a test program that tests these classes and methods.

Q.5.

Implement the compare method of the following class RectangleComparator. The method compares two rectangles. The method should return:

A positive integer if the area of the first rectangle is larger than the area of the second rectangle

A negative integer if the area of the first rectangle is smaller than the area of the second rectangle

0 if the two rectangles have the same area.

import java.util.Comparator;

import java.awt.Rectangle;

public class RectangleComparator implements Comparator<Rectangle>

`{

/**

Compares two Rectangle objects.

@param r1 the first rectangle

@param r2 the second rectangle

@return 1 if the area of the first rectangle is larger than the area of

the second rectangle, -1 if the area of the first rectangle is smaller than the area of the second rectangle or 0 if the two

rectangles have the same area

*/

public int compare(Rectangle r1, Rectangle r2)

{

...

}

}

Explanation / Answer

Hi,

I answered your questions in step by step format.

Step 1: Question

Why would you use an inner class instead of regular class?

Step 1.1: solution:

1) Inner Class have member class.

         i) member class: They are declared outside a function.

2) Inner class have local class.

         i) These are classes which are declared in the body of a function. They can only be referred to in the rest of the function. They can use local variables and parameters of the function.

3) Inner class also called as nested class but not in regular class.

4) Inner class are declared entirely within the body of another class or interface but in regular class cann't declared.

Step 2:     question

What does this code print? Why is it polymorphic?

DataSet data = new DataSet(); //line 1

data.add(new BankAccount(1000)); //line 2

data.add(new Coin(0.1, "dime")); //line 3

System.out.println(data.getAverage()); //line 4

Step 2.1: Solution

DataSet data = new DataSet(); // In this line new object will be created and call to DataSet() function.

data.add(new BankAccount(1000)); // Add the new bank account 1000

data.add(new Coin(0.1, "dime"));   // Again add new coine details and print dime.

System.out.println(data.getAverage()); /* In this line print the average value and call to getAverage() method from the function */

Output:

//line 4 system.out.println();

// print the value of exactly what we give in line 4

// in line 4 data.getaverage() means get the mean value.

ii) why it is polymorphic?

Polymorphic : More than one ability to transform.

So above program that contains bank details, bank account, bank deposit money, like wise it can change it from one to another form. Hence it is called polymorphic

Step 3: Question

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.

Step 3.1: Solution

Yes, it's possible to convert a subclass reference into a superclass reference. but not possible to convert super class reference to sub class reference.

Reason:

             1) sub class has an 'is a' relationship with its superclass. This means that a sub class is a special kind of its super class. When we talk in terms of objects, a sub class object can also be treated as a super class object. And hence, we can assign the reference of a sub class object to a super class variable type. However, the reverse is not true.

A reference of a super class object may not be assigned to a sub class variable.

Step 4: Question

Q.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 declarations, the constructors, and the methods

toString for all classes. Supply a test program that tests these classes and methods.

Step 4.1 Solution

Program:

Attributes are needed: One super class name : person

                                   Two sub class name : Student and instructor

//program:

Class Person

{

Char name;

int dob;

Void details()

{

System.out.println("Name:",+name);

System.out.println("DOB:",+dob);

}

}

Class Student extends person

{

char subject;

void stud_details()

{

void details();

System.out.println("Print major subject:"+subject);

}

public static void main(String args[])

{

student s=new student();

s.print();

}

class instructor extends person

{

int salary;

void ins_details()

{

void details();

system.out.println("enter salary:" +salary);

}

public static void main(String args[])

{

instructor i =new instructor();

i.print();

}

// As per your requirement i code it above

Step 5: Question

Q.5.

Implement the compare method of the following class RectangleComparator. The method compares two rectangles. The method should return:

Step 5.1: Solution

public int compare(Rectangle r1, Rectangle r2)

{

}

}

Note ( the value 0 if r1 is equal to r2; a value less than 0 if r1 is less than r2; and a value greater than 0 if r1 is greater than r2.

Extra Solution:

If you want extra code for execution then include this code in public int compare(Rectangle r1, Rectangle r2){}

int r1 = 324;

int r2 = 234;

if(r1 > r2)

{

System.out.println(r1 + " is greater than " + r2);

}

else if(r1 < r2){

System.out.println(r1 + " is less than " + r2);

}

else{

System.out.println(r1 + " is equal to " + r2);

}

This will extra solution:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote