0% found this document useful (0 votes)
10 views3 pages

Java Object-Oriented Design Assignment

The document outlines a programming assignment for CSE 2001 focused on Object-Oriented Design in Java. It includes tasks such as creating classes for Commission, Complex numbers, Student and Exam, PointType and CircleType, Person, Shape, and Employee, along with their respective methods and functionalities. Additionally, it requires implementing interfaces and exception handling, as well as designing a package for Student and Test classes.

Uploaded by

rout1354soumya
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)
10 views3 pages

Java Object-Oriented Design Assignment

The document outlines a programming assignment for CSE 2001 focused on Object-Oriented Design in Java. It includes tasks such as creating classes for Commission, Complex numbers, Student and Exam, PointType and CircleType, Person, Shape, and Employee, along with their respective methods and functionalities. Additionally, it requires implementing interfaces and exception handling, as well as designing a package for Student and Test classes.

Uploaded by

rout1354soumya
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

CSE 2001: Data Structure & Algorithms

Programming Assignment-II
(Object-Oriented Design)

1. A sales person is paid commission based on the sales he makes as shown by the following
table:
SALES COMMISSION
Under Rs. 500 2% of SALES
Rs. 500 and under Rs. 5000 5% of SALES
Rs. 5000 and over 8% of SALES

Write a class, Commission, which has:


 An instance variable, sales; an appropriate constructor; and a method, getCommission()
that returns the commission.
Now write a Demo class in Java to test the Commission class by reading a sale from the
user, using it to create a Commission object after validating that the value is not negative.
Finally, call the getcommission() method to get and print the commission. If the sales are
negative, your Demo class should print the message “Invalid Input”.

2. Define a class called Complex with instance variables real, imag and instance methods
setData(), display(), add(). Write a Java program to add two complex numbers.

The prototype of add method is: public Complex add(Complex, Complex).

3. Write a Java program to declare a Class named as Student which contains roll number,
name and course as instance variables and input_Student () and display_Student () as
instance methods. A derived class Exam is created from the class Student . The derived
class contains mark1, mark2, mark3 as instance variables representing the marks of three
subjects and input_Marks () and display_Result () as instance methods. Create an array of
objects of the Exam class and display the result of 5 students.

4. A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design a class,
PointType in Java, that can store and process a point in the x-y plane. You should then
perform operations on the point, such as showing the point, setting the coordinates of the
point, printing the coordinates of the point, returning the x-coordinate, and returning the y-
coordinate. Every circle has a center and a radius. Given the radius, we can determine the
circle’s area and circumference. Given the center, we can determine its position in the x-y
plane. The center of a circle is a point in the x-y plane. Design a class, CircleType that can
store the radius and center of the circle. Because the center is a point in the x-y plane and
you designed the class to capture the properties of a point from PointType class. You must
derive the class CircleType from the class PointType. You should be able to perform the
usual operations on a circle, such as setting the radius, printing the radius, calculating and
printing the area and circumference, and carrying out the usual operations on the center.

5. Let a class Person contains data members name and age. A constructor with two arguments
is used to assign name and age. Person is of two types a) Student and b) Teacher. class
Student contains data members i)course ii) Roll Number and iii)Marks and method
display() to display data related to student. Similarly, class Teacher contains data members
i) subject_assigned (May take this as a String) ii) contact_hour and method display () to
display data related to teacher. Implement this program using base class constructor in
derived class.

6. Create an abstract class Shape and the derived classes Square, Triangle and Circle. Write a
java program to display area of different shapes.

7. Define an interface to declare methods display ( ) & count ( ). Another class contains a static
data member maxcount, instance member name & method display ( ) to display name of a
person, count the no. of characters present in the name of the person.

8. Define an interface EmpInterface (void displayEmp(), void giveBonus(double amount)).


Define an abstact class Employee (empID, fName, lName, salary). Define a concrete class
Manager (bonus) subclass of Employee and define the interface methods. Perform the
followings:
a. Define the appropriate constructor in class hierarchy
b. Ensure the bonus amount should not be negative and zero using exception
handling
c. Create an array of interface reference variables and populate with manager objects.
d. Write a Test class to implement the above said requirements of interface
implement ation and exception handling.
9. Design a package that contains two classes Student & Test. The Student class has data
members as name, roll and instance methods input( ) & output( ). Similarly the Test class
has data members as mark1, mark2 and instance methods input( ), output( ), Student is
extended by Test. Another package carry interface Sports with 2 attributes score1, score2.
Find grand total mark & score in another class.

**********************

Common questions

Powered by AI

In Java, when a class extends another class and implements an interface, it follows a hierarchical structure where inheritance and interface protocols are maintained simultaneously. The structure typically involves creating a base class that provides foundational attributes and methods, which are extended by a subclass to inherit and enhance functionalities. The subclass can then implement an interface to gain additional, possibly unrelated, behavior. This allows separate concerns to be integrated into a single class. For example, a Manager class might extend an Employee superclass for shared employee attributes while implementing EmpInterface for specific managerial methods like bonus computations . This design effectively combines both inheritance and polymorphism, offering a clean structure for complex systems.

To design a class structure that handles relationships between students, their tests, and sports, start by creating a Student class with basic attributes like name and roll number. This class is extended by a Test class containing test-related attributes and methods. Meanwhile, a separate interface, Sports, can define sports-specific functions or attributes. An additional class can then be created that extends the Test and implements the Sports interface to compute grand total marks including sports scores . This design uses inheritance to model academic data and interfaces to incorporate extracurricular components, promoting separation of concerns.

To effectively represent the relationship between a point in an x-y plane and a circle using inheritance in Java, a class PointType can be designed to encapsulate the x and y coordinates. A CircleType class can then inherit from PointType to utilize its properties as the center of the circle. This allows the CircleType class to access and manipulate the position of its center point, while also storing additional properties like the circle's radius, and providing methods to compute the area and circumference .

Polymorphism in Java can be applied to calculate the area of different shapes by creating an abstract class Shape with a method for calculating area, then having derived classes like Square, Triangle, and Circle implement this method according to their specific calculations. The main advantage of polymorphism here is the ability to call the area calculation method on any Shape instance without knowing its concrete type, thus promoting flexible and reusable code structure .

In object-oriented programming, validation techniques can ensure data integrity by implementing checks and constraints right in the class methods where data is processed. For instance, in computing employee bonuses, the Manager class can implement validation logic in the giveBonus() method to ensure the bonus amount is greater than zero . By using exception handling to manage incorrect data entry, such as negative bonuses, the program maintains integrity and prevents logical errors that could lead to incorrect computational outcomes.

When designing a class hierarchy for computing commissions based on sales, it's important to consider encapsulation of sales data and proper validation to handle different categories of sales. The Commission class should encapsulate the sales amount and ensure it is not negative before processing. Additionally, separate methods for calculating commission depending on sales brackets (under Rs. 500, between Rs. 500 and under Rs. 5000, and Rs. 5000 and over) should be properly defined . This ensures modular code that's easy to maintain and extend.

When using interfaces to abstract behaviors across different classes in Java, it’s crucial to define clear and specific methods that all implementing classes must adhere to, ensuring consistency across various implementations. For example, having an interface EmpInterface with methods displayEmp() and giveBonus(double amount) allows different concrete classes like Manager to implement specific logic while maintaining a common structure . Additionally, ensuring proper input validation, such as not allowing negative bonuses, is vital to prevent misuse and maintain program integrity across implementations.

Designing a Java package that includes both Student and Test classes with an interface for sports provides modularity, code reuse, and abstraction. The Student class can encapsulate academic data, while the Test class extends it with test-related methods, allowing reuse of common functionality. An interface for sports facilitates flexibility by allowing different types of sports data implementations that can interact seamlessly with student information . This design enhances maintainability by separating concerns and clarifies the application architecture through distinct and cohesive components.

Exception handling enhances the implementation of interfaces and abstract classes in Java by providing a robust mechanism to catch and handle runtime errors, ensuring that the program does not terminate unexpectedly. For instance, in the Manager class derived from the abstract Employee class, exception handling ensures that an illegal bonus amount, such as negative or zero, is prevented by catching exceptions whenever a bonus is set . This leads to more reliable and maintainable code by enforcing input validation seamlessly across derived classes.

Encapsulation helps in organizing and maintaining complex data by ensuring that the internal representation of an object is hidden from the outside, allowing access only through publicly exposed methods. For example, the Commission class encapsulates the sales data and provides a getCommission() method to access the computed commission without exposing the internal sales processing logic directly . This promotes controlled interaction with object data, reduces code complexity, and improves maintenance by allowing changes within a class without affecting external code depending on it.

You might also like