0% found this document useful (0 votes)
9 views

Network Programming Section1

Uploaded by

basma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Network Programming Section1

Uploaded by

basma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Prepared by: Eng/ Basma Elshoky

Network Programming
Section 1: introduction to Java
IDE
you can use any IDE such as NetBeans, Eclipse, or visual code.
Visual code for java:
Download software:
https://code.visualstudio.com/Download
https://code.visualstudio.com/docs/java/java-tutorial
Setting up VS Code for Java:
https://www.youtube.com/watch?v=fbyobdxDQno
https://www.youtube.com/watch?v=BB0gZFpukJU
If not run click next link.
https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack

Example of java program:


// Importing classes from packages
import java.io.*;
// Main class
public class Section1 {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println("Welcome to GeeksforGeeks"); } }

Prepared by: Eng Basma Elshoky


Prepared by: Eng/ Basma Elshoky

Example for student Circle include properties or attribute, construct and


method.

A class called circle is designed as shown in the following class diagram. It contains:
• Two private instance variables: radius (of the type double) and color (of
the type String), with default value of 1.0 and "red", respectively.
• Two overloaded constructors - a default constructor with no argument,
and a constructor which takes a double argument for radius.
• Two public methods: getRadius() and getArea(), which return the radius
and area of this instance, respectively.
The source codes for Circle.java is as follows:
/**
* The Circle class models a circle with a radius and color.
*/
public class Circle { // Save as "Circle.java"
// private instance variable, not accessible from outside this class
private double radius;
private String color;

// Constructors (overloaded)
/** Constructs a Circle instance with default value for radius and color */
public Circle() { // 1st (default) constructor
radius = 1.0;
color = "red";
}

/** Constructs a Circle instance with the given radius and default color */
public Circle(double r) { // 2nd constructor
radius = r;
color = "red";
}

/** Returns the radius */


public double getRadius() {
return radius;

Prepared by: Eng Basma Elshoky


Prepared by: Eng/ Basma Elshoky

/** Returns the area of this Circle instance */


public double getArea() {
return radius*radius*Math.PI;
}
}

Compile "Circle.java". Can you run the Circle class? Why?


This Circle class does not have a main() method. Hence, it cannot be run directly.
This Circle class is a “building block” and is meant to be used in another program.
Let us write a test program called TestCircle (in another source file
called TestCircle.java) which uses the Circle class, as follows:
/**
* A Test Driver for the Circle class
*/
public class TestCircle { // Save as "TestCircle.java"
public static void main(String[] args) {
// Declare an instance of Circle class called c1.
// Construct the instance c1 by invoking the "default" constructor
// which sets its radius and color to their default value.
Circle c1 = new Circle();
// Invoke public methods on instance c1, via dot operator.
System.out.println("The circle has radius of "
+ c1.getRadius() + " and area of " + c1.getArea());
//The circle has radius of 1.0 and area of 3.141592653589793

// Declare an instance of class circle called c2.


// Construct the instance c2 by invoking the second constructor
// with the given radius and default color.
Circle c2 = new Circle(2.0);
// Invoke public methods on instance c2, via dot operator.
System.out.println("The circle has radius of "
+ c2.getRadius() + " and area of " + c2.getArea());
//The circle has radius of 2.0 and area of 12.566370614359172
}
}

Prepared by: Eng Basma Elshoky


Prepared by: Eng/ Basma Elshoky

Lab task
A class called Employee, which models an employee with an ID, name and salary,
is designed as shown in the following class diagram. The method raiseSalary(percent)
increases the salary by the given percentage. Write the Employee class.

Public class TestMain {


public static void main(String[] args) {
// Test constructor and toString()
Employee e1 = new Employee(8, "Peter", "Tan", 2500);
System.out.println(e1); // toString();

// Test Setters and Getters


e1.setSalary(999);
System.out.println(e1); // toString();
System.out.println("id is: " + e1.getId());
System.out.println("firstname is: " + e1.getFirstName());
System.out.println("lastname is: " + e1.getLastName());
System.out.println("salary is: " + e1.getSalary());

System.out.println("name is: " + e1.getName());


System.out.println("annual salary is: " + e1.getAnnualSalary()); // Test method

// Test raiseSalary()

Prepared by: Eng Basma Elshoky


Prepared by: Eng/ Basma Elshoky

System.out.println(e1.raiseSalary(10));
System.out.println(e1);
}
}

The expected out is:

Employee[id=8,name=Peter Tan,salary=2500]
Employee[id=8,name=Peter Tan,salary=999]
id is: 8
firstname is: Peter
lastname is: Tan
salary is: 999
name is: Peter Tan
annual salary is: 11988
1098
Employee[id=8,name=Peter Tan,salary=1098]

Prepared by: Eng Basma Elshoky


Prepared by: Eng/ Basma Elshoky

Homework task
A class called Author, which models an author of a book, is designed as shown in
the class diagram. A class called Book, which models a book written by ONE
author and composes an instance of Author as its instance variable, is also shown.
Write the Author and Book classes.

Prepared by: Eng Basma Elshoky

You might also like