Java Instanceof keyword
The Java instanceof operator is used to test if the object or instance is an instanceof the specified type. In this problem we have given you three classes in the editor: Student class Rockstar class Hacker class In the main method, we populated an ArrayList with several instances of these classes. count method calculates how many instances of each type is present in the ArrayList. The code prints three integers, the number of instance of Student class, the number of instance of Rocksta
View Solution →Java Iterator
Java Iterator class can help you to iterate through every element in a collection. Here is a simple example: import java.util.*; public class Example{ public static void main(String []args){ ArrayList mylist = new ArrayList(); mylist.add("Hello"); mylist.add("Java"); mylist.add("4"); Iterator it = mylist.iterator(); while(it.hasNext()){ Object element = it.next(); System.out.println((String)element);
View Solution →Java Exception Handling (Try-catch)
Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing – often changing the normal flow of program execution. (Wikipedia) Java has built-in mechanism to handle exceptions. Using the try statement we can test a block of code for errors. The catch block contains the code that says what to do if exception occurs. This problem will test your knowledge on try-catch block. You will
View Solution →Java Exception Handling
You are required to compute the power of a number by implementing a calculator. Create a class MyCalculator which consists of a single method long power(int, int). This method takes two integers, n and p, as parameters and finds . If either or is negative, then the method must throw an exception which says "n or p should not be negative". Also, if both n and p are zero, then the method must throw an exception which says "n and p should not be zero" For example, -4 and -5 would result in jav
View Solution →Java Varargs - Simple Addition
You are given a class Solution and its main method in the editor. Your task is to create the class Add and the required methods so that the code prints the sum of the numbers passed to the function add. Note: Your add method in the Add class must print the sum as given in the Sample Output Input Format There are six lines of input, each containing an integer. Output Format There will be only four lines of output. Each line contains the sum of the integers passed as the parameters
View Solution →