Search This Blog

Tuesday, 11 October 2011

Java Programs for Practice

Create a class called CheckingAccount with the following members.
· Account number
· Name of account holder
· Current balance
· Constructor

· Accept a deposit
· Process a check
· Get the current balance
· Display details of an account

Create the object; initialize the three data items.  Create a driver class called CheckingAccountTest to test this class.

1.      Define a class named CheckingAcc having three data members as follows:
String accNo;
String custName;
double balAmount;
2.      Implement the constructors – default and parameterized constructor for initialization:
CheckingAcc(String aNo, String aName, double bAmt);
3.      Implement the method Deposit(double transAmt) for depositing the amount given as input.
4.      Implement another method Withdrawal(double transAmt) for withdrawing the money.
5.      Introduce a method for getting the balance from the current account object.
6.      Provide a method for displaying all the details of the Checking Account.
7.      Create a driver class named  CheckingAccountTest which conains the main() method having code for creating and testing the methods of the CurrentAcc object.

Create and Experiment a class for Rational Numbers:
Rational numbers are numbers that can be represented as a fraction p / q where p is an integer number and q is a positive integer (q != 0).
Design and implement a Java class RationalNumber for representing such numbers. Implement methods to add und mutliply rational numbers. Implement a method for return the value of a rational number as a double value. Make sure that the numerator p and denominator q do not have common divisors in your implementation. Use the algorithm for calculation the greatest common divisor to ensure this property.

1.      Define a class named RationalNumber with two data members:
int p, int q;
2.      Add two methods – one for Addion and another for Multiplication of rational numbers:
RationalNumber Add(RationalNumber rNumber);
RationalNumber Multiply(RationalNumber rNumber);
3.      Introduce another method for returning the rational number as a double value:
double getRational();
4.      Implement a method such as setRational() for setting the value for p and q.  Before assignement check where the numbers are having common divisiors or not.
5.      Write a driver class for creating and testing the objects of RationalNumber.

Create and experiment with Time Class:
Create a class called Time that has separate int member data for hours, minutes and seconds. One constructor should initialize this data to 0, and another should initialize it to fixed values. If any of the data is invalid, it should initialize the corresponding instance variable to 0.The valid range of time is [0:0:0 to 23:59:59]. Include appropriate getter and setter methods. Another method should display it, in 11:59:59 format. Another method should compare 2 time objects and print the time which is greater. The final method should add two objects of type time passed as arguments. Create a class TimeTest to test the Time class.

Define and create an Object for Circle
Create a class called Circle with radius as the instance variable and include constructors and methods to calculate the area and circumference of the circle. Create a subclass called Cylinder which has an additional instance variable height and include constructors and methods to calculate the surface area(circumference*height) and volume(area*height) of the cylinder.
Create a driver class to test the Cylinder class. Print the cross sectional area(pi*r*r) , surface area and volume of the cylinder.

Define and create an Square Object
Create a class called square with a method calcArea() to find the area of a square(a*a). Create a subclass cube and write an overriding method calcArea() that finds the surface area(6*a*a) of the cube. Write a driver class to test the 2 classes.

Create and experiment with Bank Account Object:

Implement and test the classes corresponding to the following Class diagrams, which represent a bank account.

String name
String accountnumber
Account(String, String)
String toString()

SavingsAccount extends Account
double balance
SavingsAccount(String, String, double)
void deposit(double)
void withdraw(double)
String toString()

Implement an Invoice Object for a hardware store:

Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables --- a part number, a part description, a quantity of the item being purchased and a price per item. Your class should have a constructor that initializes the four instance variables. Provide a set and get method for each instance variable. In addition, provide a method named getInvoiceAmount that calculates the invoice amount, then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0. Write a test application called InvoiceTest that demonstrates class Invoice's capabilities.

Define Employee object for Payroll Processing

Create a class called Employee that includes three pieces of information as instance variables --- a first name, a last name and a monthly salary. Your class should have a constructor that initializes the 3 instance variables. Provide a set and get method for each instance variable. If the monthly salary is not positive, set it to 0.0. The class should also include a method that returns the annual salary of the employee. Write an application named EmployeeTest that demonstates class Employee's capabilities. Create 2 employee objects and display the annual salary of both the employees and specify the first name and the last name of the employee earning a higher salary.

Implement the interface Shape as follows:

Interface Shape requires the methods
double area()
double perimeter()

The follwing classes implement Shape : Circle, Rectangle and Square.  Write code that includes the interfaces and classes. Write a method double area(Shapes[]) that computes the total area of an array of shapes. Show the Polymorphism.

Define and Implement an Interface for Stack Object:

Create an interface called Stack that has 2 methods push and pop. Create a class FixedStack that implements Stack interface.  Create another class DynamicStack that implements Stack interface. Write a driver class to test the 2 classes.

Create and Run threads as specified below:

1.      Write a java Program that creates 3 threads by extending Thread class. First thread displays “Good Morning” every 1 sec, the second thread displays “Hello” every 2 seconds and the third displays “Welcome” every 3 seconds.

2.      Write a java program that creates 3 threads by implementing Runnable interface. First thread displays “Good Morning” every 1 sec, the second thread displays “Hello” every 2 seconds and the third displays “Welcome” every 3 seconds.

Experiment with Files in Java:

Write a java program to display the contents of a text file. (.txt file)

Experiment File Copying in Java:

Write a java program to copy one text file to another.

Analyse the content of a Text file as given below:

Write a java program to count the number of words and lines in a text file.

Create a Package for holding the classes of type Shape:

Create a package called shapes. Now write a class called Point (belongs to package shape) which represents a point in 2D-space. It has two fields x and y. Write a parametrized constructor to initialize these values(do not provide default constructor).

Now write another class called Circle (belongs to package shapes) which has a field of type Point (to represent the center of circle) and another field radius. Write a parametrized constructor to initalize all fields of circle. Now write a method in this class called isInside() which takes a point as parameter and returns true if that point is inside the circle or false otherwise.

Now write main method (in CircleDemo class), create a circle and invoke isInside() method by passing a parameter to it and print the return value.  We will have Three classes Point, Circle and CircleDemo (contains main method)

Point(int x, int y){ } //parametrized constructor
Circle(Point centre, int radius){ } //parametrized constructor
boolean isInside(Point p)

Handle Exception while creating and managing a Bank Account object:

Write a class called BankAccount with the following properties and methods.
Properties: name, PIN, balance
Methods: deposit(int amt), withdraw(int amt)

Write another class called ATM with following methods and properties:
methods:deposit(Account acc, int amt), withdraw(Account acc, int amt) ,
transfer(Account acc1, Account acc2, int amt)

Assume that an account needs to have a minimum balance of 200. If an attempt is made to withdraw, which results in balance going below 200, throw an exception called MinimumBalanceException.  For this you need to create your own exception class.

Note: To create an Exception class called MyFirstException
class MyFirstException extends Exception{

Use throw and throws wherever necessary.

Develop an Applet for drawing a Circle and Rectangle dynamically:

1.      Develop an applet to draw a circle and the maximum possible square within the circle.  Also develop an applet to draw a square and the maximum possible circle within the square.
2.      Develop an applet that allows users to input values for the arguments required by the method drawRect() , then draws a rectangle using the four input values.

Leave a Reply