Hello, I want some help for those questions for Java language. Please give me so
ID: 3821858 • Letter: H
Question
Hello, I want some help for those questions for Java language. Please give me some examples to let me understand :
ArrayList Section
write code to create and fill an ArrayList of a given type. ( give example )
Modularity Section
write a method and its calling code with proper parm passing, return value and visibility. ( give example )
Inheritance Section
True or False Questions
T / F A super class constructor must execute to completion before any statements in its sub class constructor can be executed.
T / F You do not have to call a super class constructor explicitly.
T / F Java allows you to inherit directly from only one class, but you may implement multiple interfaces.
T / F When declaring a class as a super class, you should never declare your fields private; use protected instead.
T / F An interface is often thought of as a form of a very restricted super class.
T / F An interface allows us to guarantee that any class that implements it will have the behaviors specified by the interface. It is a contract.
T / F A sub class is not required to override any methods it inherits from its super class.
T / F Instance fields should be initialized in the constructor(s) of the class they were declared in if at all possible.
T / F The two benefits of inheritance are code re-use and polymorphic behavior.
Explanation / Answer
import java.util.*;
class TestArrayList{
public static void main(String args[]){
ArrayList<String> fruits=new ArrayList<String>();//Creating arraylist
fruits.add("Orange");//Adding object in arraylist
fruits.add("Apple");
fruits.add("Pear");
fruits.add("Cherry");
//Traversing list through Iterator
Iterator itr=fruits.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
output:
Orange
Apple
Pear
Cherry
class TestMaxFunction {
public static void main(String[] args) {
int x= 56;
int y = 49;
int z = MaxFunction(x,y);
System.out.println("Maximum = " + z);
}
// returns the maximum of two numbers
public static int MaxFunction(int num1, int num2) {
int max;
if (num1 > num2)
max = num1;
else
max = num2;
return max;
}
}
output:
Maximum = 56
rue or False Questions
T A super class constructor must execute to completion before any statements in its sub class constructor can be executed.
T You do not have to call a super class constructor explicitly.
T Java allows you to inherit directly from only one class, but you may implement multiple interfaces.
F When declaring a class as a super class, you should never declare your fields private; use protected instead.
F An interface is often thought of as a form of a very restricted super class.
T An interface allows us to guarantee that any class that implements it will have the behaviors specified by the interface. It is a contract.
F A sub class is not required to override any methods it inherits from its super class.
T Instance fields should be initialized in the constructor(s) of the class they were declared in if at all possible.
T The two benefits of inheritance are code re-use and polymorphic behavior.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.