Experiment 3

Strings and Java IO

1, Objectives

1)String and StringBuilder

2)Java IO

3)Collections

2, Contents

(Copy the results or source codes after each exercise. Rename this document as “Id – Name.doc” and hand in it onto yvsou.com. )

1) (Random Sentences) Write an application that uses random-number generation to createsentences. Use four arrays of strings called article, noun, verb and preposition. Create a sentenceby selecting a word at random from each array in the following order: article, noun, verb, preposition, article and noun. As each word is picked, concatenate it to the previous words in the sentence.The words should be separated by spaces. When the final sentence is output, it should startwith a capital letter and end with a period. The application should generate and display 20 sentences.

The article array should contain the articles "the", "a", "one", "some", "every"and "any"; the nounarray should contain the nouns "boy", "girl", "dog", "town" and "car"; the verb array should containthe verbs "drove", "jumped", "ran", "walked" and "skipped"; the preposition array shouldcontain the prepositions "to", "from","before", "over", "under" and "on".

packagerandomsen;

importjava.util.Random;

public class randomse

{

public static void main(String[] args)

{

String [] article={"the","a","one","some","every","any"};

String [] noun={"boy","girl","dog","town","car"};

String [] verb={"drove","jumped","ran","walked","skipped"};

String [] preposition={"to","form","before","over","under","on"};

for(inti=0;i<20;i++)

{

Random rand = new Random();

int num1 = rand.nextInt(6);

String string1=article[num1];

int num2 = rand.nextInt(5);

String string2=noun[num2];

int num3 = rand.nextInt(5);

String string3=verb[num3];

int num4 = rand.nextInt(6);

String string4=preposition[num4];

int num5 = rand.nextInt(6);

String string5=article[num5];

int num6 = rand.nextInt(5);

String string6=noun[num6];

StringBuilderrandomsen=new StringBuilder(10);

randomsen.insert(0, ".");

randomsen.insert(0, string6);

randomsen.insert(0, " ");

randomsen.insert(0, string5);

randomsen.insert(0, " ");

randomsen.insert(0, string4);

randomsen.insert(0, " ");

randomsen.insert(0, string3);

randomsen.insert(0, " ");

randomsen.insert(0, string2);

randomsen.insert(0, " ");

string1 = string1.substring(0, 1).toUpperCase() + string1.substring(1);

randomsen.insert(0, string1);

randomsen.insert(0, " ");

System.out.println(randomsen.toString());

}

}

}

2) List all the directories and files. Make a mark when showing directory. Display the file size after file name with three digits, for example, 123B, 123K, 123M, and so on.

packagelistfiles;

importjava.io.File;

importjava.io.ObjectInputStream.GetField;

public class listf

{

public static void main(String[] args)

{

File f=new File("/Users/liaomengchun/Desktop/井盖图片/破损");

String[] files = f.list();

for(inti=0;i<files.length;i++)

{

File f1=new File("/Users/liaomengchun/Desktop/井盖图片/破损/"+files[i]);

if(f1.isDirectory())

{

System.out.println("Floder:"+f1);

}

else

{

long n=f1.length();

if(n<1024)

{

System.out.println(f1+" "+n+"B");

}

else if(n<1048576)

{

System.out.println(f1+" "+n/1024+"K");

}

else if(n<1073741824)

{

System.out.println(f1+" "+n/1048576+"M");

}

else

{

System.out.println(f1+" "+n/1073741824+"G");

}

}

}

}

}

3) Some objects of Racer class are stored in file “racers.data”.

A. Read the Racer objects from “racers.data”.

B. Find all the Brazil racers and print summary of wins.

C. Compute the winning percentage of every racer and store the name and the percentage in a file. [Hints: winning percentage = wins / starts ]

( ClassRecer and file “racers.data” are presented along with this file. )

4) See Exam 17.8

Use this array:

private static final String[][] letters = { { " ", " ", " " },

{ " ", " ", " " }, { "A", "B", "C" }, { "D", "E", "F" },

{ "G", "H", "I" }, { "J", "K", "L" }, { "M", "N", "O" },

{ "P", "R", "S" }, { "T", "U", "V" }, { "W", "X", "Y" } };