Condensed Code in Java, Part 1


In my last blog I talked about programming in general. This blog is going to be more specific, and more technical.

The code is going to be java, because it’s the language I know best. But since the goal is not to learn syntax, I’m going to condense the code down as far as possible.

When I say a program is “beautiful,” what I mean is that it’s simple and elegant. Which means it has few lines of code, and each line does a lot– without being hard to understand.

I can’t write beautiful programs right now. But I think that if you’re willing to work at it for several years, you can get pretty good at it. And in this blog I’m going to try to give an idea of what that looks like by showing some of my favorite programs from books and the web.

The first program we’ll look at is a palindrome detector in java from Bruce Eckel’s book “Thinking In Java.”

import java.util.*;

import java.lang.*;

import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */

class Ideone

{

public static void main (String[] args) throws java.lang.Exception

{

// your code goes here

String input, rev=””; //Create a string variable and initialize it with nothing. Another string variable to store reversed string and initialize it with nothing as well.

Scanner sc = new Scanner(System.in); //Scanner object to read input from console.

System.out.println(“Enter a string/number to check if it is a palindrome”);

input = sc.nextLine(); //Read entire line as a string using Scanner class method nextLine(). If you use next() method, it will only read first word or character before space or tab. For example, for input “This is Java Code”, nextLine() will return entire string but next() method will return “This” only. If you use nextInt(), it will return that integer value only and not other values in the

package org.example;

import java.util.Scanner;

public class Palindrome {

public static void main(String[] args) {

System.out.println(“Please enter a string:”);

Scanner scanner = new Scanner(System.in);

String input = scanner.next();

String reversedInput = “”;

for (int i = input.length() – 1; i >= 0; i–) { //reversing the word backwards to compare with original word

reversedInput += input.charAt(i); //assigning reversed characters to a variable

}

if (reversedInput.equalsIgnoreCase(input)) { //comparing the two strings, ignore case for ‘A’ and ‘a’ etc…

System.out.println(“The given string is a palindrome”);

} else {

System.out.println(“The given string is not a palindrome”);

}

}//end of main() method

// Returns true if the given string is a palindrome.

public static boolean isPalindrome(String str) {

return str.equals(new StringBuilder(str).reverse().toString());

}

import java.util.*;

public class Palindrome {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println(“Enter a palindrome: “);

String str = sc.nextLine();

String rev = “”;

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

rev+=str.charAt(i); }

if (rev.equalsIgnoreCase(str)) { System.out.println(“This *IS* a palindrome!”);}

else{ System.out.println(“This is NOT a palindrome!”);}

}

}

public boolean isPalindrome(String str)

{

// Pointers pointing to the beginning and the end of the string

int i = 0, j = str.length() – 1;

// While there are characters toc compare

while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)){ return false; } // Increment first pointer and decrement the other i++; j--; } return true; } Let's start with one of the simplest, most common programs: a palindrome checker. The idea is to take a word or phrase and see if it reads the same from left to right as it does from right to left. A naïve attempt might look like this: if the first character of the string is equal to the last character, and the second character is equal to the second-to-last character, etc... then it's a palindrome. Implementing this could look like: while read input; do reverse="" for (( i=${


Leave a Reply

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