Computer Applications
Write a program in Java to find the roots of a quadratic equation ax2+bx+c=0 with the following specifications:
Class name — Quad
Data Members — float a,b,c,d (a,b,c are the co-efficients & d is the discriminant), r1 and r2 are the roots of the equation.
Member Methods:
- quad(int x,int y,int z) — to initialize a=x, b=y, c=z, d=0
- void calculate() — Find d=b2-4ac
If d < 0 then print "Roots not possible" otherwise find and print:
r1 = (-b + √d) / 2a
r2 = (-b - √d) / 2a
Java
Java Constructors
44 Likes
Answer
import java.util.Scanner;
public class Quad
{
private float a;
private float b;
private float c;
private float d;
private float r1;
private float r2;
public Quad(float x, float y, float z)
{
a = x;
b = y;
c = z;
d = 0;
}
public void calculate() {
d= (b * b) - (4 * a * c);
if (d < 0)
System.out.println("Roots not possible");
else {
r1 = (float)((-b + Math.sqrt(d)) / (2 * a));
r2 = (float)((-b - Math.sqrt(d)) / (2 * a));
System.out.println("r1=" + r1);
System.out.println("r2=" + r2);
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
float x = in.nextFloat();
System.out.print("Enter b: ");
float y = in.nextFloat();
System.out.print("Enter z: ");
float z = in.nextFloat();
Quad obj = new Quad(x,y,z);
obj.calculate();
}
}
Variable Description Table
Program Explanation
Output
Answered By
16 Likes
Related Questions
The population of a country in a particular year can be calculated by:
p*(1+r/100) at the end of year 2000, where p is the initial population and r is the
growth rate.Write a program by using a class to find the population of the country at the end of each year from 2001 to 2007. The Class has the following specifications:
Class name — Population
Data Members — float p,r
Member Methods:
- Population(int a,int b) — Constructor to initialize p and r with a and b respectively.
- void print() — to calculate and print the population of each year from 2001 to 2007.
Define a class named FruitJuice with the following description:
Data Members Purpose int product_code stores the product code number String flavour stores the flavour of the juice (e.g., orange, apple, etc.) String pack_type stores the type of packaging (e.g., tera-pack, PET bottle, etc.) int pack_size stores package size (e.g., 200 mL, 400 mL, etc.) int product_price stores the price of the product Member Methods Purpose FruitJuice() constructor to initialize integer data members to 0 and string data members to "" void input() to input and store the product code, flavour, pack type, pack size and product price void discount() to reduce the product price by 10 void display() to display the product code, flavour, pack type, pack size and product price The basic salary of employees is undergoing a revision. Define a class called Grade_Revision with the following specifications:
Data Members Purpose String name to store name of the employee int bas to store basic salary int expn to consider the length of service as an experience double inc to store increment double nbas to store new basic salary (basic + increment) Member Methods Purpose Grade_Revision() constructor to initialize all data members void accept() to input name, basic and experience void increment() to calculate increment based on experience as per the table given below void display() to print all the details of an employee Experience Increment Up to 3 years ₹1,000 + 10% of basic 3 years or more and up to 5 years ₹3,000 + 12% of basic 5 years or more and up to 10 years ₹5,000 + 15% of basic 10 years or more ₹8,000 + 20% of basic Write the main method to create an object of the class and call all the member methods.
Write a program by using a class in Java with the following specifications:
Class name — Stringop
Data members:
- String str
Member functions:
- Stringop() — to initialize str with NULL
- void accept() — to input a sentence
- void encode() — to replace and print each character of the string with the second next character in the ASCII table. For example, A with C, B with D and so on
- void print() — to print each word of the String in a separate line