Docsity

Java Lab: Implementing Simple Echo Tool - CpSc 215: Software Development, Spring 2008 - Pr, Lab Reports of Computer Science

Toggle sidebar
bg1
CpSc 215: Software Development Foundations Spring 2008
Closed Lab #4:
Echo!
1 Objectives
There are four objectives to this lab. First, you will gain some experience using classes designed
in a manner similar to that prescribed by the Decorator pattern1. Second, you will gain some
experience using several components of the Java IO and networking libraries. Third, you will be
introduced to the Java API documentation, and gain some experience using this documentation
to understand the library. Finally, you will gain some experience developing and testing (within
Eclipse) an application that requires command-line arguments.
2 Overview
For this lab, you will implement a simple echo tool that echoes the contents of the file passed as
argument. Two types of file paths will be supported. If a local file path is passed, your application
will open the corresponding file, print its contents to stdout, and terminate. If a URL is passed,
your application will connect to the corresponding website (via HTTP, print the contents of the
web page, and terminate. Your application should be as simple as possible. As a rule of
thumb, it should be no longer than 35 lines of Java code, including whitespace.
3 Requirements
•Your main() method should have the following signature:
public static void main(String args[]) throws Exception {...}
For now, don’t worry about the throws clause; just ignore it.
•Your application should define only one other method, used to print the contents of the
(stream-like) object passed as argument. The method should have the following signature:
public static void printContents(/*?*/) throws Exception {...}
Again, don’t worry about the throws clause.
•An important objective of this lab is for you to gain some experience using the Java API
documentation. This documentation can be found online at the following address:
http://java.sun.com/j2se/1.5.0/docs/api/.
You will likely have little familiarity with the classes required to complete the lab — by
design! While I will offer some hints, it is up to you to identify the relevant classes to use.
The documentation will be helpful.
4 Hints
•The java.io package is the main package of the Java IO library.
•The java.net package is the main package of the Java networking library.
1The classes you will use in this lab do not exactly follow the pattern. An interesting midterm question might ask
how they differ from the standard pattern.
1
pf2

Partial preview of the text

Download Java Lab: Implementing Simple Echo Tool - CpSc 215: Software Development, Spring 2008 - Pr and more Lab Reports Computer Science in PDF only on Docsity!

CpSc 215: Software Development Foundations Spring 2008

Closed Lab #4:

Echo!

1 Objectives

There are four objectives to this lab. First, you will gain some experience using classes designed in a manner similar to that prescribed by the Decorator pattern^1. Second, you will gain some experience using several components of the Java IO and networking libraries. Third, you will be introduced to the Java API documentation, and gain some experience using this documentation to understand the library. Finally, you will gain some experience developing and testing (within Eclipse) an application that requires command-line arguments.

2 Overview

For this lab, you will implement a simple echo tool that echoes the contents of the file passed as argument. Two types of file paths will be supported. If a local file path is passed, your application will open the corresponding file, print its contents to stdout, and terminate. If a URL is passed, your application will connect to the corresponding website (via HTTP, print the contents of the web page, and terminate. Your application should be as simple as possible. As a rule of thumb, it should be no longer than 35 lines of Java code, including whitespace.

3 Requirements

  • Your main() method should have the following signature: public static void main(String args[]) throws Exception {...} For now, don’t worry about the throws clause; just ignore it.
  • Your application should define only one other method, used to print the contents of the (stream-like) object passed as argument. The method should have the following signature: public static void printContents(/?/) throws Exception {...} Again, don’t worry about the throws clause.
  • An important objective of this lab is for you to gain some experience using the Java API documentation. This documentation can be found online at the following address: http://java.sun.com/j2se/1.5.0/docs/api/. You will likely have little familiarity with the classes required to complete the lab — by design! While I will offer some hints, it is up to you to identify the relevant classes to use. The documentation will be helpful.

4 Hints

  • The java.io package is the main package of the Java IO library.
  • The java.net package is the main package of the Java networking library. (^1) The classes you will use in this lab do not exactly follow the pattern. An interesting midterm question might ask how they differ from the standard pattern.

CpSc 215: Software Development Foundations Spring 2008

  • InputStream is one of the most basic input interfaces provided by the Java IO library. It provides methods for reading a single byte or an array of bytes.
  • InputStreamReader is a class that adapts InputStream to provide a higher-level programming interface. In particular, each instance of InputStreamReader wraps an instance of InputStream. It uses the lower-level byte-based interface to provide a char-based interface.
  • BufferedReader is a class that adapts InputStreamReader to provide a(n even) higher-level programming interface. In particular, each instance of BufferedReader wraps an instance of InputStreamReader. In addition to improving performance via buffering, the class uses the lower-level char-based interface to provide a String-based interface. The readline() method, in particular, is likely to be helpful in completing your lab.
  • FileInputStream implements InputStream. It provides a stream abstraction for reading from standard disk files.
  • URL implements a URL abstraction. Most important to this lab, the class can be used to convert a String-based URL to a URL instance. The class additionally provides a method for opening a connection to the URL end-point.
  • URLConnection implements an abstraction corresponding to a URL connection. An instance of this class will be created by an instance of URL. The resulting URLConnection will provide a method for opening an InputStream to read the contents of the document identified by the URL.
  • In Eclipse, the command-line arguments to be passed during a program run are included as part of the run configuration. To create a new configuration, or to modify an existing configuration, select the “Run...” item from the menu at the top of the window. It’s your job to figure it out from there!

5 Grading

To receive full credit for this lab, you must demonstrate (within Eclipse) that you have satisfied the above requirements. When you think you are ready, ask one of the TAs to check your solution. When they have checked your name off of their list, you are free to leave. If you are unable to complete the lab before the end of the period, but have made a reasonable attempt, you will receive 1/2 credit for the day. If the TA feels that you have not made a reasonable attempt, you will not receive any credit for your participation.

6 Collaboration

You may work independently or with one partner. You must not discuss the problem or the solution with classmates outside of your group. (Keep this in mind before you choose to work independently.)