Skip to content

Commit

Permalink
Added Lab2-1 and Lab5-1 Solution
Browse files Browse the repository at this point in the history
Getting start for M5
  • Loading branch information
Luke Zhang committed Jan 27, 2016
1 parent f2de6d2 commit 4516fa4
Show file tree
Hide file tree
Showing 56 changed files with 2,169 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lab2-1/lab2-1-lib/.classpath
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.7"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions lab2-1/lab2-1-lib/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Lab2-1-Lib</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>
11 changes: 11 additions & 0 deletions lab2-1/lab2-1-lib/.settings/org.eclipse.jdt.core.prefs
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.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
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.7
116 changes: 116 additions & 0 deletions lab2-1/lab2-1-lib/src/edu/roshulman/csse374/editor/TextEditor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package edu.roshulman.csse374.editor;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
* <p>
* The awesomest text editor ever developed in the
* awesomest language ever created by humans!
* </p>
*
* <p>
* It reads the text from the supplied {@link InputStream} and writes the text to
* the supplied {@link OutputStream} when a user closes the application.
* </p>
*
* @author Chandan R. Rupakheti ([email protected])
*/
public class TextEditor {
private JFrame frame;

private JScrollPane scrollPane;
private JTextArea textArea;
private JLabel label;

private InputStream in;
private OutputStream out;
private String text;


/**
* Create the text editor object with the supplied parameters.
*
* @param in Take in an {@link InputStream} to read the input file from.
* @param out Write the final text to the supplied {@link OutputStream}
* when the user closes the application.
*/
public TextEditor(InputStream in, OutputStream out) {
this.in = in;
this.out = out;

this.text = "";
try {
StringBuffer buffer = new StringBuffer();
int input = 0;
while((input = in.read()) != -1) {
buffer.append((char)input);
}
in.close();
text = buffer.toString();
}
catch(Exception e) {
e.printStackTrace();
}
}

protected void createAndShowGUI() {
frame = new JFrame("Awesomest Text Viewer");

textArea = new JTextArea(text,20, 60);
textArea.setPreferredSize(new Dimension(800, 600));
textArea.setLineWrap(true);
scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

// Add the scroll pane to the center of the window
frame.add(scrollPane, BorderLayout.CENTER);

// Add the label as status
label = new JLabel("Ready");
frame.add(label, BorderLayout.SOUTH);

frame.addWindowListener(new WindowAdapter() {

@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);

try {
for(char c: textArea.getText().toCharArray()) {
out.write(c);
}
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});

frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public void execute() throws Exception {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Basically, shows up the GUI.
createAndShowGUI();
}
});
}

}
24 changes: 24 additions & 0 deletions lab2-1/lab2-1-lib/src/headfirst/decorator/io/InputTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package headfirst.decorator.io;

import java.io.*;

public class InputTest {
public static void main(String[] args) throws IOException {
int c;

try {
InputStream in =
new LowerCaseInputStream(
new BufferedInputStream(
new FileInputStream("./input_output/in.txt")));

while((c = in.read()) >= 0) {
System.out.print((char)c);
}

in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package headfirst.decorator.io;

import java.io.*;

public class LowerCaseInputStream extends FilterInputStream {

public LowerCaseInputStream(InputStream in) {
super(in);
}

public int read() throws IOException {
int c = super.read();
return (c == -1 ? c : Character.toLowerCase((char)c));
}

public int read(byte[] b, int offset, int len) throws IOException {
int result = super.read(b, offset, len);
for (int i = offset; i < offset+result; i++) {
b[i] = (byte)Character.toLowerCase((char)b[i]);
}
return result;
}
}
9 changes: 9 additions & 0 deletions lab2-1/lab2-1-solution/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?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.7"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry combineaccessrules="false" kind="src" path="/Lab2-1-Lib"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions lab2-1/lab2-1-solution/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Lab2-1-Solution</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>
11 changes: 11 additions & 0 deletions lab2-1/lab2-1-solution/.settings/org.eclipse.jdt.core.prefs
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.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
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.7
Binary file added lab2-1/lab2-1-solution/docs/Answer.pdf
Binary file not shown.
Binary file added lab2-1/lab2-1-solution/docs/Design.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4516fa4

Please sign in to comment.