Introduction To Java Programming Language
105606Module 2: Arrays and Strings
1(b). A constructor is automatically invoked when: (i) A method is called (ii) A package is imported (iii) An object is created (iv) A class is inherited20262m
Module 2: Arrays and Strings
View this question on its own page →A constructor is automatically invoked when:
(i) A method is called
(ii) A package is imported
(iii) An object is created
(iv) A class is inheritedWorked SolutionAnswer: (iii) An object is created
A constructor runs when an object is instantiated with
new(or through another constructor). It initializes the new object's state. Constructors have the class name and no return type.class Student { Student() { System.out.println("Created"); } } Student s = new Student(); // invokes constructor4(b). A student record system stores marks of 50 students in an array. Construct a Java program to calculate the highest, lowest and average marks and analyse the time complexity of the operations.20267m
Module 2: Arrays and Strings
View this question on its own page →A student record system stores marks of 50 students in an array. Construct a Java program to calculate the highest, lowest and average marks and analyse the time complexity of the operations.
Worked SolutionFind highest, lowest and average marks
public class Marks { public static void main(String[] args) { int[] marks = {72, 85, 61, 90, 77}; // example values int max = marks[0], min = marks[0], sum = 0; for (int mark : marks) { if (mark > max) max = mark; if (mark < min) min = mark; sum += mark; } double average = (double) sum / marks.length; System.out.println("Highest = " + max); System.out.println("Lowest = " + min); System.out.println("Average = " + average); } }Replace the example array with the 50 students' marks. Initializing
maxandminfrom the first element avoids assuming marks are non-negative. A single pass calculates all three values: O(n) time and O(1) extra space.5(a). Consider the following code segment: ``java String s1 = "Java"; String s2 = new String("Java"); ` Analyse the results of s1 == s2 and s1.equals(s2)`.20267m
Module 2: Arrays and Strings
View this question on its own page →Consider the following code segment:
String s1 = "Java"; String s2 = new String("Java");Analyse the results of
s1 == s2ands1.equals(s2).Worked Solution==versus.equals()String s1 = "Java"; String s2 = new String("Java");s1 == s2is false because==compares object references, andnew String(...)creates a distinct object.s1.equals(s2)is true becauseString.equals()compares character contents.
For strings, use
.equals()when comparing text.==is appropriate when reference identity is what you intend to test.9(b). Write a short note on Constructors and Object Creation.20267m
Module 2: Arrays and Strings
View this question on its own page →Write a short note on Constructors and Object Creation.
Worked SolutionConstructors and object creation
A constructor initializes an object's state. It has the same name as its class and no return type. Constructors may be no-argument, parameterized, or overloaded; if no constructor is declared, Java supplies a default no-argument constructor.
class Book { String title; Book(String title) { this.title = title; } } Book b = new Book("Networks");newallocates an object, initializes its fields to default values, and invokes the selected constructor.this(...)calls another constructor in the same class and must be first;super(...)calls a superclass constructor and must also be first.