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: 669293 • 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

1)
>Inner classes nest within other classes.
>A normal class is a direct member of a package, a top-level class. Inner classes, which became available with Java 1.1
>Inner classes allow you to define one class inside another class – which is why they are called “inner” classes.
>A class can have member classes, just like how classes can have member variables and methods.
>There are many different types of inner classes they are:
1.Static member classes
2.Member classes
3.Local classes
4.Anonymous classes
>The most important feature of the inner class is that it allows you to turn things into objects that you normally wouldn't turn into objects. That allows your code to be even more object-oriented than it would be without inner classes.
>An inner class allows us to remove that logic and place it into its own class. So from an object-oriented point of view, we've taken functionality out of where it doesn't belong and have put it into its own class. Through the use of an inner class, we have successfully decoupled the search algorithm from the tree.
> From an organizational point of view, inner classes allow us to further organize our package structure through the use of namespaces. Instead of dumping everything in a flat package, classes can be further nested within classes.
>with Inner class we can do following
package 1
class 1
class 2
class 1
class 2
.......
class n
>An instance of an inner class can access members of an instance of the outer class, because an inner class is just another member of the outer class. And, inner classes can even access the private members of the outer class.
>An inner class does not violate encapsulation because of the fact that an inner class is authored by the same person who created the outer class.
Uses of Inner class:
1)Inner classes are used to get functionality which can be get as object better than method.
2)They can be used in case when a set of multiple operation are required and chances of reusability are good inside class and they have no use outside the class.
3)Inner classes are made to achieve multiple inheritance also.
4)Inner classes are used when they are useful in class context.
5)They are used to separate logic inside class.

3)

>A superclass reference can be converted into a subclass reference. The subclass is a special type of the superclass, hence the conversion is allowed.
Example:
SavingsAccount myAccount = new SavingsAccount(1000);
BankAccount anAccount = myAccount;

>A conversion from subclass to superclass is also allowed.
>For example when having a variable of type Object and it holds a BankAccount reference, typecasting can be used to do the conversion.
SavingsAccount myAccount = (SavingsAccount) anObject;

4)

Instructor class:

public class Instructor extends Person
{
private double salary;
public Instructor(String n, int byear, double s)
{
super(n, byear);
salary = s;
}
public String toString()
{
return "Employee[super=" + super.toString() + ",salary=" + salary + "]";
}

Person class:

public class Person
{
private String name;
private int birthYear;
public Person(String n, int byear)
{
name = n;
birthYear = byear;
}
public String toString()
{
return "Person[name=" + name + ",birthYear=" + birthYear + "]";
}
}

Student class:

public class Student extends Person
{
private String major;
public Student(String n, int byear, String m)
{
super(n, byear);
major = m;
}
public String toString()
{
return "Student[super=" + super.toString() + ",major=" + major + "]";
}
}

Tester class:

public class PersonTester
{
public static void main(String[] args)
{
Person a = new Person("praveen", 1995);
Student b = new Student("ramu", 1945, "computer science");
Instructor c = new Instructor("summit", 1994, 2500);
System.out.println(a);
System.out.println(b);
System.out.println(c);
}

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