Programming in Java - Week8
Programming in Java - Week8
PRACTICAL 1
Objective
Design a class Complex having a real part (x) and an imaginary part (y). Provide methods to
perform the following on complex numbers:
1. Add two complex numbers.
2. Multiply two complex numbers.
3. toString() method to display complex numbers in the form: x + i y
/**** Complex.java ****/
public class Complex {
private int x;
private int y;
/**
* Parameterized Constructor of Complex class
*
* @param real Real Part
* @param imaginary Imaginary Part
*/
public Complex(int real, int imaginary) {
this.x = real;
this.y = imaginary;
}
/**
* Add two Complex Objects
*
* @param o Complex Object
* @return Complex Object
*/
public Complex add(Complex o) {
return new Complex(
this.x + o.x,
this.y + o.y
);
}
/**
* Multiply two Complex Objects
*
* @param o Complex Object
* @return Complex Object
*/
public Complex multiply(Complex o) {
return new Complex(
this.x * o.x - this.y * o.y,
this.x * o.y + o.x * this.y
);
}
/**
* Type Conversion to String
*
* @return String Representation
*/
@Override
public String toString() {
return x + " + i " + y;
}
}
Output
PRACTICAL 2
Objective
Create a class TwoDim which contains private members as x and y coordinates in package
P1. Define the default constructor, a parameterized constructor and override toString()
method to display the co-ordinates. Now reuse this class and in package P2 create another
class ThreeDim, adding a new dimension as z as its private member. Define the constructors
for the subclass and override toString() method in the subclass also. Write appropriate
methods to show dynamic method dispatch. The main() function should be in a package P.
/**** P1/TwoDim.java ****/
package P1;
public class TwoDim {
private int x;
private int y;
public TwoDim() {
this.x = 0;
this.y = 0;
}
public TwoDim(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Coordinate: x = " + x + " y = " + y;
}
}
Output
PRACTICAL 3
Objective
Define an abstract class Shape in package P1. Inherit two more classes: Rectangle in package
P2 and Circle in package P3. Write a program to ask the user for the type of shape and then
using the concept of dynamic method dispatch, display the area of the appropriate subclass.
Also write appropriate methods to read the data. The main() function should not be in any
package.
Output
PRACTICAL 4
Objective
Create an Exception subclass UnderAge, which prints “Under Age” along with the age value
when an object of UnderAge class is printed in the catch statement. Write a class
exceptionDemo in which the method test() throws UnderAge exception if the variable age
passed to it as argument is less than 18. Write main() method also to show working of the
program.
/**** UnderAge.java ****/
public class UnderAge extends Exception {
final private int age;
public UnderAge(int age) {
this.age = age;
}
@Override
public String getMessage() {
return "UnderAge: " + age + " is less than 18";
}
}
Output
Case 1: Age is lesser than 18
PRACTICAL 5
Objective
Write a program to implement stack. Use exception handling to manage underflow and
overflow conditions.
// Array of Elements
private int[] array;
/**
* Push an element to the top of the stack
*
* @param e Element to be pushed
* @throws StackException Stack Overflow
*/
public void push(int e) throws StackException {
if (tos == size - 1)
throw new StackException("Stack Overflow: could not push " + e);
else
this.array[++this.tos] = e;
}
/**
* Pop an element from the stack
*
* @return Object on top of the stack
* @throws StackException Stack Underflow
*/
public int pop() throws StackException {
if (this.tos < 0) {
throw new StackException("Stack Underflow:could not pop");
} else
return this.array[this.tos--];
}
/**
* Index of the element at the top of the stack
*
* @return Index of Generic Element
*/
public int getTOS() {
return this.tos;
}
/**
* Representation of Stack Object
*
* @return String Representation
*/
@Override
public String toString() {
return "Stack<size=" + this.size + ">";
}
}
Output
PRACTICAL 6
Objective
Write a program that copies content of one file to another. Pass the names of the files
through command-line arguments.
/**** Copy.java ****/
import java.io.*;
public class Copy {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: java Copy <src> <dest>");
} else {
int i;
FileInputStream fin = new FileInputStream(args[0]);
FileOutputStream fout = new FileOutputStream(args[1]);
while ((i = fin.read()) != -1) {
fout.write(i);
}
fin.close();
fout.close();
System.out.println(
"Copied contents of" + args[0] + " to " + args[1]
);
}
}
}
References/Resources:
NOTE: Please go through the above programs carefully and practice them (on machine if
possible).