Thursday, April 8, 2010

FileStatus

package dummy;

import java.io.*;
import java.util.*;
/**
* Report on a file's status in Java
*/
public class FileStatus {
public static void main(String[] argv) throws IOException {
// Ensure that a filename (or something) was given in argv[0]
if (argv.length == 0) {
System.err.println("Usage: FileStatus filename");
System.exit(1);
}
for (int i = 0; i< argv.length; i++) {
status(argv[i]);
}
}
public static void status(String fileName) throws IOException {
System.out.println("---" + fileName + "---");
// Construct a File object for the given file.
File f = new File(fileName);
// See if it actually exists
if (!f.exists( )) {
System.out.println("file not found");
System.out.println( ); // Blank line
return;
}
// Print full name
System.out.println("Canonical name " + f.getCanonicalPath( ));
// Print parent directory if possible
String p = f.getParent( );
if (p != null) {
System.out.println("Parent directory: " + p);
}
// Check if the file is readable
if (f.canRead( )) {
System.out.println("File is readable.");
}
// Check if the file is writable
if (f.canWrite( )) {
System.out.println("File is writable.");
}
// Report on the modification time.
Date d = new Date( );
d.setTime(f.lastModified( ));
System.out.println("Last modified " + d);
// See if file, directory, or other. If file, print size.
if (f.isFile( )) {
// Report on the file's size
System.out.println("File size is " + f.length( ) + " bytes.");
} else if (f.isDirectory( )) {
System.out.println("It's a directory");
} else {
System.out.println("I dunno! Neither a file nor a directory!");
}
System.out.println( ); // blank line between entries
}
}

EnhancedForDemo

package dummy;

class EnhancedForDemo {
public static void main(String[] args){
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
}
}

EnumDemo

package dummy;

public class EnumDemo {
// Define two enum types -- remember that the definitions
// go OUTSIDE The main() routine!
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
enum Month { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }
public static void main(String[] args) {
Day tgif; // Declare a variable of type Day.
Month libra; // Declare a variable of type Month.
tgif = Day.FRIDAY; // Assign a value of type Day to tgif.
libra = Month.OCT; // Assign a value of type Month to libra.
System.out.print("My sign is libra, since I was born in ");
System.out.println(libra); // Output value will be: OCT
System.out.print("That’s the ");
System.out.print( libra.ordinal() );
System.out.println("-th month of the year.");
System.out.println(" (Counting from 0, of course!)");
System.out.print("Isn’t it nice to get to ");
System.out.println(tgif); // Output value will be: FRIDAY
System.out.println( tgif + " is the " + tgif.ordinal()
+ "-th day of the week.");
// You can concatenate enum values onto Strings!
}
}

DaysInEachMonth1

package dummy;

import java.util.Scanner;

class DaysInEachMonth1 {

public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
int month, numberOfDays = 0;
boolean isLeapYear;

System.out.print("Which month? ");
month = myScanner.nextInt();

switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numberOfDays = 31;
break;

case 4:
case 6:
case 9:
case 11:
numberOfDays = 30;
break;

case 2:
System.out.print("Leap year (true/false)? ");
isLeapYear = myScanner.nextBoolean();
if (isLeapYear) {
numberOfDays = 29;
} else {
numberOfDays = 28;
}
}

System.out.print(numberOfDays);
System.out.println(" days");
}
}

DaysInEachMonth2

package dummy;

import java.util.Scanner;

class DaysInEachMonth2 {

public enum Months { ianuarie, februarie, martie, aprilie,
mai, iunie, iulie, august, septembrie, octombrie, noiembrie, decembrie}

public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
Months month;
String luna;
int numberOfDays = 0;
boolean isLeapYear;

System.out.print("INTRODUCETI LUNA (caractere) ? ");
luna = myScanner.next();

month = Months.valueOf(luna);

switch (month) {
case ianuarie:
case martie:
case mai:
case iulie:
case august:
case octombrie:
case decembrie:
numberOfDays = 31;
break;

case aprilie:
case iunie:
case septembrie:
case noiembrie:
numberOfDays = 30;
break;

case februarie:
System.out.print("AN BISECT (true/false) ? ");
isLeapYear = myScanner.nextBoolean();
if (isLeapYear) {
numberOfDays = 29;
} else {
numberOfDays = 28;
}
}

System.out.print(numberOfDays);
System.out.println(" zile");
}
}

Java Day 020

RECAPITULARE : While, ArrayList, Vector, String Manipulation;

De parcurs cu DEBUG (STEP OVER) si WATCH in Eclipse sau NetBeans : Alphabetize,
TokenTest ;
http://leepoint.net/notes-java/data/collections/lists/ex-alphabetize.html



/*
http://leepoint.net/notes-java/data/collections/lists/ex-alphabetize.html
http://leepoint.net/notes-java/data/collections/lists/arraylist.html
Purpose: An exercise using Scanner, ArrayList, and Collections.sort.
Read words, sort them, print them.
*/

package week03;
import java.util.*;

public class Alphabetize2 {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// ArrayList words = new ArrayList();
Vector words = new Vector();
String myWord = new String();
boolean ePunct=false;
//... Read input one word at a time.
System.out.println("Introduceti cuvinte urmate de . punct - apoi ENTER");

//... Read input one word at a time, adding it to an array list.
while (!ePunct ) {
myWord = in.next();
words.add(myWord);
ePunct = (myWord.compareTo(".")==0);
}

//... Sort the words.
Collections.sort(words);

//... Print the sorted list.
System.out.println("\n\nSorted words\n");
for (String word : words) {
System.out.println(word);
}
}
}