Java Basics 1.04

Characters

Code:

     public static void main(String[] args) {

      char c1 = '1';

      char c2 = '2';

      char c3 = '3';

 

      //unicode

      char dollar = '\u0024';

      

      System.out.print(dollar);

      System.out.print(c1);

      System.out.print(c2);

      System.out.println(c3);

      

      //Wrapper Class: Character

      char a1 = 'a';

      char a2 = 'b';

      char a3 = 'c';

      System.out.print(Character.toUpperCase(a1));

      System.out.print(Character.toUpperCase(a2));

      System.out.println(Character.toUpperCase(a3));

      

   }

 

Booleans

Code:

     public static void main(String[] args) {

      boolean b1 = true;

      boolean b2 = false;

      

      System.out.println("The value of b1 is " + b1);

      //true

      

      System.out.println("The value of b2 is " + b2);

      //false

      

      boolean b3 = !b1;

      System.out.println("The value of b3 is " + b3);

      // false

      

      int i = 0; //NOT true in java

      boolean b4 = (i != 0); //translate int to boolean

      System.out.println("The value of b4 is " + b4);

      //false

 

      String s1 = "true"; // or TRUE

      boolean b5 = Boolean.parseBoolean(s1);

      System.out.println("The value of b5 is " + b5);

      // true

      

      String s2 = "FALSE"; // or false

      boolean b6 = Boolean.parseBoolean(s2);

      System.out.println("The value of b6 is " + b6);

      // false

      

      String s3 = "BLAH"; // =  false

      boolean b7 = Boolean.parseBoolean(s3);

      System.out.println("The value of b7 is " + b7);

      //false

   }

 

StringOutput

Code:

public class Main {

 

   public static void main(String[] args) {

      char c = 'z';

      boolean bool = true;

      byte b = 127;

      short s = 32000;

      int i = 2000000;

      long l = 10000000L;

      float f = 1234245.435234f;

      double d = 112312312331.34;

      

      System.out.println(c);

      System.out.println(bool);

      System.out.println(b);

      System.out.println(s);

      System.out.println(i);

      System.out.println(l);

      System.out.println(f);

      System.out.println(d); 

/*

32000

2000000

10000000

1234245.4

1.1231231233134E11

*/

      // first value in the print Math or String

      System.out.println("The value of s is " + s);

      //The value of s is 32000

      

      // if one String is involved, whole thing is string

      System.out.println(s + " The value of s is ");

      //32000 The value of s is

      

      // math converted first, then tacked onto string

      // String first, no math...all string

 

      // needs 'Date' package

      Date myDate = new Date();

      System.out.println("The new date is " + myDate);

      //The new date is Fri Aug 03 14:55:55 CDT 2012       

   }

 

}

Calculator

Code:

import java.io.*;

 

public class Calculator {

 

   public static void main(String[] args) {

      String s1 = getInput("Enter a numeric value: ");

      String s2 = getInput("Enter a numeric value: ");

      

      double d1 = Double.parseDouble(s1);

      double d2 = Double.parseDouble(s2);

      double result = d1 + d2;

      System.out.println("The answer is " + result);

//Enter a numeric value: 10

//Enter a numeric value: 25.5

//The answer is 35.5

 

/*

Enter a numeric value: xyz

Enter a numeric value: abc

Exception in thread "main" java.lang.NumberFormatException: For input string: "xyz"

   at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)

   at java.lang.Double.parseDouble(Unknown Source)

   at Calculator.main(Calculator.java:9)

*/

      

   }

  

   private static String getInput(String prompt) {

      BufferedReader stdin = new BufferedReader(

                 new InputStreamReader(System.in));

 

      System.out.print(prompt);

      System.out.flush();

      

      try {

            return stdin.readLine();

      } catch (Exception e) {

            return "Error: " + e.getMessage();

      }

   }

 

}

 

 

CompareStrings

Code:

public class Main {

 

   public static void main(String[] args) {

      int monthNumber = 7;

      

      if (monthNumber >= 1 && monthNumber <=3) {

            System.out.println("You're in Quarter 1");

      }   

      else if (monthNumber >= 4 && monthNumber <=6) {

            System.out.println("You're in Quarter 2");

      }

      else {

            System.out.println("You're not in the first half of the year!");

      }

   }

}

 

public class CompareStrings {

 

   public static void main(String[] args) {

      String month = "February";

      

      if (month.equals("February")) {

            System.out.println("It's the second month!");

      }

 

   }

 

}