In the evaluation of a Boolean expression
consisting of <Boolean expression 1> && <Boolean expression
2>, if <Boolean expression 1 is false, <Boolean expression 2> is
not evaluated since the overall expression must be false. Likewise, in the evaluation
of <Boolean expression 1> || <Boolean expression 2>, if <Boolean
expression 1> is true, <Boolean expression 2> is not evaluated since
the overall expression must be true. See the Basic Java
Code shortCircuit function for an example.
package quiz1;
public class Person {
private int id;
private String name;
public Person(int id, String name) {
this.id = id;
this.name = name;
}
public boolean equals(Object other) {
return ((Person)other).id == id;
}
public String toString() {
return String.format("%5d\t%s", id, name);
}
}
package quiz1;
public class Quiz {
public static void assign(Person[] people){
String[] names = {"Bugs Bunny",
"Wilma Flinstone",
"Barney Rubble",
"Porky Pig"};
for (int i = 0; i < people.length && i <
names.length; i++){
people[i] = new Person(i, names[i]);
}
}
public static void show(Person[] people) {
for (Person p : people) {
System.out.println(p);
}
}
public static void main(String[] args) {
Person[] people = new Person[3];
assign(people);
show(people);
}
}
int[][] array = new int[2][2];
int i=0, j=0;
while(i < array.length) {
while (j < array[i].length) {
array[i][j++] = array.length * i + j;
}
i++;
}
int[] array = {1, 2, 3, 4};
int[] array = {1, 2, 3, 4};
int sum = 0;
for(int k = 0;k < array.length; k++) {
sum += array[k];
}
char |
String |
M |
Monday |
T |
Tuesday |
W |
Wednesday |
R |
Thursday |
F |
Friday |
anything else |
Not a weekday |
String zDay = "";
char cDay = ' ';
switch (cDay) {
case 'M': zDay = "Monday"; break;
case 'T': zDay = "Tuesday"; break;
case 'W': zDay =
"Wednesday"; break;
case 'R': zDay = "Thursday"; break;
case 'F': zDay = "Friday"; break;
default: zDay = "Not a weekday";
}
package quiz1;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class FileApp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
try {
PrintWriter out = new PrintWriter(new
FileWriter("out.txt"), true);
String str = "";
do {
str = in.next();
StringBuilder builder = new StringBuilder (str);
out.println(builder.reverse());
} while (!str.equalsIgnoreCase("quit"));
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String str = "Big:boys,don’t;cry ";
StringTokenizer tokenizer = new StringTokenizer(str,
":;, ");
while(tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
Display a long listing of file
names in the current working directory
ending in .java
Display a long listing of just the directory name of your home directory.
Display the path of your current working directory.
Create a sub directory of your current working directory named person.
Display the differences between file foo.txt and file bar.txt.
Change you working directory to the person sub-directory of your current working directory.
Creates a link in sub-directory bar called foo.txt to the existing file bar.txt in sub-directory foo. This is a hard link, so these two names may actually be considered the same file. If you are interested in the distinction between hard links and soft links (created with the –s option of ln, see Bullet Proof Unix).
Copies all files with names ending in .java to the sibling directory of the current working directory called person.
Adds execution permission to the user and group of all files with names ending in .class.