Monday, June 25, 2012

First Java Class

//this is equivalent to a name space
//it is used to group related classes
package com.spconger.www;

//import java objects from the library
import java.util.ArrayList;
import java.util.Scanner;


public class FirstClass {

 /**
  * @param args
  */
 //all programs start with main
 public static void main(String[] args) {
  FirstClass fc = new FirstClass();
  //fc.method1();
  //fc.ifExample();
  fc.simpleForLoop();
  fc.simpleWhileLoop();
 }
 
 private void method1(){
  //prints a line out to the console
  System.out.println("This is a java program");
 }
 
 private void ifExample(){
  //the scanner is an object that reads input
  //from the console
  Scanner scan = new Scanner(System.in);
  
  
  System.out.println("Enter a number");
  int number = scan.nextInt();
 //if example
  if(number > 10){
   System.out.println("Take over the world");
   
  }
  else if(number > 5){
   System.out.println("oh well");
  }
  else {
   System.out.println("Stay in and have a beer");
  }
  
  
 }
 

 private void simpleForLoop(){
  
   for (int i=0;i<=10;i++){
    System.out.println(i);
   }
 }
 
 private void simpleWhileLoop(){
  
  int num = 0;
  
  while (num < 10){
   System.out.println(num);
   num ++;
  }
 }
 
 private void simpleDoLoop(){
  int num=10;
  
  do{
   System.out.println(num);
   num ++;
  }while(num<10);
 }
 
 private void simpleArray(){
  int myArray[] = new int[4] ;
  ArrayList mylist = new ArrayList();
  
  //java uses a wrapper class to provide conversions
  //between primative types like int. Integer has methods
  // to parse an integer from a string
  //or to convert an integer into a string 
  String myNumberString = "1234";
  int myInteger=Integer.parseInt(myNumberString);
  String intString=Integer.toString(myInteger);
 }

}

1 comment: