0% found this document useful (0 votes)
224 views21 pages

Assignment 3&4

The document contains 5 Java programs: 1. Calculates the area of a rectangle, circle, and square using parameterized constructors. 2. Finds the volume of a cube, cylinder, and sphere using method overloading. 3. Finds volume using command line arguments instead of input. 4. Defines a complex number class with addition, subtraction, and multiplication methods. 5. Defines an interface for calculating the involume and outvolume of a part using its 3 dimensions.

Uploaded by

Soumyajit Das
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
224 views21 pages

Assignment 3&4

The document contains 5 Java programs: 1. Calculates the area of a rectangle, circle, and square using parameterized constructors. 2. Finds the volume of a cube, cylinder, and sphere using method overloading. 3. Finds volume using command line arguments instead of input. 4. Defines a complex number class with addition, subtraction, and multiplication methods. 5. Defines an interface for calculating the involume and outvolume of a part using its 3 dimensions.

Uploaded by

Soumyajit Das
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

1.

Write a program to calculate the area of a Rectangle, a Circle & a Square using parameterized constructor using BuffredReader class object. Ans. import java.io.*; interface Area { public void calculateArea(); } class Rectangle { int a=0,b=0,area=0; public Rectangle(int c,int d) { a=c; b=d; } public void calculateArea() { area=a*b; System.out.println("The area of the Rectangle is : "+area); } } class Circle { float r=0; public Circle(float e) { r=e; }

public void calculateArea() { double Carea=3.14*r*r; System.out.println("The area of the Circle is : "+Carea);

} } class Square { float a=0; public Square(float e) { a=e; } public void calculateArea() { double Sarea=a*a; System.out.println("The area of the square is : "+Sarea);

} }

class sampleMain { public static void main(String []args) { float l=0; int m=0,n=0,p=0;

try { System.out.println("Enter The radius of a circle........."); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); l=Integer.parseInt(br.readLine()); } catch(Exception e) { System.out.println(e); } Circle obj=new Circle(l); obj.calculateArea(); try { System.out.println("Enter The length and breadth of a Rectangle........."); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); m=Integer.parseInt(br.readLine()); n=Integer.parseInt(br.readLine()); } catch(Exception e) { System.out.println(e); } Rectangle rec=new Rectangle(m,n); rec.calculateArea(); try { System.out.println("Enter The length of a Square.........");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); p=Integer.parseInt(br.readLine());

} catch(Exception e) { System.out.println(e); } Square sr=new Square(p); sr.calculateArea(); } } Output: Enter The radius of a circle......... 2 The area of the Circle is : 12.56 Enter The length and breadth of a Rectangle......... 5 2 The area of the Rectangle is : 10 Enter The length of a Square......... 8 The area of the square is : 64.0

2. Write a program to design a class Volume and then find Volume and then find out the volume of a Cube, Cylinder and Sphere using method overloading using BufferedReader class object. Ans.

import java.io.*; class Volume { void claculateVolume(int s) { int vol=0;

vol=s*s*s; System.out.println("The Volume of a Cube is : "+vol); } void claculateVolume(double r,double h) { double pi=3.1428; double vol=0.0; vol=pi*r*r*h; System.out.println("The Volume of a Cylinder is : "+vol); } void claculateVolume(double a) { double pi=3.1428; double b=(4.0/3.0); double c=b*pi; double vol=0.0; double d=a*a*a; vol=c*d; System.out.println("The Volume of a Sphere is : "+vol); } } class Main { public static void main(String args[]) { boolean i=true; Volume v=new Volume(); int ch=0;

int s=0; double r=0.0,h=0.0,a=0.0;

try {

System.out.println("\n\nEnter\n\n1. To Calculate Volume Of Cube\n\n2. To Calculate Volume Of Cylinder\n\n3. To Calculate Volume Of Sphere"); System.out.println("Enter Your Choice....."); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); ch=Integer.parseInt(br.readLine());

} catch(Exception e) { System.out.println(e); }

switch(ch) { case 1: try { System.out.println("Enter The side of a Cube........."); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); s=Integer.parseInt(br.readLine());

catch(Exception e) { System.out.println(e); } v.claculateVolume(s); break; case 2: try { System.out.println("Enter The radius of a Cylinder........."); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); r=Integer.parseInt(br.readLine()); System.out.println("Enter The height of a Cylinder........."); h=Integer.parseInt(br.readLine());

} catch(Exception e) { System.out.println(e); } v.claculateVolume(r,h); break; case 3: try { System.out.println("Enter The radius of a Sphere........."); BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

a=Integer.parseInt(br.readLine());

} catch(Exception e) { System.out.println(e); } v.claculateVolume(a); break; default: System.out.println("Invalid Input"); break; } } } Output: Enter 1. To Calculate Volume Of Cube 2. To Calculate Volume Of Cylinder 3. To Calculate Volume Of Sphere Enter Your Choice.. 1 Enter The side of a Cube......... 5 The Volume of a Cube is : 125 Enter 1. To Calculate Volume Of Cube 2. To Calculate Volume Of Cylinder

3. To Calculate Volume Of Sphere Enter Your Choice.. 2 Enter The radius of a Cylinder......... 4 Enter The height of a Cylinder......... 6 The Volume of a Cylinder is : 301.7088 Enter 1. To Calculate Volume Of Cube 2. To Calculate Volume Of Cylinder 3. To Calculate Volume Of Sphere Enter Your Choice.. 3 Enter The radius of a Sphere......... 8 The Volume of a Sphere is : 2145.48479999999997 Enter 1. To Calculate Volume Of Cube 2. To Calculate Volume Of Cylinder 3. To Calculate Volume Of Sphere Enter Your Choice.. 5 Invalid Input

3. Write a program to design a class Volume and then find Volume and then find out the volume of a Cube, Cylinder and Sphere using method overloading using command line argument. Ans.

import java.io.*; class CommandVolume { void claculateVolume(int s) { int vol=0; vol=s*s*s; System.out.println("The Volume of a Cube is : "+vol); } void claculateVolume(double r,double h) { double pi=3.1428; double vol=0.0; vol=pi*r*r*h; System.out.println("The Volume of a Cylinder is : "+vol); } void claculateVolume(double a) { double pi=3.1428; double b=(4.0/3.0); double c=b*pi; double vol=0.0; double d=a*a*a; vol=c*d; System.out.println("The Volume of a Sphere is : "+vol); } } class CommandMain

{ public static void main(String args[]) { CommandVolume v=new CommandVolume(); if(args[0].equalsIgnoreCase("Cube")) { int s=Integer.parseInt(args[1]);; v.claculateVolume(s); } if(args[0].equalsIgnoreCase("Cylinder")) { double r=Double.valueOf(args[1]).doubleValue(); double h=Double.valueOf(args[2]).doubleValue(); v.claculateVolume(r,h); } if(args[0].equalsIgnoreCase("Sphere")) { double a=Double.valueOf(args[1]).doubleValue(); v.claculateVolume(a); } } }

Output: java CommandMain Cube 2 The Volume of a Cube is : 8 Java CommandMain Cylinder 4 6 The Volume of a Cylinder is : 301.7088

java CommandMain Sphere 8 The Volume of a Sphere is : 2145.48479999999997 4. Create a complex class with data members as real and imaginary.Overload three constructors to initialize the data members(i.e. default,normal, and through object initialization).provide methods which returns object of complex class as he result for addition,subtraction,multiplication of two complex numbers. Ans. import java.io.*; class Complex { int real; int imaginary; Complex(int real,int imaginary) { this.real=real; this.imaginary=imaginary; } Complex(Complex c) { real=c.real; imaginary=c.imaginary; } Complex() {} public Complex addition(Complex c2) { Complex c3=new Complex(); c3.real=real+c2.real; c3.imaginary=imaginary+c2.imaginary; System.out.println("The Result of addition is..........");

return c3; } public Complex subtraction(Complex c2) { Complex c3=new Complex(); c3.real=real-c2.real; c3.imaginary=imaginary-c2.imaginary; System.out.println("The Result of subtraction is..........");

return c3; } public Complex multiplication(Complex c2) { Complex c3=new Complex(); c3.real=real*c2.real; c3.imaginary=imaginary*c2.imaginary; System.out.println("The Result of multiplication is..........");

return c3; } public void showComplexNo() { System.out.println("The Complex No Is\n"+real+"+i("+imaginary+")"); } } class ComplexMain {

public static void main(String args[]) { int r1=0,i1=0,r2=0,i2=0; try { System.out.println("Enter The First Complex Number........."); System.out.println("Enter The Real part of the Complex Number........."); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); r1=Integer.parseInt(br.readLine()); System.out.println("Enter The Imaginary part of the Complex Number........."); i1=Integer.parseInt(br.readLine()); } catch(Exception e) { System.out.println(e); } Complex c1=new Complex(r1,i1); c1.showComplexNo(); try { System.out.println("\nEnter The Second Complex Number........."); System.out.println("Enter The Real part of the Complex Number........."); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); r2=Integer.parseInt(br.readLine()); System.out.println("Enter The Imaginary part of the Complex Number........."); i2=Integer.parseInt(br.readLine()); } catch(Exception e)

{ System.out.println(e); } Complex c2=new Complex(r2,i2); C2.showComplexNo(); Complex c3=new Complex(); c3=c1.addition(c2); c3.showComplexNo(); Complex c4=new Complex(); c4=c1.subtraction(c2); c4.showComplexNo(); Complex c5=new Complex(); c5=c1.multiplication(c2); c5.showComplexNo(); } }

Output: Enter The First Complex Number......... Enter The Real part of the Complex Number......... 4 Enter The Imaginary part of the Complex Number......... 5 The Complex No Is 4+i(5) Enter The Second Complex Number......... Enter The Real part of the Complex Number......... 2

Enter The Imaginary part of the Complex Number......... 3 The Complex No Is 2+i(3) The Result of addition is.......... The Complex No Is 6+i(8) The Result of subtraction is.......... The Complex No Is 2+i(2) The Result of multiplication is.......... The Complex No Is 8+i(15)

5. Write a program using an interface called Volume.Assume that there is a part in a machine having three dimension s1,s2,s3 and Involume=1/3*pi*s1*s2*s3; Outvolume=4/3*pi*s1*s2*s3 Ans.

import java.io.*;

interface Volume { Double Involume(int s1, int s2, int s3); Double Outvolume(int s1, int s2, int s3); }

class inVol implements Volume {

public Double Involume(int s1, int s2, int s3) { Double p = (0.333333)*java.lang.Math.PI*s1*s2*s3; return p; } public Double Outvolume(int s1, int s2, int s3) { return null; } }

class outVol implements Volume { public Double Outvolume(int s1, int s2, int s3) { Double p = (4/3)*java.lang.Math.PI*s1*s2*s3; return p; } public Double Involume(int s1, int s2, int s3) { return null; }

} class VolumeCalculator { public static void main(String[] ar) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the value of s1: "); int s1 = Integer.parseInt(br.readLine()); System.out.print("Enter the value of s2: "); int s2 = Integer.parseInt(br.readLine()); System.out.print("Enter the value of s3: "); int s3 = Integer.parseInt(br.readLine());

Volume in = new inVol(); Volume out = new outVol();

Double invol = in.Involume(s1,s2,s3); Double outvol = out.Outvolume(s1,s2,s3);

System.out.println("Involume : "+invol); System.out.println("Outvolume : "+outvol); } }

Output: Enter the value of s1: 5 Enter the value of s2: 4 Enter the value of s3: 3 Involume : 62.83179023994279 Outvolume : 188.4955921538757

6. Define an Exception NoMatchFoundException that is thrown when Kolkata is not found from the following set of strings. City name={Kolkata,Mumbai,Delhi,Bangalore,Ahmedabad}

Ans.

import java.io.*; class MyException extends Exception { private String excp="NoMatchFoundException", exp; MyException(String exp) { this.exp = exp; } public String toString() { return "Exception: "+excp+" : "+exp; } }

public class NoMatchException { static String[] str = {"Kolkata", "Chennai", "Delhi", "Mumbai", "Bangalore", "Ahmedabad"}; static int i = str.length-1; static void find(String s) throws MyException { if(i>=0 && str[i].equals(s)) { System.out.println("Match Found!"); } else if(i>=0 && !str[i].equals(s))

{ i--; find(s); } Else { throw new MyException(s); } }

public static void main(String[] ar) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter your string --->"); String s = br.readLine(); try { find(s); } catch(Exception e) { System.out.println("Caught: "+e); } } }

Output:

Enter your string ---> Hyderabad Caught: Exception : NoMatchFoundException : Hyderabad

You might also like