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

Question 1: What is up-casting and what is down-casting? Explain it by using exa

ID: 3664569 • Letter: Q

Question

Question 1:

What is up-casting and what is down-casting? Explain it by using examples with code.

Question 2:

What is polymorphism? What are required to implement polymorphism?

Question3:

Implement a method to count number of ‘a’ and ‘A’ in the string, for example, if the string is String str = "ab12cd1A4"; the number of ‘a’ and ‘A’ in str is 2.

public class StringManipulation {

           

            public static void main(String[] args) {

                                               

                       

            }

            // implement this method

            public static int numberofAs (String str) {

                       

            }

}

Question 4:

What are the differences between FlowLayout and BorderLayout?

Answer:

What is the default layout manager of JFrame and JPanel, respectively?

Answer

Explanation / Answer

Question 1:
What is up-casting and what is down-casting? Explain it by using examples with code.
Answer:
Parent: Car
Child: Figo
Car c1 = new Figo();

=====
Upcasting:-
Method: Object c1 will refer to Methods of Class (Figo - Method must be overridden)
because class "Figo" is specified with "new".
Instance Variable: Object c1 will refer to instance variable of Declaration Class
("Car").

When Declaration class is parent and object is created of child then implicit
casting happens which is "Upcasting".

======
Downcasting:-
Figo f1 = (Figo) c1; //
Method: Object f1 will refer to Method of Class (figo) as initial object c1 is
created with class "Figo". but once down casting is done, methods which are only
present in class "Figo" can also be referred by variable f1.
Instance Variable: Object f1 will not refer to instance variable of Declaration
class of object c1 (declaration class for c1 is CAR) but with down casting it will
refer to instance variables of class Figo.

======
Use: When Object is of Child Class and declaration class is Parent and Child class
wants to access Instance variable of it's own class and not of parent class then it
can be done with "Downcasting".


Question 2:
What is polymorphism? What are required to implement polymorphism?
Answer:
Polymorphism is the capability of a method to do different things based on the
object that it is acting upon.
it is possible to define two or more methods of same name in a class, provided that
there argument list or parameters are different. This concept is known as Method
Overloading.

Child class has the same method as of base class. In such cases child class
overrides the parent class method without even touching the source code of the base
class. This feature is known as method overriding.

Question3:
Implement a method to count number of ‘a’ and ‘A’ in the string, for example, if
the string is String str = "ab12cd1A4"; the number of ‘a’ and ‘A’ in str is 2.

public class StringManipulation {

public static void main(String[] args) {


}
// implement this method
public static int numberofAs (String str) {

}
}
Answer:
/**
* @author Srinivas Palli
*
*/
public class StringManipulation {

   /**
   * @param args
   */
   public static void main(String[] args) {
       String str = "ab12cd1A4";
       int count = numberofAs(str);
       System.out.println("the number of ‘a’ and ‘A’ in str is " + count);

   }

   // implement this method
   /**
   * @param str
   * @return
   */
   public static int numberofAs(String str) {

       int count = 0;

       for (int i = 0; i < str.length(); i++) {
           if (str.charAt(i) == 'a' || str.charAt(i) == 'A') {
               count++;
           }
       }
       return count;

   }
}

Question 4:
What are the differences between FlowLayout and BorderLayout?
Answer:
FlowLayout:
The default layout manager, which we've been using all along, is called FlowLayout.
Here, all GUI components are simply placed on the GUI from left to right in the
order they're created. Components essentially act like characters in a word
processing program and wrap to the next line when the right side of the GUI is
reached.
BorderLayout:
The BorderLayout breaks the field up into five areas: north, south, east, west, and center.

We can create a BorderLayout as follows:

BorderLayout layout;
layout = new BorderLayout();
setLayout(layout);
Additionally, we could opt to specify gaps between components, e.g.

layout = new BorderLayout(horizontalGapInPixels, verticalGapInPixels);
When we add components to a container with a BorderLayout, the add() method takes a
second argument that tells in which area of the layout to place the component. If
we omit this argument, the default is center. Here are the five options:

BorderLayout.NORTH
BorderLayout.SOUTH
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.CENTER

What is the default layout manager of JFrame and JPanel, respectively?
Answer:

JFrame-->BorderLayout
JPanel-->FlowLayout

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