Answer to Question #291180 in Java | JSP | JSF for yash

Question #291180

Write a program to create a class CirclesWithException with the following information:  

radius in double with private access specifier.  

  • Use public methods setRadius() and getRadius() to access radius of the circle. setRadius() method throws an IllegalArgumentException if the argument radius is negative. 
  • Add a constructor to pass on radius of a Circle. Call setRadius() method to set the radius of circle inside the constructor. 
  • Use printRadius( ) to display the radius if object is created. 
  • Use printStackTrace() and getMessage() methods to obtain information from exception objects in corresponding catch block. 
  • Create three objects of class CirclesWithException with positive, negative and zero radius. 
1
Expert's answer
2022-01-27T11:06:26-0500


class CirclesWithException {
	private double radius;


	/**
	 * a constructor to pass on radius of a Circle. Call setRadius() method to set
	 * the radius of circle inside the constructor.
	 * 
	 * @param radius
	 */
	public CirclesWithException(double radius) {
		setRadius(radius);
	}


	/**
	 * @return the radius
	 */
	public double getRadius() {
		return radius;
	}


	/**
	 * @param radius the radius to set
	 */
	public void setRadius(double radius) {
		if (radius < 0) {
			throw new IllegalArgumentException("The argument radius is negative.");
		}


		this.radius = radius;
	}


	public void printRadius() {
		System.out.println("Radius: " + radius);
	}


}


public class App {


	/**
	 * The start point of the program
	 * 
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		// CirclesWithException with positive, negative and zero
		try {
			CirclesWithException positive = new CirclesWithException(5);
			positive.printRadius();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		try {
			CirclesWithException negative = new CirclesWithException(-5);
			negative.printRadius();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		try {
			CirclesWithException zero = new CirclesWithException(0);
			zero.printRadius();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}


	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS