Professional Documents
Culture Documents
Assign2 Network Prog
Assign2 Network Prog
ASSIGNMENT
COVER
REGION: Mash west SEMESTER: 1 YEAR: 2021
ASSIGNMENT TITLE:
Instructions
Marks will be awarded for good presentation and thoroughness in your approach.
NO marks will be awarded for the entire assignment if any part of it is found to be copied directly
from printed materials or from another student.
Complete this cover and attach it to your assignment. Insert your scanned signature.
Student declaration
I declare that:
I understand what is meant by plagiarism
The implications of plagiarism have been explained to me by the institution
This assignment is all my own work and I have acknowledged any use of the published or
unpublished works of other people.
B) What is an UnknownHostException.
This is a subclass of IOException, so it is a checked exception. It emerges when you are trying to
connect to a remote host using its host name, but the IP address of that Host cannot be resolved.
C) What is a stream?
A stream is a sequence of objects that supports various methods which can be pipelined to produce
the desired results.
D) Explain in your own words, your understanding of persistence objects and serialized
objects.
When we implement persistence objects, we could simply require all classes of persistence objects
to provide a member function that inserts each member variable into a given file stream ( this
process is done by serialized objects).
class persistent
{
public void serialized(fstream);
}
Persistent objects are class used by an entity class. They do not have indices but instead are stored
or retrieved when an entity class makes direct use of them.
Serialized objects they convert state of objects into a byte stream in a way that the byte stream can
be reverted back into a copy of the objects.
QUESTION 2
Write a GUI programme called MyBrowserExplorer
Your interface should contain at least one text control to enter the URL to visit and a GO
button.
You should also provide a large text area control that displays the contents of the url that is
visited.
This study source was downloaded by 100000800936633 from CourseHero.com on 04-12-2023 04:40:27 GMT -05:00
Your code should make use of the URL class and the openstream() method to read
https://www.coursehero.com/file/133752957/Assign2-Network-progdoc/
interactively the contents of a website. Insert the comments on each line to explain why you
put the code.
//MyBrowserExplorer.java
//importing java parkages
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
//declaring browser class
public class MyBrowserExplorer extends Frame Implements ActionListener
{
//declaring variables
private TextField txtInput;
private TextArea contents;
private Button btnGo;
private Button exit;
//the main
public static void main(String[] args)
{
MyBrowserExplorer m = new MyBrowserExplorer();
m.makeGUI();
m.setSize(550, 400);
m.setVisible(true);
}
public void makeGUI()
{
//creating a layout
setLayout(new FlowLayout());
//creating and adding a text input that we can enter url address to the frame
txtInput = new TextField(50);
add(txtInput);
//creating and adding a button Go to the frame
btnGo = new Button(“Go”);
add(btnGo);
//creating and adding button Exit
exit = new Button(“Exit”);
add(exit);
exit.addActionListener(this);
//creatinjg a text area to display contents when the button Go is pressed
contents = new TextArea(“ “, 20, 60, TextArea.SCROLLBARS-VERTICAL-ONLY);
add(contents);
btnGo.addActionListener(this);
}
//action performed by the button
public void actionPerformed(ActionEvent event)
{
if (event.getSource()==exit);
System.exit(0);
//getting the input from user into the text field
String line;
String location = txtInput.getText();
try{
// reading and writing the contents of URL entered in the text field
URL url = new URL(location);
bufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()));
This study source was downloaded by 100000800936633 from CourseHero.com on 04-12-2023 04:40:27 GMT -05:00
while((line =input.readline())!=null)
{
https://www.coursehero.com/file/133752957/Assign2-Network-progdoc/
contents.append(line);
contents.append(“/n”);
}
input.close();
}
catch (MalformedURLException e)
{
contents.setText(“Invalid URL format”);
}
catch (IOException io)
{
contents.setText(io.toString());
}
}
}
This study source was downloaded by 100000800936633 from CourseHero.com on 04-12-2023 04:40:27 GMT -05:00
https://www.coursehero.com/file/133752957/Assign2-Network-progdoc/
Powered by TCPDF (www.tcpdf.org)