0% found this document useful (0 votes)
6 views11 pages

Java1 4

The document outlines a lab report for an Advanced Java Programming course, detailing various experiments conducted by a student, including creating classes with parameterized constructors, demonstrating polymorphism, and implementing socket programming. Each program includes objectives, theoretical explanations, and source code examples. The report also features a grading rubric for evaluating the student's understanding and implementation of the concepts.

Uploaded by

unclechips002
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Java1 4

The document outlines a lab report for an Advanced Java Programming course, detailing various experiments conducted by a student, including creating classes with parameterized constructors, demonstrating polymorphism, and implementing socket programming. Each program includes objectives, theoretical explanations, and source code examples. The report also features a grading rubric for evaluating the student's understanding and implementation of the concepts.

Uploaded by

unclechips002
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Advanced Java Programming Lab

CIE-306P

Faculty Name: Ms. Prachi Dahiya Student Name: Samaksh Arzare


Roll no: 05214812723
Semester: 6th
Batch: AIML IV B (CST)
[Link]
Pr
og
ra

Experiment
mm
in
Student Enrollment No:

Is able to identify and define the

2
g
R1

objective of the given problem?


in
Is proposed design /procedure

2
R2

/algorithm solves the problem? J


a
v
Has the understanding of the a

2
L
R3

tool/programming language to a

Marks Marks Marks


implement the proposed solution?
b
Are the result(s) verified using (CIC

2
R4

sufficient test data to support the

Marks
conclusions?
INDEX

-
25
2 Individuality of submission?
R5

8
Marks )
L
a
(10)
Total
Marks

b
Ass
Student Name:

ess
ment
Remarks

S
he
et
Faculty
Signature
Total Remarks
R1 R2 R3 R4 R5 Faculty
[Link]. Experiment Marks
(2) (2) (2) (2) (2) Signature
(10)
Program 1
Aim: Create class BOX that uses parameterised constructors to initialize dimensions of box height, width,
length. Class also have method to return volume and surface area of box. Create object of class BOX and test
functionalities (Inheritance).
Software used: VS Code, JDK, JVM
Theory:
A class is used to model a real-world object by encapsulating its properties and behavior. Parameterized
constructors allow object data members to be initialized at the time of object creation, ensuring proper
assignment of values. Methods are implemented within the class to operate on these dimensions and compute
required results. Creating and using objects of the class helps in understanding constructor execution, access
to class members, and abstraction in Java.
Source Code:
public static class Box{
float length;
float width;
float height;
public Box(float l, float w, float h){
[Link] = l;
[Link] = w;
[Link] = h;
}
public float surface_area(){
return length*width;
}
public float volume(){
return length*width*height;
}
}
public static class Box_Object extends Box{
public Box_Object(float l, float w, float h){
super(l,w,h);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
float l,w,h;
[Link]("Enter length, width & height: ");
l = [Link]();
w = [Link]();
h = [Link]();

Box b = new Box_Object(l, w, h);


[Link]("Surface area of box is "+b.surface_area());
[Link]("Volume of box is "+[Link]());
}

Output:
Program 2
Aim: WAP to create class Shape. It contains 2 methods draw() & erase(). Also create 3 classes Circle, Triangle
& Square and each overrides the parent class function draw() & erase(). Draw() method should print “Drawing
Circle”, “Drawing Triangle” and “Drawing Square”. Erase method should print “Erasing Circle”, “Erasing
Triangle” and “Erasing Square”. Create objects of Circle, Triangle, Square and observe polymorphic nature
of class by calling methods on each object. (Polymorphism)
Software used: VS Code, JDK, JVM
Theory:
Polymorphism in Java is a fundamental object-oriented concept that allows the same method or interface to
take multiple forms depending on the object that invokes it. It enables a parent class reference to point to
different child class objects and call their respective overridden methods at runtime, which is known as runtime
polymorphism. Java also supports compile-time polymorphism through method overloading, where methods
share the same name but differ in parameters. By promoting flexibility, reusability, and scalability,
polymorphism helps developers write cleaner and more maintainable code.
Source Code:
public class Exp2 {
static abstract class Shape {
public void draw(){
[Link]("Drawing Shape");
}
public void erase(){
[Link]("Erasing Shape");
}
}
static class Circle extends Shape {
public void draw(){
[Link]("Drawing Circle");
}
public void erase(){
[Link]("Erasing Circle");
}
}
static class Triangle extends Shape {
public void draw(){
[Link]("Drawing Triangle");
}
public void erase(){
[Link]("Erasing Triangle");
}
}
static class Square extends Shape {
public void draw(){
[Link]("Drawing Square");
}
public void erase(){
[Link]("Erasing Square");
}
}
public static void main(String[] args) {
Shape c = new Circle();
Shape t = new Triangle();
Shape s = new Square();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}

Output:
Program 3
Aim: WAP to demonstrate the concept of socket programming.
Software used: VS Code, JDK, JVM
Theory:
Socket programming is a method of enabling communication between two devices over a network using
endpoints called sockets. A socket allows processes to send and receive data. In a typical client-server model,
the server creates a socket, binds it to an IP address and port number, and listens for incoming connection
requests, while the client creates a socket and connects to the server using the specified IP address and port.
Communication is established using protocols such as TCP (Transmission Control Protocol), which provides
reliable, connection-oriented data transfer, or UDP (User Datagram Protocol), which offers faster but
connectionless communication. Through socket programming, data can be exchanged over local networks or
the internet, forming the basis of many network applications such as web browsers, email systems, and chat
applications.
Source Code:
Output:
Program 4
Aim: Implement tcp socket programming in java for both server and client sockets.
Software used: VS Code, JDK
Theory:
TCP (Transmission Control Protocol) socket programming in Java enables reliable communication between
two applications over a network using a client-server architecture. TCP is a connection-oriented protocol that
ensures error-free, ordered, and complete delivery of data. In Java, TCP communication is implemented using
the ServerSocket and Socket classes from the [Link] package. When a client sends a connection request, the
server establishes a dedicated Socket for communication. The client creates a Socket object by specifying the
server’s IP address and port number to initiate the connection. Data is exchanged between client and server
using input and output streams. TCP socket programming ensures reliable data transfer, making it suitable for
applications like web services, file transfer systems, and messaging applications.
Source Code:
Output:

Common questions

Powered by AI

Using practical Java programming exercises like those involving the Box and Shape classes is an effective pedagogical approach to learning object-oriented concepts. These exercises provide hands-on experience with class design, inheritance, polymorphism, and other key principles, promoting deeper understanding through implementation. By directly engaging with code examples, students can better grasp abstract concepts and explore the impacts of design decisions. Such active learning strategies are crucial for developing problem-solving skills and fostering a robust understanding of object-oriented paradigms in software development .

Polymorphism in Java allows methods to take many forms, either at runtime via dynamic method dispatch or compile-time by method overloading, promoting flexibility, reusability, and scalability. For example, in shape drawing, different shapes like Circle, Triangle, and Square inherit from a common Shape class and override the draw() and erase() methods. This allows the same method call to operate on different objects and perform shape-specific actions. It enables developers to introduce new shapes without altering existing code, which enhances code reusability and simplifies maintenance .

Implementing polymorphism in object-oriented programming can present challenges such as understanding method binding, managing overridden methods, and ensuring type compatibility. In the Shape class example, each subclass overrides the draw() and erase() methods, which requires careful synchronization to avoid errors like method signature mismatches. Additionally, developers must be wary of upcasting and downcasting, ensuring that method calls are correctly bound at runtime. Such complexities necessitate thorough understanding and proper planning to effectively implement polymorphism and harness its benefits .

In Java socket programming, the ServerSocket and Socket classes facilitate client-server communication. The ServerSocket class is used by the server to create a socket that listens for incoming connection requests on a specified port, establishing a communication endpoint. When a client requests a connection, the server accepts it using the ServerSocket and creates a dedicated Socket for interaction. Meanwhile, the client uses the Socket class to connect to the server by specifying the server's IP address and port, enabling data transfer via input and output streams. This structured communication channel allows reliable data exchange over TCP .

TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) differ primarily in terms of reliability and connection orientation. TCP is a reliable, connection-oriented protocol that guarantees ordered, error-free, and complete delivery of data, making it suitable for applications like web services and file transfer systems. Conversely, UDP is a connectionless protocol that does not guarantee delivery, order, or error checking, hence it is faster and suitable for applications like streaming where speed is prioritized over reliability. Developers choose between these based on the application's requirements for speed and reliability .

Java models real-world objects through abstraction by defining classes that encapsulate data and behavior. The Box class program exemplifies this approach by using a class to represent a box's dimensions and methods to compute properties like volume and surface area. This abstraction allows complex real-world entities to be simplified into manageable code units, promoting understandability and reuse. By focusing on relevant attributes and behaviors, Java's abstraction helps encapsulate complexity while providing functionality that mirrors real-world counterparts .

Parameterized constructors in Java allow for the initialization of an object's data members at the time of object creation, ensuring proper assignment of values. This provides a structured and efficient way to initialize objects with specific values, enhancing data encapsulation and reducing the risk of errors associated with uninitialized variables. For instance, in the provided code for the Box class, the constructor ensures dimensions like length, width, and height are assigned upon instantiation, preventing misuse of the object due to uninitialized states .

Inheritance in Java facilitates code reusability by enabling new classes to derive functionality from existing ones. In the Box class example, the Box_Object class inherits from the Box class, reusing its methods for computing volume and surface area. This avoids code duplication and promotes modularity, as shared logic is maintained in the parent class. By reusing and extending existing code, developers can implement new features efficiently, maintain consistency, and ensure that bug fixes or improvements in the base class automatically propagate to derived classes, making inheritance a powerful tool for scalable software design .

Testing software solutions involves executing a program with a range of inputs to ensure functionality meets requirements and behaves as expected. This process is crucial as it verifies the correctness and reliability of the code. In Java programming, results are verified using sufficient test data to confirm the implementation's intended behavior. For instance, the Java assessment criteria emphasize testing the solution with adequate data to support conclusions, ensuring that code not only runs but produces accurate and expected results .

Compile-time polymorphism in Java is exemplified by method overloading and parameterized constructors. Method overloading allows multiple methods with the same name to exist, differing by their parameters. This enables a single method call to perform various functions based on the provided arguments, resolved at compile time. Similarly, parameterized constructors are special methods called at object creation, enabling different forms of object initialization, also resolved at compile time. Together, they enhance flexibility and allow variance in object creation and functionality within the same class framework .

You might also like