You are on page 1of 9

mport java. swing.*; import java.awt.event.*; import java.util.

Calendar; class CalendarExample { public static void main(String args[]) { String months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; Calendar calendar = Calendar.getInstance(); System.out.println("Present Date: "); System.out.println(Months[calendar.get(Calendar.MONTH)]); System.out.println(" " + calendar.get(Calendar.DATE) + " "); System.out.println(calendar.get(Calendar.YEAR)); System.out.println("Present Time: "); System.out.println(calendar.get(Calendar.HOUR) + ":"); System.out.println(calendar.get(Calendar.MINUTE) + ":"); System.out.println(calendar.get(Calendar.SECOND)); calendar.set(Calendar.HOUR, 10); calendar.set(Calendar.MINUTE, 29); calendar.set(Calendar.SECOND, 22); System.out.println("Edit time: "); System.out.println(calendar.get(Calendar.HOUR) + ":"); System.out.println(calendar.get(Calendar.MINUTE) + ":"); System.out.println(calendar.get(Calendar.SECOND)); } }

com.mindprod.common11.BigDate java.text.DateFormat java.text.SimpleDateFormat java.util.Calendar java.util.Date java.util.GregorianCalendar java.util.TimeZone

// Demonstrate Calendar import java.util.Calendar; class CalendarDemo { public static void main(String args[]) { String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; // Create a calendar initialized with the // current date and time in the default

// locale and timezone. Calendar calendar = Calendar.getInstance(); // Display current time and date information. System.out.print("Date: "); System.out.print(months[calendar.get(Calendar.MONTH)]); System.out.print(" " + calendar.get(Calendar.DATE) + " "); System.out.println(calendar.get(Calendar.YEAR)); System.out.print("Time: "); System.out.print(calendar.get(Calendar.HOUR) + ":"); System.out.print(calendar.get(Calendar.MINUTE) + ":"); System.out.println(calendar.get(Calendar.SECOND)); // Set the time and date information and display it. calendar.set(Calendar.HOUR, 10); calendar.set(Calendar.MINUTE, 29); calendar.set(Calendar.SECOND, 22); System.out.print("Updated time: "); System.out.print(calendar.get(Calendar.HOUR) + ":"); System.out.print(calendar.get(Calendar.MINUTE) + ":"); System.out.println(calendar.get(Calendar.SECOND)); } }

import java.io.*; class FileWrite { public static void main(String args[]) { try{ // Create file FileWriter fstream = new FileWriter("out.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write("Hello Java"); //Close the output stream out.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } }

import java.io.*; class FileRead { public static void main(String args[]) { try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("textfile.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println (strLine); } //Close the input stream in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } }

calender

/************************************************************************* * Compilation: javac Calendar.java * Execution: java Calendar M Y * * This program takes the month M and year Y and prints a * calendar for that month. * * % java Calendar 7 2005 * July 2005 * S M T W Th F S * 1 2 * 3 4 5 6 7 8 9 * 10 11 12 13 14 15 16 * 17 18 19 20 21 22 23 * 24 25 26 27 28 29 30 * 31 * *************************************************************************/ public class Calendar { /********************************************************************** * Given the month (M), day (D), and year (Y), return which day * of the week it falls on according to the Gregorian calendar. * For M use 1 for January, 2 for February, and so forth. Outputs * 0 for Sunday, 1 for Monday, and so forth. **********************************************************************/ public static int day(int M, int D, int Y) { int y = Y - (14 - M) / 12; int x = y + y/4 - y/100 + y/400; int m = M + 12 * ((14 - M) / 12) - 2; int d = (D + x + (31*m)/12) % 7; return d; }

// return true if the given year is a leap year public static boolean isLeapYear(int year) { if ((year % 4 == 0) && (year % 100 != 0)) return true; if (year % 400 == 0) return true; return false; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); // month (Jan = 1, Dec = 12) int Y = Integer.parseInt(args[1]); // year // months[i] = name of month i String[] months = { "", // leave empty so that months[1] = "January" "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; // days[i] = number of days in month i int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // check for leap year if (M == 2 && isLeapYear(Y)) days[M] = 29; // print calendar header StdOut.println(" " + months[M] + " " + Y); StdOut.println(" S M Tu W Th F S"); // starting day int d = day(M, 1, Y); // print the calendar for (int i = 0; i < d; i++) StdOut.print(" "); for (int i = 1; i <= days[M]; i++) { StdOut.printf("%2d ", i); if (((i + d) % 7 == 0) (i == days[M])) StdOut.println(); } } }

public class Gambler { public static int stake int goal int T void main(String[] args) { = Integer.parseInt(args[0]); = Integer.parseInt(args[1]); = Integer.parseInt(args[2]); // gambler's stating bankroll // gambler's desired bankroll // number of trials to perform

int bets = 0; int wins = 0;

// total number of bets made // total number of games won

// repeat N times for (int t = 0; t < T; t++) { // do one gambler's ruin simulation int cash = stake; while (cash > 0 && cash < goal) { bets++; if (Math.random() < 0.5) cash++; else cash--; } if (cash == goal) wins++; esired goal? } // print results System.out.println(wins + " wins of " + T); System.out.println("Percent of games won = " + 100.0 * wins / T); System.out.println("Avg # bets = " + 1.0 * bets / T); } }

// win $1 // lose $1 // did gambler go achieve d