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

Question 1. 1. (TCO 8) Briefly describe best practices as it relates to naming c

ID: 3851416 • Letter: Q

Question

Question 1.1. (TCO 8) Briefly describe best practices as it relates to naming classes. (Points : 18)

Question 2.2. (TCO 2) Sometimes the terms Encapsulation and Data/Information Hiding in object-oriented programming are used interchangeably. Can you give an example, which shows that they are actually different? (Points : 18)

Question 3.3. (TCO 2) Given the following list of classes, attributes and methods,

·         identify which items are classes, which items are attributes and which items are methods;
·         identify which class each attribute and method belongs to; and
·         suggest a class hierarchy given your list of classes.

*Note - no particular capitalization scheme is used in the list below to differentiate between classes, methods, and attributes.

Brew, DecreaseTemperature, Manufacturer, MinCups, Price, TurnOff, Oven, MaxTemperature, BrewStrength, NumberOfRacks, IncreaseTemperature, TurnOn, StartTimer, CoffeeMaker, KitchenAppliance, MaxCups, YearBuilt, Grind (Points : 18)

Question 4.4. (TCO 7) What are the differences between a normal class and an abstract class? Provide an example of an abstract class. (Points : 18)

Question 5.5. (TCO 4) What is inheritance and what are the major benefits of using inheritance? What programming technique shall you follow when the base class methods are not appropriate for the derived class object? (Points : 18)

Question 6.6. (TCO 6) What is static binding? What is dynamic binding? How do you distinguish between static binding and dynamic binding? (Points : 18)

Question 7.7. (TCO 2) Define and implement the overloaded constructors that support the following test function for a Rectangle class. The data members for the Rectangle class are:

• length - integer number
• width - integer number

int main()
{ Rectangle r1(); //r1 will take all default value
Rectangle r2(4, 5); //r2 will take all supplied value
Rectangle r3(10); //r3 will take supplied length, width will take default value
Rectangle r4= r2; //r4 will take the same value of r2

//the rest of the code
}
  (Points : 22)

Question 1.1. (TCO 8) Briefly describe best practices as it relates to naming classes. (Points : 18)

      
      

Question 2.2. (TCO 2) Sometimes the terms Encapsulation and Data/Information Hiding in object-oriented programming are used interchangeably. Can you give an example, which shows that they are actually different? (Points : 18)

      
      

Question 3.3. (TCO 2) Given the following list of classes, attributes and methods,

·         identify which items are classes, which items are attributes and which items are methods;
·         identify which class each attribute and method belongs to; and
·         suggest a class hierarchy given your list of classes.

*Note - no particular capitalization scheme is used in the list below to differentiate between classes, methods, and attributes.

Brew, DecreaseTemperature, Manufacturer, MinCups, Price, TurnOff, Oven, MaxTemperature, BrewStrength, NumberOfRacks, IncreaseTemperature, TurnOn, StartTimer, CoffeeMaker, KitchenAppliance, MaxCups, YearBuilt, Grind (Points : 18)

      
      

Question 4.4. (TCO 7) What are the differences between a normal class and an abstract class? Provide an example of an abstract class. (Points : 18)

      
      

Question 5.5. (TCO 4) What is inheritance and what are the major benefits of using inheritance? What programming technique shall you follow when the base class methods are not appropriate for the derived class object? (Points : 18)

      
      

Question 6.6. (TCO 6) What is static binding? What is dynamic binding? How do you distinguish between static binding and dynamic binding? (Points : 18)

      
      

Question 7.7. (TCO 2) Define and implement the overloaded constructors that support the following test function for a Rectangle class. The data members for the Rectangle class are:

• length - integer number
• width - integer number

int main()
{ Rectangle r1(); //r1 will take all default value
Rectangle r2(4, 5); //r2 will take all supplied value
Rectangle r3(10); //r3 will take supplied length, width will take default value
Rectangle r4= r2; //r4 will take the same value of r2

//the rest of the code
}
  (Points : 22)

      
      

Explanation / Answer

Question 1.1

Answer:

Question 2.2

Answer:

Encapsulation is an OOP concept where object state(class fields) and it's behaviour(methods) is wrapped together. Java provides encapsulation using

Data hiding: mechanism for restricting access to some of the object's components.

It Conceals how an object implements its functionality behind the abstraction of its API.

As an example of information hiding/encapsulation, consider this class:

public class MyBankAccount {

    public int dollars;

}

The implementation of this class is completely unencapsulated, which means it is inflexible (e.g. we cannot easily add support for individual cents in the future) and unsafe (e.g. the account can changed to be negative). However, if we hide the data behind a formally defined interface of methods, we gain flexibility and safety.

public class BankAccount {

    private int dollars;

    public void deposit(int dollars) {

        this.dollars += Math.max(0, dollars);

    }

}

We now have control over how the state is modified, and we can also change the implementation without breaking client code:

public class BankAccount {

    private int cents;

    public void deposit(int dollars) {

        deposit(dollars, 0);

    }

    public void deposit(int dollars, int cents) {

        this.cents += Math.max(0, 100 * dollars) + Math.max(0, cents);

    }

}

The class is now better encapsulated because we have hidden information about its underlying implementation.

Question 3.3

Answer:

Classes: Oven, CoffeeMaker, KitchenAppliance

Methods: DecreaseTemperature, Brew, IncreaseTemperature, Grind

Attributes: MinCups, Price, TurnOff, MaxTemperature, BrewStrength, NumberOfRacks, TurnOn, StartTimer, Manufacturer, MaxCups, YearBuilt,

Class hierarchy: KitchenAppliance would be the parent class, and oven and coffee maker its subsequent child class.

Question 4.4.

Answer:

A partially implemented class is called an abstract class , Where as fully implemented class is a normal class.

Abstract class:

If not , the derived class also become anabstract class.

Normal class:

Example of abstract class:

abstract class Bank{

    

abstract int getRateOfInterest();    

}    

Question 5.5

Answer:

Inheritance is the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.

The class which inherits the properties of other is known as base class and the class whose properties are inherited is known as derived class (base class, parent class).

By using inheritance code reuse is promoted where we can use already defined methods in another class, and add only the methods which are new. No need to create repetitive same methods.

If base class methods are not appropriate we can create new methods in the child class, avoiding using the base class methods in the child cass.

Question 6.6.

Static binding:

When type of the object is determined at compiled time(by the compiler), it is known as static binding.

Example:

public class NewClass

{

    public static class superclass

    {

        static void print()

        {

            System.out.println("print in superclass.");

        }

    }

    public static class subclass extends superclass

    {

        static void print()

        {

            System.out.println("print in subclass.");

        }

    }

    public static void main(String[] args)

    {

        superclass A = new superclass();

        superclass B = new subclass();

        A.print();

        B.print();

    }

}

Output:

As you can see, in both cases print method of superclass is called. Lets see how this happens

§ We have created one object of subclass and one object of superclass with the reference of the superclass.

§ Since the print method of superclass is static, compiler knows that it will not be overridden in subclasses and hence compiler knows during compile time which print method to call and hence no ambiguity.

Dynamic binding:

When type of the object is determined at run-time, it is known as dynamic binding.

   

Example:

public class NewClass

{

    public static class superclass

    {

        void print()

        {

            System.out.println("print in superclass.");

        }

    }

    public static class subclass extends superclass

    {

        @Override

        void print()

        {

            System.out.println("print in subclass.");

        }

    }

    public static void main(String[] args)

    {

        superclass A = new superclass();

        superclass B = new subclass();

        A.print();

        B.print();

    }

}

Output:

Here the output differs.

§ Methods are not static in this code.

§ During compilation, the compiler has no idea as to which print has to be called since compiler goes only by referencing variable not by type of object and therefore the binding would be delayed to runtime and therefore the corresponding version of print will be called based on type on object.

Question 7.7

Answer:

class Rectangle{

int i1;

int i2;

Rectangle(){

    i1=0;

    i2=0;

    }

Rectangle(int i11){

    i1=i11;

    }

    Rectangle(int i11, int i22){

    i1=i11;

    i2=i22;

    }

   

    Rectangle(Rectangle r){

    i1 = r.i1;

     i2 = r.i2;

    }

void display(){

    System.out.println(i1+" "+i2);

   

}

   

}

public class HelloWorld{

     public static void main(String []args){

        Rectangle r1=new Rectangle();

        Rectangle r2=new Rectangle(4,5);

        Rectangle r3=new Rectangle(10);

        Rectangle r4=r2;

        r1.display();

        r2.display();

        r3.display();

       r4.display();

     }

}

output:

0 0

4 5

10

4 5

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