MTH 162 Lecture-Module #06:
More Java Details

main()

(Sections 7.15.2 and E.1)

beyond  BlueJ's scheme involving 'manually' using pieces of programs,
standard program element is method appearing as follows:
    public static void main(String[] args) {
    .
    .
    .
    }
Java programs more standard than BlueJ ones put their essential code in the body of such a method  main()
and standard execution invokes  main()
e.g.:
//********************************************************************
//  TempConverter.java       Author: Lewis/Loftus
//
//  Demonstrates the use of primitive data types and arithmetic
//  expressions.
//********************************************************************

public class TempConverter
{
   //-----------------------------------------------------------------
   //  Computes the Fahrenheit equivalent of a specific Celsius
   //  value using the formula F = (9/5)C + 32.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      final int BASE = 32;
      final double CONVERSION_FACTOR = 9.0 / 5.0;

      int celsiusTemp;
      double fahrenheitTemp;

      celsiusTemp = 24;  // value to convert
      fahrenheitTemp  =  celsiusTemp * CONVERSION_FACTOR  +  BASE;

      System.out.println ("Celsius Temperature: " + celsiusTemp);
      System.out.println ("Fahrenheit Equivalent: " + fahrenheitTemp);
   }
}
things like this  main()  which you've seen previously include  SimpleDate.test(),  StockDemo.demo()



rem. use single quotes  ''  to use an individual literal character, e.g.:
String name = "";
System.out.println("Hi, " + name + '!');

the type you can use for characters is  char
used like  int,  String  to declare variables
e.g.:
char  ch;
ch = 'e';
System.out.println("I guess: " + ch);
char  ch0 = '\t', ch1 = 'a', ch2 = '\n', ch3 = 'c', ch4 = '\\';

a couple of basic operations on characters are as follows:
char Character.toUpperCase(char)
char Character.toLowerCase(char)
these do what their names indicate
   
    like some names
        (e.g. "Harry Potter", "Severus Snape", ...)

returning different case of the given character
e.g.  'r'  ↔  'R'
e.g. usage:
char ch1, ch2, ch3, ch4, ch5;
ch1 = 'g';
ch2 = Character.toUpperCase(ch1);   // now |ch2| is 
ch3 = Character.toUpperCase(ch2);   // already uppercase -- get same back: 
ch4 = '%';
ch5 = Character.toUpperCase(ch4);   // not a letter -- get same char. back: '%'

input

add elements as follows to basic program to do input:


public class program_name {
  public static void main(String[] args) {
    
    declarations
      int n;
      double r;
    other_statements
    
    
    // Use |scanner.nextInt()| to input an |int|
    // or |scanner.nextDouble()| to input a |double|
    
    
    other_statements
    }
  }
e.g.:
/**
 * Demonstrates the use of primitive data types and arithmetic
 * expressions.
 * 
 * @author Lewis/Loftus
 */



public class TempConverter
{
    public static void main(String[] args) {
        final int BASE = 32;
        final double CONVERSION_FACTOR = 9.0 / 5.0;

        
        
        System.out.print("Enter Celsius Temperature: ");
        double celsiusTemp 
        
        System.out.println();

        double fahrenheitTemp;
        
        fahrenheitTemp  =  celsiusTemp * CONVERSION_FACTOR  +  BASE;
        
        System.out.println ("Celsius Temperature: " + celsiusTemp);
        System.out.println("Fahrenheit Equivalent: " + fahrenheitTemp);
    }
}
note want  System.out.println()  sometimes,  System.out.print()  others

input some other types as follows:
  String s = scanner.nextLine();
  char ch = scanner.nextLine().charAt(0);
e.g.:




(Copyright © 2009 by Hugh McGuire   — for thoughts about this, see   http://www.cis.gvsu.edu/~mcguire/teaching/copyright_thoughts.html .)