CS 455/555 - Intro to Networks and Communication
Spring 2013: Tues/Thurs 11am-12:15pm, Const 1009

Print - Admin

Announcements

Staff

  • Dr. Michele Weigle
  • mweigle at cs.odu.edu
  • E&CS 3214
  • Office Hours:
    M 1:30-3pm
    Th 9:30-10:45am

Schedule

Syllabus

Links

Program 1: Java/Python Refresher

Assigned: Tuesday, January 14, 2013
Due: Tuesday, January 22, 2013

Motivation

You may use either Java or Python for programming assignments throughout the semester. It is assumed that you already know one of these languages or feel comfortable enough in your programming skills to be able to learn enough to complete the assignments. The reason we are using Java/Python is that the socket programming constructs used to develop network applications are much simpler than in C or C++.

Note that the 6th edition of the textbook has socket programming examples in Python, while earlier editions use Java. When we get to Chapter 2, I will show examples in class using both languages.

The programming assignments during the semester will involve reading user input from the command line, parsing strings returned in network messages, and parsing URLs. This assignment is designed to refresh your memory on (or help you learn about) these particular parts of Java or Python.

If you cannot complete this assignment in just a few (2-4) hours, please come talk to me this week (so, start early!).

Description

Task 1: Read in the command-line arguments.

The program should take 2 command-line arguments:

  1. a URL beginning with http://
  2. an integer
  • If there are not 2 command-line arguments, exit after printing the usage statement
Usage: java Prog1 URL integer
OR
Usage: python Prog1.py URL integer
  • If the URL does not start with http://, exit after printing the error message
ERR - URL format
  • If the second argument is not an integer, exit after printing the error message
ERR - arg 2

Task 2: Output the number of characters in the URL after http://

  • Output only the number of characters on a single line.

Task 3: Output the product of the number calculated in Task 2 and the integer command-line argument

  • Output only the resulting number on a single line.

Task 4: Output the hostname in the URL

  • Output the hostname, even if it is not a valid hostname.

For this assignment, valid URLs will be written in the form http://hostname[/path], where hostname is the web server's hostname, and path is the path from the web server's main directory to the requested file and is optional. Only lowercase letters will be used.

Rules

  • As with all projects, you are not permitted to work with anyone else (even students not in the class) - all of the coding and documentation must be your own.
  • Your program must compile (if Java) and run on the CS Unix machines. If you do not have an account, create one today!
  • You must write neat code and document it well. You will lose points for sloppy programs that contain little or no comments.

Testing

A large part of your program's grade will be determined by how well it handles a set of inputs. You should test your program rigorously before submitting. Because your programs will be run and tested using a script, you must format your output exactly as I have described or you will lose points.

In these examples, if you use Python, replace java Prog1 with python Prog1.py

Example 1

java Prog1 
Usage: java Prog1 URL integer

Example 2

java Prog1 http://amazon.com 3
10
30
amazon.com

Example 3

java Prog1 http://amazon.edu.com 5
14
70
amazon.edu.com

Example 4

java Prog1 http://www.amazon.com/myaccount.html 5
29
145
www.amazon.com

Example 5

java Prog1 http://amazon/myaccount.html 5
21
105
amazon

Example 6

java Prog1 ftp://amazon.com 4
ERR - URL format

Example 7

java Prog1 'http://www.cnn.com/2011/WORLD/asiapcf/01/06/afghanistan.us.marines/index.html?hpt=T1' 10
77
770
www.cnn.com

Example 8

java Prog1 http://amazon/myaccount.html 1.5
ERR - arg 2

Submission

You must name your source file Prog1.java or Prog1.py (capitalize only the first letter). For Java, make sure that you submit all files necessary to compile your program. But, do not submit compiled .class files.

Directions for submitting your assignment through Blackboard

Getting Started

Java

If you've never programmed with Java before, here are some helpful tips:

  • Java syntax is very, very similar to C++.
  • Only one class is allowed per file, and the name of the class must match (exactly!) the name of the file. For Program 1, you are to name your source file Prog1.java, so you must name your class Prog1.
  • The "import" statement is used give access to built-in libraries, much like the "#include" statement in C/C++.
  • To print output (instead of printf), use System.out.print() or System.out.println(). println() adds a newline character to the end of the String given.
  • The main method should be declared as public static void main (String argv[]). The String array, argv, gives you access to the command-line arguments. The first element of the array (argv[0]) is the first argument (and not the program name as in C/C++).
  • To compile your program, use javac. Ex: javac Prog1.java
  • To run your program, use java. Ex: java Prog1 http://www.amazon.com 3
  • The Java String class reference will be useful in completing this assignment.
  • See the other Java references on the Links page.

Here is the skeleton for Program 1 (to be saved as Prog1.java):

// Insert Your Name 
// Insert Your Class (CS 455 or CS 555), Spring 2013
// Program 1 - Java/Python Refresher

import java.io.*; 

class Prog1 { 

   public static void main (String argv[]) 
   { 

       // insert your code here 

   } 
}

Python

If you've never programmed with Python before, here are some helpful tips:

  • Python syntax is very different from Java, C, C++
    • proper indention is essential. It's used to group statements (such as branches in an if..else statement)
    • newline delimits the end of a statement, not a semicolon (;)
  • The "import" statement is used give access to built-in libraries, much like the "#include" statement in C/C++.
  • The list sys.argv gives you access to the command-line arguments. The first element of the array (argv[0]) is the program name (Prog1.py).
  • Python is interpreted, so there's no need for compilation. To run your program, use python. Ex: python Prog1.py http://www.amazon.com 3
  • The Python String methods reference will be useful in completing this assignment.
  • See the other Python links on the Links page.

Here is the skeleton for Program 1 (to be saved as Prog1.py):

# Insert Your Name 
# Insert Your Class (CS 455 or CS 555), Spring 2013
# Program 1 - Java/Python Refresher

import sys

# insert your code here 

Unix

If you've never used Unix before, here are some helpful tips:

  • See Dr. Zeil's page on Software needed. You'll want to get Xming and PuTTY for your own machine. Computers in the CS lab have XWin32 (similar to Xming) and either PuTTY or the ssh Secure Shell Client.
    • Xming - allows Unix windows (such as terminal windows or Emacs editing windows) to be displayed on your PC
    • PuTTY (or ssh Secure Shell Client) - allows you to securely login to a Unix machine
  • Dr. Zeil has put notes from his entire CS 252 - Intro to Unix course online. If you have never used Unix before, please look at these notes. You may want to pay special attention to Section 4 on using X Windows and editing files with emacs. See also the Unix links on this course's links page.
  • Start Xming (or XWin32)
  • Securely login to one of the CS Unix machines. See the detailed instructions.