Recent Changes - Search:

Network Programming

This website demonstrates using wikis as teaching and learning tool.

The course instructor is happy to share the teaching materials here with those who find it readable.

Java Network Programming Basics - Classes and Objects

Overview: We use this tutorial to learn more about basic concepts on Java network programming.


Activity 1

  • Study the following Java program. Then answer the following questions.
import java.net.*;
import java.io.*;

public class ScanPorts {

  public static void main(String[] args) {

    Socket theSocket;
    String host = "localhost";

    if (args.length > 0) {
      host = args[0];
    }
    for (int i = 0; i < 1024; i++) {
      try {
        theSocket = new Socket(host, i);
        System.out.println("There is a server on port " + i + " of " + host);
      }
      catch (UnknownHostException e) {
        System.err.println(e);
        break;
      }
      catch (IOException e) {
        System.out.println("*** There is no server on port " + i + " of " + host);
      }
    }

  }

}
(Q1): What is the function of the import directive in the first two lines?
(Q2): What is the class name of the Java code?
(Q3): List all the functions (or methods) within the class.
(Q4): List all the objects used by the function main.
(Q5): What is the function of new operator in Java language?
(Q6): Guess the program flow within the try-catch block.
  • Compile and run the Java program.
  • (Q7): Briefly describe what the Java program does.
  • (Q8): Modify the program so that it can find out which of the TCP ports at or above 1024 seem to be used. Send your Java program sources to Steven.

Activity 2

  • Read this reading: The Java Tutorial - Lesson: Classes and Objects
  • Try to find the answers for the following questions.
    The roles of classes and objects in Java programming
    What are instance variables (also called object variables)?
    What are instance methods (also called object methods)?
    What are static variables (also called class variables)?
    What are static methods (also called class methods)?
  • Consider the following class: (Extract from: http://download.oracle.com/javase/tutorial/java/javaOO/QandE/creating-questions.html). Then answer the following questions.
        public class IdentifyMyParts {
            public static int x = 7; 
            public int y = 3; 
        }
(Q9) What are the class variables?
(Q10) What are the instance variables?
(Q11) What is the output from the following code:
            IdentifyMyParts a = new IdentifyMyParts();
            IdentifyMyParts b = new IdentifyMyParts();
            a.y = 5;
            b.y = 6;
            a.x = 1;
            b.x = 2;
            System.out.println("a.y = " + a.y);
            System.out.println("b.y = " + b.y);
            System.out.println("a.x = " + a.x);
            System.out.println("b.x = " + b.x);
            System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);
  • What are System.in and System.out?
System.in - The "standard" input stream. This stream is already open and ready to supply input data. Typically this stream corresponds to keyboard input or another input source specified by the host environment or user.
System.out - The "standard" output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user.
For simple stand-alone Java applications, a typical way to write a line of output data is:
         System.out.println(data)

Activity 3

  • Write a Java program to verify your answers in Activity 2.
Write a class named IdentifyMyParts and another class named ActivityTwoTest.
(Q12) Send your Java program sources to Steven.

Activity 4

  • The following diagram illustrates the concept of object encapsulation.
  • (Q13): Briefly describe the concept of object encapsulation.
  • (Q14): What is the difference between public methods and private methods of an object?
  • (Q15): How do you make the object's data accessible from the outside?

Submission

  • Please submit your work to Steven by email: sochoy@ouhk.edu.hk.
  • Please use SXXXXXXX - NP Tutorial 05 Submission as the email subject.
Edit - History - Print - Recent Changes - Search
Page last modified on October 06, 2011, at 04:15 PM