WIPRO TRAINING ASSIGNMENT DAY 4
OOPS / INHERITANCE
Name = Harshit More
Enrollment = 0103CS193D05
Q1. Create a class Box that uses a parameterized method to initialize the
dimensions of a box.(dimensions are width, height, depth of double type).
The class should have a method that can return volume. Obtain an object
and print the corresponding volume in main() function.
package class_project;
import java.util.*;
class box
{
float height_obj;
float width_obj;
float depth_obj;
public box(float height, float width, float depth)
{
height_obj = height;
width_obj= width;
depth_obj = depth;
}
public void volume()
{
float volume = height_obj*width_obj*depth_obj;
System.out.println("The volume is "+volume);
}
}
public class volume
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Height of the Box: ");
float height = sc.nextFloat();
System.out.println("Enter width of the Box: ");
float width = sc.nextFloat();
System.out.println("Enter Depth of the Box: ");
float depth = sc.nextFloat();
box b1 = new box(height,width,depth);
b1.volume();
}
}
Q.2 Create a new class called “Calculator” which contains the following:
1. A static method called powerInt(int num1,int num2) that accepts two
integers and returns num1 to the power of num2 (num1 power num2).
2. A static method called powerDouble(double num1,int num2) that accepts
one double and one integer and returns num1 to the power of num2 (num1
power num2).
3. Call your method from another class without instantiating the class (i.e.
call it like Calculator.powerInt(12,10) since your methods are defined to be
static)
Hint: Use Math.pow(double,double) to calculate the power.
package class_project;
import java.util.*;
class calculator
{
public static int powerInt(int num1 , int num2)
{
int result = 1;
for(int i =0;i<num2;i++)
{
result = result*num1;
}
return result;
}
public static double powerDouble(double num1 , int num2)
{
double result = 1;
for(int i =0;i<num2;i++)
{
result = num1*result;
}
return result;
}
}
public class calculator_base
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number one: ");
int num1 = sc.nextInt();
System.out.println("Enter number two: ");
int num2 = sc.nextInt();
System.out.println("PowerInt method answer
"+calculator.powerInt(num1, num2));
System.err.println("PowerDouble method answer
"+calculator.powerDouble(num1, num2));
}
}
Q3. Design a class that can be used by a health care professional to keep
track of a patient’s vital statistics. Here’s what the class should do:
1. Construct a class called Patient
2. Store a String name for the patient
3. Store weight and height for patient as doubles
4. Construct a new patient using these values
5. Write a method called BMI which returns the patient’s BMI as a double.
BMI can be calculated as BMI = ( Weight in Pounds / ( Height in inches x
Height in inches ) ) x 703
6. Next, construct a class called “Patients” and create a main method.
Create a Patient object and assign some height and weight to that object.
Display the BMI of that patient.
package class_project;
import java.util.*;
class patient
{
String name;
double height, weight;
public patient(String name , double height, double weight)
{
this.name = name;
this.height = height;
this.weight = weight;
}
public void BMI()
{
double bmi;
bmi = (weight/(height * height )*703);
System.out.printf("BMI is %.2f",bmi);
}
}
public class Patients
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter name :");
String name = sc.nextLine();
System.out.println("Enter height :");
double height = sc.nextDouble();
System.out.println("Enter weight :");
double weight= sc.nextDouble();
patient obj1 = new patient(name, height, weight);
obj1.BMI();
}
}