Network Programming Section1
Network Programming Section1
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
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";
}
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.
// Test raiseSalary()
System.out.println(e1.raiseSalary(10));
System.out.println(e1);
}
}
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]
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.