Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    94
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Explaining the following code

    Hi guys, I've been given some example codes that accompany a book I've recently bought, I need to comment it so I can understand how it works, so any help is much appreciated.

    Thankyou.

    Code:
    import java.util.Scanner;
    
    public class BookFinder {
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
    Scanner myScan = new Scanner(System.in);
    String string1;
    System.out.println("Enter lines of text - finish with \"end\":");
    do {
          string1 = myScan.nextLine();
          if (string1.contains ("book") || string1.contains ("Book")){
        string1 = string1.replaceFirst("[bB]ook", "[printedpages]");
        System.out.println(string1);
        }
    }
    while (!string1.equalsIgnoreCase("end"));
        }
    }
    Code:
    import java.text.DecimalFormat;
    import java.util.Scanner;
    
    
    public class CubeSquare {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    
             Scanner scan = new Scanner(System.in);
    
    
            double x, y, xcbrt, ysqrt, result;
    
            System.out.println("Enter 2 numbers:");
            x = scan.nextDouble();
            y = scan.nextDouble();
    
            y = Math.abs(y);
    
            xcbrt = Math.cbrt(x);
            ysqrt = Math.sqrt(y);
    
            result = Math.max(xcbrt, ysqrt);
    
            DecimalFormat fmt1 = new DecimalFormat("0.##");
    
            System.out.println(fmt1.format(result));
            
        }
    
    }
    Code:
    import java.util.Scanner;
    
    public class WordCharCount {
    
        public static void main(String[] args) {
    
    
           int CharacterCount = 0;
           String WordCount, wordChar1, wordChar2, wordChar3;
    
            Scanner myScanner = new Scanner(System.in);
    
            System.out.println("Please enter 3 words:");
            wordChar1 = myScanner.next();
            wordChar2 = myScanner.next();
            wordChar3 = myScanner.next();
    
            WordCount = wordChar1 + wordChar2 + wordChar3;
            CharacterCount = WordCount.length();
    
            System.out.println("You entered " + CharacterCount + " characters");
    
        }
    
    }
    Code:
    import java.util.Scanner;
    
    public class Wrapper {
    
        public static void main(String[] args) {
    
            {
    
                Scanner scan = new Scanner(System.in);
                int int1 = 0;
                int1 = scan.nextInt();
                System.out.println(Integer.toHexString(int1));
    
                byte byte1;
                byte byte2;
                System.out.print(Byte.MAX_VALUE + " ");
                System.out.print(Byte.MIN_VALUE);
    
                int dbl1;
                dbl1 = Double.SIZE;
                System.out.println();
                System.out.println(dbl1);
    
    
    
            }
        }
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    There is a lot of coverage here from different classes.
    Can you be more specific on what you are needing explanation with (datatyping, conditional branches, looping, Scanner IO, etc)?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Feb 2011
    Posts
    48
    Thanks
    1
    Thanked 2 Times in 2 Posts
    The first reads input data. If your input contains "book" or "Book", the first occurence will be replaced with "[printedpages]". If your input is "end" (it's case insensitive), the method is finished. If your input is not "end" (it's case insensitive), another input will be read. So, you are reading while the input is not some case of "end" (case insensitive) and in each iteration you replace the first occurence of "book" or "Book" with "[printedpages]"
    Example:
    input: "my favourite Book is facebook"
    output "my favourite [printedpages] is facebook"
    input: "lorem ipsum"
    input: "I'm booking some tickets for the train where I'll read a book"
    output:"I'm [printedpages]ing some tickets for the train where I'll read a book"
    input: "eNd"

    The second reads to doubles: x and y.
    y will be updated to it's absolute value
    xcbrt will have the value of the cube root of x, ysqrt will have the square root of y.
    result's value will be the maximum of xcbrt and ysqrt.
    The result will be printed out in the given format.

    The third counts the total number of characters of your input.

    The fourth reads an input from you, prints out it's hex value, after that it prints the biggest value of byte type (127) and types the lowest value of byte types (-128). At the end it prints out the size of a double variable.
    Last edited by DistantJob; 02-10-2011 at 07:29 PM.

  • Users who have thanked DistantJob for this post:

    swiltch (02-22-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •