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(); } } }
first of all, thank you for your sincere work and choosing to uploading it for free, when will be the next chapters coming?
ReplyDeletewaiting for next tutorials
ReplyDeleteplz help me, i'v installed java s\w in D drive, but while running its showing access denied. whats the prob with this?
ReplyDeleteRun the command prompt as administrator and try again. If you right click command prompt icon, you will see Run as Administrator option.
Deletetry to upload chapter 4 soon
ReplyDeletesorry about the delay. I will upload soon.
DeleteHi... really very useful to learn from this video..tat too in my mother toungue.... :)
ReplyDeletethank you sir i am waiting for chapter 4 plz upload
ReplyDeletehai sir,
ReplyDeletei need next chapter pls upload ...!
sir i need java videos..is der only 3 videos or having more..please tel me
ReplyDeletehai sir pls upload 4 chapter .....pls
ReplyDeletepls pls .. upload 4 chapter....
ReplyDeletehi sir pls upload chapter 4.i wanna be a programmer..plzz help me by uploading chapter 4..by your student richard
ReplyDeleteHello sir pls uploda it soon !!
ReplyDelete