Lab 1
Lab 1
1: Stewie
Write a complete Java program in a class named Stewie that prints the following output:
//////////////////////
|| Victory is mine! ||
\\\\\\\\\\\\\\\\\\\\\\
1
public class Stewie {
2
public static void main(String[] args) {
3
System.out.println("//////////////////////");
4
System.out.println("|| Victory is mine! ||");
5
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
6
}
7
}
1.2: Spikey
Write a complete Java program in a class named Spikey that prints the following output:
\/
\\//
\\\///
///\\\
//\\
/\
1
public class Spikey {
2
public static void main(String[] args) {
3
System.out.println(" \\/");
4
System.out.println(" \\\\//");
5
System.out.println("\\\\\\///");
1
6
System.out.println("///\\\\\\");
7
System.out.println(" //\\\\");
8
System.out.println(" /\\");
9
}
10
}
1.3: WellFormed
Write a complete Java program in a class named WellFormed that prints the following output:
A System.out.println statement
has ( and ) and usually a
String that starts and ends
with a " character.
(But we type \" instead!)
1
public class WellFormed {
2
public static void main(String[] args) {
3
System.out.println("A well-formed Java program has\na main method with { and }\nbraces.\n");
4
System.out.println("A System.out.println statement");
5
System.out.println("has ( and ) and usually a\nString that starts and ends");
6
System.out.println("with a \" character.");
7
System.out.println("(But we type \\\" instead!)");
8
};
9
};
1.4: Difference
Write a complete Java program in a class named Difference that prints the following output:
2
a ' and a "? Or between a " and a \"?
1
public class Difference {
2
public static void main(String[] args) {
3
System.out.println("What is the difference between");
4
System.out.println("a ' and a \"? Or between a \" and a \\\"?\n");
5
System.out.println("One is what we see when we're typing our program.");
6
System.out.println("The other is what appears on the \"console.\"");
7
}
8
}
1.5: MuchBetter
Write a complete Java program in a class named MuchBetter that prints the following output:
A "quoted" String is
'much' better if you learn
the rules of "escape sequences."
Also, "" represents an empty String.
Don't forget: use \" instead of " !
'' is not the same as "
1
public class MuchBetter {
2
public static void main(String[] args) {
3
System.out.println("A \"quoted\" String is");
4
System.out.println("'much' better if you learn");
5
System.out.println("the rules of \"escape sequences.\"");
6
System.out.println("Also, \"\" represents an empty String.");
3
7
System.out.println("Don't forget: use \\\" instead of \" !");
8
System.out.println("'' is not the same as \"");
9
}
10
}
1.6: Meta
Write a complete Java program called Meta whose output is the text that would be the
source code of a Java program named Hello that prints "Hello, world!" as its output:
Your program must produce exactly the output shown in order to pass (using exactly four
spaces for each increment of indentation in the output).
Type your solution here:
1
public class Meta {
2
public static void main(String[] args) {
3
System.out.println("public class Hello {");
4
System.out.println(" public static void main(String[] args) {");
5
System.out.println(" System.out.println(\"Hello, world!\");");
6
System.out.println(" }");
7
System.out.println("}");
8
}
9
}
1.7: Mantra
4
Write a complete Java program in a class named Mantra that produces the following output.
Remove its redundancy by adding a method.
1
public class Mantra {
2
public static void main(String[] args) {
3
message1();
4
System.out.print("\n");
5
message1();
6
}
7
public static void message1() {
8
System.out.println("There's one thing every coder must understand:");
9
System.out.println("The System.out.println command.");
10
}
11
}
1.8: Stewie2
Write a complete Java program in a class named Stewie2 that prints the following output.
Use at least one static method besides main to remove redundancy.
//////////////////////
|| Victory is mine! ||
\\\\\\\\\\\\\\\\\\\\\\
|| Victory is mine! ||
\\\\\\\\\\\\\\\\\\\\\\
|| Victory is mine! ||
\\\\\\\\\\\\\\\\\\\\\\
|| Victory is mine! ||
\\\\\\\\\\\\\\\\\\\\\\
|| Victory is mine! ||
\\\\\\\\\\\\\\\\\\\\\\
1.9: Egg
Write a complete Java program in a class named Egg that displays the following output:
_______
/ \
/ \
-"-'-"-'-"-
\ /
\_______/
1
public class Egg {
2
public static void main(String[] args) {
6
3
System.out.println(" _______");
4
System.out.println(" / \\");
5
System.out.println("/ \\");
6
System.out.println("-\"-'-\"-'-\"-");
7
System.out.println("\\ /");
8
System.out.println(" \\_______/");
9
}
10
}
1.10: Egg2
Write a complete Java program in a class named Egg2 that generates the following output.
Use static methods to show structure and eliminate redundancy in your solution.
_______
/ \
/ \
\ /
\_______/
-"-'-"-'-"-
_______
/ \
/ \
\ /
\_______/
-"-'-"-'-"-
\ /
\_______/
_______
/ \
/ \
-"-'-"-'-"-
\ /
\_______/
1
public class Egg2 {
2
public static void main(String[] args) {
3
nuatren();
4
nuaduoi();
7
5
phancach();
6
nuatren();
7
nuaduoi();
8
phancach();
9
nuaduoi();
10
nuatren();
11
phancach();
12
nuaduoi();
13
}
14
15
public static void nuatren() {
16
System.out.println(" _______");
17
System.out.println(" / \\");
18
System.out.println("/ \\");
19
}
20
public static void nuaduoi() {
21
System.out.println("\\ /");
22
System.out.println(" \\_______/");
23
}
24
public static void phancach() {
25
System.out.println("-\"-'-\"-'-\"-");
26
}
27
}