ADD THE SLIDER CODE HERE

-

Thursday, March 15, 2012

Chapter 03: Run first, Learn next




This chapter contains some example programs for practice. Execute them one by one and see if the desired output is achieved. Do not think about how it works. Just run it. The more programs you run, more confidence you will gain.

We will also see PATH settings and few other concepts.

Example 1: Finding Area and Perimeter of a Circle

import java.io.*;
class CircleArea{
  public static void main(String[] args){
  int r=0;
 try{
  BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter Radius of Circle  : ");
  r = Integer.parseInt(br1.readLine());
  double area = java.lang.Math.PI*r*r;
  System.out.println("Area of Circle : "+area);
  double  perimeter =2*java.lang.Math.PI*r ;
  System.out.println("Perimeter of Circle : "+perimeter);
  }
  catch(Exception e){
  System.out.println("Error : "+e);
  }  
  }
}
Example 2: Printing Calendar of a Particular Month and Year

import java.util.*;
import java.text.*;

public class MonthCalender {
  
  public final static String[] monthcalender = {
  "January", "February", "March", "April", "May", "June", "July","August", "September", "October", "November", "December"};
  
  public final static int daysinmonths[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  
  private void displayMonth(int month, int year) {
  
  // The number of days to leave blank at
  // the start of this month.
  
  int blankdays = 0;
  System.out.println("  " + monthcalender[month] + " " + year);
  
  if (month < 0 || month > 11) {
  throw new IllegalArgumentException(
  "Month " + month + " is not valid and must lie in between 0 and 11");
  }
  
  GregorianCalendar cldr = new GregorianCalendar(year, month, 1);
  System.out.println("Sunday Monday Tuesday Wednesday Thursday Friday Saturday");
  
  // Compute how much to leave before before the first day of the month.
  // getDay() returns 0 for Sunday.
  
  blankdays = cldr.get(Calendar.DAY_OF_WEEK)-1;
  int daysInMonth = daysinmonths[month];
  
  if (cldr.isLeapYear(cldr.get(Calendar.YEAR)) && month == 1) {
    ++daysInMonth;
  }
  
  // Blank out the labels before 1st day of the month
  for (int i = 0; i < blankdays; i++) {
  System.out.print(" ");
  }
  
  for (int i = 1; i <= daysInMonth; i++) {
  
  if (i<=9) {
  System.out.print(" ");
  }
  System.out.print(i);

  if ((blankdays + i) % 7 == 0) { // Wrap if EOL
  System.out.println();
  }
  else {
  System.out.print(" ");
  }
  }
  }
  
  /**
 * Sole entry point to the class and application.
 * @param args Array of String arguments.
 */
  public static void main(String[] args) {
  
  int mon, yr;
  MonthCalender moncldr = new MonthCalender();

  if (args.length == 2) {
  moncldr.displayMonth(Integer.parseInt(args[0])-1,

 Integer.parseInt(args[1]));
  }
  else {
  Calendar todaycldr = Calendar.getInstance();
  moncldr.displayMonth(todaycldr.get(Calendar.MONTH), todaycldr.get(Calendar.YEAR));
  }
  }
} 

Example 3: Calculating Word Count of a File


import java.io.*;

public class  WordCount{
  private static void linecount(String fName, BufferedReader in) 
  throws IOException{
  long numChar = 0;
  long numLine=0;
  long numWords = 0;
  String line;
  do{
  line = in.readLine();
  if (line != null){
  numChar += line.length();
  numWords += wordcount(line);
  numLine++;
  }
  }while(line != null);
  System.out.println("File Name: " + fName);
  System.out.println("Number of characters: " + numChar);
  System.out.println("Number of words: " + numWords);
  System.out.println("Number of Lines: " + numLine);
  }
  private static void linecount(String fileName){
  BufferedReader in = null;
  try{
  FileReader fileReader = new FileReader(fileName);
  in = new BufferedReader(fileReader);
  linecount(fileName,in);
  }
  catch(IOException e){
  e.printStackTrace();
  }
  }
  private static long wordcount(String line){
  long numWords = 0;
  int index = 0;
  boolean prevWhiteSpace = true;
  while(index < line.length()){
  char c = line.charAt(index++);
  boolean currWhiteSpace = Character.isWhitespace(c);
  if(prevWhiteSpace && !currWhiteSpace){
  numWords++;
  }
  prevWhiteSpace = currWhiteSpace;
  }
  return numWords;
  }
  public static void main(String[] args){
    long numChar = 0;
  long numLine=0;
  String line;
  try{
  if (args.length == 0)
  {
  BufferedReader in = 
  new BufferedReader(new InputStreamReader(System.in));
  line = in.readLine();
  numChar = line.length();
  if (numChar != 0){
  numLine=1;
  }
  System.out.println("Number of characters: " + numChar);
  System.out.println("Number of words: " + wordcount(line));
  System.out.println("Number of lines: " + numLine);
  }else{
  for(int i = 0; i < args.length; i++){
  linecount(args[i]);
  }
  }
  }
  catch(IOException e){
  e.printStackTrace();
  }
  }
}


Our Google Group

Our Facebook Group