Knowee
Questions
Features
Study Tools

Select the correct answerWhat will be the output of the following Java program? class shape {  int breadth;  int height;  int area;  void area()  {   area = 1/2 * breadth * height;  }   void area(int x)  {   area = x;  }  }  class triangle  {  public static void  main(String args[])  {   shape obj = new shape();   obj.breadth = 4;   obj.height = 4;   obj.area(4);    System.out.println(obj.area);   }  }Options04816

Question

Select the correct answerWhat will be the output of the following Java program? class shape {  int breadth;  int height;  int area;  void area()  {   area = 1/2 * breadth * height;  }   void area(int x)  {   area = x;  }  }  class triangle  {  public static void  main(String args[])  {   shape obj = new shape();   obj.breadth = 4;   obj.height = 4;   obj.area(4);    System.out.println(obj.area);   }  }Options04816

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct answer is 4.

Here's the step-by-step explanation:

  1. The shape class has two methods named area. One is a no-argument method that calculates the area of a triangle using the formula 1/2 * breadth * height. The other is a single-argument method that simply assigns the value of its argument to the area field.

  2. In the main method of the triangle class, an object obj of the shape class is created.

  3. The breadth and height fields of obj are set to 4.

  4. The area method with a single argument is called on obj, passing 4 as the argument. This method assigns the value of its argument to the area field, so area is now 4.

  5. The value of area is printed, which is 4.

Note: The area method without arguments is never called, so the calculation 1/2 * breadth * height is not performed. If it were, the result would be 0, not 4, because 1/2 is an integer division and its result is 0. To get a correct result, you would need to use 0.5 instead of 1/2.

This problem has been solved

Similar Questions

Select the correct answerWhat will be the output of the following Java code?  class Area {    public static void main(String args[])     {          double radii, pi, area;       radii = 6.6;       pi = 3.14;       area = pi * radii * radii;       System.out.println(area);    }   }Options136136.7784136.77136.7760000

Select the correct answerWhat will be the output of the following Java code?class rectangle { int width; int length; int area; void area(int width, int length) { this.width = width; this.length = length; }  } class Output { public static void main(String args[]) { area obj = new area(); obj.area(5 , 6); System.out.println(obj.length + " " + obj.width); } }Options6 55 630 00 30

elect the correct answerWhat will be the output of the following Java code?class rhombus    {        int d1;        int d2;        int area;    public void area()        {            area = (d1*d2)/2;            System.out.println(area);        }    }    class Output    {        public static void main(String args[])        {            rhombus obj = new rhombus();            obj.d1=8;            obj.d2=2;            obj.area();        }    }OptionsRun time error168Compilation error

Consider the base class "Shape" with methods draw() [prints message "Draw"]. Create three subclasses: Circle, Square, and Triangle. Override the draw() method in each subclass to draw the respective shape, and override the calculateArea() method to calculate and return the area of each shape. Implement the above scenario with suitable pseudocode and appropiate java application.User Choice may be "C/S/T". a.)  C (Circle), then must read "Radius"b.)  S (Square), then must read "Side"c.)  T (Triange), then must read both "Height and Base".Identify the suitable data members for the above derived classes and ensure then all are private.

Create an abstract class named shape that contains two integers and an empty method named printarea().Provide three classes named rectangle, triangle and circle such that each one of the classes extends the class Shape. Each of the inherited class from shape class should provide the implementation for the method printarea(). Get the input and calculate the area of rectangle, circle and triangle.In the Main class, create the objects for the three inherited classes and invoke the methods and display the  area values of the different shapes.2(length of rectangle)3(breadth)5(triangle breadth)6(Triangle height)4(circle radius)

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.