Java Assignments 5th Sem
Java Assignments 5th Sem
Sample Questions:
1. Write a program in java that sorts half of element in ascending and rest half of the elements in
descending order.
2. Write a program in java that accepts a 2D matrix and prints the matrix with row minimum and
column minimum values.
4 3 5 3
1 0 7 0
8 4 6 4
1 0 5
3. Write a program in java to delete all consonants from an input string and print the result string.
4. A class called MyPoint, which models a 2D point with x and y coordinates. It contains:
• Two instance variables x (int) and y (int).
• A default (or "no-argument" or "no-arg") constructor that construct a point at the default location
of (0, 0).
• A overloaded constructor that constructs a point with the given x and y coordinates.
• A method setXY() to set both x and y.
• A method getXY() which returns the x and y in a 2-element int array.
• A toString() method that returns a string description of the instance in the format "(x, y)".
• A method called distance(int x, int y) that returns the distance from this point to another point at the
given (x, y) coordinates, Write the MyPoint class. Also write a test driver (called TestMyPoint) to test
all the public methods defined in the class.
5. Create a superclass ‘Person’ and two subclasses ‘Student’ and ‘Staff’. The following are the instance
variables and methods:
a. For ‘Person’ instance variables: name:String, address:String. Initiate variable through
constructor, incorporate one method setPerson() that updates Person variables , another
method tostring() that shows Person details as “Person[name=?,address=?”.
b. For ‘Student’ sub class instance variables: program:String, year:String, fees:double. Initiate
both ‘Student’ and ‘Person’ variables through constructor, incorporate one method
setStudent() that updates both student and ‘Person’ data, another method tostring() that shows
‘Person-Student’ details as “Person[name=?,address=?,Program=?,Year=?,Fees=?”.
c. For ‘Staff’ subclass instance variables: school:String, pay:double. Initiate both ‘Staff’ and
‘Person’ variables through constructor, incorporate one method setStaff() that updates both
‘staff’ and ‘Person’ data, another method tostring() that shows ‘Person-Staff’ details as
“Person[name=?,address=?,School=?,Pays=?”.
Write the classes and a test driver main class to test all functions mentioned above.
6. Create a base class ‘Square’ having instance variable side:double. Initiate variable using constructor,
a method ‘getVolume() : double’ that calculates volume and print it. Create a derived class ‘Cylinder’
having instance variable height:double. Initiate variables of both classes through constructor,
override method ‘getVolume() : double’ to calculate volume of cylinder taking ‘side’ variable of base
class as ‘radius’ and print it.
7. Consider you are designing vehicles engine with ‘speed:int, gear:int’. you can define your engine
functionalities ‘speedUp(value)’ and ‘changeGear(value) in an interface. The class which is
implementing the interface should implement all the methods in the interface.