Saturday, September 13, 2008

Java Basics

Basic Example With User Input
import java.util.Scanner;
public class Sample1
{
public static void main( String args[] )
{
Scanner userInput = new Scanner( System.in );
int number1, half;

System.out.print( "Enter integer: " );
number1 = userInput.nextInt();

half = number1 /2;
System.out.printf( "Half of %d is %d\n", number1, half );
}
}

Read Next User Input
y = userInput.nextInt();
y = userInput.nextFloat();
x = userInput.nextDouble( );
y = userInput.next(); // for one word
y = userInput.nextLine(); // string

char c1 = userInput.findInLine(".").charAt(0); // for one char
int z = userInput.nextInt();
String word2 = userInput.next( ); // one word only
String line = userInput.nextLine( );

Data Types
all variables must be declared before using
* int : integer
* long : large integer
* float : small number with decimals
* double : large number with decimals
* char : a single character.
* String : immutable, once created, values can't change

int n3 = 2 , n4; // integer - can be defined
double d2 = 75.25 , d8;
long d3 = 450000;
String n5 = "abc de"; // string takes double quotes
char d5 = 'A'; // 1 char only with single quote

Math
int sum = number1 + number2  ;    // add numbers
gradeAv = (double)(grade1 + grade2)/2; // double to show decimal places

Assignments:
i += 2; // Same as "i = i + 2"
i -= 2; // Same as "i = i - 2"
i /= 2; // Same as "i = i / 2"
i *= 2; // Same as "i = i * 2"
i %= 2; // Same as "i = I % 2"

System.out.println(c++ ); // print then postincrement
System.out.println(++c ); // preincrement then print

User Input From Keyboard
import java.util.Scanner;
Scanner userInput = new Scanner(System.in);
number1 = userInput.nextInt();

Main Method
public static void main(String[] args)
{
} // end method main

Display Results On Screen -or-
Prompt For User Input
System.out.println("You entered " + d1 + " and " + d2);

System.out.print( "Enter 1st grade: " ); // cursor stays on line
System.out.println("Next enter two numbers.");

If Then Else

if (y == 1)
{
x = 1+2;
}
else if (y == 2)
{
x = 1*2;
}
else
{
x = 1/2;
}

Printf Formatting
%[flags][width][.precision]conversion

%-5c lj one character in a five character space
%+0d signed, padded integer
%.2f 2 decimal places

conversion specifying how to display the argument:
'd': decimal integer 'f': float
'c': 1 character 's': string.

flags: '-' LJ '^' uppercase
'+' sign '0' padded

\n - move cursor to new line

System.out.printf("n1 = %d \n", n1); // d for integer & long
System.out.printf("r1 = %.2f \n", r1); // f for float & double
System.out.printf("n5 = %s \n", n5); // s for string
System.out.printf("c1 = %c \n", c1); // c for one character
System.out.printf("n2 = %+d \n", n2); // whole # '+' for + or minus
System.out.printf("n4 = %20s \n", n4); // 20 character rj string
System.out.printf("n3 = %-20s\n", n3); // 20 character lj string

System.out.printf("All Data : %d %d %.2f %s %c \n",
n1, d1, r1, s2, n2 );

Comment
// this is a comment

Escape A Character
\  (i.e., "\"" would print " )

Continuation
System.out.printf( "Student:%s "   +
"Status:%s \n",
studentName, passStatus );

Creating An Instance Of A Class
   Func a1  = new Func();             // the class is 'Func'.    the instance is 'a1'
Scanner inp = new Scanner(System.in); // the class is 'Scanner'. the instance is 'inp'

fileName+ .java
public class fileName  // the exact name (including case) of the actual file name

.java is the source code
.class is the object code

Clear \n From Buffer after Number and before String or Character
This is necessary if you've requested a number from the user, and then want to get alphabetic data. If you don't do this, the prompt will be on the same line with the numeric answer.
String junk = userInput.nextLine( );

Words, Misc.
class - String is a class, as is Math
public class Student1
Scanner userInput = new Scanner( System.in );
public - program is accessible to any other code
method/function - list of things to do with a name. i.e., main
instance or object - userInput
inheritance - a new class enhances an existing class members.
superclass - the existing class
subclass - the new class
Constructors - create an instance/object of a class.

Two kinds of members in a class:
1. data (sometimes called fields)
2. methods or functions

// The object owns instance variables but not static variables.
final int size = 5; // 'final' indicates a constant. this is a data member
static int total = 0; // Static indicates a class variable -- they don't belong to the instance. static is shared by all objects.
myArray3 m1 = new myarray(); //m1 (an instance) would own a set of the variables
myArray3 m2 = new myarray(); //m2 (an instance) would own a set of the variables

//only 1 extend in a program. 1 parent can have more than one child.
      class myArray1 
class myArray2 extend myArray1
final class myArray3 extend myArray2 // can't have any children

public void computeTotal() // Method Header
{
t = n1+n2; // Method Body
System.out.printf("TOTAL = %d",t); // Method Body
}

Assigment: i += 2;
Assignment: a = b = c = 0; //evaluated from right to left
Declaration: int g1;

No comments:

Post a Comment