MUST BE JAVE CODE Question 1: What is up-casting and what is down-casting? Expla
ID: 3553173 • Letter: M
Question
MUST BE JAVE CODE
Question 1:
What is up-casting and what is down-casting? Explain it by using examples with code.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?>
Question 2:
What is polymorphism? Explain it by using examples with code.
Question3:
1. Implement a method to count number of digits in the string, for example, if the string is String str = "ab12cd14"; the number of digits in str is 4.
2. Given a full name, please return the first name. In the full name, there is only one space between first name and last name.
public classStringManipulation {
public static void main(String[] args) {
}
// implement this method
public static int numberofDigits (String str) {
}
//implement this method
public static String getFirstName(String fullName) {
}
}
Explanation / Answer
I included answers for your three questions below in Java. I tried to provide comments in the code so you can tell what's going on. Let me know if you have any questions and I'd be glad to explain more! :)
1. Upcasting is when a subclass object is treated as a superclass object. Downcasting is when a superclass object is treated as a subclass object. A simple example of this is if you have an Animal class (the superclass), and a cat class (the subclass):
class Animal {
int health = 100;
}
class Cat extends Animal { }
Now if we want to create a Cat but treat it as an Animal, we have to upcast our cat. A code example of this is below:
public static void main(String[] args) {
Cat c = new Cat();
Animal catAnimal = c; // Upcasting
}
Our cat c is the same animal after upcasting, just labeled as an Animal rather than a Cat. This is allowed because Cats are Animals.
If we want to downcast our catAnimal back to a Cat rather than an Animal, we have to manually cast it back down to a Cat using (Cat):
public static void main(String[] args) {
Cat c = new Cat();
Animal catAnimal = c; // Upcasting
Cat cc = (Cat) catAnimal; // Downcasting
}
Now, our Cat cc is just like the Cat c we started out with.
2. Polymorphism is when an object can take on many forms. A simple example of this is if we have an Object class. Our object can take on the form of an Object, a String, or an Integer, for example.
public static void main(String[] args) {
Object o = new Object(); //o can hold the reference of any subtype
// Object o = new String(); // o can also be a String
// Object o = new Integer(1); // o can also be an Integer
}
We can put our Object o in form String or Integer because String is a subclass of the Object class and Integer is also a subclass of the Object class. Polymorphism is when we refer to our subclass (String or Integer in this example) using our superclass (Object in this example).
3. Below is your completed code along with a couple tests. Let me know if you need more explanation! :)
public class StringManipulation {
public static void main(String[] args) {
System.out.println(numberofDigits("ab12cd14")); // Returns 4
System.out.println(getFirstName("John Smith")); // Returns John
}
public static int numberofDigits (String str) {
int numCount = 0;
for (char c: str.toCharArray()) { // For each character in the string,
if (Character.isDigit(c)) { // if the character is a number,
// increment the count of numbers in the string.
numCount++;
}
}
return numCount; // Return the count of numbers in the string.
}
public static String getFirstName(String fullName) {
String[] firstLast = fullName.split(" "); // Split the full name on a space to get a string array with firstLast[0] as first name and firstLast[1] as last name
return firstLast[0]; // Return the first name
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.