Lab 6


LAB TASK 1

The great circle distance is the distance between two points on the surface of a sphere. Let (x1, y1) and (x2, y2) be the geographi-cal latitude and longitude of two points. The great circle distance between the two points can be computed using the following formula: 

Write a program that prompts the user to enter the latitude and longitude of two points on the earth in degrees and displays its great circle distance. The average earth radius is 6,371.01 km. Note that you need to convert the degrees into radians using the Math.toRadians method since the Java trigonometric methods use radians. The latitude and longitude degrees in the formula are for north and west. Use negative to indicate south and east degrees. Here is a sample run: 


CODE:


import java.util.Scanner;

public class Main {

    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter point 1 longitude in degrees: ");

        double long1 = scanner.nextDouble();

        long1 = Math.toRadians(long1);


        System.out.print("Enter point 1 latitude in degrees: ");

        double lat1 = scanner.nextDouble();

        lat1 = Math.toRadians(lat1);


        System.out.print("Enter point 2 longitude in degrees: ");

        double long2 = scanner.nextDouble();

        long2 = Math.toRadians(long2);


        System.out.print("Enter point 2 latitude in degrees: ");

        double lat2 = scanner.nextDouble();

        lat2 = Math.toRadians(lat2);


        double distance = 6371.01 * Math.acos((Math.sin(lat1) * Math.sin(lat2)) + (Math.cos(lat1) * Math.cos(lat2) * Math.cos(long1 - long2)));


        System.out.printf("The distance between two points is %.5f km",distance);

       


        scanner.close();

    }

}




LAB TASK 2

a) Write a program that receives an ASCII code (an integer between 0 and 127) and displays its character. Here is a sample run: 

Enter an ASCII code: 69 

The character for ASCII code 69 is E 

b) Write a program that receives a character and displays its Unicode. Here is a sample run: 

Enter a character: E 

The Unicode for the character E is 69


CODE:


import java.util.Scanner;

public class Main {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter an ASCII code(0-127): ");

        int code = scanner.nextInt();

        char character = (char) code;

        System.out.println(character);


        System.out.print("\nEnter a character: ");

        character = scanner.next().charAt(0);


        code = (int) character;


        System.out.printf("Code for %c is %d",character,code);

        scanner.close();

       

    }

}




LAB TASK 3

a) Write a program that prompts the user to enter an integer between 0 and 15 and displays its corresponding hex number. Here are some sample runs: 

Enter a decimal value (0 to 15): 11 

The hex value is B Enter a decimal value (0 to 15): 5 

The hex value is 5 Enter a decimal value (0 to 15): 31 31 is an invalid input 

b) Write a program that prompts the user to enter a hex digit and displays its corresponding binary number. Here is a sample run:

Enter a hex digit: B 

The binary value is 1011 

Enter a hex digit: G G is an invalid input 


CODE:


import java.util.Scanner;

public class Main {


    public static void main(String[] args) {



        Scanner scanner = new Scanner(System.in);

        String hex = "";



        System.out.print("Enter an integer(0-15): ");

        int num = scanner.nextInt();


        if(num>15 || num<0){

            System.out.println("Invalid input!");

        }

        else{

            hex = Integer.toHexString(num).toUpperCase();

            System.out.println(hex);

            scanner.nextLine();


        }


        System.out.print("Enter a hex digit: ");

        hex = scanner.nextLine();


        num = Integer.parseInt(hex,16);

        System.out.println(num);



        scanner.close();  

    }

}




LAB TASK 4

Write a program that displays a random uppercase letter using the Math.random() method


CODE:


import java.util.Scanner;

public class Main {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);


        double num = Math.random()*26;

        int numInt = (int) num;


        numInt = numInt+65;


        char character = (char) numInt;


        System.out.println(character);        

        scanner.close();

    }

}



LAB TASK 5

Write a program that checks whether a string is a palindrome. A string is a palindrome if it reads the same forward and backward. The words “mom,” “dad,” and “noon,” for instance, are all palindromes. Sample run: 

Enter a string: noon 

noon is a palindrome 

Enter a string: moon 

moon is not a palindrome 

CODE:


import java.util.Scanner;

public class Main {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);


        System.out.print("Enter a word: ");

        String word = scanner.nextLine();

       

        String wordReverse = "";

        for(int i = word.length()-1 ; i >=0 ; i-- ){

            wordReverse = wordReverse+ word.charAt(i);


        }



        if(word.equalsIgnoreCase(wordReverse)){

            System.out.println("This word is palindrome");

        }

        else{

            System.out.println("This word in not palindrome");

        }


        scanner.close();

       

    }

}



LAB TASK 6

Given a string consisting of exactly two words separated by a space. Print a new string with the first and second word positions swapped (the second word is printed first). This task should not use loops and if. 

Sample Run: 

Input: Hello, world! 

Correct Answer: world! Hello,


CODE:


import java.util.Scanner;

public class Main {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);


        System.out.print("Enter two words separated by a space: ");

        // HELLO WORLD

        String word = scanner.nextLine();



        int spaceIndex = word.indexOf(' ');


        String part1 = word.substring(0, spaceIndex);

        String part2 = word.substring(spaceIndex+1);


        String wordSwapped = "";

        wordSwapped = part2 +" "+ part1;




        System.out.println(wordSwapped);

        scanner.close();

    }

}




LAB TASK 7

Given a string that may or may not contain a letter of interest. Print the index location of the first and last appearance of f. If the letter f occurs only once, then output its index. If the letter f does not occur, then do not print anything. 

Input: office 

Correct Answer: 1 2 


CODE:


import java.util.Scanner;


public class Main {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a word: ");

        String word = scanner.nextLine();


        int index1 = word.indexOf('f');

        int index2 = word.lastIndexOf('f');


        System.out.println(index1);

        System.out.println(index2);


        if(index1==index2 && index1>=0){

            System.out.println(index1);

        }


        else{

            if(index1>=0 && index2>=0){

                System.out.println(index1+" "+index2);

            }

            else if(index1>=0){

                System.out.println(index1);

            }

            else if(index2>=0){

                System.out.println(index2);

            }

        }


        scanner.close();

    }

}




LAB TASK 8

Given a string in which the letter h occurs at least twice. Remove from that string the first and the last occurrence of the letter h, as well as all the characters between them. 

Sample Run: 

Input: In the hole in the ground there lived a hobbit 

Correct Answer: In tobbit 

CODE:


import java.util.Scanner;

public class Main {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);


        System.out.print("Enter a phrase that contain 'h' at least twice: ");

        String phrase = scanner.nextLine();


        int index1 = phrase.indexOf('h');

        int index2 = phrase.lastIndexOf('h');


        String first = phrase.substring(0, index1);

        String last = phrase.substring(index2+1);


        String phrase2 = first+last;


        System.out.println(phrase2);



       

        scanner.close();

    }

}




LAB TASK 9

Given a string. Replace every occurrence of the letter h by the letter H, except for the first and the last ones. 

Input: In the hole in the ground there lived a hobbit 

Correct Answer: In the Hole in tHe ground tHere lived a hobbit 


CODE:


import java.util.Scanner;

public class Main {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);



        System.out.print("Enter the phrase: ");

        String phrase = scanner.nextLine();

        String phrase2 = "";


        int index1 = phrase.indexOf('h');

        int index2 = phrase.lastIndexOf('h');


        for(int i=0 ; i<phrase.length() ; i++){

            if((phrase.charAt(i)=='h') && (i!=index1 && i!=index2)){

                phrase2 = phrase2 + Character.toUpperCase(phrase.charAt(i));

            }

            else{

                phrase2 = phrase2 + phrase.charAt(i);

            }


        }


        System.out.println(phrase2);



       

        scanner.close();


    }

}



LAB TASK 10

You are given a string. In the first line, print the third character of this string.

In the second line, print the second to last character of this string.

In the third line, print the first five characters of this string.

In the fourth line, print all but the last two characters of this string.

In the fifth line, print all the characters of this string with even indices (remember indexing starts at 0, so the characters are displayed starting with the first).

In the sixth line, print all the characters of this string with odd indices (i.e. starting with the second character in the string).

In the seventh line, print all the characters of the string in reverse order.

In the eighth line, print every second character of the string in reverse order, starting from the last one.

In the ninth line, print the length of the given string. 


CODE:


import java.util.Scanner;

public class Main {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a string: ");

        String word = scanner.nextLine();

        String output5 = "";

        String output6 = "";

        String output7 = "";

        String output8 = "";


        System.out.println("Output 1: "+word.charAt(2));

        System.out.println("Output 2: "+word.charAt(word.length()-2));

        System.out.println("Output 3: "+word.substring(0,5));

        System.out.println("Output 4: "+word.substring(0,word.length()-2));


       for(int i=0 ; i < word.length() ; i+=2){

            output5 = output5 + word.charAt(i);

        }

        System.out.println("Output 5: "+output5);


        for(int i=1 ; i < word.length() ; i+=2){

            output6 = output6 + word.charAt(i);

        }

        System.out.println("Output 6: "+output6);


        for(int i=word.length()-1 ; i>=0 ; i--){

            output7 = output7 + word.charAt(i);

        }

        System.out.println("Output 7: "+output7);



        for(int i=word.length()-1 ; i>=0 ; i-=2){

            output8 = output8 + word.charAt(i);

        }

        System.out.println("Output 8: "+output8);

        System.out.println("Output 9: "+word.length());

        scanner.close();

    }

}