Scientific Programming II

Programming in Java

Unit 3 - Example Programs


Example 1

Write a program that uses the String method compareTo to compare two words that are input by the user. Output if the first word is less than, equal to or greater than the second word.
ASCII Table

import java.util.Scanner;
 
public class Comparing_String {
    public static void main(String[] args) {
        System.out.println("Comparing Strings");
        Scanner scanner = new Scanner(System.in);
       
        System.out.println("First Word?");
        String word1 = scanner.nextLine();
       
        System.out.println("Second Word?");
        String word2 = scanner.nextLine();
       
        if(word1.compareTo(word2) < 0) {
            System.out.println(word1 + " is less than " + word2);
        }
        else if (word1.compareTo(word2) == 0) {
            System.out.println(word1 + " is equal to " + word2);
        }
        else {
           System.out.println(word1 + " is greater than " + word2);        
        }
    }
}
Output 1
First Word?
> Hello
Second Word?
> hello
Hello is less than hello
Output 2
First Word?
> among
Second Word?
> amend
among is greater than amend
Output 3
First Word?
> apple
Second Word?
> orange
apple is less than orange
Output 4
First Word?
> Hello
Second Word?
> Hello
Hello is equal to Hello

Example 2

The input to your program will be a sentence. Your program has to determine if the input sentence is a palindrome. You should consider upper and lowercase versions of the same letter to be equal and ignore non-alphabetical characters.

import java.util.Scanner;
 
public class Sentence_Extract_Words {
    public static boolean is_palindrome (String input) {
        String y = "";
      
        for (int i = input.length() -1 ; i >= 0; i--) {
            y += input.charAt(i);
        }
        // y.equals(String) would check equality, but would consider
        // upper- and lowercase letters different
        if (y.equalsIgnoreCase(input)) {
            return true;
        }
        else {
            return false;
        }
    }
 
    public static void main(String[] args) {
        System.out.println("Checking for Palindromes");
        Scanner scanner = new Scanner(System.in);
       
        System.out.println("Input Your Sentence");
        String sentence = scanner.nextLine();
        String letters = "";
       
        for (int i = 0; i < sentence.length(); i++) {
            if (sentence.charAt(i) >= 'a' && sentence.charAt(i) <= 'z') {
                letters += sentence.charAt(i);
            }
        }
            
        if (is_palindrome(letters) == true) {
            System.out.println(sentence + " is a palindrome ");
        }
        else {
            System.out.println(sentence + " is not a palindrome ");
        }
    }
}
Output 1
> Madam I'm Adam.
The input sentence is a palindrome
Output 2
> Able was I ere I saw Elba.
The input sentence is a palindrome
Output 3
> I played soccer all day.
The input sentence is not a palindrome

Example 3

The input to your program will be a word. Your program should do the following:

import java.util.Scanner;
 
public class RemoveDuplicateLetters {      
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println ("Input Word");
        String word = scanner.nextLine();
        String ans = "";
       
        for (int i = 0; i <word.length(); i++) {
            int count = 0;
            for (int j = 0; j <ans.length(); j++) {
                if (word.charAt(i) == ans.charAt(j)) {
                    count++;
                }
            }
            if (count == 0) {
                ans += word.charAt(i);
            }
        }
           
        if (word.equals (ans)) {
            System.out.println("No repeated letters");
        } else {
            System.out.println (ans);
        }
    }
}
Output 1
> mattress
matres
Output 2
> fine
No repeated letters