Skip to main content

Posts

Showing posts from May, 2020

Program on method overloading

Write a program with overloaded methods to print values. Method overloading is the process where you write multiple methods in a class with same name but different number/type or parameters. When such an overloaded method is called, then the compiler selects the appropriate method depending on the number and type of arguments. e.g. class Number{       void printSquare(int n){          .....       }       void printSquare(double n){          ......      } }   The class above has two method with the same name printSquare(). Now if the we call Number obj = new Number(); obj.printSquare(10); we are calling the first method with integer parameter. But if we call obj.printSquare(1.56) we are calling second method with double parameter. Please remember that the overloading can not be done based only on return type of methods. Here is our requir...

Compare two Hashsets

Write a program to compare two hashsets in Java. HashSet is one of a data structures in the Java Collections API . It stores unique elements and permits nulls.It's backed by a HashMap. It doesn't maintain insertion order. And it's  not thread-safe. Let us write a function to create a HashSet with n elements and return it. HashSet<string> createSet(int n){ System.out.println("Enter the set elements"); HashSet<string>set = new HashSet<string>(); for(int i=0;i<n;i++){ Scanner scanner = new Scanner(System.in); System.out.println("Enter a string"); String str = scanner.nextLine(); if(set.add(str)==false){ System.out.println("Not able to add the string to set"); } } return set; } We create a Hashset using its constructor - in this example we have a set of Strings - hence new HashSet...

TreeSet Program Java

Write a Java program to find the numbers less than n in a tree set.   Java TreeSet class implements the Set interface that uses a tree for storage. It inherits AbstractSet class and implements the NavigableSet interface. The objects of the TreeSet class are stored in ascending order. And as it is a set, the elements in a treeset are unique - no duplicates. The difference between a hashset and treeset is that, in hashset the elements are not in any order but in a treeset they are in descending order.  Once we create a TreeSet, you can add elements to it using add () method.  To iterate over the elements of a TreeSet, you can use an iterator interface or you can use a for each loop. But if you just want to print the elements, you can give the treeset object as a parameter to println() method. Numbers less than n :  To obtain elements which are less than a given key, you can use the method headSet (). This method returns a SortedSet w...

Median of an array

Write a program to find the median of an array. A median is the middle value - that is to say it has equal number of elements which are smaller and those which are larger.  So obviously, the array need to be sorted. And once we have a sorted array, the n/2th element is the median.  As simple as that. But what if the array has even number of elements? In that case we have to find half of sum middle two elements .  Here is the complete program. import java.util.Arrays ; import java.util.Scanner ;   public class ArrayMedian { public static void main ( String args [ ] ) { int n ; System . out . println ( "Size of array=" ) ; Scanner scanner = new Scanner ( System . in ) ; n = scanner. nextInt ( ) ; int arr [ ] = new int [ n ] ; System . out . println ( "Enter array elements:" ) ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = scanner. nextInt ( ) ...

Convert String to Integer

Write a program to implement atoi function.  1) discard all the leading spaces 2) the string may optionally contain leading sign character -12 +998 are valid 3) After these, if the next character is not a digit, then the return value must be 0 4) If there are alphabets or other characters after digits, the conversion should stop. e.g. -12ab89 = -12 ab129 = 0 890jkl = 890 How do we write code for this? For spaces, we must use trim function. For sign we should check if the 0th character is sign and if it is -, set the negative flag. While the character is digit,  scan the character d Multiply previous sum s by 10 Add the d -> s = s*10 +d Not too complicated right. If we assign s to 0 initially, if there are no leading digits, we stop conversion and return 0. If there are digits followed by alpha, we stop at first alpha then return the value. import java.util.Scanner ; public class Atoi { int stringToInteger ( String ...

Read a file in Java

Write a program to display the contents of a file. The filename is given as command line argument. Command line argument :   When running a program as a command, we can specify one or more options with the program name. These are called command line arguments.  e.g. java myprogram hello world Here hello and world are command line arguments. When we define our main () method as .. main(String args[]) , the arguments are stored args [ 0 ], args [ 1 ] and so on. FileReader class FileReader is a class for reading character streams. It takes the file name to be opened for reading as constructor parameter. You can read one character at a time using read() method. When end of file is reached, the read() returns -1. FileReader reader = new FileReader("a.txt"); char ch =''; while ( (ch=reader.read())!=-1)     System.out.print(ch);  Now let us see the complete program. import java.io.* ; public class filedisplay { ...