-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of code samples for Java Succinctly Part 2.
- Loading branch information
0 parents
commit 36a563a
Showing
113 changed files
with
3,524 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>Example 1 Packages</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.8 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package MainPackage; | ||
|
||
// import OtherPackage.*; | ||
import OtherPackage.OtherClass; | ||
|
||
public class MainClass { | ||
public static void main(String[] args) { | ||
OtherClass o = new OtherClass(); | ||
o.SayHello(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package OtherPackage; | ||
|
||
public class OtherClass { | ||
public void SayHello() { | ||
System.out.println("Nope, can't be bothered, say it yourself!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package MainPackage; | ||
|
||
public class MainClass { | ||
public static void main(String[] args) { | ||
System.out.println("This class belongs to the MainPackage package!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package OtherPackage; | ||
|
||
public class OtherClass { | ||
public void SayHello() { | ||
System.out.println("Nope, can't be bothered, say it yourself!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package MainPackage; | ||
|
||
// import OtherPackage.*; | ||
import OtherPackage.OtherClass; | ||
|
||
public class MainClass { | ||
public static void main(String[] args) { | ||
OtherClass o = new OtherClass(); | ||
o.SayHello(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.PrintWriter; | ||
|
||
public class MainClass { | ||
public static void main(String[] args) throws FileNotFoundException { | ||
|
||
// Create a file: | ||
File myFile = new File("Example.txt"); | ||
|
||
// Create a writer using the file | ||
PrintWriter writer = new PrintWriter(myFile); | ||
|
||
// Write a line of text to the file | ||
writer.println("This is some example text!"); | ||
|
||
// Close the writer | ||
writer.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.PrintWriter; | ||
|
||
public class MainClass { | ||
public static void main(String[] args) { | ||
|
||
// Create a file: | ||
File myFile = new File("Example.txt"); | ||
|
||
// Surround all file manipulation with try/catch | ||
try { | ||
// Create a writer using the file | ||
PrintWriter writer = new PrintWriter(myFile); | ||
|
||
// Write a line of text to the file | ||
writer.println("This is some example text!"); | ||
|
||
// Close the writer | ||
writer.close(); | ||
|
||
} | ||
catch (FileNotFoundException e) { | ||
// File could not be opened, show an error message: | ||
System.out.println("The file could not be opened."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
public class MainClass { | ||
public static void main(String[] args) { | ||
try { | ||
// Create a file writer with the "append" parameter as "true": | ||
FileWriter file = new FileWriter("Example.txt", true); | ||
|
||
// Create a writer object from the file: | ||
PrintWriter writer = new PrintWriter(file); | ||
|
||
// Write some new text: | ||
writer.println("This text will be added to the end!"); | ||
|
||
// Close the writer: | ||
writer.close(); | ||
} | ||
catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.PrintWriter; | ||
|
||
public class MainClass { | ||
public static void main(String[] args) { | ||
File file = new File("test.txt"); | ||
try { | ||
PrintWriter out = new PrintWriter(file); | ||
// Writing Text | ||
out.println("Be good. If you can't be good, be lucky!\n\t~ Alan Davis"); | ||
|
||
// Characters/floats/Boolean/doubles are all written in | ||
// human readable form: | ||
out.println( 129 ); // Integers | ||
out.println( 2.7183f ); // Floats | ||
out.println( true ); // Boolean | ||
out.println( 1.618034 ); // Double | ||
// Close writers after using them so they can be opened by | ||
// other programs: | ||
out.close(); | ||
} | ||
catch (FileNotFoundException e) { | ||
System.out.println("File not found: " + e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// \n causes a new line: | ||
System.out.print("First line\nSecondline!"); | ||
|
||
// \t inserts a tab, i.e. a small block of whitespace | ||
System.out.print("This will be separated from\tThis with a tab!"); | ||
|
||
// Use \" to write " and \' to write ' | ||
System.out.print("Then Jenny said, \"It\'s above the fridge\"."); | ||
|
||
// To print a slash | ||
System.out.print("\\ wears a top hat!"); | ||
|
||
|
||
// Some systems require \r\n in order to use a new line. | ||
// Other systems will read this as two new lines, i.e. one | ||
// carriage return and one new line, both of which look the same. | ||
System.out.print("New\r\nLine!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.util.Scanner; | ||
|
||
public class MainClass { | ||
public static void main(String[] args) { | ||
File file = new File("test.txt"); | ||
|
||
try { | ||
// Create a scanner from our file: | ||
Scanner in = new Scanner(file); | ||
|
||
// Read the first two lines into a string: | ||
String s = in.nextLine() + in.nextLine(); | ||
|
||
// Reading variables: | ||
int i = in.nextInt(); | ||
float f = in.nextFloat(); | ||
boolean b = in.nextBoolean(); | ||
double d = in.nextDouble(); | ||
|
||
// Close the scanner: | ||
in.close(); | ||
|
||
// Print out the results: | ||
System.out.println( | ||
"String: " + s + "\n" + | ||
"int: " + i + "\n" + | ||
"float: " + f + "\n" + | ||
"boolean: " + b + "\n" + | ||
"double: " + d ); | ||
} | ||
catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import java.io.Serializable; | ||
|
||
public class Animal implements Serializable { | ||
// Member variables | ||
float height; | ||
String name; | ||
boolean extinct; | ||
|
||
// Constructor | ||
public Animal(String name, float height, boolean extinct) { | ||
this.name = name; | ||
this.height = height; | ||
this.extinct = extinct; | ||
} | ||
|
||
// Output method | ||
public void print() { | ||
System.out.println("Name: " + name + "\n" + | ||
"Height: " + height + "\n" + | ||
"Extinct: " + extinct + "\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.ObjectOutputStream; | ||
import java.io.Serializable; | ||
|
||
public class MainClass implements Serializable { | ||
public static void main(String[] args) throws FileNotFoundException, IOException { | ||
|
||
// Create some animals from our Serializable class: | ||
Animal stego = new Animal("Stegosaurus", 12.5f, true); | ||
Animal croc = new Animal("Crocodile", 3.2f, false); | ||
Animal mozzie = new Animal("Mosquito", 0.2f, false); | ||
|
||
// Output to the console: | ||
stego.print(); | ||
croc.print(); | ||
mozzie.print(); | ||
|
||
// Specify the name of our file: | ||
File file = new File("animals.dat"); | ||
|
||
// Create a FileOutputStream for writing to the file | ||
FileOutputStream fileOutput = new FileOutputStream(file); | ||
|
||
// Create object output stream to write serialized objects | ||
// to the file stream: | ||
ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput); | ||
|
||
// Write our objects to the stream: | ||
objectOutput.writeObject(stego); | ||
objectOutput.writeObject(croc); | ||
objectOutput.writeObject(mozzie); | ||
|
||
// Close the streams: | ||
objectOutput.close(); | ||
fileOutput.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.Serializable; | ||
|
||
public class MainClass implements Serializable { | ||
public static void main(String[] args) throws FileNotFoundException, IOException { | ||
|
||
// Specify the name of our file: | ||
File file = new File("animals.dat"); | ||
|
||
// Declare an array to hold the animals we read: | ||
Animal[] animals = new Animal[3]; | ||
|
||
// Create a file and an object input stream: | ||
FileInputStream fileInput = new FileInputStream(file); | ||
ObjectInputStream objectInput = new ObjectInputStream(fileInput); | ||
|
||
// Read the objects from the file: | ||
try { | ||
animals[0] = (Animal) objectInput.readObject(); | ||
animals[1] = (Animal) objectInput.readObject(); | ||
animals[2] = (Animal) objectInput.readObject(); | ||
|
||
// Close the streams: | ||
objectInput.close(); | ||
fileInput.close(); | ||
} | ||
catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
// Print the objects: | ||
System.out.println("Objects read from file: "); | ||
for(int i = 0; i < 3; i++) { | ||
animals[i].print(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import java.io.EOFException; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
|
||
public class MainClass implements Serializable { | ||
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { | ||
// Specify the name of our file: | ||
File file = new File("animals.dat"); | ||
|
||
// Declare an array to hold the animals we read: | ||
ArrayList<Animal> animals = new ArrayList<Animal>(); | ||
|
||
// Create a file and an object input stream: | ||
FileInputStream fileInput = new FileInputStream(file); | ||
|
||
ObjectInputStream objectInput = new ObjectInputStream(fileInput); | ||
try { | ||
// Read all the animals specified in the file, | ||
// storing them in an array list: | ||
for(;;) { | ||
animals.add((Animal) objectInput.readObject()); | ||
} | ||
} | ||
catch (EOFException e) { | ||
// We do not have to do anything here, this is the normal | ||
// termination of the loop above when all objects have | ||
// been read. | ||
} | ||
|
||
// Close the streams: | ||
objectInput.close(); | ||
fileInput.close(); | ||
for(Animal a: animals) { | ||
a.print(); | ||
} | ||
} | ||
} |
Oops, something went wrong.