DCS Lab 2 - Java PDF

Title DCS Lab 2 - Java
Author rudra raval
Course Data Communications
Institution Monash University
Pages 11
File Size 561 KB
File Type PDF
Total Downloads 89
Total Views 135

Summary

DCS Lab 2 - Java...


Description

Name: Neelkanth Girsh Raval

Student ID:102628598

Swinburne University of Technology Faculty of Science Engineering and Technology

Data Communications & Security Lab 2, Java – Sockets 1. Assumptions You have basic skills in working with an Object Oriented Programming language. You have completed lab 0 and have a working Java and BlueJ installation available at all times. 2. Notes You can do your LAB work in pairs, this will make it quicker and enhance your learning. In the LAB Command line Programming with Java 1. Open a command window 2. Navigate to C:\temp or /tmp 3. type java –version

What version is installed?

13.0.2 204. type javac –version

What version is installed?

13.0.2 5. type path

Where is java.exe located on your computer?

C:\Program Files\Java Where is javac.exe located on your computer?

C:\Program Files\Java\jdk-13.0.2\bin 6. Review the HelloWorld tutorial

http://java.sun.com/docs/books/tutorial/getStarted/application/index.html

7. Open Notepad (//or a text editor of your choice - not MS Word!) and type the HelloWorld program in. //you are more likely to remember the code if you type it. Also copying and pasting can introduce invisible characters which will prevent the code from compiling.

Data Comms and Security

Lab 2

Page 1

Name: Neelkanth Girsh Raval

Student ID:102628598

8. Save the program. //save to C:\temp\HelloWorldApp.java, selecting the *.* file type 9. Compile the program (javac HelloWorldApp.java) 10. Run the program (java HelloWorldApp)

//problems? Check the environment

path. If you have installed Java before you may need to remove old parts of the path - or the JAVAPATH variable.

11. What is the connection between the file name and the class name?

The java file name and class name is recommended to be same as the class name is the entry point of the java program. .java

12. What is the file extension for the source code?

13. What is the file extension for the byte code?

.class

14. How do you compile a program at the command line? 15. How do you run a program at the command line?

javac filename.java java classname

16. Modify the HelloWorld program so it accepts your name from the command line at the time you run the program.

17. What happens if you forget to enter the argument when you run your program?

It will through an error while compiling

Data Comms and Security

Lab 2

Page 2

Name: Neelkanth Girsh Raval

Student ID:102628598

Using the BlueJ IDE Review the following video https://www.youtube.com/watch?v=UMcijMC4Dwk

1. Open BlueJ

4.2.2

2. What version is installed? 3. Create a new project Lab1

4. Add a new Class Test

5. Compile your code (Right click on the Class)

6. Make a Test object (Right click and select new Test())

7. Execute its methods (Right Click on the object)

8. Review the class code (Right click and select open editor) a. Note the use of Javadoc i. Class header ii. Method headers iii. Use of @param, @return @author @version (all your written submissions should have these) iv. Update the class header so you are the author and set version to 1.0

Data Comms and Security

Lab 2

Page 3

Name: Neelkanth Girsh Raval

Student ID:102628598

v. Select Documentation in the top right hand corner and view the javadoc b. Note the use of // to provide inline comments c. Note the start of a class is defined by the keyword “class” and the name of the class. d. The IDE renders all key words in red e. Note the format of the constructor i. Same name as class ii. May take parameters iii. No return type f.

Find the instance variables i. Instance variables belong to object instances ii. They are by convention placed at the top of the class iii. They include types and scope iv. The types may be primitive (Start with lower case) v. or reference types (Start with upper case letter) vi. Variables may also be declared static, if they are static then all objects use the same variable and only one exists. They are also known as class variables vii. Instance variables are normally preceded by an access modifier. It is good practice to make them private this prevents any code outside of the class accessing the variable.

g. Review the methods and note i. These may be public (access outside of the class) or private, only used from within the class ii. If they return a value, then they will contain the keyword return and the type returned will be shown on the first line (otherwise “void”)

Data Comms and Security

Lab 2

Page 4

Name: Neelkanth Girsh Raval

Student ID:102628598

iii. The first line (in this unit) will be referred to as then “Method Signature” iv. A method may have none, one or more parameters v. Parameters and return types may be primitive types or Reference Types h. Calling Methods i. From within the class use the method name ii. From outside the class 1. If the method is static, use the class name followed by the method name 2. If the method is not static (belongs to an object) use the name of the variable that has been assigned the object instance followed by the method name.

Data Comms and Security

Lab 2

Page 5

Name: Neelkanth Girsh Raval

Student ID:102628598

HelloWorld in BlueJ http://facweb.cs.depaul.edu/noriko/howto-bluej.html 1. Add a new Class called HelloWorld 2. Copy the code for your last version of hello world that you ran at the command line 3. Compile, debug and test Notes: * Right-click to compile the class * Check for brown markers in editor - syntax errors * If it compiles, right-click on class and select main() * Enter command-line input in double quotes inside the braces.

BlueJ expects that strings are in "" quotation marks Anything else is deemed to be a type that the program should be able to find (this includes a variable name that refers to an object or a primitive type such as an 8) Java Syntax The java language is very similar to many other programming languages and the constructs that we use to write programs should be very familiar. It would be useful for you to make some notes about these constructs. Here is a list of the key constructs and links to resources that define the syntax for them

variables https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Data Comms and Security

Lab 2

Page 6

Name: Neelkanth Girsh Raval

Student ID:102628598

for loops https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

while & do loops https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

switch https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

if statements https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

Scanner (for input output) http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Exceptions https://docs.oracle.com/javase/tutorial/essential/exceptions/

Java API

Java comes with many classes. These classes are defined in the API. There are so many classes that they are further organized into packages. The API is an invaluable resource for finding out how a class works and how to make objects based on that class. A different version of the API exists for version 6/7 and 8 of Java. Most classes in the API are not accessible to your code unless you import the correct package.

1. Open the API for Java 9 and locate the String Class https://docs.oracle.com/javase/9/docs/api/overview-summary.html Hint: sometimes it is easier to let Google do the job type Java API 9 String. Things to note: a. The String class is located in the java.lang package and this package is made available to all code you write. b. The API often provides examples on how to use a class c. Constructors define the different ways you can build a String object d. The methods define what you can do with a String object and how they can be used

Data Comms and Security

Lab 2

Page 7

Name: Neelkanth Girsh Raval

Student ID:102628598

e. Most classes implement interfaces that define additional behaviour f.

All classes inherit form Object but may inherit from other classes that define additional behaviour

2. Now locate the Math class a. Note this class is also in the java.lang package b. Many methods are static, this means they can be used without creating an object e.g. double d = Math.random(); 3. Now locate the InputStreamReader class a. This is in the java.io package, and to use this class you would need to add the statement import java.io.* or import java.io.InputStreamReader at the top of your code. 4. Locate and review the following https://docs.oracle.com/javase/tutorial/networking/sockets/

Sockets are used by java to support data communication. Sockets maybe used on one machine but are most often used on multiple machines Server code will open a socket for clients to connect to. Java Your first Server/Client Code

The following link defines code for a client and server using UDP https://systembash.com/a-simple-java-udp-server-and-udp-client/

1. Create a new BlueJ Project called UDP_Chat 2. Add a class called UDPServer 3. Add a class Called UDPClient 4. Cut and paste the code form the web site into your BlueJ project 5. Make a note at the top of the source code identifying the URL you obtained the code from (coders need to act with integrity) 6. Make sure both classes compile 7. Change the server socket to 4000 (was 9876) 8. Change the client code so the port is 4000 (was 9876)

Data Comms and Security

Lab 2

Page 8

Name: Neelkanth Girsh Raval

Student ID:102628598

9. Save all your work 10. Run on one computer Note, BlueJ only allows one static void main to run a. Open up a second BlueJ instance b. Open up the UDP_Chat project in the second instance c. Read the code and see if you can understand what it should do d. Run the server code in one instance e. Run the client code in the other instance //Hint (view terminal will help you see what’s happening) The restless scroll bar indicates the code is running. Click on the hooked arrow to stop it.

Data Comms and Security

Lab 2

Page 9

Name: Neelkanth Girsh Raval

1. Does the server code run in a loop?

Student ID:102628598

Yes

2. What will happen after the server "replies" to the client?

Server gets a message

3. Does the client code run in a loop?

Yes

4. What will happen after the client receives the reply?

Client can read and respond

Doesn’t work? – Try Stepping through the code

1. Set a break point in the server code https://www.youtube.com/watch?v=Q625RZTc1Ho

2. Start the server code 3. Step through until it stops and waits for a client 4. Set a break point in the client 5. Start the client 6. Step the client code

If all else fails - ask your tutor for help.

Advanced

1. Open the server on one physical or one Virtual machine (Start Virtual Machine Launcher – COS20012/COS70007)

Data Comms and Security

Lab 2

Page 10

Name: Neelkanth Girsh Raval

Student ID:102628598

2. Open the client on another physical machine or VM 3. Prove they talk to each other. HINT: You will need to change the IP address in the client code to match the location of the server. Use ipconfig to find the IP address of the local machine. You can replace "localhost" with "x.x.x.x" where x.x.x.x is the IPv4 address of the server. 4. Convert the server to a time server - get the time: String now = new Date().toString(); and return it. //import java.util

Data Comms and Security

Lab 2

Page 11...


Similar Free PDFs