Java Basic Certification

1 How Will You Compare?


Write a Comparator class with the following 3 overloaded compare methods

  1. boolean compare(int a, int b); Return true if int a = int b, otherwise return false.
  2. boolean compare(string a, string b). Return true if string a = string b, otherwise return false.
  3. boolean compare(int[] a, int[] b); Return true if both of the following conditions hold true:
    Otherwise, return false.
    · Arrays a and b are of equal length.
    · For each index / (where 0 si < |a|, |b|), a[i] = b[i].
  4. Note: For C++, both parameters are of type Vector.

    Constraints
    · For strings, 1 ≤ |a|, | b| ≤ 2000
    · For integers, 0 ≤ a, b ≤ 10000000
    · For integer arrays, 0 ≤ |a|, |b| ≤ 10
  5. Input Format for Custom Testing
    Input from stdin will be processed as follows and passed to the function.
    The first line contains an integer 7, the number of test cases.
    Each of the next T sets of lines is in one of the following formats:
    .The first line contains the integer 4 representing the comparison type (1, 2 or 3 for int, string or array comparison respectively). The next two lines contain strings a and b.
    .The first line contains the integer 2 representing the overloaded function type. The next two lines contain integers a and b.
    .The first line contains the integer 3-representing the overloaded function type. The next three
    Two space-separated integers fan and + m, the lengths of arrays a and b.
    A line of ła n space-separated integers a[i].
    A line of lb m space-separated integers b[i].

Solution Implementarion

import java.io.*;
import java.util.*;  
class Comparator
{

    boolean compare(int a, int b)
    {
        if (a == b)
            return true;
        else
            return false;
    }

    boolean compare(String a, String b)
    {
        if (a.equals(b))
            return true;
        else
            return false;
    }
    boolean compare(int[] a, int[] b)
    {
        if (a.length != b.length)
            return false;
        else
        {
            for (int i = 0; i < a.length; i++)
            {
                if (a[i] != b[i])
                    return false;
            }
            return true;
        }

    }

}

public class How_will_you_compare
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        Comparator comp = new Comparator();

        int testCases = Integer.parseInt(scan.nextLine());
        while (testCases-- > 0)
        {
            int condition = Integer.parseInt(scan.nextLine());
            switch (condition)
            {
                case 1 :
                    String s1 = scan.nextLine().trim();
                    String s2 = scan.nextLine().trim();

                    System.out.println(
                            (comp.compare(s1, s2)) ? "Same" : "Different");
                    break;
                case 2 :
                    int num1 = scan.nextInt();
                    int num2 = scan.nextInt();

                    System.out.println(
                            (comp.compare(num1, num2)) ? "Same" : "Different");
                    if (scan.hasNext())
                    { // avoid exception if this last test case
                        scan.nextLine(); // eat space until next line
                    }
                    break;
                case 3 :
                    // create and fill arrays
                    int[] array1 = new int[scan.nextInt()];
                    int[] array2 = new int[scan.nextInt()];
                    for (int i = 0; i < array1.length; i++)
                    {
                        array1[i] = scan.nextInt();
                    }
                    for (int i = 0; i < array2.length; i++)
                    {
                        array2[i] = scan.nextInt();
                    }

                    System.out.println(comp.compare(array1, array2)
                            ? "Same"
                            : "Different");
                    if (scan.hasNext())
                    { // avoid exception if this last test case
                        scan.nextLine(); // eat space until next line
                    }
                    break;
                default :
                    System.err.println("Invalid input.");
            }// end switch
        } // end while
        scan.close();
    }
}

2 Java : Shape

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

class Shape {
    public int length , breadth ;
    public Shape( int length , int breadth ) {
        this.length = length ;
        this.breadth = breadth ;
    }
    public void area() {
        System.out.print(length + " " + breadth);
    }
}

class Rectangle extends Shape {
    Rectangle(int l , int b) {
        super(l,b);
    }
    @Override
    public void area() {
        System.out.print("\n" + length*breadth);
    }
}

public class Solution {
    public static void main(String args[] ) throws Exception {
        Scanner sc = new Scanner(System.in);
        int l = sc.nextInt();
        int b = sc.nextInt();

        Shape shape = new Shape(l,b);
        shape.area();

        Shape rectangle = new Rectangle(l,b);
        rectangle.area();
    }
}

BitPuzzle

public class BitPuzzle {
    public static void main(String[] args) {
        try {
            Float f = new Float("3.0");
            int x = f.intValue();
            byte b = f.byteValue();
            double d = f.doubleValue();
            System.out.println(x + b + d);
        } catch (NumberFormatException exception) {
            System.out.println("ahhhh");
        }
    }
}

TestThread

class SampleDemo implements Runnable {
    private Thread t;
    private String threadName;

    SampleDemo(String threadName) {
        this.threadName = threadName;
    }

    @Override
    public void run() {
        while (true) {
            System.out.println(threadName);
        }
    }

    public void start() {
        if (t == null) {
            t = new Thread(this, threadName);
            t.start();
        }
    }
}

public class TestThread {
    public static void main(String[] args) {
        SampleDemo A = new SampleDemo("A");
        SampleDemo B = new SampleDemo("B");
        B.start();
        A.start();
    }
}

TheAdderClass

abstract class Calculator {
    abstract int add(int a, int b);
}

class Adder extends Calculator {

    @Override
    int add(int a, int b) {
        return a + b;
    }
}

public class TheAdderClass {
}

The Above Solution Help you to get the Certificate in Java Basic

Start Test

Leave a Reply

Your email address will not be published. Required fields are marked *